cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
network.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 ##
20 # @package src.cm.views.admin_cm.network
21 # @author Maciej Nabo┼╝ny <mn@mnabozny.pl>
22 #
23 # Functions for creating and deleting networks.
24 #
25 from cm.models.user_network import UserNetwork
26 from cm.models.available_network import AvailableNetwork
27 from cm.models.user import User
28 from cm.utils.exception import CMException
29 from cm.utils.decorators import admin_cm_log
30 from common.states import available_network_states
31 
32 from netaddr import IPNetwork
33 
34 
35 @admin_cm_log(log=True)
36 ##
37 #
38 # Creates new AvailableNetwork
39 #
40 # @cmview_admin_cm
41 # @param_post{address,string}
42 # @param_post{mask,int}
43 #
44 def add(caller_id, address, mask):
45 
46  # Find duplicate
47  ipnet = IPNetwork('%s/%d' % (address, mask))
48  if AvailableNetwork.network_exists(ipnet):
49  raise CMException('network_exists')
50 
51  # Add new network
52  new_net = AvailableNetwork()
53  new_net.address = ipnet.network
54  new_net.mask = mask
55 
56  if ipnet.is_private():
57  new_net.state = available_network_states['ok']
58  else:
59  new_net.state = available_network_states['locked']
60 
61  new_net.save()
62 
63 
64 @admin_cm_log(log=True)
65 ##
66 #
67 # Releases and deletes AvailableNetwork (if not used).
68 #
69 # @cmview_admin_cm
70 # @param_post{pool_id,int} id of the AvailableNetwork to delete
71 #
72 def delete_available_network(caller_id, pool_id):
73  try:
74  net = AvailableNetwork.objects.get(id=pool_id)
75  except:
76  raise CMException('available_network_not_found')
77  net.release()
78  net.delete()
79 
80 
81 @admin_cm_log(log=True)
82 ##
83 #
84 # Releases and deletes UserNetwork (if not used).
85 #
86 # @cmview_admin_cm
87 # @param_post{network_id,int} id of the UserNetwork to delete
88 #
89 def delete_user_network(caller_id, network_id):
90  try:
91  net = UserNetwork.objects.get(id=network_id)
92  except:
93  raise CMException('network_not_found')
94  net.release()
95  net.delete()
96 
97 
98 @admin_cm_log(log=True)
99 ##
100 #
101 # @cmview_admin_cm
102 # @response{list(dict)} AvailableNetwork.dict property for each requested AvailableNetwork
103 #
104 def list_available_networks(caller_id):
105  response = []
106  for network in AvailableNetwork.objects.all():
107  response.append(network.dict)
108  return response
109 
110 
111 @admin_cm_log(log=True)
112 ##
113 #
114 # @cmview_admin_cm
115 # @param_post{user_id,int} (optional) if specified, only networks belonging
116 # to specigied User are fetched.
117 # @param_post{only_unused,bool} (optional) if @val{True}, only unused
118 # networks are returned
119 #
120 # @response{list(dict)} UserNetwork.dict property for each requested
121 # UserNetwork
122 #
123 def list_user_networks(caller_id, user_id=None, only_unused=False):
124  try:
125  user_networks = []
126  if user_id:
127  user = User.get(user_id)
128  user_networks = UserNetwork.objects.filter(user=user)
129  else:
130  user_networks = UserNetwork.objects.all()
131  except:
132  raise CMException('network_not_found')
133 
134  response = []
135  for network in user_networks:
136  if not (only_unused and network.is_in_use()):
137  response.append(network.dict)
138  return response
139 
140 
141 @admin_cm_log(log=True)
142 ##
143 #
144 # @cmview_admin_cm
145 # @param_post{network_id} id of the requested UserNetwork
146 #
147 # @response{list{dict}} Lease.dict property for each Lease in specified
148 # UserNetwork
149 #
150 def list_leases(caller_id, network_id):
151  try:
152  user_network = UserNetwork.objects.get(id=network_id)
153  except:
154  raise CMException('network_not_found')
155 
156  response = []
157  for lease in user_network.lease_set.all():
158  response.append(lease.dict)
159  return response
160 
161 
162 @admin_cm_log(log=True)
163 ##
164 #
165 # Sets AvailableNetwork's state as @val{locked}.
166 #
167 # @cmview_admin_cm
168 # @param_post{pool_id,int} id of the AvailableNetwork to lock
169 #
170 def lock(caller_id, pool_id):
171  try:
172  network = AvailableNetwork.objects.get(id=pool_id)
173  except:
174  raise CMException('available_network_not_found')
175  network.state = available_network_states['locked']
176  network.save()
177 
178 
179 @admin_cm_log(log=True)
180 ##
181 #
182 # Sets AvailableNetwork's state as @val{ok}.
183 #
184 # @cmview_admin_cm
185 # @param_post{pool_id,int} id of the AvailableNetwork to unlock
186 #
187 def unlock(caller_id, pool_id):
188  try:
189  network = AvailableNetwork.objects.get(id=pool_id)
190  except:
191  raise CMException('available_network_not_found')
192  network.state = available_network_states['ok']
193  network.save()
194