Example #1
0
def add_services():
    url = get_url() + "services"
    headers = {
        'content-type': 'application/json'
    }

    data = {
        "name": "volume provision",
        "description": "Volume Service",
        "tenant_id": get_project_id(),
        "user_id": get_user_id(),
        "input": "",
        "constraint": "",
        "group": "provisioning",
        "workflows": [
            {
                "definition_source": "opensds.provision-volume",
                "wfe_type": "st2"
            },
            {
                "definition_source": "opensds.snapshot-volume",
                "wfe_type": "st2"
            }

        ]

    }
    resp = requests.post(url=url, data=json.dumps(data), headers=headers)
    if resp.status_code != 200:
        print("Request for Register Services failed", resp.status_code)

    print(resp.text)
Example #2
0
def create_topic(client, topic):
    """
    Check if topix exists if not create it.

    :param client:
    :param topic:
    :return:
    """
    project = 'projects/{}'.format(utils.get_project_id())
    dest_topic = project + '/topics/' + topic

    @backoff.on_exception(backoff.expo,
                          HttpError,
                          max_tries=3,
                          giveup=utils.fatal_code)
    def _do_get_request():
        return client.projects().topics().get(topic=dest_topic).execute()

    @backoff.on_exception(backoff.expo,
                          HttpError,
                          max_tries=3,
                          giveup=utils.fatal_code)
    def _do_create_request():
        client.projects().topics().create(name=dest_topic, body={}).execute()

    try:
        _do_get_request()
    except HttpError as e:
        if e.resp.status == 404:
            _do_create_request()
        else:
            logging.error(e)
            raise PubSubException(e)
Example #3
0
def publish(client, body, topic):
    """Publish a message to a Pub/Sub topic."""
    project = 'projects/{}'.format(utils.get_project_id())
    dest_topic = project + '/topics/' + topic

    @backoff.on_exception(backoff.expo,
                          HttpError,
                          max_tries=3,
                          giveup=utils.fatal_code)
    def _do_request():
        client.projects().topics().publish(topic=dest_topic,
                                           body=body).execute()

    try:
        _do_request()
    except HttpError as e:
        logging.error(e)
        raise PubSubException(e)
Example #4
0
def pull(client, sub, endpoint):
    """Register a listener endpoint."""
    subscription = get_full_subscription_name(utils.get_project_id(), sub)
    body = {'pushConfig': {'pushEndpoint': endpoint}}

    @backoff.on_exception(backoff.expo,
                          HttpError,
                          max_tries=3,
                          giveup=utils.fatal_code)
    def _do_request():
        client.projects().subscriptions().modifyPushConfig(
            subscription=subscription, body=body).execute()

    try:
        _do_request()
    except HttpError as e:

        logging.error(e)
        return 'Error', 500
    return 'ok, 204'
Example #5
0
def create_subscriptions(client, sub, topic):
    """
    Create a subscription in pub/sub.

    :param client:
    :param sub:
    :param topic:
    :return:
    """
    project = 'projects/{}'.format(utils.get_project_id())
    dest_sub = project + '/subscriptions/' + sub
    dest_topic = project + '/topics/' + topic
    body = {'topic': dest_topic}
    logging.info("Create topic %sub on topic %s in project %s", sub, topic,
                 project)

    def _do_get_request():
        return client.projects().subscriptions().get(
            subscription=dest_sub).execute()

    @backoff.on_exception(backoff.expo,
                          HttpError,
                          max_tries=3,
                          giveup=utils.fatal_code)
    def _do_create_request():
        res = client.projects().subscriptions().create(name=dest_sub,
                                                       body=body).execute()
        logging.debug(res)

    try:
        _do_get_request()
    except Exception as e:
        if e.resp.status == 404:
            logging.error(e)
            _do_create_request()
        else:
            logging.error(e)
            raise PubSubException(e)
Example #6
0
def run_instance(service_id):
    url = get_url() + "instances"
    headers = {'content-type': 'application/json'}

    data = {
        "id": service_id,
        "action": "opensds.provision-volume",
        "parameters": {
            "ip_addr": OPENSDS_IP,
            "port": "50040",
            "tenant_id": get_project_id(),
            "size": 1,
            "name": "full",
            "auth_token": OPENSDS_TOKEN
        }
    }

    print(data)
    resp = requests.post(url=url, data=json.dumps(data), headers=headers)
    if resp.status_code != 200:
        print("Request for Run Provision Volume Services failed",
              resp.status_code)

    print(resp.text)
Example #7
0
 def fromID(id):
     return Project(utils.get_project_id(id))