aboutsummaryrefslogtreecommitdiffstats
path: root/lib/auto/testcase/resiliency/AutoResilItfCloud.py
blob: 302a662c22fed845ac72153586ae53a99bf30d4a (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python3

# ===============LICENSE_START=======================================================
# Apache-2.0
# ===================================================================================
# Copyright (C) 2018 Wipro. 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.
# ===============LICENSE_END=========================================================


# OPNFV Auto project
# https://wiki.opnfv.org/pages/viewpage.action?pageId=12389095

# Use case 02: Resilience Improvements
# Use Case description: https://wiki.opnfv.org/display/AUTO/Auto+Use+Cases
# Test case design: https://wiki.opnfv.org/display/AUTO/Use+case+2+%28Resilience+Improvements+through+ONAP%29+analysis

# This module: interfaces with cloud managers (OpenStack, Kubernetes, AWS, ...)


######################################################################
# import statements
import AutoResilGlobal
import time

# for method 1 and 2
import openstack

#for method 3
#from openstack import connection

def openstack_list_servers(conn):
    """List OpenStack servers."""
    # see https://docs.openstack.org/python-openstacksdk/latest/user/proxies/compute.html
    if conn != None:
        print("\nList Servers:")

        try:
            i=1
            for server in conn.compute.servers():
                print('Server',str(i))
                print('  Name:',server.name)
                print('  ID:',server.id)
                print('  key:',server.key_name)
                print('  status:',server.status)
                print('  AZ:',server.availability_zone)
                print('Details:\n',server)
                i+=1
        except Exception as e:
            print("Exception:",type(e), e)
            print("No Servers\n")


def openstack_list_networks(conn):
    """List OpenStack networks."""
    # see https://docs.openstack.org/python-openstacksdk/latest/user/proxies/network.html
    if conn != None:
        print("\nList Networks:")

        try:
            i=1
            for network in conn.network.networks():
                print('Network',str(i),'\n',network,'\n')
                i+=1
        except Exception as e:
            print("Exception:",type(e), e)
            print("No Networks\n")


def openstack_list_volumes(conn):
    """List OpenStack volumes."""
    # see https://docs.openstack.org/python-openstacksdk/latest/user/proxies/block_storage.html
    # note: The block_storage member will only be added if the service is detected.
    if conn != None:
        print("\nList Volumes:")

        try:
            i=1
            for volume in conn.block_storage.volumes():
                print('Volume',str(i))
                print('  Name:',volume.name)
                print('  ID:',volume.id)
                print('  size:',volume.size)
                print('  status:',volume.status)
                print('  AZ:',volume.availability_zone)
                print('Details:\n',volume)
                i+=1
        except Exception as e:
            print("Exception:",type(e), e)
            print("No Volumes\n")


def openstack_list_users(conn):
    """List OpenStack users."""
    # see https://docs.openstack.org/python-openstacksdk/latest/user/guides/identity.html
    if conn != None:
        print("\nList Users:")

        try:
            i=1
            for user in conn.identity.users():
                print('User',str(i),'\n',user,'\n')
                i+=1
        except Exception as e:
            print("Exception:",type(e), e)
            print("No Users\n")

def openstack_list_projects(conn):
    """List OpenStack projects."""
    # see https://docs.openstack.org/python-openstacksdk/latest/user/guides/identity.html
    if conn != None:
        print("\nList Projects:")

        try:
            i=1
            for project in conn.identity.projects():
                print('Project',str(i),'\n',project,'\n')
                i+=1
        except Exception as e:
            print("Exception:",type(e), e)
            print("No Projects\n")


def openstack_list_domains(conn):
    """List OpenStack domains."""
    # see https://docs.openstack.org/python-openstacksdk/latest/user/guides/identity.html
    if conn != None:
        print("\nList Domains:")

        try:
            i=1
            for domain in conn.identity.domains():
                print('Domain',str(i),'\n',domain,'\n')
                i+=1
        except Exception as e:
            print("Exception:",type(e), e)
            print("No Domains\n")







def gdtest_openstack():

    # Method 1 (preferred) : assume there is a clouds.yaml file in PATH, starting path search with local directory
    #conn = openstack.connect(cloud='armopenstack', region_name='RegionOne')
    #conn = openstack.connect(cloud='hpe16openstackEuphrates', region_name='RegionOne')
    conn = openstack.connect(cloud='hpe16openstackFraser', region_name='RegionOne')
    # if getting error: AttributeError: module 'openstack' has no attribute 'connect', check that openstack is installed for this python version


    # Method 2: pass arguments directly, all as strings
    # see details at https://docs.openstack.org/python-openstacksdk/latest/user/connection.html
    # conn = openstack.connect(
        # auth_url='https://10.10.50.103:5000/v2.0',
        # project_name='admin',
        # username='admin',
        # password='opnfv_secret',
        # region_name='RegionOne',
        # )
    # conn = openstack.connect(
        # auth_url='http://10.16.0.101:5000/v2.0',
        # project_name='admin',
        # username='admin',
        # password='opnfv_secret',
        # region_name='RegionOne',
        # )
    # if getting error: AttributeError: module 'openstack' has no attribute 'connect', check that openstack is installed for this python version


    # Method 3: create Connection object directly
    # auth_args = {
        # #'auth_url': 'https://10.10.50.103:5000/v2.0',  # Arm
        # #'auth_url': 'http://10.16.0.101:5000/v2.0',  # hpe16, Euphrates
        # 'auth_url': 'http://10.16.0.107:5000/v3',  # hpe16, Fraser
        # 'project_name': 'admin',
        # 'username': 'admin',
        # 'password': 'opnfv_secret',
        # 'region_name': 'RegionOne',
        # 'domain': 'Default'}
    # conn = connection.Connection(**auth_args)

    #conn = connection.Connection(
        #auth_url='http://10.16.0.107:5000/v3',
        #project_name='admin',
        #username='admin',
        #password='opnfv_secret')


    openstack_list_servers(conn)
    openstack_list_networks(conn)
    openstack_list_volumes(conn)
    openstack_list_users(conn)
    openstack_list_projects(conn)
    openstack_list_domains(conn)

    # VM: hpe16-Auto-UC2-gdtest-compute1
    gds_ID = '715c677a-7914-4ca8-8c6d-75bf29eeb940'
    gds = conn.compute.get_server(gds_ID)
    print('\ngds.name=',gds.name)
    print('gds.status=',gds.status)
    print('suspending...')
    conn.compute.suspend_server(gds_ID)  # NOT synchronous: returns before suspension action is completed
    wait_seconds = 10
    print('  waiting',wait_seconds,'seconds...')
    time.sleep(wait_seconds)
    gds = conn.compute.get_server(gds_ID)  # need to refresh data; not maintained live
    print('gds.status=',gds.status)
    print('resuming...')
    conn.compute.resume_server(gds_ID)
    print('  waiting',wait_seconds,'seconds...')
    time.sleep(wait_seconds)
    gds = conn.compute.get_server(gds_ID)  # need to refresh data; not maintained live
    print('gds.status=',gds.status)



    #VM: test3
    gds_ID = 'd3ceffc3-5967-4f18-b8b5-b1b2bd7ab76d'
    gds = conn.compute.get_server(gds_ID)
    print('\ngds.name=',gds.name)
    print('gds.status=',gds.status)
    print('suspending...')
    conn.compute.suspend_server(gds_ID)  # NOT synchronous: returns before suspension action is completed
    wait_seconds = 10
    print('  waiting',wait_seconds,'seconds...')
    time.sleep(wait_seconds)
    gds = conn.compute.get_server(gds_ID)  # need to refresh data; not maintained live
    print('gds.status=',gds.status)
    print('resuming...')
    conn.compute.resume_server(gds_ID)
    print('  waiting',wait_seconds,'seconds...')
    time.sleep(wait_seconds)
    gds = conn.compute.get_server(gds_ID)  # need to refresh data; not maintained live
    print('gds.status=',gds.status)

    #Volume: hpe16-Auto-UC2-gdtest-volume1
    gdv_ID = '5a6c1dbd-5097-4a9b-8f79-6f03cde18bf6'
    gdv = conn.block_storage.get_volume(gdv_ID)
    # no API for stopping/restarting a volume... only delete. ONAP would have to completely migrate a VNF depending on this volume
    print('\ngdv.name=',gdv.name)
    print('gdv.status=',gdv.status)
    #gdv_recreate = gdv
    #print('deleting...')
    #conn.block_storage.delete_volume(gdv_ID)
    #conn.block_storage.delete_volume(gdv)
    #print('recreating...')
    #gdv = conn.block_storage.create_volume(<attributes saved in gdv_recreate>)


    # get_server(server): Get a single Server
    # Parameters:    server – The value can be the ID of a server or a Server instance.
    # conn.compute.get_server(server)

    # suspend_server(server): Suspends a server and changes its status to SUSPENDED.
    # Parameters:    server – Either the ID of a server or a Server instance.
    # conn.compute.suspend_server(server)

    # resume_server(server): Resumes a suspended server and changes its status to ACTIVE.
    # Parameters:    server – Either the ID of a server or a Server instance.
    # conn.compute.resume_server(server)


def main():

    print("\nTest Auto Cloud Interface")

    gdtest_openstack()

    print("\nCiao\n")

if __name__ == "__main__":
    main()


# OpenStack HTTP API: https://developer.openstack.org/api-ref/compute/
#{your_compute_service_url}/servers/{server_id}/action
#GET
#http://mycompute.pvt/compute/v2.1/servers/{server_id}/suspend
#http://mycompute.pvt/compute/v2.1/servers/{server_id}/resume
# but better use the python unified client