1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2010 OpenStack LLC.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
23
from nova import flags
28
flags.DEFINE_string('melange_host',
30
'HOST for connecting to melange')
32
flags.DEFINE_string('melange_port',
34
'PORT for connecting to melange')
36
json_content_type = {'Content-type': "application/json"}
39
# FIXME(danwent): talk to the Melange folks about creating a
40
# client lib that we can import as a library, instead of
41
# have to have all of the client code in here.
42
class MelangeConnection(object):
44
def __init__(self, host=None, port=None, use_ssl=False):
46
host = FLAGS.melange_host
48
port = int(FLAGS.melange_port)
51
self.use_ssl = use_ssl
54
def get(self, path, params=None, headers=None):
55
return self.do_request("GET", path, params=params, headers=headers)
57
def post(self, path, body=None, headers=None):
58
return self.do_request("POST", path, body=body, headers=headers)
60
def delete(self, path, headers=None):
61
return self.do_request("DELETE", path, headers=headers)
63
def _get_connection(self):
65
return httplib.HTTPSConnection(self.host, self.port)
67
return httplib.HTTPConnection(self.host, self.port)
69
def do_request(self, method, path, body=None, headers=None, params=None):
70
headers = headers or {}
73
url = "/%s/%s.json" % (self.version, path)
75
url += "?%s" % urllib.urlencode(params)
77
connection = self._get_connection()
78
connection.request(method, url, body, headers)
79
response = connection.getresponse()
80
response_str = response.read()
81
if response.status < 400:
83
raise Exception(_("Server returned error: %s" % response_str))
84
except (socket.error, IOError), e:
85
raise Exception(_("Unable to connect to "
86
"server. Got error: %s" % e))
88
def allocate_ip(self, network_id, vif_id,
89
project_id=None, mac_address=None):
90
tenant_scope = "/tenants/%s" % project_id if project_id else ""
91
request_body = (json.dumps(dict(network=dict(mac_address=mac_address,
92
tenant_id=project_id)))
93
if mac_address else None)
94
url = ("ipam%(tenant_scope)s/networks/%(network_id)s/"
95
"interfaces/%(vif_id)s/ip_allocations" % locals())
96
response = self.post(url, body=request_body,
97
headers=json_content_type)
98
return json.loads(response)['ip_addresses']
100
def create_block(self, network_id, cidr,
101
project_id=None, dns1=None, dns2=None):
102
tenant_scope = "/tenants/%s" % project_id if project_id else ""
104
url = "ipam%(tenant_scope)s/ip_blocks" % locals()
106
req_params = dict(ip_block=dict(cidr=cidr, network_id=network_id,
107
type='private', dns1=dns1, dns2=dns2))
108
self.post(url, body=json.dumps(req_params),
109
headers=json_content_type)
111
def delete_block(self, block_id, project_id=None):
112
tenant_scope = "/tenants/%s" % project_id if project_id else ""
114
url = "ipam%(tenant_scope)s/ip_blocks/%(block_id)s" % locals()
116
self.delete(url, headers=json_content_type)
118
def get_blocks(self, project_id=None):
119
tenant_scope = "/tenants/%s" % project_id if project_id else ""
121
url = "ipam%(tenant_scope)s/ip_blocks" % locals()
123
response = self.get(url, headers=json_content_type)
124
return json.loads(response)
126
def get_allocated_ips(self, network_id, vif_id, project_id=None):
127
tenant_scope = "/tenants/%s" % project_id if project_id else ""
129
url = ("ipam%(tenant_scope)s/networks/%(network_id)s/"
130
"interfaces/%(vif_id)s/ip_allocations" % locals())
132
response = self.get(url, headers=json_content_type)
133
return json.loads(response)['ip_addresses']
135
def deallocate_ips(self, network_id, vif_id, project_id=None):
136
tenant_scope = "/tenants/%s" % project_id if project_id else ""
138
url = ("ipam%(tenant_scope)s/networks/%(network_id)s/"
139
"interfaces/%(vif_id)s/ip_allocations" % locals())
141
self.delete(url, headers=json_content_type)