blob: 83e6b2a8a6e015237533499c1b0823a37261f982 [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 '''
Krzysztof Szukiełojć15b62b72017-02-15 08:58:18 +010050
Ales Komarek663b85c2016-03-11 14:26:42 +010051 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
smolaon22f95ae2016-03-11 17:37:22 +010082 response = maas.get(u"nodegroups/", "list").read()
83 LOG.debug("Response: " + response)
Krzysztof Szukiełojć15b62b72017-02-15 08:58:18 +010084
smolaon22f95ae2016-03-11 17:37:22 +010085 object_list = json.loads(response)
Ales Komarek0fafa572016-03-11 14:56:44 +010086
Krzysztof Szukiełojć15b62b72017-02-15 08:58:18 +010087
Ales Komarek0fafa572016-03-11 14:56:44 +010088 for cluster in object_list:
smolaon22f95ae2016-03-11 17:37:22 +010089 if cluster.get('cluster_name') == cluster_name:
smolaonc3385f82016-03-11 19:01:24 +010090 return {cluster.get('cluster_name'): cluster}
Ales Komarek0fafa572016-03-11 14:56:44 +010091 return {'Error': 'Could not find specified cluster'}
Ales Komarek663b85c2016-03-11 14:26:42 +010092
93
94def cluster_list(**connection_args):
95 '''
Ales Komarek0fafa572016-03-11 14:56:44 +010096 Return a list of MAAS clusters
Ales Komarek663b85c2016-03-11 14:26:42 +010097
98 CLI Example:
99
100 .. code-block:: bash
101
102 salt '*' maas.cluster_list
103 '''
104 maas = _auth(**connection_args)
105 ret = {}
106
smolaon27359ae2016-03-11 17:15:34 +0100107 response = maas.get(u"nodegroups/", "list").read()
Krzysztof Szukiełojć15b62b72017-02-15 08:58:18 +0100108
smolaon27359ae2016-03-11 17:15:34 +0100109 LOG.debug("Clusters in maas: " + response )
Krzysztof Szukiełojć15b62b72017-02-15 08:58:18 +0100110
111 object_list = json.loads(response)
Ales Komarek663b85c2016-03-11 14:26:42 +0100112
Ales Komarek0fafa572016-03-11 14:56:44 +0100113 for cluster in object_list:
smolaonc3385f82016-03-11 19:01:24 +0100114 ret[cluster.get('cluster_name')] = cluster
Ales Komarek663b85c2016-03-11 14:26:42 +0100115 return ret
116
117
118def cluster_create(cluster_name=None, **connection_args):
119 '''
120 Create MAAS cluster
121
122 CLI Examples:
123
124 .. code-block:: bash
125
126 salt '*' maas.cluster_create cluster
127 '''
128 maas = auth(**connection_args)
129 if project_name:
130 project = _get_project(maas, project_name)
131 else:
132 project = _get_project_by_id(maas, project_id)
133 if not project:
134 return {'Error': 'Unable to resolve project'}
135 create = True
136 for cluster in maas.getprojectclusters(project.get('id')):
137 if cluster.get('url') == cluster_url:
138 create = False
Krzysztof Szukiełojć15b62b72017-02-15 08:58:18 +0100139 if create:
Ales Komarek663b85c2016-03-11 14:26:42 +0100140 maas.addprojectcluster(project['id'], cluster_url)
141 return cluster_get(cluster_url, project_id=project['id'])
142
143
144def cluster_delete(cluster_name=None, **connection_args):
145 '''
146 Delete MAAS cluster
147
148 CLI Examples:
149
150 .. code-block:: bash
151
152 salt '*' maas.cluster_delete 'https://cluster.url/' project_id=300
153 '''
154 maas = _auth(**connection_args)
155 project = _get_project(maas, project_name)
156
157 for cluster in maas.getprojectclusters(project.get('id')):
158 if cluster.get('url') == cluster_url:
159 return maas.deleteprojectcluster(project['id'], cluster['id'])
160 return {'Error': 'Could not find cluster'}
smolaonc3385f82016-03-11 19:01:24 +0100161
162
163def cluster_update(cluster_id=None, old_cluster_name=None, new_cluster_name=None, domain=None, status=None, **connection_args):
164 '''
165 Update information in specific MAAS cluster
166
167 CLI Examples:
168
169 .. code-block:: bash
170
171 salt '*' maas.cluster_update cluster_id cluster_name dns_name status
172 '''
173 maas = _auth(**connection_args)
Krzysztof Szukiełojć15b62b72017-02-15 08:58:18 +0100174
smolaonc3385f82016-03-11 19:01:24 +0100175 cluster = {}
176
177 if not cluster_id and old_cluster_name:
178 cluster = cluster_get(old_cluster_name)
179 if cluster.get("Error"):
180 return cluster
181 else:
182 cluster_id = cluster_get(old_cluster_name).get(old_cluster_name).get("uuid")
183
184 else:
185 return {'Error': 'No cluster id or name specified'}
Krzysztof Szukiełojć15b62b72017-02-15 08:58:18 +0100186
smolaonc3385f82016-03-11 19:01:24 +0100187 if new_cluster_name:
Krzysztof Szukiełojć15b62b72017-02-15 08:58:18 +0100188 cluster["cluster_name"] = new_cluster_name
smolaonc3385f82016-03-11 19:01:24 +0100189
190 if domain:
191 cluster["name"] = domain
Krzysztof Szukiełojć15b62b72017-02-15 08:58:18 +0100192
smolaonc3385f82016-03-11 19:01:24 +0100193 if status:
194 cluster["status"] = status
Krzysztof Szukiełojć15b62b72017-02-15 08:58:18 +0100195
smolaonc3385f82016-03-11 19:01:24 +0100196 LOG.debug("Cluster id: " + cluster_id)
197 LOG.debug("New cluster info: " + str(cluster))
Krzysztof Szukiełojć15b62b72017-02-15 08:58:18 +0100198
smolaonc3385f82016-03-11 19:01:24 +0100199 response = maas.put(u"nodegroups/" + cluster_id + "/", **cluster)
Krzysztof Szukiełojć15b62b72017-02-15 08:58:18 +0100200
smolaonc3385f82016-03-11 19:01:24 +0100201 #TODO check response status
202 return {'Status': True}
203