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 | |
| 19 | LOG = logging.getLogger(__name__) |
| 20 | |
| 21 | # Import third party libs |
| 22 | HAS_MASS = False |
| 23 | try: |
| 24 | from apiclient.maas_client import MAASClient, MAASDispatcher, MAASOAuth |
| 25 | HAS_MASS = True |
| 26 | except ImportError: |
| 27 | pass |
| 28 | |
| 29 | |
| 30 | def __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 | |
| 42 | def _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 | |
| 66 | def 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) |
| 77 | if project_name: |
| 78 | project = _get_project(maas, project_name) |
| 79 | else: |
| 80 | project = _get_project_by_id(maas, project_id) |
| 81 | if not project: |
| 82 | return {'Error': 'Unable to resolve project'} |
| 83 | for cluster in maas.getprojectclusters(project.get('id'), per_page=PER_PAGE): |
| 84 | if cluster.get('url') == cluster_url: |
| 85 | return {cluster.get('url'): cluster} |
| 86 | return {'Error': 'Could not find cluster for the specified project'} |
| 87 | |
| 88 | |
| 89 | def cluster_list(**connection_args): |
| 90 | ''' |
| 91 | Return a list of available clusters for project |
| 92 | |
| 93 | CLI Example: |
| 94 | |
| 95 | .. code-block:: bash |
| 96 | |
| 97 | salt '*' maas.cluster_list |
| 98 | ''' |
| 99 | maas = _auth(**connection_args) |
| 100 | ret = {} |
| 101 | |
| 102 | project = _get_project(maas, project) |
| 103 | |
| 104 | if not project: |
| 105 | return {'Error': 'Unable to resolve project'} |
| 106 | for cluster in maas.getprojectclusters(project.get('id')): |
| 107 | ret[cluster.get('url')] = cluster |
| 108 | return ret |
| 109 | |
| 110 | |
| 111 | def cluster_create(cluster_name=None, **connection_args): |
| 112 | ''' |
| 113 | Create MAAS cluster |
| 114 | |
| 115 | CLI Examples: |
| 116 | |
| 117 | .. code-block:: bash |
| 118 | |
| 119 | salt '*' maas.cluster_create cluster |
| 120 | ''' |
| 121 | maas = auth(**connection_args) |
| 122 | if project_name: |
| 123 | project = _get_project(maas, project_name) |
| 124 | else: |
| 125 | project = _get_project_by_id(maas, project_id) |
| 126 | if not project: |
| 127 | return {'Error': 'Unable to resolve project'} |
| 128 | create = True |
| 129 | for cluster in maas.getprojectclusters(project.get('id')): |
| 130 | if cluster.get('url') == cluster_url: |
| 131 | create = False |
| 132 | if create: |
| 133 | maas.addprojectcluster(project['id'], cluster_url) |
| 134 | return cluster_get(cluster_url, project_id=project['id']) |
| 135 | |
| 136 | |
| 137 | def cluster_delete(cluster_name=None, **connection_args): |
| 138 | ''' |
| 139 | Delete MAAS cluster |
| 140 | |
| 141 | CLI Examples: |
| 142 | |
| 143 | .. code-block:: bash |
| 144 | |
| 145 | salt '*' maas.cluster_delete 'https://cluster.url/' project_id=300 |
| 146 | ''' |
| 147 | maas = _auth(**connection_args) |
| 148 | project = _get_project(maas, project_name) |
| 149 | |
| 150 | for cluster in maas.getprojectclusters(project.get('id')): |
| 151 | if cluster.get('url') == cluster_url: |
| 152 | return maas.deleteprojectcluster(project['id'], cluster['id']) |
| 153 | return {'Error': 'Could not find cluster'} |