2012-11-19 23:08:35 +00:00
|
|
|
# Copyright 2012 Managed I.T.
|
|
|
|
#
|
|
|
|
# Author: Kiall Mac Innes <kiall@managedit.ie>
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
import requests
|
|
|
|
from monikerclient import exceptions
|
|
|
|
from monikerclient.auth import KeystoneAuth
|
2013-02-07 17:45:03 +00:00
|
|
|
from monikerclient.v1 import diagnostics
|
2012-11-19 23:08:35 +00:00
|
|
|
from monikerclient.v1 import domains
|
|
|
|
from monikerclient.v1 import records
|
|
|
|
from monikerclient.v1 import servers
|
|
|
|
|
|
|
|
|
|
|
|
class Client(object):
|
|
|
|
""" Client for the Moniker v1 API """
|
|
|
|
|
|
|
|
def __init__(self, endpoint=None, auth_url=None, username=None,
|
|
|
|
password=None, tenant_id=None, tenant_name=None, token=None,
|
2013-01-24 09:27:47 +00:00
|
|
|
region_name=None, service_type='dns',
|
2013-02-24 16:47:50 +01:00
|
|
|
endpoint_type='publicURL', sudo_tenant_id=None):
|
2012-11-19 23:08:35 +00:00
|
|
|
"""
|
|
|
|
:param endpoint: Endpoint URL
|
|
|
|
:param auth_url: Keystone auth_url
|
|
|
|
:param username: The username to auth with
|
|
|
|
:param password: The password to auth with
|
|
|
|
:param tenant_id: The tenant ID
|
|
|
|
:param tenant_name: The tenant name
|
|
|
|
:param token: A token instead of username / password
|
|
|
|
:param region_name: The region name
|
|
|
|
:param endpoint_type: The endpoint type (publicURL for example)
|
|
|
|
"""
|
|
|
|
if auth_url:
|
|
|
|
auth = KeystoneAuth(auth_url, username, password, tenant_id,
|
2013-01-24 09:27:47 +00:00
|
|
|
tenant_name, token, service_type,
|
2013-02-24 16:47:50 +01:00
|
|
|
endpoint_type, sudo_tenant_id)
|
2012-12-30 00:01:03 +00:00
|
|
|
self.endpoint = auth.get_url()
|
2012-11-19 23:08:35 +00:00
|
|
|
elif endpoint:
|
|
|
|
auth = None
|
2012-12-30 00:01:03 +00:00
|
|
|
self.endpoint = endpoint
|
2012-11-19 23:08:35 +00:00
|
|
|
else:
|
2012-12-11 16:41:50 -08:00
|
|
|
raise ValueError('Either an endpoint or auth_url must be supplied')
|
2012-11-19 23:08:35 +00:00
|
|
|
|
|
|
|
headers = {'Content-Type': 'application/json'}
|
2012-12-30 00:01:03 +00:00
|
|
|
self.requests = requests.Session()
|
|
|
|
self.requests.auth = auth
|
|
|
|
self.requests.headers.update(headers)
|
2012-11-19 23:08:35 +00:00
|
|
|
|
2013-02-07 17:45:03 +00:00
|
|
|
self.diagnostics = diagnostics.DiagnosticsController(client=self)
|
2012-11-19 23:08:35 +00:00
|
|
|
self.domains = domains.DomainsController(client=self)
|
|
|
|
self.records = records.RecordsController(client=self)
|
|
|
|
self.servers = servers.ServersController(client=self)
|
|
|
|
|
|
|
|
def wrap_api_call(self, func, *args, **kw):
|
|
|
|
"""
|
|
|
|
Wrap a self.<rest function> with exception handling
|
|
|
|
|
|
|
|
:param func: The function to wrap
|
|
|
|
"""
|
2012-12-30 00:01:03 +00:00
|
|
|
# Prepend the endpoint URI
|
|
|
|
args = list(args)
|
|
|
|
args[0] = '%s/%s' % (self.endpoint, args[0])
|
|
|
|
|
|
|
|
# Trigger the request
|
2012-11-19 23:08:35 +00:00
|
|
|
response = func(*args, **kw)
|
|
|
|
|
2012-12-11 06:24:48 -08:00
|
|
|
if response.status_code == 400:
|
2013-03-26 17:16:07 +00:00
|
|
|
raise exceptions.BadRequest(**response.json)
|
2012-12-11 06:24:48 -08:00
|
|
|
elif response.status_code in (401, 403):
|
2013-03-26 17:16:07 +00:00
|
|
|
raise exceptions.Forbidden(**response.json)
|
2012-11-19 23:08:35 +00:00
|
|
|
elif response.status_code == 404:
|
2013-03-26 17:16:07 +00:00
|
|
|
raise exceptions.NotFound(**response.json)
|
2012-11-19 23:08:35 +00:00
|
|
|
elif response.status_code == 409:
|
2013-03-26 17:16:07 +00:00
|
|
|
raise exceptions.Conflict(**response.json)
|
2012-11-19 23:08:35 +00:00
|
|
|
elif response.status_code == 500:
|
2013-03-26 17:16:07 +00:00
|
|
|
raise exceptions.Unknown(**response.json)
|
2012-11-19 23:08:35 +00:00
|
|
|
else:
|
|
|
|
return response
|
|
|
|
|
|
|
|
def get(self, path, **kw):
|
|
|
|
return self.wrap_api_call(self.requests.get, path, **kw)
|
|
|
|
|
|
|
|
def post(self, path, **kw):
|
|
|
|
return self.wrap_api_call(self.requests.post, path, **kw)
|
|
|
|
|
|
|
|
def put(self, path, **kw):
|
|
|
|
return self.wrap_api_call(self.requests.put, path, **kw)
|
|
|
|
|
|
|
|
def delete(self, path, **kw):
|
|
|
|
return self.wrap_api_call(self.requests.delete, path, **kw)
|