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 create_cluster_template(magnum, cluster_template_config):
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
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)
52 def delete_cluster_template(magnum, tmplt_id):
54 Deletes a Cluster Template from OpenStack
55 :param magnum: the Magnum client
56 :param tmplt_id: the cluster template ID to delete
58 logger.info('Deleting cluster template with ID [%s]', tmplt_id)
59 magnum.cluster_templates.delete(tmplt_id)
62 def __map_os_cluster_template(os_tmplt):
64 Returns a SNAPS ClusterTemplate object from an OpenStack ClusterTemplate
66 :param os_tmplt: the OpenStack ClusterTemplate object
67 :return: SNAPS ClusterTemplate object
69 return ClusterTemplate(
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,
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