Implemented the ability to create Magnum Cluster Type objects.
[snaps.git] / snaps / openstack / utils / magnum_utils.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 import logging
16
17 from magnumclient.client import Client
18
19 from snaps.domain.cluster_template import ClusterTemplate
20 from snaps.openstack.utils import keystone_utils
21
22 __author__ = 'spisarski'
23
24 logger = logging.getLogger('magnum_utils')
25
26
27 def magnum_client(os_creds):
28     """
29     Retrieves the Magnum client
30     :param os_creds: the OpenStack credentialsf
31     :return: the client
32     """
33     logger.debug('Retrieving Magnum Client')
34     return Client(str(os_creds.magnum_api_version),
35                   session=keystone_utils.keystone_session(os_creds))
36
37
38 def create_cluster_template(magnum, cluster_template_config):
39     """
40     Creates a Magnum Cluster Template object in OpenStack
41     :param magnum: the Magnum client
42     :param cluster_template_config: a ClusterTemplateConfig object
43     :return: a SNAPS ClusterTemplate domain object
44     """
45     config_dict = cluster_template_config.magnum_dict()
46     os_cluster_template = magnum.cluster_templates.create(**config_dict)
47     logger.info('Creating cluster template named [%s]',
48                 cluster_template_config.name)
49     return __map_os_cluster_template(os_cluster_template)
50
51
52 def delete_cluster_template(magnum, tmplt_id):
53     """
54     Deletes a Cluster Template from OpenStack
55     :param magnum: the Magnum client
56     :param tmplt_id: the cluster template ID to delete
57     """
58     logger.info('Deleting cluster template with ID [%s]', tmplt_id)
59     magnum.cluster_templates.delete(tmplt_id)
60
61
62 def __map_os_cluster_template(os_tmplt):
63     """
64     Returns a SNAPS ClusterTemplate object from an OpenStack ClusterTemplate
65     object
66     :param os_tmplt: the OpenStack ClusterTemplate object
67     :return: SNAPS ClusterTemplate object
68     """
69     return ClusterTemplate(
70         id=os_tmplt.uuid,
71         name=os_tmplt.name,
72         image=os_tmplt.image_id,
73         keypair=os_tmplt.keypair_id,
74         network_driver=os_tmplt.network_driver,
75         external_net=os_tmplt.external_network_id,
76         floating_ip_enabled=os_tmplt.floating_ip_enabled,
77         docker_volume_size=os_tmplt.docker_volume_size,
78         server_type=os_tmplt.server_type,
79         flavor=os_tmplt.flavor_id,
80         master_flavor=os_tmplt.master_flavor_id,
81         coe=os_tmplt.coe,
82         fixed_net=os_tmplt.fixed_network,
83         fixed_subnet=os_tmplt.fixed_subnet,
84         registry_enabled=os_tmplt.registry_enabled,
85         insecure_registry=os_tmplt.insecure_registry,
86         docker_storage_driver=os_tmplt.docker_storage_driver,
87         dns_nameserver=os_tmplt.dns_nameserver,
88         public=os_tmplt.public,
89         tls_disabled=os_tmplt.tls_disabled,
90         http_proxy=os_tmplt.http_proxy,
91         https_proxy=os_tmplt.https_proxy,
92         no_proxy=os_tmplt.no_proxy,
93         volume_driver=os_tmplt.volume_driver,
94         master_lb_enabled=os_tmplt.master_lb_enabled,
95         labels=os_tmplt.labels
96     )