def handle_post(self, request, user, *args, **kwargs): """Treat requests POST to add Group l3. URL: groupl3/ """ try: self.log.info('Add Group l3') # User permission if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') raise UserNotAuthorizedError(None) # Load XML data xml_map, attrs_map = loads(request.raw_post_data) # XML data format networkapi_map = xml_map.get('networkapi') if networkapi_map is None: return self.response_error(3, u'There is no value to the networkapi tag of XML request.') group_l3_map = networkapi_map.get('group_l3') if group_l3_map is None: return self.response_error(3, u'There is no value to the group_l3 tag of XML request.') # Get XML data name = group_l3_map.get('name') try: GrupoL3.get_by_name(name) raise GrupoL3NameDuplicatedError( None, u'Já existe um grupo l3 com o valor name %s.' % name) except GroupL3NotFoundError: pass l3_group = GrupoL3() # set variables l3_group.nome = name try: # save Group l3 l3_group.save() except Exception, e: self.log.error(u'Failed to save the Group l3.') raise AmbienteError(e, u'Failed to save the Group l3.') l3_group_map = dict() l3_group_map['group_l3'] = model_to_dict( l3_group, exclude=['nome']) return self.response(dumps_networkapi(l3_group_map))
def handle_delete(self, request, user, *args, **kwargs): """Treat requests DELETE to remove Logical Environment. URL: logicalenvironment/<id_logicalenvironment>/ """ try: self.log.info("Remove Logical Environment") # User permission if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') raise UserNotAuthorizedError(None) id_logicalenvironment = kwargs.get('id_logicalenvironment') # Valid ID Logical Environment if not is_valid_int_greater_zero_param(id_logicalenvironment): self.log.error( u'The id_logicalenvironment parameter is not a valid value: %s.', id_logicalenvironment) raise InvalidValueError(None, 'id_logicalenvironment', id_logicalenvironment) # Find Logical Environment by ID to check if it exist loc_env = AmbienteLogico.get_by_pk(id_logicalenvironment) with distributedlock(LOCK_LOGICAL_ENVIRONMENT % id_logicalenvironment): try: if loc_env.ambiente_set.count() > 0: raise AmbienteLogicoUsedByEnvironmentError( None, u"O Ambiente Lógico %s tem ambiente associado." % loc_env.id) # remove Logical Environment loc_env.delete() except AmbienteLogicoUsedByEnvironmentError, e: raise e except Exception, e: self.log.error( u'Failed to remove the Logical Environment.') raise AmbienteError( e, u'Failed to remove the Logical Environment.')
def handle_delete(self, request, user, *args, **kwargs): """Treat requests DELETE to remove Division Dc. URL: divisiondc/<id_divisiondc>/ """ try: self.log.info('Remove Division Dc') # User permission if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') raise UserNotAuthorizedError(None) id_divisiondc = kwargs.get('id_divisiondc') # Valid ID Division Dc if not is_valid_int_greater_zero_param(id_divisiondc): self.log.error( u'The id_divisiondc parameter is not a valid value: %s.', id_divisiondc) raise InvalidValueError(None, 'id_divisiondc', id_divisiondc) # Find Division Dc by ID to check if it exist division_dc = DivisaoDc.get_by_pk(id_divisiondc) with distributedlock(LOCK_DC_DIVISION % id_divisiondc): try: if division_dc.ambiente_set.count() > 0: raise DivisaoDcUsedByEnvironmentError( None, u'A Divisão DC %s tem ambiente associado.' % division_dc.id) # remove Division Dc division_dc.delete() except DivisaoDcUsedByEnvironmentError, e: raise e except Exception, e: self.log.error(u'Failed to remove the Division Dc.') raise AmbienteError(e, u'Failed to remove the Division Dc.')
def handle_put(self, request, user, *args, **kwargs): """Treat requests PUT to edit Division Dc. URL: divisiondc/<id_divisiondc>/ """ try: self.log.info('Edit Division Dc') # User permission if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') raise UserNotAuthorizedError(None) id_divisiondc = kwargs.get('id_divisiondc') # Load XML data xml_map, attrs_map = loads(request.raw_post_data) # XML data format networkapi_map = xml_map.get('networkapi') if networkapi_map is None: return self.response_error( 3, u'There is no value to the networkapi tag of XML request.' ) division_dc_map = networkapi_map.get('division_dc') if division_dc_map is None: return self.response_error( 3, u'There is no value to the division_dc tag of XML request.' ) # Get XML data name = division_dc_map.get('name') # Valid ID Division Dc if not is_valid_int_greater_zero_param(id_divisiondc): self.log.error( u'The id_divisiondc parameter is not a valid value: %s.', id_divisiondc) raise InvalidValueError(None, 'id_divisiondc', id_divisiondc) # Valid name if not is_valid_string_minsize( name, 2) or not is_valid_string_maxsize(name, 80): self.log.error(u'Parameter name is invalid. Value: %s', name) raise InvalidValueError(None, 'name', name) # Find Division Dc by ID to check if it exist division_dc = DivisaoDc.get_by_pk(id_divisiondc) with distributedlock(LOCK_DC_DIVISION % id_divisiondc): try: if division_dc.nome.lower() != name.lower(): DivisaoDc.get_by_name(name) raise DivisaoDcNameDuplicatedError( None, u'Já existe um Divisão Dc com o valor name %s.' % name) except DivisaoDcNotFoundError: pass # set variables division_dc.nome = name try: # update Division Dc division_dc.save() except Exception, e: self.log.error(u'Failed to update the Division Dc.') raise AmbienteError(e, u'Failed to update the Division Dc.') return self.response(dumps_networkapi({})) except InvalidValueError, e: return self.response_error(269, e.param, e.value)
(self.rack.id, self.ambiente.id)) raise EnvironmentRackError( e, u'Error trying to insert EnvironmentRack: %s/%s.' % (self.rack.id, self.ambiente.id)) def get_by_rack_environment(self, rack_id, environment_id): try: return EnvironmentRack.objects.get(ambiente__id=environment_id, rack__id=rack_id) except ObjectDoesNotExist, e: raise EnvironmentRackNotFoundError( e, u'There is no EnvironmentRack with rack = %s and environment = %s.' % (rack_id, environment_id)) except Exception, e: self.log.error(u'Error trying to search EnvironmentRack %s/%s.' %(rack_id, environment_id)) raise EnvironmentRackError( e, u'Error trying to search EnvironmentRack.') @classmethod def get_by_rack(cls, rack_id): """"Get Environment by racks id. @return: Environment. """ try: return EnvironmentRack.objects.filter(rack=rack_id) except ObjectDoesNotExist, e: raise RackError( e, u'Dont there is a Environment by rack = %s.' % rack_id) except Exception, e: cls.log.error(u'Failure to search the Environment.') raise AmbienteError(e, u'Failure to search the Environment.')
def handle_post(self, request, user, *args, **kwargs): """Treat requests POST to add Division Dc. URL: divisiondc/ """ try: self.log.info('Add Division Dc') # User permission if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') raise UserNotAuthorizedError(None) # Load XML data xml_map, attrs_map = loads(request.raw_post_data) # XML data format networkapi_map = xml_map.get('networkapi') if networkapi_map is None: return self.response_error( 3, u'There is no value to the networkapi tag of XML request.' ) division_dc_map = networkapi_map.get('division_dc') if division_dc_map is None: return self.response_error( 3, u'There is no value to the division_dc tag of XML request.' ) # Get XML data name = division_dc_map.get('name') try: DivisaoDc.get_by_name(name) raise DivisaoDcNameDuplicatedError( None, u'Já existe um divisào dc com o valor name %s.' % name) except DivisaoDcNotFoundError: pass division_dc = DivisaoDc() # set variables division_dc.nome = name try: # save Division Dc division_dc.save() except Exception, e: self.log.error(u'Failed to save the Division Dc.') raise AmbienteError(e, u'Failed to save the Division Dc.') division_dc_map = dict() division_dc_map['division_dc'] = model_to_dict(division_dc, exclude=['nome']) return self.response(dumps_networkapi(division_dc_map))
def handle_post(self, request, user, *args, **kwargs): """Treat requests POST to add Logical Environment. URL: logicalenvironment/ """ try: self.log.info("Add Logical Environment") # User permission if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') raise UserNotAuthorizedError(None) # Load XML data xml_map, attrs_map = loads(request.raw_post_data) # XML data format networkapi_map = xml_map.get('networkapi') if networkapi_map is None: return self.response_error( 3, u'There is no value to the networkapi tag of XML request.' ) logical_environment_map = networkapi_map.get('logical_environment') if logical_environment_map is None: return self.response_error( 3, u'There is no value to the logical_environment tag of XML request.' ) # Get XML data name = logical_environment_map.get('name') try: AmbienteLogico.get_by_name(name) raise AmbienteLogicoNameDuplicatedError( None, u'Já existe um Ambiente Lógico com o valor name %s.' % name) except AmbienteLogicoNotFoundError: pass log_env = AmbienteLogico() # set variables log_env.nome = name try: # save Logical Environment log_env.save() except Exception, e: self.log.error(u'Failed to save the Logical Environment.') raise AmbienteError( e, u'Failed to save the Logical Environment.') log_env_map = dict() log_env_map['logical_environment'] = model_to_dict( log_env, exclude=["nome"]) return self.response(dumps_networkapi(log_env_map))
def handle_put(self, request, user, *args, **kwargs): """Treat requests PUT to edit Logical Environment. URL: logicalenvironment/<id_logicalenvironment>/ """ try: self.log.info("Edit Logical Environment") # User permission if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') raise UserNotAuthorizedError(None) id_logicalenvironment = kwargs.get('id_logicalenvironment') # Load XML data xml_map, attrs_map = loads(request.raw_post_data) # XML data format networkapi_map = xml_map.get('networkapi') if networkapi_map is None: return self.response_error( 3, u'There is no value to the networkapi tag of XML request.' ) logical_environment_map = networkapi_map.get('logical_environment') if logical_environment_map is None: return self.response_error( 3, u'There is no value to the logical_environment tag of XML request.' ) # Get XML data name = logical_environment_map.get('name') # Valid ID Logical Environment if not is_valid_int_greater_zero_param(id_logicalenvironment): self.log.error( u'The id_logicalenvironment parameter is not a valid value: %s.', id_logicalenvironment) raise InvalidValueError(None, 'id_logicalenvironment', id_logicalenvironment) # Valid name if not is_valid_string_minsize( name, 2) or not is_valid_string_maxsize(name, 80): self.log.error(u'Parameter name is invalid. Value: %s', name) raise InvalidValueError(None, 'name', name) # Find Logical Environment by ID to check if it exist loc_env = AmbienteLogico.get_by_pk(id_logicalenvironment) with distributedlock(LOCK_LOGICAL_ENVIRONMENT % id_logicalenvironment): try: if loc_env.nome.lower() != name.lower(): AmbienteLogico.get_by_name(name) raise AmbienteLogicoNameDuplicatedError( None, u'Já existe um Ambiente Lógico com o valor name %s.' % name) except AmbienteLogicoNotFoundError: pass # set variables loc_env.nome = name try: # update Logical Environment loc_env.save() except Exception, e: self.log.error( u'Failed to update the Logical Environment.') raise AmbienteError( e, u'Failed to update the Logical Environment.') return self.response(dumps_networkapi({})) except InvalidValueError, e: return self.response_error(269, e.param, e.value)
def handle_put(self, request, user, *args, **kwargs): """Treat requests PUT to edit Group l3. URL: groupl3/<id_groupl3>/ """ try: self.log.info('Edit Group l3') # User permission if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION): self.log.error( u'User does not have permission to perform the operation.') raise UserNotAuthorizedError(None) id_groupl3 = kwargs.get('id_groupl3') # Load XML data xml_map, attrs_map = loads(request.raw_post_data) # XML data format networkapi_map = xml_map.get('networkapi') if networkapi_map is None: return self.response_error( 3, u'There is no value to the networkapi tag of XML request.' ) group_l3_map = networkapi_map.get('group_l3') if group_l3_map is None: return self.response_error( 3, u'There is no value to the group_l3 tag of XML request.') # Get XML data name = group_l3_map.get('name') # Valid ID Group L3 if not is_valid_int_greater_zero_param(id_groupl3): self.log.error( u'The id_groupl3 parameter is not a valid value: %s.', id_groupl3) raise InvalidValueError(None, 'id_groupl3', id_groupl3) # Valid name if not is_valid_string_minsize( name, 2) or not is_valid_string_maxsize(name, 80): self.log.error(u'Parameter name is invalid. Value: %s', name) raise InvalidValueError(None, 'name', name) # Find GroupL3 by ID to check if it exist groupl3 = GrupoL3.get_by_pk(id_groupl3) with distributedlock(LOCK_GROUP_L3 % id_groupl3): try: if groupl3.nome.lower() != name.lower(): GrupoL3.get_by_name(name) raise GrupoL3NameDuplicatedError( None, u'Já existe um grupo l3 com o valor name %s.' % name) except GroupL3NotFoundError: pass # set variables groupl3.nome = name try: # update Group l3 groupl3.save() except Exception, e: self.log.error(u'Failed to update the Group l3.') raise AmbienteError(e, u'Failed to update the Group l3.') return self.response(dumps_networkapi({})) except InvalidValueError, e: return self.response_error(269, e.param, e.value)