blob: 09c86d34fa5f6b659bb19ba37f230b29f6157397 [file] [log] [blame]
Ales Komarek663b85c2016-03-11 14:26:42 +01001# -*- coding: utf-8 -*-
2'''
3Module for handling maas calls.
4
5:optdepends: pyapi-maas Python adapter
6:configuration: This module is not usable until the following are specified
7 either in a pillar or in the minion's config file::
8
9 maas.url: 'https://maas.domain.com/'
10 maas.token: fdsfdsdsdsfa:fsdfae3fassd:fdsfdsfsafasdfsa
11
12'''
13
14from __future__ import absolute_import
15
16import logging
17import os
18
smolaon27359ae2016-03-11 17:15:34 +010019import json
20
Ales Komarek663b85c2016-03-11 14:26:42 +010021LOG = logging.getLogger(__name__)
22
23# Import third party libs
24HAS_MASS = False
25try:
26 from apiclient.maas_client import MAASClient, MAASDispatcher, MAASOAuth
27 HAS_MASS = True
28except ImportError:
29 pass
30
31
32def __virtual__():
33 '''
34 Only load this module if maas-client
35 is installed on this minion.
36 '''
37 if HAS_MASS:
38 return 'maas'
39 return False
40
41__opts__ = {}
42
43
44def _auth(**connection_args):
45 '''
46 Set up maas credentials
47
48 Only intended to be used within maas-enabled modules
49 '''
50
51 prefix = "maas."
52
53 # look in connection_args first, then default to config file
54 def get(key, default=None):
55 return connection_args.get('connection_' + key,
56 __salt__['config.get'](prefix + key, default))
57
58 api_token = get('token')
59 api_url = get('url', 'https://localhost/')
60
smolaon27359ae2016-03-11 17:15:34 +010061 LOG.debug("MAAS url: " + api_url)
62 LOG.debug("MAAS token: " + api_token)
Ales Komarek663b85c2016-03-11 14:26:42 +010063 auth = MAASOAuth(*api_token.split(":"))
64 dispatcher = MAASDispatcher()
65 client = MAASClient(auth, dispatcher, api_url)
66
67 return client
68
69
70def cluster_get(cluster_name=None, **connection_args):
71 '''
72 Return a specific cluster
73
74 CLI Example:
75
76 .. code-block:: bash
77
78 salt '*' maas.cluster_get cluster
79 '''
80 maas = _auth(**connection_args)
Ales Komarek0fafa572016-03-11 14:56:44 +010081
82 object_list = maas.get(u"nodegroups/", "list").read()
83
84 for cluster in object_list:
85 if cluster.get('name') == cluster_name:
86 return {cluster.get('name'): cluster}
87 return {'Error': 'Could not find specified cluster'}
Ales Komarek663b85c2016-03-11 14:26:42 +010088
89
90def cluster_list(**connection_args):
91 '''
Ales Komarek0fafa572016-03-11 14:56:44 +010092 Return a list of MAAS clusters
Ales Komarek663b85c2016-03-11 14:26:42 +010093
94 CLI Example:
95
96 .. code-block:: bash
97
98 salt '*' maas.cluster_list
99 '''
100 maas = _auth(**connection_args)
101 ret = {}
102
smolaon27359ae2016-03-11 17:15:34 +0100103 response = maas.get(u"nodegroups/", "list").read()
104
105 LOG.debug("Clusters in maas: " + response )
106
107 object_list = json.loads(response)
Ales Komarek663b85c2016-03-11 14:26:42 +0100108
Ales Komarek0fafa572016-03-11 14:56:44 +0100109 for cluster in object_list:
110 ret[cluster.get('name')] = cluster
Ales Komarek663b85c2016-03-11 14:26:42 +0100111 return ret
112
113
114def cluster_create(cluster_name=None, **connection_args):
115 '''
116 Create MAAS cluster
117
118 CLI Examples:
119
120 .. code-block:: bash
121
122 salt '*' maas.cluster_create cluster
123 '''
124 maas = auth(**connection_args)
125 if project_name:
126 project = _get_project(maas, project_name)
127 else:
128 project = _get_project_by_id(maas, project_id)
129 if not project:
130 return {'Error': 'Unable to resolve project'}
131 create = True
132 for cluster in maas.getprojectclusters(project.get('id')):
133 if cluster.get('url') == cluster_url:
134 create = False
135 if create:
136 maas.addprojectcluster(project['id'], cluster_url)
137 return cluster_get(cluster_url, project_id=project['id'])
138
139
140def cluster_delete(cluster_name=None, **connection_args):
141 '''
142 Delete MAAS cluster
143
144 CLI Examples:
145
146 .. code-block:: bash
147
148 salt '*' maas.cluster_delete 'https://cluster.url/' project_id=300
149 '''
150 maas = _auth(**connection_args)
151 project = _get_project(maas, project_name)
152
153 for cluster in maas.getprojectclusters(project.get('id')):
154 if cluster.get('url') == cluster_url:
155 return maas.deleteprojectcluster(project['id'], cluster['id'])
156 return {'Error': 'Could not find cluster'}