VMware Orchestrator API调用Workflow 示例

1、获取Workflow信息

1.1 workflow id

 

1.2 workflow input

 

1.3 workflow output

 

2、Python示例代码

import requests
import time
from django.conf import settings


def get_result(result_url, headers, user, password):
    """
    执行Workflow执行结果查询
    """
    result_response = requests.get(url=result_url, headers=headers, auth=(user, password))
    return result_response.json()


def get_vm_owner(user, password, workflow_id, vm_name):
    """
    通过VM名称获取VM Owner
    """
    submit_url = "https://vro-server-ip/vco/api/workflows/{}/executions".format(workflow_id)
    headers = {
        "Content-Type": "Application/Json"
    }

    request_body = {
        "parameters": [
            {
                "type": "string",  # input参数类型
                "name": "vm_name",  # input参数名称 
                "value": {
                    "string": {
                        "value": vm_name  # input参数值
                    }
                }
            }
        ]
    }
    response = requests.post(url=submit_url, headers=headers, json=request_body, auth=(user, password))  # 执行Workflow API异步调用
    if response.status_code == 202:
        result_url = response.headers.get("Location")  # 获取Workflow执行结果的URL
        time.sleep(10)  # 等待Workflow查询执行完成

        try_number = 3  # 尝试3次
        while try_number:
            result_json = get_result(result_url, headers, user, password)  # 查询Workflow执行结果
            if result_json.get("state") == "running":  # 如果Workflow还在执行中,继续循环
                try_number = try_number - 1
                time.sleep(3)
         continue
if result_json.get("state") == "completed":  # 如果Workflow执行完成,获取output值 return result_json.get("output-parameters")[0]["value"]["string"]["value"] if result_json.get("state") in ["failed", "canceled"]: return None

if __name__ == '__main__':
owner = get_vm_owner("vro_user", "vro_password", "workflow_id", "vm_name")
print(owner)

 

posted @ 2020-03-21 10:04  Vincen_shen  阅读(349)  评论(0编辑  收藏  举报