~thedac/charms/trusty/hacluster/telco-ha

« back to all changes in this revision

Viewing changes to ocf/maas/maasclient/__init__.py

  • Committer: David Ames
  • Date: 2016-05-13 18:40:58 UTC
  • Revision ID: david.ames@canonical.com-20160513184058-gbvluzr281oaqna9
MAAS DNS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'''
 
2
Created on May 14, 2015
 
3
 
 
4
@author: wolsen
 
5
'''
 
6
import logging
 
7
 
 
8
from .apidriver import APIDriver
 
9
 
 
10
log = logging.getLogger('vmaas.main')
 
11
 
 
12
 
 
13
class MAASException(Exception):
 
14
    pass
 
15
 
 
16
 
 
17
class MAASDriverException(Exception):
 
18
    pass
 
19
 
 
20
 
 
21
class MAASClient(object):
 
22
    """
 
23
    A wrapper for the python maas client which makes using the API a bit
 
24
    more user friendly.
 
25
    """
 
26
 
 
27
    def __init__(self, api_url, api_key, **kwargs):
 
28
        self.driver = self._get_driver(api_url, api_key, **kwargs)
 
29
 
 
30
    def _get_driver(self, api_url, api_key, **kwargs):
 
31
        return APIDriver(api_url, api_key)
 
32
 
 
33
    def _validate_maas(self):
 
34
        try:
 
35
            resp = self.driver.validate_maas()
 
36
            logging.info("Validated MAAS API")
 
37
            return True
 
38
        except Exception as e:
 
39
            logging.error("MAAS API validation has failed. "
 
40
                           "Check maas_url and maas_credentials. Error: {}"
 
41
                           "".format(e))
 
42
            return False
 
43
        
 
44
    ###########################################################################
 
45
    #  DNS API - http://maas.ubuntu.com/docs2.0/api.html#dnsresource
 
46
    ###########################################################################
 
47
    def get_dnsresources(self):
 
48
        """
 
49
        Get a listing of DNS resources which are currently defined.
 
50
 
 
51
        :returns: a list of DNS objects
 
52
        DNS object is a dictionary of the form:
 
53
        {'fqdn': 'keystone.maas',
 
54
         'resource_records': [],
 
55
         'address_ttl': None,
 
56
         'resource_uri': '/MAAS/api/2.0/dnsresources/1/',
 
57
         'ip_addresses': [],
 
58
         'id': 1}
 
59
        """
 
60
        resp = self.driver.get_dnsresources()
 
61
        if resp.ok:
 
62
            return resp.data
 
63
        return []
 
64
 
 
65
    def update_dnsresource(self, rid, fqdn, ip_address):
 
66
        """
 
67
        Updates a DNS resource with a new ip_address
 
68
 
 
69
        :param rid: The dnsresource_id i.e.
 
70
                    /api/2.0/dnsresources/{dnsresource_id}/
 
71
        :param fqdn: The fqdn address to update
 
72
        :param ip_address: The ip address to update the A record to point to
 
73
        :returns: True if the DNS object was updated, False otherwise.
 
74
        """
 
75
        resp = self.driver.update_dnsresource(rid, fqdn, ip_address)
 
76
        if resp.ok:
 
77
            return True
 
78
        return False
 
79
 
 
80
    def create_dnsresource(self, fqdn, ip_address, address_ttl=None):
 
81
        """
 
82
        Creates a new DNS resource
 
83
 
 
84
        :param fqdn: The fqdn address to update
 
85
        :param ip_address: The ip address to update the A record to point to
 
86
        :param adress_ttl: DNS time to live
 
87
        :returns: True if the DNS object was updated, False otherwise.
 
88
        """
 
89
        resp = self.driver.create_dnsresource(fqdn, ip_address, address_ttl)
 
90
        if resp.ok:
 
91
            return True
 
92
        return False
 
93
 
 
94
    ###########################################################################
 
95
    #  IP API - http://maas.ubuntu.com/docs2.0/api.html#ip-address
 
96
    ###########################################################################
 
97
    def get_ipaddresses(self):
 
98
        """
 
99
        Get a list of ip addresses
 
100
 
 
101
        :returns: a list of ip address dictionaries
 
102
        """
 
103
        resp = self.driver.get_ipaddresses()
 
104
        if resp.ok:
 
105
            return resp.data
 
106
        return []
 
107
 
 
108
    def create_ipaddress(self, ip_address, hostname=None):
 
109
        """
 
110
        Creates a new IP resource
 
111
 
 
112
        :param ip_address: The ip address to register
 
113
        :param hostname: the hostname to register at the same time
 
114
        :returns: True if the DNS object was updated, False otherwise.
 
115
        """
 
116
        resp = self.driver.create_ipaddress(ip_address, hostname)
 
117
        if resp.ok:
 
118
            return True
 
119
        return False