def _check_valid_name(self, repository, abstract, context): """ """ if abstract.name[0] != '@': abstract.name = '@%s' % abstract.name if abstract.name[-5:] != '.spec': abstract.name = '%s.spec' % abstract.name response = self._storage_controller.get_all( user_identifier=context.session.root_object.id, parent=repository, resource_name=abstract.rest_name, filter='name == "%s"' % abstract.name) if response.count and response.data[0].id != abstract.id: context.add_error( GAError( type=GAError.TYPE_CONFLICT, title='Duplicate Name', description='Another abstract exists with the name %s' % abstract.name, property_name='name')) return False if not abstract.name or not len( abstract.name) or abstract.name == '.spec': context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute name is mandatory.', property_name='name')) return False return True
def test_make_response_with_errors(self): """ """ session = GASession(garuda_uuid='xxx-xxx-xxx-xxx') request = GARequest(action=GARequest.ACTION_READ) context = GAContext(session=session, request=request) error1 = GAError(type=GAError.TYPE_INVALID, title='title1', description='description2', suggestion='nope', property_name='prop1') error2 = GAError(type=GAError.TYPE_CONFLICT, title='title2', description='description3', suggestion='nope', property_name='prop2') error3 = GAError(type=GAError.TYPE_NOTFOUND, title='title3', description='description4', suggestion='nope', property_name='prop3') context.add_errors([error1, error2, error3]) response = context.make_response() self.assertEquals(response.__class__, GAResponseFailure) self.assertEquals(response.content, [error1, error2, error3])
def will_perform_write(self, context): """ """ if context.request.action in (GARequest.ACTION_DELETE): return context specification = context.parent_object attribute = context.object response = self.core_controller.storage_controller.get_all(user_identifier=context.session.root_object.id, parent=specification, resource_name=self._sdk.SDAttribute.rest_name, filter='name == "%s"' % attribute.name) if response.count and response.data[0].id != attribute.id: context.add_error(GAError(type=GAError.TYPE_CONFLICT, title='Duplicate Name', description='Another attribute exists with the name %s' % attribute.name, property_name='name')) if not attribute.name or not len(attribute.name): context.add_error(GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute name is mandatory.', property_name='name')) if not attribute.type or not len(attribute.type): context.add_error(GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute type is mandatory.', property_name='type')) if attribute.type != 'enum' and not (attribute.type == 'list' and attribute.subtype == 'enum'): attribute.allowed_choices = None if attribute.type != 'string': attribute.min_length = None attribute.max_length = None attribute.format = None attribute.allowed_chars = None if attribute.type != 'integer' and attribute.type != 'float': attribute.min_value = None attribute.max_value = None return context
def will_perform_update(self, context): """ """ apiinfo = context.object if apiinfo.prefix[1] == '/': apiinfo.prefix = apiinfo.prefix[1:] if apiinfo.prefix[-1] == '/': apiinfo.prefix = apiinfo.prefix[:-1] if not apiinfo.version or not len(apiinfo.version): context.add_error(GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute version is mandatory.', property_name='version')) try: float(apiinfo.version) except: context.add_error(GAError(type=GAError.TYPE_CONFLICT, title='Wrong attribute', description='Attribute version must be a float.', property_name='version')) if not apiinfo.root or not len(apiinfo.root): context.add_error(GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute root is mandatory.', property_name='root')) if not apiinfo.prefix or not len(apiinfo.prefix): context.add_error(GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute prefix is mandatory.', property_name='prefix')) return context
def test_clear_errors(self): """ """ session = GASession(garuda_uuid='xxx-xxx-xxx-xxx') request = GARequest(action=GARequest.ACTION_READ) context = GAContext(session=session, request=request) error1 = GAError(type=GAError.TYPE_INVALID, title='title1', description='description2', suggestion='nope', property_name='prop1') error2 = GAError(type=GAError.TYPE_CONFLICT, title='title2', description='description3', suggestion='nope', property_name='prop2') error3 = GAError(type=GAError.TYPE_NOTFOUND, title='title3', description='description4', suggestion='nope', property_name='prop3') context.add_errors([error1, error2, error3]) self.assertEquals(len(context.errors), 3) self.assertEquals(context.has_errors, True) context.clear_errors() self.assertEquals(len(context.errors), 0) self.assertEquals(context.has_errors, False)
def test_serialize(self): """ """ error = GAError(type=GAError.TYPE_INVALID, title='title', description='description', suggestion='nope', property_name='name') expected = {'title': 'title', 'description': 'description'} data = error.to_dict() self.assertEquals(data, expected)
def test_copy(self): """ """ session = GASession(garuda_uuid='xxx-xxx-xxx-xxx') request = GARequest(action=GARequest.ACTION_READ) context = GAContext(session=session, request=request) context.add_error( GAError(type=GAError.TYPE_INVALID, title='title1', description='description2', suggestion='nope', property_name='prop1')) context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='title2', description='description3', suggestion='nope', property_name='prop2')) context.add_error( GAError(type=GAError.TYPE_NOTFOUND, title='title3', description='description4', suggestion='nope', property_name='prop3')) context.add_event( GAPushEvent(action=GARequest.ACTION_UPDATE, entity=tstdk.GAEnterprise())) context.add_event( GAPushEvent(action=GARequest.ACTION_CREATE, entity=tstdk.GAEnterprise())) context.add_event( GAPushEvent(action=GARequest.ACTION_DELETE, entity=tstdk.GAEnterprise())) context.object = tstdk.GAEnterprise(name='enterprise1') context.objects = [ tstdk.GAEnterprise(name='enterprise2'), tstdk.GAEnterprise(name='enterprise3') ] context_copy = context.copy() self.assertEquals(context_copy.session.uuid, session.uuid) self.assertEquals(context_copy.request.action, GARequest.ACTION_READ) self.assertEquals([obj.name for obj in context_copy.objects], [obj.name for obj in context.objects]) self.assertEquals(context_copy.object.name, context.object.name) self.assertEquals(context_copy.has_errors, True) self.assertEquals(len(context_copy.errors), 3) self.assertEquals(context_copy.has_events, True) self.assertEquals(len(context_copy.events), 3)
def _prepare_context_for_write_operation(self): """ """ action = self.context.request.action resource = self.context.request.resources[-1] if action != GARequest.ACTION_CREATE and action != GARequest.ACTION_ASSIGN and resource.value is None: self.context.add_error( GAError( type=GAError.TYPE_NOTALLOWED, title='Action not allowed', description='Unable to %s a resource without its identifier' % action)) return if action == GARequest.ACTION_CREATE: self._populate_context_for_create_with_resource(resource=resource) elif action == GARequest.ACTION_UPDATE: self._populate_context_for_update_with_resource(resource=resource) elif action == GARequest.ACTION_DELETE: self._populate_context_for_delete_with_resource(resource=resource) elif action == GARequest.ACTION_ASSIGN: self._populate_context_for_assign_with_resource(resource=resource)
def run(self): """ """ action = self.context.request.action resources = self.context.request.resources if not self.context.session.root_object or not self.context.session.root_object.id: self.context.add_error( GAError(type=GAError.TYPE_AUTHENTICATIONFAILURE, title='Not authenticated', description= 'You must be authenticated to perform this request.')) return self.user_identifier = self.context.session.root_object.id if len(resources) == 2: parent_resource = resources[0] response = self.storage_controller.get( user_identifier=self.user_identifier, resource_name=parent_resource.name, identifier=parent_resource.value) if response.has_errors: self.context.add_errors(response.errors) return self.context.parent_object = response.data if action is GARequest.ACTION_READALL: self._perform_readall_operation(count_only=False) elif action is GARequest.ACTION_COUNT: self._perform_readall_operation(count_only=True) elif action is GARequest.ACTION_READ: self._perform_read_operation() elif action in (GARequest.ACTION_CREATE, GARequest.ACTION_UPDATE, GARequest.ACTION_DELETE, GARequest.ACTION_ASSIGN): self._perform_write_operation() else: self.context.add_error( GAError(type=GAError.TYPE_INVALID, title='Bad request', description='Unknown action.'))
def _validate(self, resource): """ """ if resource.validate(): return None errors = [] for property_name, error in resource.errors.iteritems(): errors.append( GAError(type=GAError.TYPE_CONFLICT, title=error["title"], description=error["description"], property_name=error['remote_name'])) return errors
def execute_model_request(self, request): """ """ session_uuid = self.sessions_controller.extract_session_identifier( request=request) session = None logger.debug("finding session: %s" % session_uuid) if session_uuid: session = self.sessions_controller.get_session( session_uuid=session_uuid) if not session: session = self.sessions_controller.create_session(request=request) if session: return GAResponseSuccess(content=[session.root_object]) context = GAContext(session=session, request=request) if not session: error = GAError( type=GAError.TYPE_UNAUTHORIZED, title='Unauthorized access', description='Could not grant access. Please log in.') context.add_error(error) return GAResponseFailure(content=context.errors) # reset the session ttl self.sessions_controller.reset_session_ttl(session) logger.debug('Execute action %s on session UUID=%s' % (request.action, session_uuid)) operations_controller = GAOperationsController( context=context, logic_controller=self.logic_controller, storage_controller=self.storage_controller) operations_controller.run() response = context.make_response() if len(context.events) > 0: # pragma: no cover self.push_controller.push_events(events=context.events) return response
def _populate_context_for_create_with_resource(self, resource): """ """ self.context.object = self.storage_controller.instantiate( resource_name=resource.name) if not self.context.object: self.context.add_error( GAError( type=GAError.TYPE_INVALID, title='Bad request', description='Could not find object of type %s in the models' % resource.name)) return self.context.object.from_dict(self.context.request.content)
def will_perform_delete(self, context): """ """ token = context.object session_username = context.session.root_object.id response = self._storage_controller.get_all( user_identifier=session_username, resource_name=self._sdk.SDRepository.rest_name, parent=None, filter="associatedTokenID == '%s'" % token.id) if response.count: context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Token in use', description='This token is in used.')) return context
def execute_events_request(self, request): """ """ session_uuid = request.token session = self.sessions_controller.get_session( session_uuid=session_uuid) context = GAContext(session=session, request=request) if session is None: error = GAError( type=GAError.TYPE_UNAUTHORIZED, title='Unauthorized access', description='Could not grant access. Please login.') context.add_error(error) return (None, GAResponseFailure(content=context.errors)) # reset the session ttl self.sessions_controller.reset_session_ttl(session) return (session, None)
def will_perform_write(self, context): """ """ if context.request.action in (GARequest.ACTION_DELETE): return context api = context.object if not api.associated_specification_id or not len( api.associated_specification_id): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description= 'Attribute associatedSpecificationID is mandatory.', property_name='associatedSpecificationID')) self._update_path(specification=context.parent_object, api=api, session_username=context.session.root_object.id) return context
def will_perform_write(self, context): """ """ if context.request.action in (GARequest.ACTION_DELETE): return context repository = context.object if not repository.name or not len(repository.name): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute name is mandatory.', property_name='name')) if not repository.url or not len(repository.url): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute url is mandatory.', property_name='url')) if not repository.associated_token_id or not len( repository.associated_token_id): context.add_error( GAError( type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute associatedTokenID is mandatory.', property_name='associatedTokenID')) if not repository.organization or not len(repository.organization): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute organization is mandatory.', property_name='organization')) if not repository.repository or not len(repository.repository): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute repository is mandatory.', property_name='repository')) if not repository.branch or not len(repository.branch): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute branch is mandatory.', property_name='branch')) if not repository.path or not len(repository.path): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute path is mandatory.', property_name='path')) context.object.status = 'NEEDS_PULL' context.object.owner = context.request.username return context
def will_perform_write(self, context): """ """ if context.request.action in (GARequest.ACTION_DELETE): return context repository = context.parent_object specification = context.object action = context.request.action specification.name = '%s.spec' % (specification.object_rest_name.strip() if specification.object_rest_name and specification.object_rest_name.strip() else specification.entity_name.strip().lower()) response = self._storage_controller.get_all(user_identifier=context.session.root_object.id, parent=repository, resource_name=self._sdk.SDSpecification.rest_name, filter='name == "%s"' % specification.name) if response.count and response.data[0].id != specification.id: context.add_error(GAError(type=GAError.TYPE_CONFLICT, title='Duplicate Name', description='Another specification exists with the name %s' % specification.name, property_name='name')) if not specification.entity_name or not len(specification.entity_name): context.add_error(GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute entityName is mandatory.', property_name='entityName')) if action == GARequest.ACTION_UPDATE: response = self._storage_controller.get(user_identifier=context.session.root_object.id, resource_name=self._sdk.SDSpecification.rest_name, identifier=specification.id) if response.data and response.data.name != specification.name: self._old_names[context.request.uuid] = response.data.name response = self._storage_controller.get_all(user_identifier=context.session.root_object.id, resource_name=self._sdk.SDAPIInfo.rest_name, parent=repository) apiinfo = response.data[0] if response.count else None if specification.root: response = self._storage_controller.get_all(user_identifier=context.session.root_object.id, resource_name=self._sdk.SDSpecification.rest_name, parent=repository, filter='name == "%s.spec"' % apiinfo.root) current_root_specification = response.data[0] if response.count else None if current_root_specification and current_root_specification.id != specification.id: current_root_specification.root = False response = self._storage_controller.update(user_identifier=context.session.root_object.id, resource=current_root_specification) context.add_event(GAPushEvent(action=GARequest.ACTION_UPDATE, entity=current_root_specification)) response = self._storage_controller.get_all(user_identifier=context.session.root_object.id, resource_name=self._sdk.SDChildAPI.rest_name, parent=current_root_specification) for api in response.data: api.relationship = 'child' response = self.core_controller.storage_controller.get(user_identifier=context.session.root_object.id, resource_name=self._sdk.SDSpecification.rest_name, identifier=api.associated_specification_id) api.path = '/%s/id/%s' % (current_root_specification.object_resource_name, response.data.object_resource_name) self._storage_controller.update(user_identifier=context.session.root_object.id, resource=api) context.add_event(GAPushEvent(action=GARequest.ACTION_UPDATE, entity=api)) self._github_operations_controller.commit_specification(repository=repository, specification=current_root_specification, commit_message="Updated specification %s" % current_root_specification.name, session_username=context.session.root_object.id) response = self._storage_controller.get_all(user_identifier=context.session.root_object.id, resource_name=self._sdk.SDChildAPI.rest_name, parent=specification) for api in response.data: api.relationship = 'root' api.path = '/%s' % api.path.split('/')[3] self._storage_controller.update(user_identifier=context.session.root_object.id, resource=api) context.add_event(GAPushEvent(action=GARequest.ACTION_UPDATE, entity=api)) if apiinfo and apiinfo.root != specification.object_rest_name: apiinfo.root = specification.object_rest_name self._storage_controller.update(user_identifier=context.session.root_object.id, resource=apiinfo) context.add_event(GAPushEvent(action=GARequest.ACTION_UPDATE, entity=apiinfo)) self._github_operations_controller.commit_apiinfo(repository=repository, apiinfo=apiinfo, commit_message="Updated api.info", session_username=context.session.root_object.id) return context
def will_perform_write(self, context): """ """ monolithe_config = context.object if not monolithe_config.product_name or not len( monolithe_config.product_name): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute productName is mandatory.', property_name='productName')) if not monolithe_config.product_accronym or not len( monolithe_config.product_accronym): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute productAccronym is mandatory.', property_name='productAccronym')) if not monolithe_config.output or not len(monolithe_config.output): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute output is mandatory.', property_name='output')) if not monolithe_config.name or not len(monolithe_config.name): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute name is mandatory.', property_name='name')) if not monolithe_config.class_prefix or not len( monolithe_config.class_prefix): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute classPrefix is mandatory.', property_name='classPrefix')) if not monolithe_config.bambou_version or not len( monolithe_config.bambou_version): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute bambouVersion is mandatory.', property_name='bambouVersion')) if not monolithe_config.version or not len(monolithe_config.version): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute version is mandatory.', property_name='version')) if not monolithe_config.revision_number or not len( monolithe_config.revision_number): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute revisionNumber is mandatory.', property_name='revisionNumber')) if not monolithe_config.cli_name or not len(monolithe_config.cli_name): context.add_error( GAError(type=GAError.TYPE_CONFLICT, title='Missing attribute', description='Attribute CLIName is mandatory.', property_name='CLIName')) return context