def handle_put(self, request, user, *args, **kwargs): """ Handles PUT requests to create a relationship of Environment with EnvironmentVip. URL: environment/<environment_id>/environmentvip/<environment_vip_id>/ """ self.log.info( 'Create a relationship of Environment with EnvironmentVip') try: # Commons Validations # 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) # Valid Environment environment_id = kwargs.get('environment_id') if not is_valid_int_greater_zero_param(environment_id): self.log.error( u'The environment_id parameter is not a valid value: %s.', environment_id) raise InvalidValueError(None, 'environment_id', environment_id) # Valid EnvironmentVip ID environment_vip_id = kwargs.get('environment_vip_id') if not is_valid_int_greater_zero_param(environment_vip_id): self.log.error( u'The id_environment_vip parameter is not a valid value: %s.', environment_vip_id) raise InvalidValueError(None, 'environment_vip_id', environment_vip_id) # Business Validations # Existing Environment ID environment = Ambiente.get_by_pk(environment_id) # Existing EnvironmentVip ID environment_vip = EnvironmentVip.get_by_pk(environment_vip_id) with distributedlock(LOCK_ENVIRONMENT_VIP % environment_vip_id): # Business Rules # Set new values environment_environment_vip = EnvironmentEnvironmentVip() environment_environment_vip.environment = environment environment_environment_vip.environment_vip = environment_vip # Existing EnvironmentEnvironmentVip environment_environment_vip.validate() # Persist environment_environment_vip.save() # Return XML environment_environment_vip_map = {} environment_environment_vip_map[ 'environment_environment_vip'] = model_to_dict( environment_environment_vip, fields=['id']) return self.response( dumps_networkapi(environment_environment_vip_map)) except UserNotAuthorizedError: return self.not_authorized() except InvalidValueError, e: return self.response_error(269, e.param, e.value)
def handle_delete(self, request, user, *args, **kwargs): """ Handles DELETE requests to create a relationship of Environment with EnvironmentVip. URL: environment/<environment_id>/environmentvip/<environment_vip_id>/ """ self.log.info( 'Remove a relationship of Environment with EnvironmentVip') try: # Commons Validations # 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) # Valid Environment environment_id = kwargs.get('environment_id') if not is_valid_int_greater_zero_param(environment_id): self.log.error( u'The environment_id parameter is not a valid value: %s.', environment_id) raise InvalidValueError(None, 'environment_id', environment_id) # Valid EnvironmentVip ID environment_vip_id = kwargs.get('environment_vip_id') if not is_valid_int_greater_zero_param(environment_vip_id): self.log.error( u'The id_environment_vip parameter is not a valid value: %s.', environment_vip_id) raise InvalidValueError(None, 'environment_vip_id', environment_vip_id) # Business Validations # Existing Environment ID environment = Ambiente.get_by_pk(environment_id) # Existing EnvironmentVip ID environment_vip = EnvironmentVip.get_by_pk(environment_vip_id) # Business Rules environment_environment_vip = EnvironmentEnvironmentVip( ).get_by_environment_environment_vip(environment.id, environment_vip.id) server_pool_list = EnvironmentEnvironmentVip.get_server_pool_by_environment_environment_vip( environment_environment_vip) # Check if there are any pool from this environment used in any vip # of this environment vip if server_pool_list: raise EnvironmentEnvironmentServerPoolLinked( {'environment': environment.name}) # Delete environment_environment_vip.delete() # Return nothing return self.response(dumps_networkapi({})) except UserNotAuthorizedError: return self.not_authorized() except InvalidValueError, e: return self.response_error(269, e.param, e.value)