summaryrefslogtreecommitdiffstats
path: root/vio/vio/pub/vim/vimapi/nova/OperateServers.py
blob: 3075276993eef782d46260a4774f88653249506e (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Copyright (c) 2017-2018 VMware, 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.

import base64
import logging

from openstack import exceptions
from rest_framework import status

from vio.pub.exceptions import VimDriverVioException
from vio.pub.vim.vimapi.nova.OperateNova import OperateNova

logger = logging.getLogger(__name__)


class OperateServers(OperateNova):

    def create_server(self, data, project_id, create_req):
        param = {'username': data['username'],
                 'user_domain_name': 'default',
                 'project_domain_name': 'default',
                 'password': data['password'],
                 'auth_url': data['url'],
                 'project_id': project_id}
        cc = self.compute(param)
        req = {
            "name": create_req['name'],
            "flavorRef": cc.find_flavor(create_req['flavorId']).id
        }
        boot = create_req['boot']
        boot_type = boot['type']
        if int(boot_type) == 1:
            # boot from vol
            req['block_device_mapping_v2'] = [{
                'boot_index': "0",
                'uuid': boot["volumeId"],
                'source_type': 'volume',
                'destination_type': 'volume',
                'delete_on_termination': False
            }]
        elif int(boot_type) == 2:
            req['imageRef'] = cc.find_image(boot['imageId']).id
        else:
            raise VimDriverVioException(
                'Boot type should be 1 or 2.',
                status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
        networks = create_req.get('nicArray', [])
        if networks:
            req['networks'] = [{'port': n['portId']} for n in networks]
        az = create_req.get('availabilityZone', None)
        if az:
            req['availability_zone'] = az
        md = create_req.get('metadata', [])
        if md:
            req['metadata'] = {n['keyName']: n['value'] for n in md}
        userdata = create_req.get('userdata', None)
        if userdata:
            req['user_data'] = base64.encodestring(userdata)
        sg = create_req.get('securityGroups', [])
        if sg:
            req['security_groups'] = []
            for v in sg:
                req['security_groups'].append({'name': v})
        # todo attach volumes after server created
        volumes = create_req.get('volumeArray', [])
        if volumes:
            if not req.get('block_device_mapping_v2'):
                req['block_device_mapping_v2'] = []
            if 'imageRef' in req:
                req['block_device_mapping_v2'].append(
                    {
                        'boot_index': 0,
                        'uuid': req['imageRef'],
                        'source_type': 'image',
                        'destination_type': 'local',
                        'delete_on_termination': True
                    }
                )
            for vol in volumes:
                req['block_device_mapping_v2'].append(
                    {
                        'boot_index': -1,
                        'uuid': vol["volumeId"],
                        'source_type': 'volume',
                        'destination_type': 'volume',
                        'delete_on_termination': False
                    }
                )
        inject_files = create_req.get("contextArray", [])
        if inject_files:
            req["personality"] = []
            for i in inject_files:
                req["personality"].append(
                    {
                        "path": i["dest_path"],
                        "contents": i["source_data_base64"]
                    }
                )

        return cc.create_server(**req)

    def list_servers(self, data, project_id, **query):
        param = {'username': data['username'],
                 'user_domain_name': 'default',
                 'project_domain_name': 'default',
                 'password': data['password'],
                 'auth_url': data['url'],
                 'project_id': project_id}
        servers = self.compute(param).list_servers(**query)
        servers = list(servers)
        return servers

    def list_server_interfaces(self, data, project_id, server):
        param = {'username': data['username'],
                 'user_domain_name': 'default',
                 'project_domain_name': 'default',
                 'password': data['password'],
                 'auth_url': data['url'],
                 'project_id': project_id}
        interfaces = self.compute(param).list_server_interfaces(server)
        return list(interfaces)

    def get_server(self, data, project_id, server_id):
        param = {'username': data['username'],
                 'user_domain_name': 'default',
                 'project_domain_name': 'default',
                 'password': data['password'],
                 'auth_url': data['url'],
                 'project_id': project_id}
        try:
            project = self.compute(param).get_server(server_id=server_id)
            return project
        except exceptions.ResourceNotFound:
            return None

    def find_server(self, data, project_id, server_id):
        param = {'username': data['username'],
                 'user_domain_name': 'default',
                 'project_domain_name': 'default',
                 'password': data['password'],
                 'auth_url': data['url'],
                 'project_id': project_id}
        server = self.compute(param).find_server(
            server_id, ignore_missing=True)
        return server

    def delete_server(self, data, project_id, server_id):
        param = {'username': data['username'],
                 'user_domain_name': 'default',
                 'project_domain_name': 'default',
                 'password': data['password'],
                 'auth_url': data['url'],
                 'project_id': project_id}
        project = self.compute(param).delete_server(server_id)
        return project