def provision_atorchestrator(context, instance): context.user_admin = context.config["env_data"]["users"]["user_1"]["user_name"] context.password_admin = context.config["env_data"]["users"]["user_1"]["user_password"] context.service_admin = context.config["env_data"]["users"]["user_1"]["user_service"] context.services = orc_get_services(context) for service in context.services: if context.service == service["name"]: __logger__.debug("service retrieved: {} {}".format(service["name"], service["id"])) context.service_id = service["id"] break # Get config env credentials context.user_admin = context.config["env_data"]["users"]["user_1"]["user_name"] context.password_admin = context.config["env_data"]["users"]["user_2"]["user_password"] try: if "service_id" in context: delete_response = orc_delete_service(context, context.service_id) except ValueError: __logger__.error("[Error] Service to delete ({}) not found".format(context.service)) # Composing payloads payload_service = dict(context.table[0:6]) context.user = payload_service["NEW_SERVICE_ADMIN_USER"] context.pwd = payload_service["NEW_SERVICE_ADMIN_PASSWORD"] payload_service.update({"KEYPASS_PROTOCOL": "http", "KEYPASS_HOST": "localhost"}) if "NEW_SERVICE_DESCRIPTION" not in payload_service: payload_service["NEW_SERVICE_DESCRIPTION"] = payload_service["NEW_SERVICE_NAME"] if "SERVICE_NAME" not in payload_service: payload_service["SERVICE_NAME"] = payload_service["NEW_SERVICE_NAME"] payload_servicepath = dict(context.table[6:]) payload_servicepath.update({"KEYPASS_PROTOCOL": "http", "KEYPASS_HOST": "localhost", "KEYSTONE_PROTOCOL": "http", "KEYSTONE_HOST": "localhost"}) if "SERVICE_NAME" not in payload_servicepath: payload_servicepath["SERVICE_NAME"] = payload_service["NEW_SERVICE_NAME"] if "SERVICE_ADMIN_USER" not in payload_servicepath: payload_servicepath["SERVICE_ADMIN_USER"] = payload_service["NEW_SERVICE_ADMIN_USER"] if "SERVICE_ADMIN_PASSWORD" not in payload_servicepath: payload_servicepath["SERVICE_ADMIN_PASSWORD"] = payload_service["NEW_SERVICE_ADMIN_PASSWORD"] if "NEW_SUBSERVICE_DESCRIPTION" not in payload_servicepath: payload_servicepath["NEW_SUBSERVICE_DESCRIPTION"] = payload_servicepath["NEW_SUBSERVICE_NAME"] context.headers = {"Accept": "application/json", "Content-type": "application/json"} context.instance_ip = context.config['components'][instance]['instance'] context.instance_port = context.config['components'][instance]['port'] context.instance_protocol = context.config['components'][instance]['protocol'] context.url_component = get_endpoint(context.instance_protocol, context.instance_ip, context.instance_port) # Service provision url_service = '{}/v1.0/service'.format(context.url_component) json_orcservice = json.dumps(payload_service) __logger__.debug("Create service: {}, \n url: {}".format(json_orcservice, url_service)) context.r = requests.post(url=url_service, headers=context.headers, data=json_orcservice) context.create_service = context.r.content jsobject = json.loads(context.create_service) context.headers["Fiware-Service"] = context.service context.headers["Fiware-ServicePath"] = context.servicepath context.service_id = jsobject["id"] context.token_service = jsobject["token"] # ServicePath provision context.headers = {'Accept': 'application/json', 'Content-type': 'application/json'} url_subservice = '{}/v1.0/service/{}/subservice'.format(context.url_component, context.service_id) json_servicepath = json.dumps(payload_servicepath) __logger__.debug("Create subservice: {}, \n url: {}".format(json_servicepath, url_subservice)) context.resp = requests.post(url=url_subservice, headers=context.headers, data=json_servicepath) context.create_servicepath = context.resp.content jsobject_s = json.loads(context.create_servicepath) context.subservice_id = jsobject_s["id"]
def happy_path_request_collector(context, INSTANCE, REQUEST, ACTION): """ :type context behave.runner.Context :type INSTANCE str :type REQUEST str :type ACTION str """ context.instance = INSTANCE # Setup default headers context.headers = {} context.headers = {"Accept": "application/json", "Content-type": "application/json"} # recover the data to send the request context.instance_ip = context.config['components'][INSTANCE]['instance'] context.instance_port = context.config['components'][INSTANCE]['port'] context.instance_protocol = context.config['components'][INSTANCE]['protocol'] context.url_component = get_endpoint(context.instance_protocol, context.instance_ip, context.instance_port) if INSTANCE == "ORC" and REQUEST == "SERVICE_ENTITY" and ACTION == "CREATE": url = '{}/v1.0/service/{}/subservice/{}/register_service'.format(context.url_component, context.service_id, context.subservice_id) context.headers.update({"Fiware-Service": "{}".format(context.service)}) context.headers.update({"Fiware-ServicePath": "/{}".format(context.servicepath)}) payload_table = dict(context.table) # Depends on Orquestator version, in old version is needed: # payload_table['ATT_TIMEOUT'] = int(payload_table['ATT_TIMEOUT']) # Properties replacement Var environment tp_url = payload_table['ATT_ENDPOINT'] if "TP" in tp_url: tp_endpoint = "{}://{}:{}".format(context.config['components']["TP"]['protocol'], context.config['components']["TP"]['instance'], context.config['components']["TP"]['port']) payload_table['ATT_ENDPOINT'] = tp_url.replace("TP", tp_endpoint) json_payload = json.dumps(payload_table) print (json_payload) __logger__.debug("Create service: {}, \n url: {}".format(json_payload, url)) try: context.r = requests.post(url=url, headers=context.headers, data=json_payload) eq_(context.r.status_code, 201, "[ERROR] when calling {} responsed a HTTP {}".format(url, context.r.status_code)) context.create_service_entity = context.r.content print (context.create_service_entity) except: eq_(True, False, "# Exception # Error Creating SERVICE_ENTITY") if INSTANCE == "ORC" and REQUEST == "SERVICE" and ACTION == "CREATE": url = str("{0}/v1.0/service".format(context.url_component)) payload = dict(context.table) if context.table else context.table_create_service json_payload = json.dumps(payload) print ("\n JSON TABLE SERVICE: \n {}\n".format(json_payload)) __logger__.debug("Create service: {}, \n url: {}".format(json_payload, url)) context.r = requests.post(url=url, headers=context.headers, data=json_payload) __logger__.debug(context.r.content) __logger__.debug(context.r.status_code) eq_(context.r.status_code, 201, "[ERROR] when calling {} responsed a HTTP {}".format(url, context.r.status_code)) context.create_service = context.r.content jsobject = json.loads(context.create_service) context.service_id = jsobject["id"] context.token_service = jsobject["token"] context.service_admin = payload["NEW_SERVICE_ADMIN_USER"] context.service_admin_pass = payload["NEW_SERVICE_ADMIN_PASSWORD"] print ("\n --->> ID service: {} <<--- \n".format(context.service_id)) print ("TOKEN service: {} \n".format(context.token_service)) if INSTANCE == "ORC" and REQUEST == "SERVICE" and ACTION == "DELETE": # Get list of services if needed if "service_id" not in context: for service in context.services: if context.service == service["name"]: print ("#>> Service Targeted: {} {}".format(service["name"], service["id"])) context.service_id = service["id"] break # Get config test env credentials context.user_admin = context.config["env_data"]["users"]["user_1"]["user_name"] context.password_admin = context.config["env_data"]["users"]["user_1"]["user_password"] context.domain_admin = context.config["env_data"]["users"]["user_1"]["user_service"] # In case delete using token is needed # domain_token = ks_get_token(context, # service=context.domain_admin, # user=context.user_admin, # password=context.password_admin) if "service_id" in context: delete_response = orc_delete_service(context, context.service_id) eq_(204, delete_response, "[ERROR] Deleting Service {} \n Orch responded: {}".format(context.service_id, delete_response)) else: eq_(True, False, "[Error] Service to delete ({}) not found".format(context.service)) if INSTANCE == "ORC" and REQUEST == "SUBSERVICE" and ACTION == "DELETE": # Get service id if needed if "service_id" not in context: for service in context.services: if context.service == service["name"]: print ("service retrieved: {} {}".format(service["name"], service["id"])) context.service_id = service["id"] break # Get subservice id for subservice in context.subservices: if "/" + context.subservice == subservice["name"]: print ("#>> Subservice Targeted: {} {}".format(subservice["name"], subservice["id"])) context.subservice_id = subservice["id"] break else: print ("#>> Subservice Targeted: {} {}".format(subservice["name"], subservice["id"])) context.user_admin = context.config["env_data"]["users"]["user_3"]["user_name"] context.password_admin = context.config["env_data"]["users"]["user_3"]["user_password"] context.token_scope = ks_get_token_with_scope(context, context.token, context.service, context.subservice) delete_response = orc_delete_subservice(context, context.service_id, context.subservice_id, context.token_scope) eq_(204, delete_response, "[ERROR] Deleting Service {} responsed a HTTP {}".format(context.service_id, delete_response)) if INSTANCE == "IOTA_MQTT" and REQUEST == "DEVICE" and ACTION == "CREATE": url = str("{0}/iot/devices".format(context.url_component)) dictio = dict(context.table) print ("\n{}\n".format(dictio)) # create dict to contain the payload payload = dict({}) payload["devices"] = [dictio] context.headers.update({"Fiware-Service": "{}".format(context.service)}) context.headers.update({"Fiware-ServicePath": "/{}".format(context.servicepath)}) __logger__.debug("[MQTT] Create Device request: {}, \n url: {}".format(payload, url)) context.mqtt_create_request = payload context.mqtt_create_url = url