blob: 20ccc48f283e4edf324a825f5dd1c9682ebc22b0 [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
19LOG = logging.getLogger(__name__)
20
21# Import third party libs
22HAS_MASS = False
23try:
24 from apiclient.maas_client import MAASClient, MAASDispatcher, MAASOAuth
25 HAS_MASS = True
26except ImportError:
27 pass
28
29
30def __virtual__():
31 '''
32 Only load this module if maas-client
33 is installed on this minion.
34 '''
35 if HAS_MASS:
36 return 'maas'
37 return False
38
39__opts__ = {}
40
41
42def _auth(**connection_args):
43 '''
44 Set up maas credentials
45
46 Only intended to be used within maas-enabled modules
47 '''
48
49 prefix = "maas."
50
51 # look in connection_args first, then default to config file
52 def get(key, default=None):
53 return connection_args.get('connection_' + key,
54 __salt__['config.get'](prefix + key, default))
55
56 api_token = get('token')
57 api_url = get('url', 'https://localhost/')
58
59 auth = MAASOAuth(*api_token.split(":"))
60 dispatcher = MAASDispatcher()
61 client = MAASClient(auth, dispatcher, api_url)
62
63 return client
64
65
66def cluster_get(cluster_name=None, **connection_args):
67 '''
68 Return a specific cluster
69
70 CLI Example:
71
72 .. code-block:: bash
73
74 salt '*' maas.cluster_get cluster
75 '''
76 maas = _auth(**connection_args)
Ales Komarek0fafa572016-03-11 14:56:44 +010077
78 object_list = maas.get(u"nodegroups/", "list").read()
79
80 for cluster in object_list:
81 if cluster.get('name') == cluster_name:
82 return {cluster.get('name'): cluster}
83 return {'Error': 'Could not find specified cluster'}
Ales Komarek663b85c2016-03-11 14:26:42 +010084
85
86def cluster_list(**connection_args):
87 '''
Ales Komarek0fafa572016-03-11 14:56:44 +010088 Return a list of MAAS clusters
Ales Komarek663b85c2016-03-11 14:26:42 +010089
90 CLI Example:
91
92 .. code-block:: bash
93
94 salt '*' maas.cluster_list
95 '''
96 maas = _auth(**connection_args)
97 ret = {}
98
Ales Komarek0fafa572016-03-11 14:56:44 +010099 object_list = maas.get(u"nodegroups/", "list").read()
Ales Komarek663b85c2016-03-11 14:26:42 +0100100
Ales Komarek0fafa572016-03-11 14:56:44 +0100101 for cluster in object_list:
102 ret[cluster.get('name')] = cluster
Ales Komarek663b85c2016-03-11 14:26:42 +0100103 return ret
104
105
106def cluster_create(cluster_name=None, **connection_args):
107 '''
108 Create MAAS cluster
109
110 CLI Examples:
111
112 .. code-block:: bash
113
114 salt '*' maas.cluster_create cluster
115 '''
116 maas = auth(**connection_args)
117 if project_name:
118 project = _get_project(maas, project_name)
119 else:
120 project = _get_project_by_id(maas, project_id)
121 if not project:
122 return {'Error': 'Unable to resolve project'}
123 create = True
124 for cluster in maas.getprojectclusters(project.get('id')):
125 if cluster.get('url') == cluster_url:
126 create = False
127 if create:
128 maas.addprojectcluster(project['id'], cluster_url)
129 return cluster_get(cluster_url, project_id=project['id'])
130
131
132def cluster_delete(cluster_name=None, **connection_args):
133 '''
134 Delete MAAS cluster
135
136 CLI Examples:
137
138 .. code-block:: bash
139
140 salt '*' maas.cluster_delete 'https://cluster.url/' project_id=300
141 '''
142 maas = _auth(**connection_args)
143 project = _get_project(maas, project_name)
144
145 for cluster in maas.getprojectclusters(project.get('id')):
146 if cluster.get('url') == cluster_url:
147 return maas.deleteprojectcluster(project['id'], cluster['id'])
148 return {'Error': 'Could not find cluster'}