~rackspace-titan/nova/image-service-cleanup

« back to all changes in this revision

Viewing changes to nova/network/quantum/melange_connection.py

  • Committer: Brian Waldon
  • Date: 2011-09-08 19:40:30 UTC
  • mfrom: (1517.1.17 nova)
  • Revision ID: brian.waldon@rackspace.com-20110908194030-8k3z1un277ysngxt
merging trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2010 OpenStack LLC.
 
4
# All Rights Reserved.
 
5
#
 
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
 
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, WITHOUT
 
14
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
15
#    License for the specific language governing permissions and limitations
 
16
#    under the License.
 
17
 
 
18
import httplib
 
19
import socket
 
20
import urllib
 
21
import json
 
22
 
 
23
from nova import flags
 
24
 
 
25
 
 
26
FLAGS = flags.FLAGS
 
27
 
 
28
flags.DEFINE_string('melange_host',
 
29
                    '127.0.0.1',
 
30
                    'HOST for connecting to melange')
 
31
 
 
32
flags.DEFINE_string('melange_port',
 
33
                    '9898',
 
34
                    'PORT for connecting to melange')
 
35
 
 
36
json_content_type = {'Content-type': "application/json"}
 
37
 
 
38
 
 
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):
 
43
 
 
44
    def __init__(self, host=None, port=None, use_ssl=False):
 
45
        if host is None:
 
46
            host = FLAGS.melange_host
 
47
        if port is None:
 
48
            port = int(FLAGS.melange_port)
 
49
        self.host = host
 
50
        self.port = port
 
51
        self.use_ssl = use_ssl
 
52
        self.version = "v0.1"
 
53
 
 
54
    def get(self, path, params=None, headers=None):
 
55
        return self.do_request("GET", path, params=params, headers=headers)
 
56
 
 
57
    def post(self, path, body=None, headers=None):
 
58
        return self.do_request("POST", path, body=body, headers=headers)
 
59
 
 
60
    def delete(self, path, headers=None):
 
61
        return self.do_request("DELETE", path, headers=headers)
 
62
 
 
63
    def _get_connection(self):
 
64
        if self.use_ssl:
 
65
            return httplib.HTTPSConnection(self.host, self.port)
 
66
        else:
 
67
            return httplib.HTTPConnection(self.host, self.port)
 
68
 
 
69
    def do_request(self, method, path, body=None, headers=None, params=None):
 
70
        headers = headers or {}
 
71
        params = params or {}
 
72
 
 
73
        url = "/%s/%s.json" % (self.version, path)
 
74
        if params:
 
75
            url += "?%s" % urllib.urlencode(params)
 
76
        try:
 
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:
 
82
                return response_str
 
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))
 
87
 
 
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']
 
99
 
 
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 ""
 
103
 
 
104
        url = "ipam%(tenant_scope)s/ip_blocks" % locals()
 
105
 
 
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)
 
110
 
 
111
    def delete_block(self, block_id, project_id=None):
 
112
        tenant_scope = "/tenants/%s" % project_id if project_id else ""
 
113
 
 
114
        url = "ipam%(tenant_scope)s/ip_blocks/%(block_id)s" % locals()
 
115
 
 
116
        self.delete(url, headers=json_content_type)
 
117
 
 
118
    def get_blocks(self, project_id=None):
 
119
        tenant_scope = "/tenants/%s" % project_id if project_id else ""
 
120
 
 
121
        url = "ipam%(tenant_scope)s/ip_blocks" % locals()
 
122
 
 
123
        response = self.get(url, headers=json_content_type)
 
124
        return json.loads(response)
 
125
 
 
126
    def get_allocated_ips(self, network_id, vif_id, project_id=None):
 
127
        tenant_scope = "/tenants/%s" % project_id if project_id else ""
 
128
 
 
129
        url = ("ipam%(tenant_scope)s/networks/%(network_id)s/"
 
130
           "interfaces/%(vif_id)s/ip_allocations" % locals())
 
131
 
 
132
        response = self.get(url, headers=json_content_type)
 
133
        return json.loads(response)['ip_addresses']
 
134
 
 
135
    def deallocate_ips(self, network_id, vif_id, project_id=None):
 
136
        tenant_scope = "/tenants/%s" % project_id if project_id else ""
 
137
 
 
138
        url = ("ipam%(tenant_scope)s/networks/%(network_id)s/"
 
139
           "interfaces/%(vif_id)s/ip_allocations" % locals())
 
140
 
 
141
        self.delete(url, headers=json_content_type)