Closing keystone sessions after done with them.
[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, session=None):
28     """
29     Retrieves the Magnum client
30     :param os_creds: the OpenStack credentialsf
31     :param session: the keystone session object (optional)
32     :return: the client
33     """
34     logger.debug('Retrieving Magnum Client')
35     if not session:
36         session = keystone_utils.keystone_session(os_creds)
37
38     return Client(str(os_creds.magnum_api_version), session=session)
39
40
41 def get_cluster_template(magnum, template_config=None, template_name=None):
42     """
43     Returns the first ClusterTemplate domain object that matches the parameters
44     :param magnum: the Magnum client
45     :param template_config: a ClusterTemplateConfig object (optional)
46     :param template_name: the name of the template to lookup
47     :return: ClusterTemplate object or None
48     """
49     name = None
50     if template_config:
51         name = template_config.name
52     elif template_name:
53         name = template_name
54
55     os_templates = magnum.cluster_templates.list()
56     for os_template in os_templates:
57         if os_template.name == name:
58             return __map_os_cluster_template(os_template)
59
60
61 def get_cluster_template_by_id(magnum, tmplt_id):
62     """
63     Returns the first ClusterTemplate domain object that matches the parameters
64     :param magnum: the Magnum client
65     :param tmplt_id: the template's ID
66     :return: ClusterTemplate object or None
67     """
68     return __map_os_cluster_template(magnum.cluster_templates.get(tmplt_id))
69
70
71 def create_cluster_template(magnum, cluster_template_config):
72     """
73     Creates a Magnum Cluster Template object in OpenStack
74     :param magnum: the Magnum client
75     :param cluster_template_config: a ClusterTemplateConfig object
76     :return: a SNAPS ClusterTemplate domain object
77     """
78     config_dict = cluster_template_config.magnum_dict()
79     os_cluster_template = magnum.cluster_templates.create(**config_dict)
80     logger.info('Creating cluster template named [%s]',
81                 cluster_template_config.name)
82     return __map_os_cluster_template(os_cluster_template)
83
84
85 def delete_cluster_template(magnum, tmplt_id):
86     """
87     Deletes a Cluster Template from OpenStack
88     :param magnum: the Magnum client
89     :param tmplt_id: the cluster template ID to delete
90     """
91     logger.info('Deleting cluster template with ID [%s]', tmplt_id)
92     magnum.cluster_templates.delete(tmplt_id)
93
94
95 def __map_os_cluster_template(os_tmplt):
96     """
97     Returns a SNAPS ClusterTemplate object from an OpenStack ClusterTemplate
98     object
99     :param os_tmplt: the OpenStack ClusterTemplate object
100     :return: SNAPS ClusterTemplate object
101     """
102     return ClusterTemplate(
103         id=os_tmplt.uuid,
104         name=os_tmplt.name,
105         image=os_tmplt.image_id,
106         keypair=os_tmplt.keypair_id,
107         network_driver=os_tmplt.network_driver,
108         external_net=os_tmplt.external_network_id,
109         floating_ip_enabled=os_tmplt.floating_ip_enabled,
110         docker_volume_size=os_tmplt.docker_volume_size,
111         server_type=os_tmplt.server_type,
112         flavor=os_tmplt.flavor_id,
113         master_flavor=os_tmplt.master_flavor_id,
114         coe=os_tmplt.coe,
115         fixed_net=os_tmplt.fixed_network,
116         fixed_subnet=os_tmplt.fixed_subnet,
117         registry_enabled=os_tmplt.registry_enabled,
118         insecure_registry=os_tmplt.insecure_registry,
119         docker_storage_driver=os_tmplt.docker_storage_driver,
120         dns_nameserver=os_tmplt.dns_nameserver,
121         public=os_tmplt.public,
122         tls_disabled=os_tmplt.tls_disabled,
123         http_proxy=os_tmplt.http_proxy,
124         https_proxy=os_tmplt.https_proxy,
125         no_proxy=os_tmplt.no_proxy,
126         volume_driver=os_tmplt.volume_driver,
127         master_lb_enabled=os_tmplt.master_lb_enabled,
128         labels=os_tmplt.labels
129     )