-
Notifications
You must be signed in to change notification settings - Fork 3
/
blog.py
183 lines (149 loc) · 8.97 KB
/
blog.py
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
from __future__ import absolute_import
import logging
import uuid
import sys
import time
logging.basicConfig( stream=sys.stderr, format='%(funcName)s:%(levelname)s:%(message)s', level=logging.DEBUG )
import xiiot_api
from xiiot_api.api.application_api import ApplicationApi
from xiiot_api.rest import ApiException
XI_IOT_ENDPOINT='https://iot.nutanix.com'
USER_EMAIL = ""
USER_PWD = ""
class ApplicationApiWrapper():
"""ApplicationApi Test Class"""
def __init__(self, xi_iot_endpoint, userEmail, userPwd):
self.configuration, _ = self.loginUsingAuthTag(xi_iot_endpoint, userEmail, userPwd)
self.authorization = self.configuration.get_api_key_with_prefix('Authorization')
self.api_client = xiiot_api.ApiClient(configuration=self.configuration)
self.ApplicationApi = xiiot_api.api.application_api.ApplicationApi(api_client=self.api_client)
self.AppStatusApi = xiiot_api.api.application_status_api.ApplicationStatusApi(api_client=self.api_client)
self.ProjectApi = xiiot_api.ProjectApi(api_client=self.api_client)
self.EdgeApi = xiiot_api.api.edge_api.EdgeApi(api_client=self.api_client)
def loginUsingAuthTag(self, xi_iot_endpoint, userEmail, userPwd):
configuration = xiiot_api.Configuration()
configuration.host = xi_iot_endpoint
api_client = xiiot_api.ApiClient(configuration=configuration)
# create an instance of the API class
api_instance = xiiot_api.AuthApi(api_client=api_client)
request = xiiot_api.Credential(userEmail, userPwd)
try:
# Lets the user log in.
api_response = api_instance.login_call_v2(request)
# Configure API key authorization: BearerToken
configuration.api_key['Authorization'] = api_response.token
configuration.api_key_prefix['Authorization'] = 'Bearer'
configuration.debug = False
return configuration, api_response
except ApiException as e:
logging.error("Exception when calling AuthApi->login_call_v2: %s", e)
raise
def create_application(self, app_name, app_manifest, project_id, edge_ids):
# ApplicationV2 | Describes the application creation request.
body = xiiot_api.ApplicationV2(name=app_name, app_manifest=app_manifest, project_id=project_id, edge_ids=edge_ids)
try:
api_response = self.ApplicationApi.application_create_v2(body, self.authorization)
# returns CreateDocumentResponseV2 object
return api_response
except ApiException as e:
logging.error("Exception when calling ApplicationApi->application_create_v2: %s", e)
raise
def delete_application(self, app_id):
try:
api_response = self.ApplicationApi.application_delete_v2(self.authorization, app_id)
# returns DeleteDocumentResponseV2 object
return api_response
except ApiException as e:
logging.error("Exception when calling ApplicationApi->application_create_v2: %s", e)
raise
def get_projects(self, project_name=None):
try:
if project_name is not None:
# int | 0-based index of the page to fetch results. (optional)
page_index = 0
page_size = 100 # int | Item count of each page. (optional)
# order_by = ['order_by_example'] # list[str] | Specify result order. Zero or more entries with format: <key> [desc] where orderByKeys lists allowed keys in each response. (optional)
# str | Specify result filter. Format is similar to a SQL WHERE clause. For example, to filter object by name with prefix foo, use: name LIKE 'foo%'. Supported filter keys are the same as order by keys. (optional)
filter = "name = \'%s\'" % project_name
api_response = self.ProjectApi.project_list_v2(
self.authorization, page_index=page_index, page_size=page_size, filter=filter)
else:
api_response = self.ProjectApi.project_list_v2(self.authorization)
# returns ProjectListPayload object
return api_response
except ApiException as e:
logging.error("Exception when calling ProjectApi->project_list_v2: %s",e)
raise
def get_applications_statuses(self, app_id=None):
try:
if app_id is not None:
api_response = self.AppStatusApi.application_status_get_v2(
self.authorization, app_id)
else:
api_response = self.AppStatusApi.application_status_list_v2(self.authorization)
logging.info("application_status_list_v2 API output: %s" % api_response)
# returns ApplicationStatusListPayload object
return api_response
except ApiException as e:
logging.error("Exception when calling ApplicationStatusApi->application_status_list/get_v2: %s", e)
raise
def get_edges(self):
# page_index = 789 # int | 0-based index of the page to fetch results. (optional)
# page_size = 789 # int | Item count of each page. (optional)
# order_by = ['order_by_example'] # list[str] | Specify result order. Zero or more entries with format: <key> [desc] where orderByKeys lists allowed keys in each response. (optional)
# filter = 'filter_example' # str | Specify result filter. Format is similar to a SQL WHERE clause. For example, to filter object by name with prefix foo, use: name LIKE 'foo%'. Supported filter keys are the same as order by keys. (optional)
try:
#api_response = api_instance.edge_list_v2(authorization, page_index=page_index, page_size=page_size, order_by=order_by, filter=filter)
api_response = self.EdgeApi.edge_list_v2(self.authorization)
logging.info("edge_list API output: %s" % api_response)
# returns EdgeListPayload object
return api_response
except ApiException as e:
logging.error("Exception when calling Edgepi->edge_list_v2: %s", e)
raise
def project_get_edges(self, project_id):
# page_index = 789 # int | 0-based index of the page to fetch results. (optional)
# page_size = 789 # int | Item count of each page. (optional)
# order_by = ['order_by_example'] # list[str] | Specify result order. Zero or more entries with format: <key> [desc] where orderByKeys lists allowed keys in each response. (optional)
# filter = 'filter_example' # str | Specify result filter. Format is similar to a SQL WHERE clause. For example, to filter object by name with prefix foo, use: name LIKE 'foo%'. Supported filter keys are the same as order by keys. (optional)
try:
#api_response = api_instance.edge_list_v2(authorization, page_index=page_index, page_size=page_size, order_by=order_by, filter=filter)
api_response = self.EdgeApi.project_get_edges_v2(project_id, self.authorization)
logging.info("edge_list API output: %s" % api_response)
# returns EdgeListPayload object
return api_response
except ApiException as e:
logging.error("Exception when calling Edgepi->project_get_edges_v2: %s", e)
raise
def main():
"""Test application_create_v2 and application_delete_v2 APIs
"""
app_api_wrapper = ApplicationApiWrapper(XI_IOT_ENDPOINT, USER_EMAIL, USER_PWD)
PROJECT_NAME="Default Project"
logging.info("Getting id of project: %s", PROJECT_NAME)
projectListPayload = app_api_wrapper.get_projects(PROJECT_NAME)
for project in projectListPayload.result:
logging.info("%s project id: %s", PROJECT_NAME, project.id)
project_id = project.id
break
app_name = "flask-web-server-blog"
with open("./flask-web-server.yaml", "r") as yamlFile:
app_manifest = yamlFile.read()
edgeListPayload = app_api_wrapper.project_get_edges(project_id)
edge_ids = []
for edge in edgeListPayload.result:
logging.info("edge name: %s | edge serial number: %s" % (edge.name, edge.serial_number))
edge_ids.append(edge.id)
#This test does not pass project_id as its documented as optional
logging.info("Creating flask-web-server app...")
createDocumentResponseV2 = app_api_wrapper.create_application(app_name, app_manifest, project_id, edge_ids)
logging.info("Created application with id: %s", createDocumentResponseV2.id)
time.sleep(60)
logging.info("Getting app status for app id: %s...", createDocumentResponseV2.id)
applicationStatusListPayload = app_api_wrapper.get_applications_statuses(createDocumentResponseV2.id)
logging.info("Received app status for app id: %s", createDocumentResponseV2.id)
logging.info("Deleteing application with id: %s...", createDocumentResponseV2.id)
deleteDocumentResponseV2 = app_api_wrapper.delete_application(createDocumentResponseV2.id)
logging.info("Deleted application with id: %s", deleteDocumentResponseV2.id)
if __name__ == "__main__":
main()