forked from ungleich/python-oca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvn.py
More file actions
166 lines (130 loc) · 3.84 KB
/
vn.py
File metadata and controls
166 lines (130 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# -*- coding: UTF-8 -*-
from .pool import XMLElement, Pool, PoolElement, Template
class Lease(XMLElement):
XML_TYPES = {
'ip': str,
'mac': str,
}
def __init__(self, xml):
super(Lease, self).__init__(xml)
self._convert_types()
class LeasesList(list):
def __init__(self, xml):
if xml is None:
return
self.xml = xml
for element in self.xml:
self.append(self._factory(element))
def _factory(self, xml):
v = Lease(xml)
v._convert_types()
return v
class AddressRange(XMLElement):
XML_TYPES = {
'id': ["AR_ID", lambda xml: int(xml.text)],
'size': int,
'leases': ['LEASES', LeasesList],
# 'template' : ['TEMPLATE', Template],
}
def __init__(self, xml):
super(AddressRange, self).__init__(xml)
self._convert_types()
self.id = self['AR_ID'] if self['AR_ID'] else None
class AddressRangeList(list):
def __init__(self, xml):
self.xml = xml
for element in self.xml:
self.append(self._factory(element))
def _factory(self, xml):
v = AddressRange(xml)
v._convert_types()
return v
class VirtualNetwork(PoolElement):
METHODS = {
'info': 'vn.info',
'allocate': 'vn.allocate',
'delete': 'vn.delete',
'publish': 'vn.publish',
'chown': 'vn.chown',
'hold': 'vn.hold',
'release': 'vn.release',
}
XML_TYPES = {
'id': int,
'uid': int,
'gid': int,
'uname': str,
'gname': str,
'name': str,
# 'type' : int,
'bridge': str,
# 'public' : bool,
'used_leases': int,
'template': ['TEMPLATE', Template],
'address_ranges': ['AR_POOL', AddressRangeList],
}
ELEMENT_NAME = 'VNET'
@staticmethod
def allocate(client, template):
"""
allocates a new virtual network in OpenNebula
Arguments
``template``
a string containing the template of the virtual network
"""
vn_id = client.call(VirtualNetwork.METHODS['allocate'], template)
return vn_id
def __init__(self, xml, client):
super(VirtualNetwork, self).__init__(xml, client)
self.id = self['ID'] if self['ID'] else None
def publish(self):
"""
Publishes a virtual network.
"""
self.client.call(self.METHODS['publish'], self.id, True)
def unpublish(self):
"""
Unpublishes a virtual network.
"""
self.client.call(self.METHODS['publish'], self.id, False)
def chown(self, uid, gid):
"""
Changes the owner/group
Arguments
``uid``
New owner id. Set to -1 to leave current value
``gid``
New group id. Set to -1 to leave current value
"""
self.client.call(self.METHODS['chown'], self.id, uid, gid)
def release(self, ip):
"""
Releases given IP
Arguments
``ip``
IP to realse
"""
self.client.call(self.METHODS['release'], self.id, 'LEASES=[IP={}]'.format(ip))
def hold(self, ip):
"""
Holds given IP
Arguments
``ip``
IP to hold
"""
self.client.call(self.METHODS['hold'], self.id, 'LEASES=[IP={}]'.format(ip))
def __repr__(self):
return '<oca.VirtualNetwork("%s")>' % self.name
class VirtualNetworkPool(Pool):
METHODS = {
'info': 'vnpool.info',
}
def __init__(self, client, preload_info=False):
self.preload_info = preload_info
super(VirtualNetworkPool, self).__init__('VNET_POOL', 'VNET', client)
def _factory(self, xml):
v = VirtualNetwork(xml, self.client)
v._convert_types()
if self.preload_info:
v.info()
return v