1
# Copyright 2016 Canonical Ltd
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
18
from apiclient import maas_client as maas
19
from .driver import MAASDriver
20
from .driver import Response
23
from urllib2 import HTTPError
25
from urllib3.exceptions import HTTPError
27
log = logging.getLogger('vmaas.main')
31
class APIDriver(MAASDriver):
33
A MAAS driver implementation which uses the MAAS API.
36
def __init__(self, api_url, api_key, *args, **kwargs):
37
if api_url[-1] != '/':
39
if api_url.find('/api/') < 0:
40
api_url = api_url + 'api/2.0/'
41
super(APIDriver, self).__init__(api_url, api_key, *args, **kwargs)
55
self._client = maas.MAASClient(auth=self.oauth,
56
dispatcher=maas.MAASDispatcher(),
57
base_url=self.api_url)
63
MAAS OAuth information for interacting with the MAAS API.
71
api_key = self.api_key.split(':')
72
self._oauth = maas.MAASOAuth(consumer_key=api_key[0],
73
resource_token=api_key[1],
74
resource_secret=api_key[2])
79
def validate_maas(self):
82
def _get(self, path, **kwargs):
84
Issues a GET request to the MAAS REST API, returning the data
85
from the query in the python form of the json data.
87
response = self.client.get(path, **kwargs)
88
payload = response.read()
89
log.debug("Request %s results: [%s] %s", path, response.getcode(),
92
if response.getcode() == OK:
93
return Response(True, yaml.load(payload))
95
return Response(False, payload)
97
def _post(self, path, op='update', **kwargs):
99
Issues a POST request to the MAAS REST API.
102
response = self.client.post(path, **kwargs)
103
payload = response.read()
104
log.debug("Request %s results: [%s] %s", path, response.getcode(),
107
if response.getcode() == OK:
108
return Response(True, yaml.load(payload))
110
return Response(False, payload)
111
except HTTPError as e:
112
log.error("Error encountered: %s for %s with params %s",
113
str(e), path, str(kwargs))
114
return Response(False, None)
115
except Exception as e:
116
log.error("Post request raised exception: %s", e)
117
return Response(False, None)
119
def _put(self, path, **kwargs):
121
Issues a PUT request to the MAAS REST API.
124
response = self.client.put(path, **kwargs)
125
payload = response.read()
126
log.debug("Request %s results: [%s] %s", path, response.getcode(),
128
if response.getcode() == OK:
129
return Response(True, payload)
131
return Response(False, payload)
132
except HTTPError as e:
133
log.error("Error encountered: %s with details: %s for %s with "
134
"params %s", e, e.read(), path, str(kwargs))
135
return Response(False, None)
136
except Exception as e:
137
log.error("Put request raised exception: %s", e)
138
return Response(False, None)
140
###########################################################################
141
# DNS API - http://maas.ubuntu.com/docs2.0/api.html#dnsresource
142
###########################################################################
143
def get_dnsresources(self):
145
Get a listing of the MAAS dnsresources
147
:returns: a list of MAAS dnsresrouce objects
149
return self._get('/dnsresources/')
151
def update_dnsresource(self, rid, fqdn, ip_address):
153
Updates a DNS resource with a new ip_address
155
:param rid: The dnsresource_id i.e.
156
/api/2.0/dnsresources/{dnsresource_id}/
157
:param fqdn: The fqdn address to update
158
:param ip_address: The ip address to update the A record to point to
159
:returns: True if the DNS object was updated, False otherwise.
161
return self._put('/dnsresources/{}/'.format(rid), fqdn=fqdn,
162
ip_addresses=ip_address)
164
def create_dnsresource(self, fqdn, ip_address, address_ttl=None):
166
Creates a new DNS resource
168
:param fqdn: The fqdn address to update
169
:param ip_address: The ip address to update the A record to point to
170
:param adress_ttl: DNS time to live
171
:returns: True if the DNS object was updated, False otherwise.
173
fqdn = bytes(fqdn, encoding='utf-8')
174
ip_address = bytes(ip_address, encoding='utf-8')
176
return self._post('/dnsresources/',
178
ip_addresses=ip_address,
179
address_ttl=address_ttl)
181
return self._post('/dnsresources/',
183
ip_addresses=ip_address)
185
###########################################################################
186
# IP API - http://maas.ubuntu.com/docs2.0/api.html#ip-addresses
187
###########################################################################
188
def get_ipaddresses(self):
190
Get a dictionary of a given ip_address
192
:param ip_address: The ip address to get information for
193
:returns: a dictionary for a given ip
195
return self._get('/ipaddresses/')
197
def create_ipaddress(self, ip_address, hostname=None):
199
Creates a new IP resource
201
:param ip_address: The ip address to register
202
:param hostname: the hostname to register at the same time
203
:returns: True if the DNS object was updated, False otherwise.
206
return self._post('/ipaddresses/', op='reserve',
207
ip_addresses=ip_address,
210
return self._post('/ipaddresses/', op='reserve',
211
ip_addresses=ip_address)