summaryrefslogtreecommitdiffstats
path: root/pike/vagrant/test/extsys.py
blob: ca87631c2914de6c346cb02931eab416d72923e8 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Copyright (c) 2017-2018 Wind River Systems, Inc.
#
# 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 json
import logging
import re

from rest_framework import status
from common.exceptions import VimDriverNewtonException
from common.utils import restcall

logger = logging.getLogger(__name__)

tisr4 = {
    "createTime": "2017-04-01 02:22:27",
    "domain": "Default",
    "name": "TiS_R4",
    "password": "admin",
    "tenant": "admin",
    "type": "openstack",
    "url": "http://192.168.1.10:5000/v3",
    "userName": "admin",
    "vendor": "OpenStack",
    "version": "pike",
    "vimId": "openstack-hudson-dc_RegionOne",
    'cloud_owner': 'openstack-hudson-dc',
    'cloud_region_id': 'RegionOne',
    'cloud_extra_info': '',
    'insecure': 'True',
}


def mock_get_vim_by_id(method):
    def wrapper(vimid):
        return tisr4
    return wrapper


def mock_delete_vim_by_id(method):
    def wrapper(vimid):
        return status.HTTP_202_ACCEPTED
    return wrapper

# def get_vims():
#    retcode, content, status_code = \
#        req_by_msb("/api/aai-cloudInfrastructure/v1/cloud-infrastructure/cloud-regions", "GET")
#    if retcode != 0:
#        logger.error("Status code is %s, detail is %s.", status_code, content)
#        raise VimDriverNewtonException("Failed to query VIMs from extsys.")
#    return json.JSONDecoder().decode(content)


@mock_get_vim_by_id
def get_vim_by_id(vim_id):

    cloud_owner, cloud_region_id = decode_vim_id(vim_id)

    if cloud_owner and cloud_region_id:
        retcode, content, status_code = \
            restcall.req_to_aai("/cloud-infrastructure/cloud-regions/cloud-region/%s/%s"
                                % (cloud_owner, cloud_region_id), "GET")
        if retcode != 0:
            logger.error("Status code is %s, detail is %s.", status_code, content)
            raise VimDriverNewtonException(
                "Failed to query VIM with id (%s:%s,%s)." % (vim_id, cloud_owner, cloud_region_id),
                status_code, content)
        tmp_viminfo = json.JSONDecoder().decode(content)

        # assume esr-system-info-id is composed by {cloud-owner} _ {cloud-region-id}
        vimid = cloud_owner + "_" + cloud_region_id
        base_url = "/cloud-infrastructure/cloud-regions/cloud-region"
        esr_url = "/%s/%s/esr-system-info-list/esr-system-info/%s" % (cloud_owner, cloud_region_id, vimid)
        retcode2, content2, status_code2 = restcall.req_to_aai(base_url + esr_url, "GET")
        if retcode2 != 0:
            logger.error("Status code is %s, detail is %s.", status_code, content)
            raise VimDriverNewtonException(
                "Failed to query ESR system with id (%s:%s,%s)." % (vim_id, cloud_owner, cloud_region_id),
                status_code, content)
        tmp_authinfo = json.JSONDecoder().decode(content2)

        # convert vim information
        if tmp_viminfo:
            viminfo = {}
            viminfo['vimId'] = vim_id
            viminfo['cloud_owner'] = cloud_owner
            viminfo['cloud_region_id'] = cloud_region_id
            viminfo['type'] = tmp_viminfo['cloud-type']
            viminfo['name'] = tmp_viminfo['complex-name']
            viminfo['version'] = tmp_viminfo['cloud-region-version']
            viminfo['cloud_extra_info'] = tmp_viminfo['cloud-extra-info']

            if tmp_authinfo:
                viminfo['userName'] = tmp_authinfo['user-name']
                viminfo['password'] = tmp_authinfo['password']
                viminfo['domain'] = tmp_authinfo['cloud-domain']
                viminfo['url'] = tmp_authinfo['service-url']
                viminfo['tenant'] = tmp_authinfo['default-tenant']
                viminfo['cacert'] = tmp_authinfo['ssl-cacert']
                viminfo['insecure'] = tmp_authinfo['ssl-insecure']
            else:
                return None

            return viminfo
        else:
            return None
    else:
        return None


@mock_delete_vim_by_id
def delete_vim_by_id(vim_id):
    cloud_owner, cloud_region_id = decode_vim_id(vim_id)
    if cloud_owner and cloud_region_id:
        del_url = "/cloud-infrastructure/cloud-regions/cloud-region/%s/%s"
        retcode, content, status_code = restcall.req_to_aai(del_url % (cloud_owner, cloud_region_id),
                                                            "DELETE")
        if retcode != 0:
            logger.error("Status code is %s, detail is %s.", status_code, content)
            raise VimDriverNewtonException(
                "Failed to delete VIM in AAI with id (%s:%s,%s)." % (vim_id, cloud_owner, cloud_region_id),
                status_code, content)
        return 0
    # return non zero if failed to decode cloud owner and region id
    return 1


def decode_vim_id(vim_id):
    m = re.search(r'^([0-9a-zA-Z-]+)_([0-9a-zA-Z_-]+)$', vim_id)
    cloud_owner, cloud_region_id = m.group(1), m.group(2)
    return cloud_owner, cloud_region_id