def create_management_access_token(self): """Does a POST request to /managementTokens. Use this api to get a new management api token. Returns: ManagementAccessToken: Response from the API. Successfully created new token. Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ try: self.logger.info('create_management_access_token called.') # Prepare query URL self.logger.info( 'Preparing query URL for create_management_access_token.') _url_path = '/managementTokens' _query_builder = Configuration.get_base_uri() _query_builder += _url_path _query_url = APIHelper.clean_url(_query_builder) # Prepare headers self.logger.info( 'Preparing headers for create_management_access_token.') _headers = {'accept': 'application/json'} # Prepare and execute request self.logger.info( 'Preparing and executing request for create_management_access_token.' ) _request = self.http_client.post(_query_url, headers=_headers) CustomAuth.apply(_request) _context = self.execute_request( _request, name='create_management_access_token') # Endpoint and global error handling using HTTP status codes. self.logger.info( 'Validating response for create_management_access_token.') if _context.response.status_code == 401: raise APIException('Invalid token.', _context) elif _context.response.status_code == 500: raise ErrorException('Unexpected error.', _context) self.validate_response(_context) # Return appropriate type return APIHelper.json_deserialize( _context.response.raw_body, ManagementAccessToken.from_dictionary) except Exception as e: self.logger.error(e, exc_info=True) raise
def get_app_settings(self): """Does a GET request to /appSettings. Returns app settings object. Returns: AppSettings: Response from the API. Successful operation Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ try: self.logger.info('get_app_settings called.') # Prepare query URL self.logger.info('Preparing query URL for get_app_settings.') _url_path = '/appSettings' _query_builder = Configuration.get_base_uri() _query_builder += _url_path _query_url = APIHelper.clean_url(_query_builder) # Prepare headers self.logger.info('Preparing headers for get_app_settings.') _headers = {'accept': 'application/json'} # Prepare and execute request self.logger.info( 'Preparing and executing request for get_app_settings.') _request = self.http_client.get(_query_url, headers=_headers) OAuth2.apply(_request) _context = self.execute_request(_request, name='get_app_settings') # Endpoint and global error handling using HTTP status codes. self.logger.info('Validating response for get_app_settings.') if _context.response.status_code == 401: raise APIException('Invalid token', _context) elif _context.response.status_code == 500: raise ErrorErrorException('Unexpected error', _context) elif _context.response.status_code == 502: raise APIException('Bad Gateway.', _context) elif _context.response.status_code == 504: raise APIException('Gateway Timeout.', _context) self.validate_response(_context) # Return appropriate type return APIHelper.json_deserialize(_context.response.raw_body, AppSettings.from_dictionary) except Exception as e: self.logger.error(e, exc_info=True) raise
def __init__(self, reason, context): """Constructor for the ErrorException class Args: reason (string): The reason (or error message) for the Exception to be raised. context (HttpContext): The HttpContext of the API call. """ super(ErrorException, self).__init__(reason, context) dictionary = APIHelper.json_deserialize(self.context.response.raw_body) if isinstance(dictionary, dict): self.unbox(dictionary)
def get_volume(self, volume_name): """Does a GET request to /volumes/{volumeName}. Gets the status of persistent volume owned by this app. Args: volume_name (string): Name of the volume unique within the app instance. Returns: VolumeInfo: Response from the API. Successful operation Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ try: self.logger.info('get_volume called.') # Prepare query URL self.logger.info('Preparing query URL for get_volume.') _url_path = '/volumes/{volumeName}' _url_path = APIHelper.append_url_with_template_parameters(_url_path, { 'volumeName': volume_name }) _query_builder = Configuration.get_base_uri() _query_builder += _url_path _query_url = APIHelper.clean_url(_query_builder) # Prepare headers self.logger.info('Preparing headers for get_volume.') _headers = { 'accept': 'application/json' } # Prepare and execute request self.logger.info('Preparing and executing request for get_volume.') _request = self.http_client.get(_query_url, headers=_headers) OAuth2.apply(_request) _context = self.execute_request(_request, name = 'get_volume') # Endpoint and global error handling using HTTP status codes. self.logger.info('Validating response for get_volume.') if _context.response.status_code == 401: raise APIException('Unauthorized', _context) elif _context.response.status_code == 404: raise APIException('Volume doesn\'t exist.', _context) elif _context.response.status_code == 500: raise ErrorErrorException('Unexpected error', _context) elif _context.response.status_code == 502: raise APIException('Bad Gateway.', _context) elif _context.response.status_code == 504: raise APIException('Gateway Timeout.', _context) self.validate_response(_context) # Return appropriate type return APIHelper.json_deserialize(_context.response.raw_body, VolumeInfo.from_dictionary) except Exception as e: self.logger.error(e, exc_info = True) raise
def get_protected_source_volume_info(self, source_id): """Does a GET request to /protectedSourceVolumeInfo/{sourceId}. Gets the list of volumes for a snapshot of a protected source. Args: source_id (int): Unique ID of the protected source. Returns: ProtectedSourceVolumeInfo: Response from the API. Successful operation Raises: APIException: When an error occurs while fetching the data from the remote API. This exception includes the HTTP Response code, an error message, and the HTTP body that was received in the request. """ try: self.logger.info('get_protected_source_volume_info called.') # Prepare query URL self.logger.info( 'Preparing query URL for get_protected_source_volume_info.') _url_path = '/protectedSourceVolumeInfo/{sourceId}' _url_path = APIHelper.append_url_with_template_parameters( _url_path, {'sourceId': source_id}) _query_builder = Configuration.get_base_uri() _query_builder += _url_path _query_url = APIHelper.clean_url(_query_builder) # Prepare headers self.logger.info( 'Preparing headers for get_protected_source_volume_info.') _headers = {'accept': 'application/json'} # Prepare and execute request self.logger.info( 'Preparing and executing request for get_protected_source_volume_info.' ) _request = self.http_client.get(_query_url, headers=_headers) OAuth2.apply(_request) _context = self.execute_request( _request, name='get_protected_source_volume_info') # Endpoint and global error handling using HTTP status codes. self.logger.info( 'Validating response for get_protected_source_volume_info.') if _context.response.status_code == 401: raise APIException('Unauthorized', _context) elif _context.response.status_code == 404: raise APIException('Snapshot does not exist.', _context) elif _context.response.status_code == 500: raise ErrorErrorException('Unexpected error', _context) elif _context.response.status_code == 502: raise APIException('Bad Gateway.', _context) elif _context.response.status_code == 504: raise APIException('Gateway Timeout.', _context) self.validate_response(_context) # Return appropriate type return APIHelper.json_deserialize( _context.response.raw_body, ProtectedSourceVolumeInfo.from_dictionary) except Exception as e: self.logger.error(e, exc_info=True) raise