1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 from magnumclient.client import Client
19 from snaps.domain.cluster_template import ClusterTemplate
20 from snaps.openstack.utils import keystone_utils
22 __author__ = 'spisarski'
24 logger = logging.getLogger('magnum_utils')
27 def magnum_client(os_creds):
29 Retrieves the Magnum client
30 :param os_creds: the OpenStack credentialsf
33 logger.debug('Retrieving Magnum Client')
34 return Client(str(os_creds.magnum_api_version),
35 session=keystone_utils.keystone_session(os_creds))
38 def get_cluster_template(magnum, template_config=None, template_name=None):
40 Returns the first ClusterTemplate domain object that matches the parameters
41 :param magnum: the Magnum client
42 :param template_config: a ClusterTemplateConfig object (optional)
43 :param template_name: the name of the template to lookup
44 :return: ClusterTemplate object or None
48 name = template_config.name
52 os_templates = magnum.cluster_templates.list()
53 for os_template in os_templates:
54 if os_template.name == name:
55 return __map_os_cluster_template(os_template)
58 def get_cluster_template_by_id(magnum, tmplt_id):
60 Returns the first ClusterTemplate domain object that matches the parameters
61 :param magnum: the Magnum client
62 :param tmplt_id: the template's ID
63 :return: ClusterTemplate object or None
65 return __map_os_cluster_template(magnum.cluster_templates.get(tmplt_id))
68 def create_cluster_template(magnum, cluster_template_config):
70 Creates a Magnum Cluster Template object in OpenStack
71 :param magnum: the Magnum client
72 :param cluster_template_config: a ClusterTemplateConfig object
73 :return: a SNAPS ClusterTemplate domain object
75 config_dict = cluster_template_config.magnum_dict()
76 os_cluster_template = magnum.cluster_templates.create(**config_dict)
77 logger.info('Creating cluster template named [%s]',
78 cluster_template_config.name)
79 return __map_os_cluster_template(os_cluster_template)
82 def delete_cluster_template(magnum, tmplt_id):
84 Deletes a Cluster Template from OpenStack
85 :param magnum: the Magnum client
86 :param tmplt_id: the cluster template ID to delete
88 logger.info('Deleting cluster template with ID [%s]', tmplt_id)
89 magnum.cluster_templates.delete(tmplt_id)
92 def __map_os_cluster_template(os_tmplt):
94 Returns a SNAPS ClusterTemplate object from an OpenStack ClusterTemplate
96 :param os_tmplt: the OpenStack ClusterTemplate object
97 :return: SNAPS ClusterTemplate object
99 return ClusterTemplate(
102 image=os_tmplt.image_id,
103 keypair=os_tmplt.keypair_id,
104 network_driver=os_tmplt.network_driver,
105 external_net=os_tmplt.external_network_id,
106 floating_ip_enabled=os_tmplt.floating_ip_enabled,
107 docker_volume_size=os_tmplt.docker_volume_size,
108 server_type=os_tmplt.server_type,
109 flavor=os_tmplt.flavor_id,
110 master_flavor=os_tmplt.master_flavor_id,
112 fixed_net=os_tmplt.fixed_network,
113 fixed_subnet=os_tmplt.fixed_subnet,
114 registry_enabled=os_tmplt.registry_enabled,
115 insecure_registry=os_tmplt.insecure_registry,
116 docker_storage_driver=os_tmplt.docker_storage_driver,
117 dns_nameserver=os_tmplt.dns_nameserver,
118 public=os_tmplt.public,
119 tls_disabled=os_tmplt.tls_disabled,
120 http_proxy=os_tmplt.http_proxy,
121 https_proxy=os_tmplt.https_proxy,
122 no_proxy=os_tmplt.no_proxy,
123 volume_driver=os_tmplt.volume_driver,
124 master_lb_enabled=os_tmplt.master_lb_enabled,
125 labels=os_tmplt.labels