def get_one(self, lcp_id): logger.info('Received a GET request for LCP %s' % (id, )) authentication.authorize(request, 'lcp:get_one') zones = [] try: zones = get_zones() except Exception as exception: logger.error(exception.message) raise err_utils.get_error(request.transaction_id, status_code=500, error_details=exception.message) for zone in zones: if zone["id"] == lcp_id: logger.debug('Returning: %s' % (zone, )) return zone error_msg = 'LCP %s not found' % (lcp_id, ) logger.info(error_msg) raise err_utils.get_error(request.transaction_id, message=error_msg, status_code=404)
def put(self, region_id, metadata_input): """Handle put request. :param region_id: region_id to update metadata to. :param metadata_input: json data :return: 201 created on success, 404 not found """ logger.info("Entered update region metadata") logger.debug("Got metadata: {}".format(metadata_input)) authentication.authorize(request, 'metadata:update') try: self._validate_request_input() # May raise an exception which will return status code 400 result = RegionService.update_region_metadata(region_id, metadata_input.metadata) logger.debug("Metadata was successfully added to " "region: {}. New metadata: {}".format(region_id, result)) event_details = 'Region {} metadata updated'.format(region_id) utils.audit_trail('update metadata', request.transaction_id, request.headers, region_id, event_details=event_details) return MetaData(result) except error_base.ErrorStatus as e: logger.error(e.message) raise err_utils.get_error(request.transaction_id, message=e.message, status_code=e.status_code) except Exception as exception: logger.error(exception.message) raise err_utils.get_error(request.transaction_id, status_code=500, error_details=exception.message)
def delete(self, region_id, metadata_key): """Handle delete request. :param region_id: region_id to update metadata to. :param metadata_key: metadata key to be deleted :return: 204 deleted """ logger.info("Entered delete region metadata with " "key: {}".format(metadata_key)) authentication.authorize(request, 'metadata:delete') try: # May raise an exception which will return status code 400 result = RegionService.delete_metadata_from_region(region_id, metadata_key) logger.debug("Metadata was successfully deleted.") event_details = 'Region {} metadata {} deleted'.format( region_id, metadata_key) utils.audit_trail('delete metadata', request.transaction_id, request.headers, region_id, event_details=event_details) except error_base.ErrorStatus as e: logger.error(e.message) raise err_utils.get_error(request.transaction_id, message=e.message, status_code=e.status_code) except Exception as exception: logger.error(exception.message) raise err_utils.get_error(request.transaction_id, status_code=500, error_details=exception.message)
def put(self, level): """update log level. :param level: the log level text name :return: """ logger.info("Changing log level to [{}]".format(level)) authentication.authorize(request, 'log:update') try: log_level = logging._levelNames.get(level.upper()) if log_level is not None: self._change_log_level(log_level) result = "Log level changed to {}.".format(level) logger.info(result) else: raise Exception( "The given log level [{}] doesn't exist.".format(level)) return LogChangeResult(result) except Exception as exception: logger.error("Fail to change log_level. Reason: {}".format( exception.message)) raise err_utils.get_error(request.transaction_id, status_code=500, error_details=exception.message)
def get(self, id=None): """Handle get request. :param id: Group ID :return: 200 OK on success, 404 Not Found otherwise. """ logger.info("Entered Get Group: id = {}".format(id)) authentication.authorize(request, 'group:get_one') try: result = GroupService.get_groups_data(id) logger.debug('Returning group, regions: {}'.format(result.regions)) return result except error_base.NotFoundError as e: logger.error("GroupsController - Group not found") raise err_utils.get_error(request.transaction_id, message=e.message, status_code=404) except Exception as exception: logger.error(exception.message) raise err_utils.get_error(request.transaction_id, status_code=500, error_details=exception.message)
def put(self, region_id, new_status): """Handle put request to modify region status :param region_id: :param new_status: :return: 200 for updated, 404 for region not found 400 invalid status """ logger.info("Entered update region status") logger.debug("Got status: {}".format(new_status.status)) authentication.authorize(request, 'status:put') try: allowed_status = conf.region_options.allowed_status_values[:] if new_status.status not in allowed_status: logger.error("Invalid status. Region status " "must be one of {}".format(allowed_status)) raise error_base.InputValueError( message="Invalid status. Region status " "must be one of {}".format(allowed_status)) # May raise an exception which will return status code 400 status = RegionService.update_region_status( region_id, new_status.status) base_link = 'https://{0}:{1}{2}'.format(conf.server.host, conf.server.port, pecan.request.path) link = {'self': base_link} logger.debug("Region status for region id {}, was successfully " "changed to: {}.".format(region_id, new_status.status)) event_details = 'Region {} status updated to {}'.format( region_id, new_status.status) utils.audit_trail('Update status', request.transaction_id, request.headers, region_id, event_details=event_details) return RegionStatus(status, link) except error_base.ErrorStatus as e: logger.error(e.message) raise err_utils.get_error(request.transaction_id, message=e.message, status_code=e.status_code) except Exception as exception: logger.error(exception.message) raise err_utils.get_error(request.transaction_id, status_code=500, error_details=exception.message)
def test_authorize_gke_failed(self, mock_iae, mock_gke, mock_policy): request = mock.MagicMock() action = 'test:test' # Success when authentication is disabled mock_iae.return_value = False authentication.authorize(request, action) # Success when authentication is enabled mock_iae.return_value = True authentication.authorize(request, action)
def get(self, dump_to_log='false'): """get method. :param dump_to_log: A boolean string that says whether the configuration should be written to log :return: A pretty string that contains the service's configuration """ logger.info("Get configuration...") authentication.authorize(request, 'configuration:get') dump = dump_to_log.lower() == 'true' utils.set_utils_conf(conf) result = utils.report_config(conf, dump, logger) return result
def post(self, group_input): """Handle post request. :param group_input: json data :return: 201 created on success, 409 otherwise. """ logger.info("Entered Create Group") logger.debug( "id = {}, name = {}, description = {}, regions = {}".format( group_input.id, group_input.name, group_input.description, group_input.regions)) authentication.authorize(request, 'group:create') try: # May raise an exception which will return status code 400 GroupService.create_group_in_db(group_input.id, group_input.name, group_input.description, group_input.regions) logger.debug("Group created successfully in DB") # Create the group output data with the correct timestamp and link group = OutputResource( group_input.id, group_input.name, repr(int(time.time() * 1000)), { 'self': '{}/v2/orm/groups/{}'.format(request.application_url, group_input.id) }) event_details = 'Region group {} {} created with regions: {}'.format( group_input.id, group_input.name, group_input.regions) utils.audit_trail('create group', request.transaction_id, request.headers, group_input.id, event_details=event_details) return Result(group) except error_base.ErrorStatus as e: logger.error("GroupsController - {}".format(e.message)) raise err_utils.get_error(request.transaction_id, message=e.message, status_code=e.status_code) except Exception as exception: logger.error(exception.message) raise err_utils.get_error(request.transaction_id, status_code=500, error_details=exception.message)
def get_all(self): logger.info("gett all groups") authentication.authorize(request, 'group:get_all') try: logger.debug("api-get all groups") groups_wrraper = GroupService.get_all_groups() logger.debug("got groups {}".format(groups_wrraper)) except Exception as exp: logger.error("api--fail to get all groups") logger.exception(exp) raise err_utils.get_error(request.transaction_id, status_code=500, error_details=exception.message) return groups_wrraper
def get(self, region_id): logger.info("Get metadata for region id: {}".format(region_id)) authentication.authorize(request, 'metadata:get') try: region = RegionService.get_region_by_id_or_name(region_id) logger.debug("Got region metadata: {}".format(region.metadata)) return MetaData(region.metadata) except error_base.ErrorStatus as exp: logger.error("RegionsController - {}".format(exp.message)) raise err_utils.get_error(request.transaction_id, message=exp.message, status_code=exp.status_code) except Exception as exp: logger.exception(exp.message) raise err_utils.get_error(request.transaction_id, status_code=500, error_details=exp.message)
def get_one(self, id_or_name): logger.info( "API: Entered get region by id or name: {}".format(id_or_name)) authentication.authorize(request, 'region:get_one') try: result = RegionService.get_region_by_id_or_name(id_or_name) logger.debug("API: Got region {} success: {}".format( id_or_name, result)) except error_base.ErrorStatus as exp: logger.error("RegionsController {}".format(exp.message)) raise err_utils.get_error(request.transaction_id, message=exp.message, status_code=exp.status_code) except Exception as exp: logger.exception(exp.message) raise err_utils.get_error(request.transaction_id, status_code=500, error_details=exp.message) return result
def get_all(self): """This function is called when receiving /lcp without a parameter. parameter: None. return: entire list of lcp. """ logger.info('Received a GET request for all LCPs') authentication.authorize(request, 'lcp:get_all') zones = [] try: zones = get_zones() logger.debug('Returning LCP list: %s' % (zones, )) return zones except Exception as exception: logger.error(exception.message) raise err_utils.get_error(request.transaction_id, status_code=500, error_details=exception.message)
def put(self, region_id, region): logger.info("API: update region") authentication.authorize(request, 'region:update') try: logger.debug("region to update {} with{}".format( region_id, region)) result = RegionService.update_region(region_id, region) logger.debug("API: region {} updated".format(region_id)) event_details = 'Region {} {} modified: AICversion {}, OSversion {}, CLLI {}'.format( region.name, region.design_type, region.ranger_agent_version, region.open_stack_version, region.clli) utils.audit_trail('update region', request.transaction_id, request.headers, region_id, event_details=event_details) except error_base.NotFoundError as exp: logger.exception("region {} not found".format(region_id)) raise err_utils.get_error(request.transaction_id, status_code=exp.status_code, message=exp.message) except error_base.InputValueError as exp: logger.exception("not valid input {}".format(exp.message)) raise err_utils.get_error(request.transaction_id, status_code=exp.status_code, message=exp.message) except Exception as exp: logger.exception( "API: error in updating region {}.. reason:- {}".format( region_id, exp)) raise err_utils.get_error(request.transaction_id, status_code=500, message=exp.message) return result
def post(self, full_region_input): logger.info("API: CreateRegion") authentication.authorize(request, 'region:create') try: logger.debug( "API: create region .. data = : {}".format(full_region_input)) result = RegionService.create_full_region(full_region_input) logger.debug("API: region created : {}".format(result)) event_details = 'Region {} {} created: AICversion {}, OSversion {}, CLLI {}'.format( full_region_input.name, full_region_input.design_type, full_region_input.ranger_agent_version, full_region_input.open_stack_version, full_region_input.clli) utils.audit_trail('create region', request.transaction_id, request.headers, full_region_input.id, event_details=event_details) except error_base.InputValueError as exp: logger.exception("Error in save region {}".format(exp.message)) raise err_utils.get_error(request.transaction_id, status_code=exp.status_code, message=exp.message) except error_base.ConflictError as exp: logger.exception("Conflict error {}".format(exp.message)) raise err_utils.get_error(request.transaction_id, message=exp.message, status_code=exp.status_code) except Exception as exp: logger.exception( "Error in creating region .. reason:- {}".format(exp)) raise err_utils.get_error(request.transaction_id, status_code=500, message=exp.message) return result
def put(self, group_id, group): logger.info("update group") authentication.authorize(request, 'group:update') try: logger.debug("update group - id {}".format(group_id)) result = GroupService.update_group(group, group_id) logger.debug("group updated to :- {}".format(result)) # build result group_result = OutputResource( result.id, result.name, repr(int(time.time() * 1000)), { 'self': '{}/v2/orm/groups/{}'.format(request.application_url, result.id) }) event_details = 'Region group {} {} updated with regions: {}'.format( group_id, group.name, group.regions) utils.audit_trail('update group', request.transaction_id, request.headers, group_id, event_details=event_details) except error_base.ErrorStatus as exp: logger.error("group to update not found {}".format(exp)) logger.exception(exp) raise err_utils.get_error(request.transaction_id, message=exp.message, status_code=exp.status_code) except Exception as exp: logger.error("fail to update groupt -- id {}".format(group_id)) logger.exception(exp) raise return Result(group_result)
def delete(self, region_id): logger.info("Delete Region") authentication.authorize(request, 'region:delete') try: logger.debug("delete region {}".format(region_id)) result = RegionService.delete_region(region_id) logger.debug("region deleted") event_details = 'Region {} deleted'.format(region_id) utils.audit_trail('delete region', request.transaction_id, request.headers, region_id, event_details=event_details) except Exception as exp: logger.exception( "error in deleting region .. reason:- {}".format(exp)) raise err_utils.get_error(request.transaction_id, status_code=500, message=exp.message) return
def delete(self, group_id): logger.info("delete group") authentication.authorize(request, 'group:delete') try: logger.debug("delete group with id {}".format(group_id)) GroupService.delete_group(group_id) logger.debug("done") event_details = 'Region group {} deleted'.format(group_id) utils.audit_trail('delete group', request.transaction_id, request.headers, group_id, event_details=event_details) except Exception as exp: logger.exception("fail to delete group :- {}".format(exp)) raise err_utils.get_error(request.transaction_id, status_code=500, error_details=exp.message) return
def test_authorize_gke_failed(self, mock_iae, mock_gke, mock_policy): request = mock.MagicMock() action = 'test:test' mock_policy.authorize = self.mock_authorize_no_keystone authentication.authorize(request, action)
def get_all(self, type=None, status=None, metadata=None, rangerAgentVersion=None, clli=None, regionname=None, osversion=None, valet=None, state=None, country=None, city=None, street=None, zip=None): """get regions. :param type: query field :param status: query field :param metadata: query field :param rangerAgentVersion: query field :param clli: query field :param regionname: query field :param osversion: query field :param valet: query field :param state: query field :param country: query field :param city: query field :param street: query field :param zip: query field :return: json from db :exception: EntityNotFoundError 404 """ logger.info("Entered Get Regions") authentication.authorize(request, 'region:get_all') url_args = { 'type': type, 'status': status, 'metadata': metadata, 'rangerAgentVersion': rangerAgentVersion, 'clli': clli, 'regionname': regionname, 'osversion': osversion, 'valet': valet, 'state': state, 'country': country, 'city': city, 'street': street, 'zip': zip } logger.debug("Parameters: {}".format(str(url_args))) try: url_args = url_parm.UrlParms(**url_args) result = RegionService.get_regions_data(url_args) logger.debug("Returning regions: {}".format(', '.join( [region.name for region in result.regions]))) return result except error_base.ErrorStatus as e: logger.error("RegionsController {}".format(e.message)) raise err_utils.get_error(request.transaction_id, message=e.message, status_code=e.status_code) except Exception as exception: logger.error(exception.message) raise err_utils.get_error(request.transaction_id, status_code=500, message=exception.message)