Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | ''' |
| 3 | Module 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 | |
| 14 | from __future__ import absolute_import |
| 15 | |
| 16 | import logging |
| 17 | import os |
| 18 | |
smolaon | 27359ae | 2016-03-11 17:15:34 +0100 | [diff] [blame] | 19 | import json |
| 20 | |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 21 | LOG = logging.getLogger(__name__) |
| 22 | |
| 23 | # Import third party libs |
| 24 | HAS_MASS = False |
| 25 | try: |
| 26 | from apiclient.maas_client import MAASClient, MAASDispatcher, MAASOAuth |
| 27 | HAS_MASS = True |
| 28 | except ImportError: |
| 29 | pass |
| 30 | |
| 31 | |
| 32 | def __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 | |
| 44 | def _auth(**connection_args): |
| 45 | ''' |
| 46 | Set up maas credentials |
| 47 | |
| 48 | Only intended to be used within maas-enabled modules |
| 49 | ''' |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame^] | 50 | |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 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 | |
smolaon | 27359ae | 2016-03-11 17:15:34 +0100 | [diff] [blame] | 61 | LOG.debug("MAAS url: " + api_url) |
| 62 | LOG.debug("MAAS token: " + api_token) |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 63 | auth = MAASOAuth(*api_token.split(":")) |
| 64 | dispatcher = MAASDispatcher() |
| 65 | client = MAASClient(auth, dispatcher, api_url) |
| 66 | |
| 67 | return client |
| 68 | |
| 69 | |
| 70 | def 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 Komarek | 0fafa57 | 2016-03-11 14:56:44 +0100 | [diff] [blame] | 81 | |
smolaon | 22f95ae | 2016-03-11 17:37:22 +0100 | [diff] [blame] | 82 | response = maas.get(u"nodegroups/", "list").read() |
| 83 | LOG.debug("Response: " + response) |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame^] | 84 | |
smolaon | 22f95ae | 2016-03-11 17:37:22 +0100 | [diff] [blame] | 85 | object_list = json.loads(response) |
Ales Komarek | 0fafa57 | 2016-03-11 14:56:44 +0100 | [diff] [blame] | 86 | |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame^] | 87 | |
Ales Komarek | 0fafa57 | 2016-03-11 14:56:44 +0100 | [diff] [blame] | 88 | for cluster in object_list: |
smolaon | 22f95ae | 2016-03-11 17:37:22 +0100 | [diff] [blame] | 89 | if cluster.get('cluster_name') == cluster_name: |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 90 | return {cluster.get('cluster_name'): cluster} |
Ales Komarek | 0fafa57 | 2016-03-11 14:56:44 +0100 | [diff] [blame] | 91 | return {'Error': 'Could not find specified cluster'} |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 92 | |
| 93 | |
| 94 | def cluster_list(**connection_args): |
| 95 | ''' |
Ales Komarek | 0fafa57 | 2016-03-11 14:56:44 +0100 | [diff] [blame] | 96 | Return a list of MAAS clusters |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 97 | |
| 98 | CLI Example: |
| 99 | |
| 100 | .. code-block:: bash |
| 101 | |
| 102 | salt '*' maas.cluster_list |
| 103 | ''' |
| 104 | maas = _auth(**connection_args) |
| 105 | ret = {} |
| 106 | |
smolaon | 27359ae | 2016-03-11 17:15:34 +0100 | [diff] [blame] | 107 | response = maas.get(u"nodegroups/", "list").read() |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame^] | 108 | |
smolaon | 27359ae | 2016-03-11 17:15:34 +0100 | [diff] [blame] | 109 | LOG.debug("Clusters in maas: " + response ) |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame^] | 110 | |
| 111 | object_list = json.loads(response) |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 112 | |
Ales Komarek | 0fafa57 | 2016-03-11 14:56:44 +0100 | [diff] [blame] | 113 | for cluster in object_list: |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 114 | ret[cluster.get('cluster_name')] = cluster |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 115 | return ret |
| 116 | |
| 117 | |
| 118 | def 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ć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame^] | 139 | if create: |
Ales Komarek | 663b85c | 2016-03-11 14:26:42 +0100 | [diff] [blame] | 140 | maas.addprojectcluster(project['id'], cluster_url) |
| 141 | return cluster_get(cluster_url, project_id=project['id']) |
| 142 | |
| 143 | |
| 144 | def 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'} |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 161 | |
| 162 | |
| 163 | def 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ć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame^] | 174 | |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 175 | 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ć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame^] | 186 | |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 187 | if new_cluster_name: |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame^] | 188 | cluster["cluster_name"] = new_cluster_name |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 189 | |
| 190 | if domain: |
| 191 | cluster["name"] = domain |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame^] | 192 | |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 193 | if status: |
| 194 | cluster["status"] = status |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame^] | 195 | |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 196 | LOG.debug("Cluster id: " + cluster_id) |
| 197 | LOG.debug("New cluster info: " + str(cluster)) |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame^] | 198 | |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 199 | response = maas.put(u"nodegroups/" + cluster_id + "/", **cluster) |
Krzysztof Szukiełojć | 15b62b7 | 2017-02-15 08:58:18 +0100 | [diff] [blame^] | 200 | |
smolaon | c3385f8 | 2016-03-11 19:01:24 +0100 | [diff] [blame] | 201 | #TODO check response status |
| 202 | return {'Status': True} |
| 203 | |