Beispiel #1
0
def main():
    url = None
    if len(sys.argv) > 1:
        url = sys.argv[1]
    auth = MaasAuth(api_url=url)
    auth.get_api_key('root')
    maas_client = MaasClient(auth)

    pprint.pprint(maas_client.nodes)
def connect_to_maas(creds=None):
    if creds:
        api_host = creds["api_host"]
        api_url = "http://{}/MAAS/api/1.0".format(api_host)
        api_key = creds["api_key"]
        auth = MaasAuth(api_url=api_url, api_key=api_key)
    else:
        auth = MaasAuth()
        auth.get_api_key("root")
    maas = MaasClient(auth)
    maas_state = MaasState(maas)
    return maas, maas_state
Beispiel #3
0
 def authenticate_maas(self):
     if self.config.maas_creds:
         api_host = self.config.maas_creds['api_host']
         api_url = 'http://{}/MAAS/api/1.0/'.format(api_host)
         api_key = self.config.maas_creds['api_key']
         auth = MaasAuth(api_url=api_url,
                         api_key=api_key)
     else:
         auth = MaasAuth()
         auth.get_api_key('root')
     self.maas = MaasClient(auth)
     self.maas_state = MaasState(self.maas)
     log.debug('Authenticated against maas api.')
Beispiel #4
0
def main():
    module = AnsibleModule(argument_spec=dict(
        maas=dict(default='http://localhost/MAAS/api/1.0/'),
        key=dict(required=True),
        state=dict(default='query', choices=['query', 'import'])),
                           supports_check_mode=False)

    maas = module.params['maas']
    key = module.params['key']
    state = module.params['state']

    # Authenticate into MAAS
    auth = MaasAuth(maas, key)
    maas = MaasClient(auth)

    if state == 'query':
        res = maas.get('/boot-resources/')
        if res.ok:
            module.exit_json(changed=False, resources=json.loads(res.text))
        else:
            module.fail_json(msg=string_or_object(res.text))
    elif state == 'import':
        res = maas.post('/boot-resources/', dict(op='import'))
        if res.ok:
            module.exit_json(changed=True)
        else:
            module.fail_json(msg=string_or_object(res.text))
    else:
        module.fail_json(msg='unknown state')
Beispiel #5
0
def main():
    module = AnsibleModule(argument_spec=dict(
        maas=dict(default='http://localhost/MAAS/api/1.0/'),
        key=dict(required=True),
        sshkey=dict(required=True),
        state=dict(default='present', choices=['present', 'absent', 'query'])),
                           supports_check_mode=False)

    maas = module.params['maas']
    key = module.params['key']
    state = module.params['state']

    # Construct a sparsely populate desired state
    desired = remove_null({
        'key': module.params['sshkey'],
    })

    # Authenticate into MAAS
    auth = MaasAuth(maas, key)
    maas = MaasClient(auth)

    # Attempt to get the item from MAAS
    sshkey = get_sshkey(maas, desired['key'])

    # Actions if the item does not currently exist
    if not sshkey:
        if state == 'query':
            # If this is a query, return it is not found
            module.exit_json(changed=False, found=False)
        elif state == 'present':
            # If this should be present, then attempt to create it
            res = create_sshkey(maas, desired)
            if res['error']:
                module.fail_json(msg=res['status'])
            else:
                module.exit_json(changed=True, sshkey=res['status'])
        else:
            # If this should be absent, then we are done and in the desired state
            module.exit_json(changed=False)

        # Done with items does not exists actions
        return

    # Actions if the item does exist
    if state == 'query':
        # If this is a query, return the sshkey
        module.exit_json(changed=False, found=True, sshkey=sshkey)
    elif state == 'present':
        # If we want this to exists check to see if this is different and
        # needs updated
        # No differences, to nothing to change
        module.exit_json(changed=False, sshkey=sshkey)
    else:
        # If we don't want this item, then delete it
        res = delete_sshkey(maas, item['id'])
        if res['error']:
            module.fail_json(msg=res['status'])
        else:
            module.exit_json(changed=True, sshkey=sshkey)
def connect_to_maas(creds=None):
    if creds:
        api_host = creds['api_host']
        api_url = 'http://{}/MAAS/api/1.0'.format(api_host)
        api_key = creds['api_key']
        auth = MaasAuth(api_url=api_url, api_key=api_key)
    else:
        auth = MaasAuth()
        auth.get_api_key('root')
    maas = MaasClient(auth)
    maas_state = MaasState(maas)
    return maas, maas_state
Beispiel #7
0
def main():
    url = None
    if len(sys.argv) == 3:
        url = sys.argv[1]
        key = sys.argv[2]
        auth = MaasAuth(api_url=url, api_key=key)
    else:
        auth = MaasAuth(api_url=url)
        auth.get_api_key('root')
    maas_client = MaasClient(auth)
    pprint.pprint(maas_client.nodes)
    pprint.pprint(maas_client.get_server_config('maas_name'))
    pprint.pprint(maas_client.server_hostname)
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import unittest
import sys
import uuid
import requests
sys.path.insert(0, '../maasclient')

from maasclient import MaasClient
from maasclient.auth import MaasAuth


AUTH = MaasAuth()
AUTH.api_key = ('FUVLUr44JARNCNMFsP:'
                'rdpdeVscAqvZhnm4Te:EpDGJacNqy5Xzj22Xd58Xv5vz8MqEQNA')
AUTH.api_url = 'http://10.0.3.55/MAAS/api/1.0'

_check_maas = requests.get(AUTH.api_url)
MAAS_INSTALLED = _check_maas.ok


@unittest.skipIf(not MAAS_INSTALLED, "Maas is not installed")
class MaasAuthTest(unittest.TestCase):
    def test_get_api_key(self):
        self.assertEquals(3, len(AUTH.api_key.split(':')))


@unittest.skipIf(not MAAS_INSTALLED, "Maas is not installed")
Beispiel #9
0
def main():
    module = AnsibleModule(argument_spec=dict(
        maas=dict(default='http://localhost/MAAS/api/1.0/'),
        key=dict(required=True),
        base=dict(required=False),
        cluster_name=dict(required=True),
        name=dict(required=True),
        interface=dict(required=False),
        ip=dict(required=False),
        subnet_mask=dict(required=False),
        management=dict(default='unmanaged',
                        choices=['unmanaged', 'dhcp', 'dhcpdns']),
        ip_range_low=dict(required=False),
        ip_range_high=dict(required=False),
        static_ip_range_low=dict(required=False),
        static_ip_range_high=dict(required=False),
        broadcast_ip=dict(required=False),
        router_ip=dict(required=False),
        state=dict(default='present', choices=['present', 'absent', 'query'])),
                           supports_check_mode=False)

    maas = module.params['maas']
    key = module.params['key']
    state = module.params['state']

    management_map = {'unmanaged': 0, 'dhcp': 1, 'dhcpdns': 2}

    # Construct a sparsely populate desired state
    desired = remove_null({
        'name':
        module.params['name'],
        'interface':
        module.params['interface'],
        'ip':
        module.params['ip'],
        'subnet_mask':
        module.params['subnet_mask'],
        'management':
        management_map[module.params['management']],
        'ip_range_low':
        module.params['ip_range_low'],
        'ip_range_high':
        module.params['ip_range_high'],
        'static_ip_range_low':
        module.params['static_ip_range_low'],
        'static_ip_range_high':
        module.params['static_ip_range_high'],
        'broadcast_ip':
        module.params['broadcast_ip'],
        'router_ip':
        module.params['router_ip'],
    })

    debug = []

    # Authenticate into MAAS
    auth = MaasAuth(maas, key)
    maas = MaasClient(auth)

    # Attempt to locate the cluster on which we will be working, error out if it can't be found
    cluster = get_cluster(maas, module.params['cluster_name'])
    if not cluster:
        module.fail_json(
            msg='Unable to find specified cluster "%s", cannot continue' %
            module.params['cluster_name'])
        return

    debug.append({"desired": desired})

    # Attempt to get the cluster interface from MAAS
    cluster_interface = get_cluster_interface(maas, cluster, desired['name'])

    debug.append({"found": cluster_interface})

    # Actions if the cluster interface does not currently exist
    if not cluster_interface:
        if state == 'query':
            # If this is a query, returne it is not found
            module.exit_json(changed=False, found=False)
        elif state == 'present':
            # If this should be present, then attempt to create it
            res = create_cluster_interface(maas, cluster, desired)
            if res['error']:
                module.fail_json(msg=res['status'])
            else:
                module.exit_json(changed=True,
                                 cluster_interface=res['status'],
                                 debug=debug)
        else:
            # If this should be absent, then we are done and in the desired state
            module.exit_json(changed=False)

        # Done with cluster interfaces does not exists actions
        return

    # Actions if the cluster interface does exist
    if state == 'query':
        # If this is a query, return the cluster interface
        module.exit_json(changed=False,
                         found=True,
                         cluster_interface=cluster_interface)
    elif state == 'present':
        # If we want this to exists check to see if this is different and
        # needs updated
        if different(cluster_interface, desired, debug):
            res = update_cluster_interface(maas, cluster, cluster_interface,
                                           desired)
            if res['error']:
                module.fail_json(msg=res['status'])
            else:
                module.exit_json(changed=True,
                                 cluster_interface=res['status'],
                                 debug=debug)
        else:
            # No differences, to nothing to change
            module.exit_json(changed=False,
                             cluster_interface=cluster_interface)
    else:
        # If we don't want this cluster interface, then delete it
        res = delete_cluster_interface(maas, cluster,
                                       cluster_interface['name'])
        if res['error']:
            module.fail_json(msg=res['status'])
        else:
            module.exit_json(changed=True, cluster_interface=cluster_interface)
Beispiel #10
0
 def authenticate_maas(self):
     auth = MaasAuth()
     auth.get_api_key('root')
     self.maas = MaasClient(auth)
     self.maas_state = MaasState(self.maas)
     log.debug('Authenticated against maas api.')
Beispiel #11
0
def main():
    module = AnsibleModule(argument_spec=dict(
        maas=dict(default='http://localhost/MAAS/api/1.0/'),
        key=dict(required=True),
        name=dict(required=True),
        email=dict(required=False),
        password=dict(required=False),
        is_superuser=dict(default=False, type='bool'),
        state=dict(default='present', choices=['present', 'absent', 'query'])),
                           supports_check_mode=False)

    maas = module.params['maas']
    key = module.params['key']
    state = module.params['state']

    # Construct a sparsely populate desired state
    desired = remove_null({
        'username':
        module.params['name'],
        'email':
        module.params['email'],
        'password':
        module.params['password'],
        'is_superuser':
        0 if not module.params['is_superuser'] else 1
    })

    # Authenticate into MAAS
    auth = MaasAuth(maas, key)
    maas = MaasClient(auth)

    # Attempt to get the user from MAAS
    user = get_user(maas, desired['username'])

    # Actions if the user does not currently exist
    if not user:
        if state == 'query':
            # If this is a query, returne it is not found
            module.exit_json(changed=False, found=False)
        elif state == 'present':
            # If this should be present, then attempt to create it
            res = create_user(maas, desired)
            if res['error']:
                module.fail_json(msg=res['status'])
            else:
                module.exit_json(changed=True, user=res['status'])
        else:
            # If this should be absent, then we are done and in the desired state
            module.exit_json(changed=False)

        # Done with users does not exists actions
        return

    # Actions if the user does exist
    if state == 'query':
        # If this is a query, return the user
        module.exit_json(changed=False, found=True, user=user)
    elif state == 'present':
        # If we want this to exists check to see if this is different and
        # needs updated
        if different(user, desired):
            module.fail_json(
                msg=
                'Specified user, "%s", exists and MAAS does not allow the user to be modified programatically'
                % user['username'])
        else:
            # No differences, to nothing to change
            module.exit_json(changed=False, user=user)
    else:
        # If we don't want this user, then delete it
        res = delete_user(maas, user['username'])
        if res['error']:
            module.fail_json(msg=res['status'])
        else:
            module.exit_json(changed=True, user=user)
Beispiel #12
0
#!/usr/bin/env python
import argparse
from maasclient.auth import MaasAuth
from maasclient import MaasClient

parser = argparse.ArgumentParser()
parser.add_argument("--api_key")
parser.add_argument("--api_url")
args = parser.parse_args()

auth = MaasAuth(api_url=args.api_url, api_key=args.api_key)
maas_client = MaasClient(auth)
print(maas_client.server_hostname)
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import unittest
import sys
import uuid
import requests
sys.path.insert(0, '../maasclient')

from maasclient import MaasClient
from maasclient.auth import MaasAuth

AUTH = MaasAuth()
AUTH.api_key = 'FUVLUr44JARNCNMFsP:rdpdeVscAqvZhnm4Te:EpDGJacNqy5Xzj22Xd58Xv5vz8MqEQNA'
AUTH.api_url = 'http://10.0.3.55/MAAS/api/1.0'

_check_maas = requests.get(AUTH.api_url)
MAAS_INSTALLED = _check_maas.ok


@unittest.skipIf(not MAAS_INSTALLED, "Maas is not installed")
class MaasAuthTest(unittest.TestCase):
    def test_get_api_key(self):
        self.assertEquals(3, len(AUTH.api_key.split(':')))


@unittest.skipIf(not MAAS_INSTALLED, "Maas is not installed")
class MaasClientTest(unittest.TestCase):
Beispiel #14
0
def main():
    module = AnsibleModule(
        argument_spec = dict(
            maas=dict(default='http://localhost/MAAS/api/1.0/'),
            key=dict(required=True),
            name=dict(required=True),
            status=dict(default='enabled', choices=['enabled', 'disabled']),
            domain=dict(required=False),
            state=dict(default='present', choices=['present', 'query'])
        ),
        supports_check_mode = False
    )

    maas = module.params['maas']
    key = module.params['key']
    state = module.params['state']

    status_map = {
        'enabled': 1,
        'disabled': 2
    }

    # Construct a sparsely populate desired state
    desired = remove_null({
        'cluster_name': module.params['name'],
        'status': status_map[module.params['status']],
        'name' : module.params['domain'],
    })

    # Authenticate into MAAS
    auth = MaasAuth(maas, key)
    maas = MaasClient(auth)

    # Attempt to get the cluster from MAAS
    cluster = get_cluster(maas, desired['cluster_name'])

    # Actions if the cluster does not currently exist
    if not cluster:
        if state == 'query':
            # If this is a query, returne it is not found
            module.exit_json(changed=False, found=False)
        elif state == 'present':
            # Not able to create clusters via the API
            module.fail_json(msg='Named cluster does not exist and clusters cannot be programatically created')
        else:
            # If this should be absent, then we are done and in the desired state
            module.exit_json(changed=False)

        # Done with clusters does not exists actions
        return

    # Actions if the cluster does exist
    if state == 'query':
        # If this is a query, return the cluster
        module.exit_json(changed=False, found=True, cluster=cluster)
    elif state == 'present':
        # If we want this to exists check to see if this is different and
        # needs updated
        if different(cluster, desired):
            res = update_cluster(maas, cluster, desired)
            if res['error']:
                module.fail_json(msg=res['status'])
            else:
                module.exit_json(changed=True, cluster=res['status'])
        else:
            # No differences, to nothing to change
            module.exit_json(changed=False, cluster=cluster)
    else:
        # Not able to delete clusters via the API
        module.fail_json(msg='Named cluster exists and clusters cannot be programmatically deleted')
Beispiel #15
0
def main():
    module = AnsibleModule(
        argument_spec = dict(
            maas=dict(default='http://localhost/MAAS/api/1.0/'),
            key=dict(required=True),
            options=dict(required=False, type='list'),
            enable_http_proxy=dict(required=False),
            upstream_dns=dict(required=False),
            default_storage_layout=dict(required=False),
            default_osystem=dict(required=False),
            ports_archive=dict(required=False),
            http_proxy=dict(required=False),
            boot_images_auto_import=dict(required=False),
            enable_third_party_drivers=dict(required=False),
            kernel_opts=dict(required=False),
            main_archive=dict(required=False),
            maas_name=dict(required=False),
            curtin_verbose=dict(required=False),
            dnssec_validation=dict(required=False),
            commissioning_distro_series=dict(required=False),
            windows_kms_host=dict(required=False),
            enable_disk_erasing_on_release=dict(required=False),
            default_distro_series=dict(required=False),
            ntp_server=dict(required=False),
            default_min_hwe_kernel=dict(required=False),
            state=dict(default='present', choices=['present', 'query'])
        ),
        supports_check_mode = False
    )

    maas = module.params['maas']
    key = module.params['key']
    options = module.params['options']
    state = module.params['state']

    if state == 'query':
        desired = {x:None for x in options}
    else:
        # Construct a sparsely populate desired state
        desired = remove_null({
            'enable_http_proxy': module.params['enable_http_proxy'],
            'upstream_dns': module.params['upstream_dns'],
            'default_storage_layout': module.params['default_storage_layout'],
            'default_osystem': module.params['default_osystem'],
            'ports_archive': module.params['ports_archive'],
            'http_proxy': module.params['http_proxy'],
            'boot_images_auto_import': module.params['boot_images_auto_import'],
            'enable_third_party_drivers': module.params['enable_third_party_drivers'],
            'kernel_opts': module.params['kernel_opts'],
            'main_archive': module.params['main_archive'],
            'maas_name': module.params['maas_name'],
            'curtin_verbose': module.params['curtin_verbose'],
            'dnssec_validation': module.params['dnssec_validation'],
            'commissioning_distro_series': module.params['commissioning_distro_series'],
            'windows_kms_host': module.params['windows_kms_host'],
            'enable_disk_erasing_on_release': module.params['enable_disk_erasing_on_release'],
            'default_distro_series': module.params['default_distro_series'],
            'ntp_server': module.params['ntp_server'],
            'default_min_hwe_kernel': module.params['default_min_hwe_kernel'],
        })

    # Authenticate into MAAS
    auth = MaasAuth(maas, key)
    maas = MaasClient(auth)

    # Attempt to get the configuration from MAAS
    config = get_config(maas, desired)

    if state == 'query':
        # If this is a query, return the options
        module.exit_json(changed=False, found=True, maas=config)
    elif state == 'present':
        # If we want this to exists check to see if this is different and
        # needs updated
        if different(config, desired):
            res = update_config(maas, config, desired)
            if res['error']:
                module.fail_json(msg=res['status'])
            else:
                module.exit_json(changed=True, maas=res['status'])
        else:
            # No differences, to nothing to change
            module.exit_json(changed=False, maas=config)
Beispiel #16
0
def main():
    module = AnsibleModule(argument_spec=dict(
        maas=dict(default='http://localhost/MAAS/api/1.0/'),
        key=dict(required=True),
        name=dict(required=True),
        space=dict(required=False),
        dns_servers=dict(required=False),
        gateway_ip=dict(required=False),
        cidr=dict(required=False),
        state=dict(default='present', choices=['present', 'absent', 'query'])),
                           supports_check_mode=False)

    maas = module.params['maas']
    key = module.params['key']
    state = module.params['state']

    # Construct a sparsely populate desired state
    desired = remove_null({
        'name': module.params['name'],
        'space': module.params['space'],
        'dns_servers': module.params['dns_servers'],
        'gateway_ip': module.params['gateway_ip'],
        'cidr': module.params['cidr'],
    })

    # Authenticate into MAAS
    auth = MaasAuth(maas, key)
    maas = MaasClient(auth)

    # Attempt to get the subnet from MAAS
    subnet = get_subnet(maas, desired['name'])

    # Actions if the subnet does not currently exist
    if not subnet:
        if state == 'query':
            # If this is a query, returne it is not found
            module.exit_json(changed=False, found=False)
        elif state == 'present':
            # If this should be present, then attempt to create it
            res = create_subnet(maas, desired)
            if res['error']:
                module.fail_json(msg=res['status'])
            else:
                module.exit_json(changed=True, subnet=res['status'])
        else:
            # If this should be absent, then we are done and in the desired state
            module.exit_json(changed=False)

        # Done with subnets does not exists actions
        return

    # Actions if the subnet does exist
    if state == 'query':
        # If this is a query, return the subnet
        module.exit_json(changed=False, found=True, subnet=subnet)
    elif state == 'present':
        # If we want this to exists check to see if this is different and
        # needs updated
        if different(subnet, desired):
            res = update_subnet(maas, subnet, desired)
            if res['error']:
                module.fail_json(msg=res['status'])
            else:
                module.exit_json(changed=True,
                                 subnet=res['status'],
                                 debug=debug)
        else:
            # No differences, to nothing to change
            module.exit_json(changed=False, subnet=subnet)
    else:
        # If we don't want this subnet, then delete it
        res = delete_subnet(maas, subnet['name'])
        if res['error']:
            module.fail_json(msg=res['status'])
        else:
            module.exit_json(changed=True, subnet=subnet)
Beispiel #17
0
 def authenticate_maas(self):
     auth = MaasAuth()
     auth.get_api_key('root')
     self.maas = MaasClient(auth)
     self.maas_state = MaasState(self.maas)
     log.debug('Authenticated against maas api.')