aboutsummaryrefslogtreecommitdiffstats
path: root/lib/auto/testcase/resiliency/AutoResilItfCloud.py
blob: 69c532754ae524e8e7f87681adab7cf793a9f335 (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
#!/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

# for method 1 and 2
#import openstack

#for method 3
from openstack import connection

def os_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),'\n',server,'n')
                i+=1
        except Exception as e:
            print("Exception:",type(e), e)
            print("No Servers\n")


def os_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 os_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),'\n',volume,'n')
                i+=1
        except Exception as e:
            print("Exception:",type(e), e)
            print("No Volumes\n")

            
def os_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 os_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 os_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: 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='hpe16openstack', region_name='RegionOne')
    # getting error: AttributeError: module 'openstack' has no attribute 'connect'

    # 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',
        # )
    # getting error: AttributeError: module 'openstack' has no attribute 'connect'

    # 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')


    os_list_servers(conn)
    os_list_networks(conn)
    os_list_volumes(conn)
    os_list_users(conn)
    os_list_projects(conn)
    os_list_domains(conn)


    # 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("Ciao\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