summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/utils/magnum_utils.py
blob: c7446668dab649901b5a4192b7ea2011038d3ab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
#                    and others.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging

from magnumclient.client import Client

from snaps.domain.cluster_template import ClusterTemplate
from snaps.openstack.utils import keystone_utils

__author__ = 'spisarski'

logger = logging.getLogger('magnum_utils')


def magnum_client(os_creds):
    """
    Retrieves the Magnum client
    :param os_creds: the OpenStack credentialsf
    :return: the client
    """
    logger.debug('Retrieving Magnum Client')
    return Client(str(os_creds.magnum_api_version),
                  session=keystone_utils.keystone_session(os_creds))


def create_cluster_template(magnum, cluster_template_config):
    """
    Creates a Magnum Cluster Template object in OpenStack
    :param magnum: the Magnum client
    :param cluster_template_config: a ClusterTemplateConfig object
    :return: a SNAPS ClusterTemplate domain object
    """
    config_dict = cluster_template_config.magnum_dict()
    os_cluster_template = magnum.cluster_templates.create(**config_dict)
    logger.info('Creating cluster template named [%s]',
                cluster_template_config.name)
    return __map_os_cluster_template(os_cluster_template)


def delete_cluster_template(magnum, tmplt_id):
    """
    Deletes a Cluster Template from OpenStack
    :param magnum: the Magnum client
    :param tmplt_id: the cluster template ID to delete
    """
    logger.info('Deleting cluster template with ID [%s]', tmplt_id)
    magnum.cluster_templates.delete(tmplt_id)


def __map_os_cluster_template(os_tmplt):
    """
    Returns a SNAPS ClusterTemplate object from an OpenStack ClusterTemplate
    object
    :param os_tmplt: the OpenStack ClusterTemplate object
    :return: SNAPS ClusterTemplate object
    """
    return ClusterTemplate(
        id=os_tmplt.uuid,
        name=os_tmplt.name,
        image=os_tmplt.image_id,
        keypair=os_tmplt.keypair_id,
        network_driver=os_tmplt.network_driver,
        external_net=os_tmplt.external_network_id,
        floating_ip_enabled=os_tmplt.floating_ip_enabled,
        docker_volume_size=os_tmplt.docker_volume_size,
        server_type=os_tmplt.server_type,
        flavor=os_tmplt.flavor_id,
        master_flavor=os_tmplt.master_flavor_id,
        coe=os_tmplt.coe,
        fixed_net=os_tmplt.fixed_network,
        fixed_subnet=os_tmplt.fixed_subnet,
        registry_enabled=os_tmplt.registry_enabled,
        insecure_registry=os_tmplt.insecure_registry,
        docker_storage_driver=os_tmplt.docker_storage_driver,
        dns_nameserver=os_tmplt.dns_nameserver,
        public=os_tmplt.public,
        tls_disabled=os_tmplt.tls_disabled,
        http_proxy=os_tmplt.http_proxy,
        https_proxy=os_tmplt.https_proxy,
        no_proxy=os_tmplt.no_proxy,
        volume_driver=os_tmplt.volume_driver,
        master_lb_enabled=os_tmplt.master_lb_enabled,
        labels=os_tmplt.labels
    )