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.user.public_ip
22 #
23 # @alldecoratedby{src.cm.utils.decorators.user_log}
24 # @author Maciej Nabożny <di.dijo@gmail.com>
25 # @author Tomasz Sośnicki <tom.sosnicki@gmail.com>
26 #
27 
28 from cm.models.lease import Lease
29 from cm.utils.exception import CMException
30 from cm.utils import log
31 from cm.models.user import User
32 from cm.models.public_ip import PublicIP
33 from cm.utils.decorators import user_log
34 from datetime import datetime
35 
36 
37 @user_log(log=True)
38 ##
39 #
40 # @cmview_user
41 # @response{list(dict)} PublicIP.dict property for each caller's PublicIP
42 #
43 def get_list(caller_id):
44  user = User.get(caller_id)
45 
46  ips = PublicIP.objects.filter(user=user).all()
47  return [ip.dict for ip in ips]
48 
49 
50 @user_log(log=True)
51 ##
52 #
53 # Method requests single PublicIP address for caller. If caller's quota
54 # is exceeded, exception is raised. Otherwise caller obtains a new PublicIP
55 # address.
56 #
57 # @cmview_user
58 # @response{string} newly obtained PublicIP's address
59 #
60 # @raises{public_lease_not_found,CMException}
61 # @raises{public_lease_request,CMException}
62 #
63 def request(caller_id):
64  user = User.get(caller_id)
65 
66  if len(user.public_ips.all()) >= user.public_ip:
67  raise CMException('public_lease_limit')
68 
69  ips = PublicIP.objects.filter(user=None).all()
70  if len(ips) == 0:
71  raise CMException('public_lease_not_found')
72 
73  ip = ips[0]
74  ip.user = user
75  ip.request_time = datetime.now()
76  ip.release_time = None
77  try:
78  ip.save()
79  except Exception:
80  raise CMException('public_lease_request')
81  return ip.address
82 
83 
84 @user_log(log=True)
85 ##
86 #
87 # Method attaches caller's PublicIP to his VM. VM's Lease instance's is
88 # assigned to PublicIP.
89 #
90 # @cmview_user
91 # @param_post{lease_id} id of the Lease in caller's UserNetwork
92 # @param_post{public_ip_id} id of the Public_IP to be attached to VM
93 #
94 # @raises{lease_not_found,CMException}
95 # @raises{lease_not_assigned,CMException}
96 # @raises{public_lease_assign,CMException}
97 #
98 def assign(caller_id, lease_id, public_ip_id):
99  user = User.get(caller_id)
100 
101  try:
102  lease = Lease.objects.filter(user_network__user=user).get(id=lease_id)
103  public_ip = PublicIP.objects.filter(user=user).get(id=public_ip_id)
104  except Exception, e:
105  log.exception(caller_id, str(e))
106  raise CMException("lease_not_found")
107 
108  if lease.vm == None:
109  raise CMException('lease_not_assigned')
110 
111  if public_ip.lease != None:
112  raise CMException('public_lease_assigned')
113 
114  public_ip.assign(lease)
115  public_ip.save()
116 
117 
118 @user_log(log=True)
119 ##
120 #
121 # Method detaches PublicIP from caller's VM.
122 #
123 # @cmview_user
124 # @param_post{lease_id,int} id of the VM's Lease from which PublicIP should
125 # be detached.
126 #
127 # @raises{lease_not_found,CMException}
128 # @raises{public_lease_unassign,CMException}
129 #
130 def unassign(caller_id, lease_id):
131  user = User.get(caller_id)
132 
133  try:
134  lease = Lease.objects.get(id=lease_id, user_network__user=user)
135  except Exception, e:
136  log.exception(caller_id, str(e))
137  raise CMException("lease_not_found")
138 
139  if lease.vm == None:
140  raise CMException('lease_not_assigned')
141 
142  try:
143  public_ip = lease.publicip_set.all()[0]
144  except:
145  raise CMException('public_lease_assigned')
146 
147  public_ip.unassign()
148  public_ip.save()
149 
150 
151 @user_log(log=True)
152 ##
153 #
154 # Removes PublicIP from caller's pool and returns it to publicly available
155 # pool, provided PublicIP isn't in use.
156 #
157 # @note There's very low probability of obtaining the same PublicIP address
158 # once again.
159 #
160 # @cmview_user
161 # @param_post{public_ip_id,int} id of the PublicIP to release
162 #
163 def release(caller_id, public_ip_id):
164  user = User.get(caller_id)
165  public_lease = PublicIP.objects.filter(user=user).get(id=public_ip_id)
166 
167  if public_lease.lease:
168  raise CMException('public_lease_assigned')
169 
170  public_lease.user = None
171  public_lease.save()
172