def handle_post(self, request, user, *args, **kwargs): """Treat requests POST to add a HeltcheckExpect only expect_string field. URL: healthcheckexpect/add/expectstring/ """ try: # Business Validations # 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: msg = u'There is no value to the networkapi tag of XML request.' self.log.error(msg) return self.response_error(3, msg) healthcheck_map = networkapi_map.get('healthcheck') if healthcheck_map is None: msg = u'There is no value to the ip tag of XML request.' self.log.error(msg) return self.response_error(3, msg) # Get XML data expect_string = healthcheck_map.get('expect_string') if not is_valid_string_maxsize(expect_string, 50): self.log.error( u'Parameter expect_string is invalid. Value: %s.', expect_string) raise InvalidValueError(None, 'expect_string', expect_string) # User permission if not has_perm(user, AdminPermission.HEALTH_CHECK_EXPECT, AdminPermission.WRITE_OPERATION): return self.not_authorized() healthcheck = HealthcheckExpect() healthcheck.insert_expect_string(user, expect_string) healtchcheck_dict = dict() healtchcheck_dict['id'] = healthcheck.id return self.response( dumps_networkapi({'healthcheck_expect': healtchcheck_dict})) except InvalidValueError, e: return self.response_error(269, e.param, e.value)
def handle_get(self, request, user, *args, **kwargs): """Trata as requisições GET para consulta de HealthCheckExpects. Lista as informações dos HealthCheckExpect's de um determinado ambiente. URL: /healthcheckexpect/ambiente/<id_amb>/ """ try: if not has_perm(user, AdminPermission.HEALTH_CHECK_EXPECT, AdminPermission.READ_OPERATION): return self.not_authorized() map_list = [] environment_id = kwargs.get('id_amb') 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) environment = Ambiente().get_by_pk(environment_id) healthcheckexpects = HealthcheckExpect().search(environment_id) for healthcheckexpect in healthcheckexpects: healthcheckexpect_map = dict() healthcheckexpect_map['id'] = healthcheckexpect.id healthcheckexpect_map[ 'expect_string'] = healthcheckexpect.expect_string healthcheckexpect_map[ 'match_list'] = healthcheckexpect.match_list healthcheckexpect_map[ 'id_ambiente'] = healthcheckexpect.ambiente_id map_list.append(healthcheckexpect_map) return self.response( dumps_networkapi({'healthcheck_expect': map_list})) except InvalidValueError, e: return self.response_error(269, e.param, e.value)
def handle_post(self, request, user, *args, **kwargs): """Treat requests POST to add a HeltcheckExpect. URL: healthcheckexpect/add/ """ try: # Business Validations # 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: msg = u'There is no value to the networkapi tag of XML request.' self.log.error(msg) return self.response_error(3, msg) healthcheck_map = networkapi_map.get('healthcheck') if healthcheck_map is None: msg = u'There is no value to the ip tag of XML request.' self.log.error(msg) return self.response_error(3, msg) # Get XML data match_list = healthcheck_map.get('match_list') expect_string = healthcheck_map.get('expect_string') environment_id = healthcheck_map.get('id_ambiente') # Valid equip_id if not is_valid_int_greater_zero_param(environment_id): self.log.error( u'Parameter environment_id is invalid. Value: %s.', environment_id) raise InvalidValueError(None, 'environment_id', environment_id) if expect_string is not None: if not is_valid_string_maxsize( expect_string, 50) or not is_valid_string_minsize( expect_string, 2): self.log.error( u'Parameter expect_string is invalid. Value: %s.', expect_string) raise InvalidValueError(None, 'expect_string', expect_string) else: raise InvalidValueError(None, 'expect_string', expect_string) if match_list is not None: if not is_valid_string_maxsize( match_list, 50) or not is_valid_string_minsize( match_list, 2): self.log.error( u'Parameter match_list is invalid. Value: %s.', match_list) raise InvalidValueError(None, 'match_list', match_list) else: raise InvalidValueError(None, 'expect_string', expect_string) # User permission if not has_perm(user, AdminPermission.HEALTH_CHECK_EXPECT, AdminPermission.WRITE_OPERATION): return self.not_authorized() healthcheck = HealthcheckExpect() ambiente = Ambiente.get_by_pk(environment_id) healthcheck.insert(user, match_list, expect_string, ambiente) healtchcheck_dict = dict() healtchcheck_dict['id'] = healthcheck.id return self.response( dumps_networkapi({'healthcheck_expect': healtchcheck_dict})) except AmbienteNotFoundError, e: return self.response_error(112)