Skip to content

Commit 42d5d47

Browse files
author
Mark Beacom
authored
Merge pull request #2 from mbeacom/cli-adjustments
Pointed API usecase and CLI additions
2 parents b42202c + 054933b commit 42d5d47

File tree

5 files changed

+438
-85
lines changed

5 files changed

+438
-85
lines changed

cloudendure/api.py

Lines changed: 53 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@
99
1010
"""
1111
import datetime
12+
import json
1213
import logging
1314
import os
1415
from typing import Any, Dict, List
1516
from webbrowser import open_new_tab
1617

1718
import requests
19+
from requests.models import Response
1820

1921
from .config import CloudEndureConfig
2022
from .exceptions import CloudEndureException, CloudEndureUnauthorized
21-
from .models import Machine, Project
2223

2324
HOST: str = os.environ.get('CLOUDENDURE_HOST', 'https://console.cloudendure.com')
2425
API_VERSION: str = os.environ.get('CLOUDENDURE_API_VERSION', 'latest').lower()
2526
AUTH_TTL = datetime.timedelta(seconds=int(os.environ.get('CLOUDENDURE_AUTH_TTL', '3600'))) # Default to 60 minutes.
27+
METHOD_TYPES = ['get', 'post', 'patch', 'delete', 'put']
2628

2729
logger = logging.getLogger(__name__)
2830

@@ -49,26 +51,8 @@ def __init__(self, *args, **kwargs):
4951
"""
5052
time_now = datetime.datetime.utcnow()
5153

52-
# if config is None:
53-
# config = {
54-
# 'host': os.environ.get('CLOUDENDURE_HOST', 'https://console.cloudendure.com').lower(),
55-
# 'api_version': os.environ.get('CLOUDENDURE_API_VERSION', 'latest').lower(),
56-
# 'auth_ttl': datetime.timedelta(seconds=int(os.environ.get('CLOUDENDURE_AUTH_TTL', '3600'))),
57-
# }
58-
5954
self.api_endpoint: str = f'{HOST}/api/{API_VERSION}'
6055
self.config = CloudEndureConfig()
61-
# self.credentials = {
62-
# 'username': os.environ.get('CLOUDENDURE_USERNAME', ''),
63-
# 'password': os.environ.get('CLOUDENDURE_PASSWORD', ''),
64-
# 'token': os.environ.get('CLOUDENDURE_TOKEN', ''),
65-
# 'last_updated': time_now,
66-
# }
67-
68-
# params = ['username', 'password', 'token']
69-
# for param in params:
70-
# if not self.credentials.get(param, ''):
71-
# self.credentials[param] = self.config.get(param, '')
7256

7357
self.projects: List[str] = []
7458
self.session = requests.Session()
@@ -103,8 +87,8 @@ def login(self, username='', password=''):
10387
_auth: Dict[str, str] = {'username': _username, 'password': _password}
10488

10589
# Attempt to login to the CloudEndure API via a POST request.
106-
response: requests.Response = self.session.post(f'{self.api_endpoint}/{endpoint}', json=_auth)
107-
print('response: ', response, response.status_code)
90+
response: requests.Response = self.api_call('login', 'post', data=json.dumps(_auth))
91+
# response: requests.Response = self.session.post(f'{self.api_endpoint}/{endpoint}', json=_auth)
10892

10993
# Check whether or not the request was successful.
11094
if response.status_code not in [200, 307]:
@@ -116,7 +100,7 @@ def login(self, username='', password=''):
116100
logger.error('CloudEndure authentication failure limit reached! Please try again later!')
117101
raise CloudEndureUnauthorized()
118102

119-
print('response: ', response, response.cookies)
103+
# print('response: ', response, response.cookies)
120104
_xsrf_token: str = str(response.cookies['XSRF-TOKEN'])
121105

122106
# Strip the XSRF token of wrapping double-quotes from the cookie.
@@ -130,6 +114,52 @@ def login(self, username='', password=''):
130114
self.timestamps['last_call'] = time_now
131115
return True
132116

117+
@staticmethod
118+
def get_endpoint(path: str, api_version: str = 'latest', host: str = 'https://console.cloudendure.com') -> str:
119+
"""Build the endpoint path.
120+
121+
Returns:
122+
str: The CloudEndure API endpoint to be used.
123+
124+
"""
125+
return f'{host}/api/{api_version}/{path}'
126+
127+
def api_call(self, path: str, method: str = 'get', data=None) -> Response:
128+
"""Handle CloudEndure API calls based on the defined parameters.
129+
130+
Args:
131+
path (str): The path to be used to perform the call.
132+
133+
Keyword Args:
134+
method (str): The API method call to be performed. i.e.: get,
135+
data (dict): The data dictionary to be used to perform the request.
136+
137+
Returns:
138+
requests.models.Response: The CloudEndure API response.
139+
140+
"""
141+
method = method.lower() # Ensure the provided method is lowercase.
142+
143+
if data is None:
144+
data = {}
145+
146+
if method not in METHOD_TYPES:
147+
print('Please specify a valid method type! Must be one of: ', METHOD_TYPES)
148+
return Response()
149+
150+
if method not in ['get', 'delete'] and data is None:
151+
print('Paramater mismatch! If calling anything other than get or delete provide data!')
152+
return Response()
153+
154+
# Attempt to call the CloudEndure API.
155+
try:
156+
ce_call = getattr(self.session, method)
157+
_path = self.get_endpoint(path)
158+
return ce_call(_path, data=data)
159+
except Exception as e:
160+
print(f'Exception encountered in CloudEndure API call: ({e})')
161+
return Response()
162+
133163
def check_creds(self, login=True):
134164
threshold = datetime.datetime.utcnow() - AUTH_TTL
135165

@@ -141,16 +171,6 @@ def check_creds(self, login=True):
141171
return {'status': 'expired'}
142172
return {'status': 'valid'}
143173

144-
def get_endpoint(self, path='', child_key='items'):
145-
response: requests.Response = self.session.get(f'{self.api_endpoint}/{path}')
146-
data: Dict[str, Any] = response.json()
147-
status_code: int = response.status_code
148-
print('status: ', status_code)
149-
print('data: ', data)
150-
if status_code not in [200, ]:
151-
raise CloudEndureException()
152-
return data.get(child_key, [])
153-
154174
def post_endpoint(self, path=''):
155175
response: requests.Response = self.session.post(f'{self.api_endpoint}/{path}')
156176
return response
@@ -161,7 +181,7 @@ def get_projects(self, current_project=''):
161181
response: requests.Response = self.session.get(f'{self.api_endpoint}/projects')
162182
data: Dict[str, Any] = response.json()
163183
status_code: int = response.status_code
164-
print('Status: ', status_code)
184+
165185
if status_code not in [200, ]:
166186
raise CloudEndureException()
167187
projects: List[Any] = data['items']
@@ -172,57 +192,9 @@ def get_projects(self, current_project=''):
172192

173193
return projects
174194

175-
def get_project(self, project):
176-
found_projects: List[Any] = self.get_endpoint(path='projects')
177-
if not project:
178-
return found_projects[0]
179-
try:
180-
return next(found_project for found_project in found_projects if found_project['name'] == project)
181-
except Exception as e:
182-
# Cowardly catch all Exception here.
183-
logger.error(f'{e} - get_project - attempting to find the provided project in account projects.')
184-
return {}
185-
186-
def get_token(self, project=''):
187-
"""Get the CloudEndure project installation token."""
188-
found_project: Dict[str, Any] = self.get_project(project)
189-
return found_project[0]['agentInstallationToken']
190-
191195
@classmethod
192196
def docs(self):
193197
"""Open the CloudEndure API documentation page."""
194198
docs_url: str = os.environ.get('CLOUDENDURE_API_DOCS', 'https://console.cloudendure.com/api_doc/apis.html')
195199
open_new_tab(docs_url)
196200
return docs_url
197-
198-
# def get_token(args):
199-
# # This function fetch the project installation token
200-
# # Usage: get_token(args)
201-
# # 'args' is script user input (args.user, args.password, args.agentname)
202-
# #
203-
# # Returns: -1 on failure
204-
# print "Fetching the installation token..."
205-
# session, resp, endpoint = login(args)
206-
# if session == -1:
207-
# print "Failed to login"
208-
# return -1
209-
210-
# project_name = args.project
211-
212-
# projects_resp = session.get(url=HOST+endpoint+'projects')
213-
# projects = json.loads(projects_resp.content)['items']
214-
215-
# project = [p for p in projects if project_name==p['name']]
216-
# if not project:
217-
# print 'Error! No project with name ' + args.project+ ' found'
218-
# return -1
219-
220-
# return project[0]['agentInstallationToken']
221-
222-
223-
# class CloudEndureCredentials:
224-
# """Define the CloudEndure Credentials object."""
225-
226-
# def __init__(self, *args, **kwargs):
227-
# """Initialize the CloudEndure credentials."""
228-
# self.

0 commit comments

Comments
 (0)