Beispiel #1
0
def instance_ops():
    c = Connector().morph()
    content = request.get_json()
    rc, ret = c.execute_action(content)
    if(rc != Apiconstants.HTTP_OK):
        return jsonify(response=json.loads(ret)), rc

    ret_json = json.loads(ret)
    wf_hash = {}
    wf_hash['id'] = ret_json['id']
    wf_hash['name'] = ret_json['action']['name']
    wf_hash['input'] = json.dumps(ret_json['parameters'])
    wf_hash['workflow_definition_id'] = ret_json['action']['ref']

    # Create the record of this instance in DB
    create_workflow(None, wf_hash)
    return jsonify(response=ret_json), 200
def instance_ops(tenant_id=''):
    c = Connector().morph()
    content = request.get_json()
    AUTH_TOKEN = request.headers.get('X-Auth-Token')
    # TODO: Need to check, When orchestration APIs authentication
    # is implemented
    if AUTH_TOKEN == '' or AUTH_TOKEN is None:
        err_msg = 'Bad Request. Authentication Token is missing'
        return jsonify(err_msg), Apiconstants.HTTP_ERR_BAD_REQUEST
    if tenant_id == '':
        err_msg = 'bad URL. tenant id is empty'
        return jsonify(err_msg), Apiconstants.HTTP_ERR_NOTFOUND

    # get the service_definition id from the content and remove this from data
    try:
        sd_id = content['service_id']
        del content['service_id']
        if sd_id == '':
            raise ValueError('Empty service definition id')
        if get_service_definition(None, sd_id) is None:
            raise ValueError('Invalid service definition id')
    except Exception as e:
        err_msg = 'required input service_id is missing or incorrect'
        logger.error("%s. Exception [%s]" % (err_msg, str(e)))
        return jsonify(err_msg), Apiconstants.HTTP_ERR_BAD_REQUEST

    # Name should be provided by the instance creator
    try:
        service_name = content['name']
        del content['name']
        if service_name == '':
            raise ValueError('Empty service name')
    except Exception as e:
        err_msg = 'required input service \'name\' is missing'
        logger.error("%s. Exception [%s]" % (err_msg, str(e)))
        return jsonify(err_msg), Apiconstants.HTTP_ERR_BAD_REQUEST

    # Description of the instance getting created
    try:
        description = content['description']
        del content['description']
        if description == '':
            raise ValueError('Empty service description provided')
    except Exception as e:
        # If description is not provided, the instance creation should proceed
        logger.info("no instance description provided. Set empty %s", str(e))

    # user_id of the instance creator
    try:
        user_id = content['user_id']
        del content['user_id']
        if description == '':
            raise ValueError('Empty user id provided')
    except Exception as e:
        # If description is not provided, the instance creation should proceed
        logger.info("no user_id provided. Exception [%s]", str(e))

    # action of the instance creator
    try:
        action = content['action']
        if action == '':
            raise ValueError('Empty action provided')
        if action == 'opensds.provision-volume':
            content['parameters']['ip_addr'] = get_config(
                config_file, 'hotpot', 'host')
            content['parameters']['port'] = get_config(config_file, 'hotpot',
                                                       'port')
        else:
            content['parameters']['ip_addr'] = get_config(
                config_file, 'gelato', 'host')
            content['parameters']['port'] = get_config(config_file, 'gelato',
                                                       'port')
    except Exception as e:
        err_msg = 'required input action is missing'
        logger.error("%s. Exception [%s]" % (err_msg, str(e)))
        return jsonify(err_msg), Apiconstants.HTTP_ERR_BAD_REQUEST

    content['parameters']['tenant_id'] = tenant_id
    content['parameters']['auth_token'] = AUTH_TOKEN
    try:
        rc, ret = c.execute_action(content)
        if (rc != Apiconstants.HTTP_CREATED):
            logger.error("api response received return code[%d]", rc)
            return jsonify(json.loads(ret)), rc
    except Exception as ex:
        # The requests may throw ConnectionError. Handle it
        logger.error("recieved exception [%s] while executing action", str(ex))
        return jsonify([]), 500

    ret_json = json.loads(ret)

    # creat service attribs from the return
    service_map = {}
    service_map['name'] = service_name
    # Don't store auth_token
    del ret_json['parameters']['auth_token']
    service_map['input'] = json.dumps(ret_json['parameters'])
    # get the service definition id
    service_map['service_definition_id'] = sd_id
    service_map['description'] = description
    service_map['user_id'] = user_id
    service_map['tenant_id'] = tenant_id
    service_map['status'] = status_map[ret_json['status']]
    service_obj = create_service(None, service_map)

    # Now that service is created append appropriate values
    service_map['service_id'] = sd_id
    service_map['id'] = service_obj['id']
    service_map['created_at'] = service_obj['created_at']
    service_map['updated_at'] = service_obj['updated_at']
    service_map['input'] = ret_json['parameters']

    wf_hash = {}
    wf_hash['id'] = ret_json['id']
    wf_hash['name'] = service_name
    wf_hash['description'] = description
    wf_hash['input'] = json.dumps(ret_json['parameters'])
    wf_hash['workflow_source'] = ret_json['action']['ref']
    wf_hash['service_id'] = service_obj['id']
    wf_hash['status'] = ret_json['status']

    wd_id = ''
    try:
        service_wf_list = get_sd_wfd_association(None, sd_id)
        if not service_wf_list or service_wf_list is None:
            logger.info("could not get workflow definition for sd %s", sd_id)
        else:
            for sd, wd in service_wf_list:
                wd_id = wd.id
    except Exception as e:
        logger.error("received exception while getting wfd id %s", str(e))

    wf_hash['workflow_definition_id'] = wd_id

    # Create the record of this instance in DB
    logger.info("creating workflow table with record [%s]", str(wf_hash))

    # Create a Service of this execution.
    create_workflow(None, wf_hash)
    return jsonify(service_map), 200