cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
public_ip.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 ##
21 # @package src.cm.views.admin_cm.network
22 # @author Maciej Nabożny <mn@mnabozny.pl>
23 #
24 # Functions to manage public leases in database for CM Administrator
25 #
26 
27 from netaddr.ip import IPAddress
28 from cm.models.public_ip import PublicIP
29 from cm.utils import log
30 from cm.utils.exception import CMException
31 from cm.utils.decorators import admin_cm_log
32 from cm.models.lease import Lease
33 
34 
35 @admin_cm_log(log=True)
36 ##
37 #
38 # @cmview_admin_cm
39 # @response{list(dict)} PublicIP.dict property for each PublicIP lease
40 #
41 def get_list(caller_id):
42  leases = PublicIP.objects.all()
43  return [lease.dict for lease in leases]
44 
45 
46 @admin_cm_log(log=True)
47 ##
48 #
49 # Adds addresses pool to available addresses pool. Added pool starts from
50 # @prm{start_address} and @prm{count} is its size.
51 #
52 # @cmview_admin_cm
53 # @param_post{start_address,string} first address in pool
54 # @param_post{count,int} count of public addresses to add
55 #
56 def add(caller_id, start_address, count):
57  pool = IPAddress(start_address)
58  for i in xrange(count):
59  if PublicIP.objects.filter(address=str(pool + i)).exists() > 0:
60  log.debug(caller_id, 'Ip %s is a duplicate! Skipping' % str(pool + i))
61  elif not IPAddress(pool + 1).is_unicast():
62  log.debug(caller_id, 'Ip %s is not an unicast address! Skipping' % str(pool + i))
63  else:
64  log.debug(caller_id, 'Adding public ip %s' % str(pool + i))
65  public_lease = PublicIP()
66  public_lease.address = str(pool + i)
67  public_lease.save()
68 
69 
70 @admin_cm_log(log=True)
71 ##
72 #
73 # Removes specified addresses from available pool. Address may only be
74 # removed if not in use.
75 #
76 # @cmview_admin_cm
77 # @param_post{public_ip_id_list,list(string)} addresses to remove from
78 # available pool
79 #
80 def delete(caller_id, public_ip_id_list):
81  for ip_address in public_ip_id_list:
82  lease = PublicIP.objects.get(id=ip_address)
83  if lease.lease != None:
84  raise CMException('lease_in_use')
85 
86  for ip_address in public_ip_id_list:
87  lease = PublicIP.objects.get(id=ip_address)
88  lease.delete()
89 
90 
91 @admin_cm_log(log=True)
92 ##
93 #
94 # Detaches specified PublicIP from owners VM. Unlinks PublicLease instance
95 # from its Lease instance.
96 #
97 # @cmview_admin_cm
98 # @param_post{lease_id,int} id of the VM's lease from which IP should be detached.
99 #
100 # @raises{lease_not_found,CMException}
101 # @raises{public_lease_unassign,CMException}
102 #
103 def unassign(caller_id, lease_id):
104 
105  try:
106  lease = Lease.objects.get(id=lease_id)
107  except Exception, e:
108  log.exception(caller_id, str(e))
109  raise CMException("lease_not_found")
110 
111  if lease.vm == None:
112  raise CMException('lease_not_assigned')
113 
114  try:
115  public_ip = lease.publicip_set.all()[0]
116  except:
117  raise CMException('public_lease_assigned')
118 
119  public_ip.unassign()
120  public_ip.save()
121 
122 
123 @admin_cm_log(log=True)
124 ##
125 #
126 # Removes PublicIP addresses from their owners pool. Released addresses are
127 # back available in public pool.
128 #
129 # @cmview_admin_cm
130 # @param_post{public_ip_id_list,list(int)} list of the specified ids
131 #
132 def release(caller_id, public_ip_id_list):
133  for public_ip_id in public_ip_id_list:
134  public_lease = PublicIP.objects.get(id=public_ip_id)
135 
136  if public_lease.lease:
137  public_lease.unassign()
138 
139  public_lease.user = None
140  public_lease.save()
141 
142 
143 @admin_cm_log(log=True)
144 ##
145 #
146 # Deataches PublicIP address from the virtual machine
147 # @cmview_admin_cm
148 # @parameter{public_ip_id,int} id of the PublicIP address
149 #
150 def revoke(caller_id, public_ip_id):
151  public_lease = PublicIP.objects.get(id=public_ip_id)
152  public_lease.unassign()
153  public_lease.save()
154