注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

奕克

爱家人爱工作爱生活

 
 
 

日志

 
 

在VMWARE VRA 7中使用rest api  

2017-01-12 11:30:02|  分类: 虚拟化 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
    要使用rest api访问 VRA7 服务,官方rest 文档不够详细,后来在网页
http://www.vmtocloud.com/how-to-script-a-vrealize-automation-7-rest-api-request/
http://www.vmtocloud.com/how-to-get-the-ip-address-of-a-vm-in-vrealize-automation-7-rest-api/
http://www.vmtocloud.com/vrealize-automation-rest-api-with-curl/
    当中需要使用租户信息,后来发现网页
http://theithollow.com/2016/01/14/vrealize-automation-7-create-tenants/
    中说:
Give the new tenant a name and a description. Then enter a URL name. This name will be appended to this string: https://[vraappliance.domain.name]/vcac/org/ and will be the URL that users will login to. In my example the url is https://vra7.hollow.local/vcac/org/labtenant. Click “Submit and Next”.
 通过vcac的url可以发现租户名字,然后restapi调用时,用户名也不用写域名@的形式,类似的python调用代码如下:

import urllib2
request = urllib2.Request("https://vra ip地址/identity/api/tokens")
request.add_header('Accept', 'application/json')
request.add_header('Content-Type', 'application/json')
request.add_data('{"username":"stu","password":"123","tenant":"vlab"}')
response = urllib2.urlopen(request)
print response.read()
类似的一个python调用例子网页:
http://elasticskies.com/wp-content/uploads/2014/06/Provision-VM-vCO-Workflow-Call.txt

#!/usr/bin/python
import urllib2
import base64
import datetime 
import json
import time
import getopt
import sys

####################
# calling the server
####################


def USAGE(): 
    print "This is an example python script to run vCO Flow"
    print "It is meant to run as script name + params"
    print "E.G.: ./runvCOflow.py param1 param2 param3"
    print "Good luck!"

 
if ( len(sys.argv) != 4 ):
    USAGE()
    sys.exit(0)
  
param1 = sys.argv[1]
param2 = sys.argv[2]
param3 = sys.argv[3]

username = "user"
password = "Password"

##### url needs specific workflow ID to run.
url = "https://vcoserver:8281/api/workflows/0f2171fb-6ee2-4425-a764-f02caa450a62/executions"
request_data =  '<execution-context xmlns="http://www.vmware.com/vco"> \
 <parameters> \
 <parameter name="itemName" type="string"> <string>'+param1+'</string> </parameter> \
 <parameter name="MemorySize" type="string"><string>'+param2+'</string> </parameter> \
 <parameter name="CPUs" type="string"><string>'+param3+'</string> </parameter> \
 <parameter name="UserName" type="string"><string>'+param3+'</string> </parameter> \
</parameters></execution-context>' 



#sys.exit(0)

base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
headers = {'X-Requested-With' : 'urllib2','Content-Type': 'application/xml','Authorization': 'Basic %s' % base64string}
request = urllib2.Request(url=url,data=request_data,headers=headers)
response = urllib2.urlopen(request)
new_response = dict(response.headers)
xml_output = new_response['location']

#######################
# Here below we check the status,
# if server succeeded to run
#######################
authKey = base64.b64encode("user:Password")
headers = {"Content-Type":"application/xml", "Authorization":"Basic " + authKey}
data = { "param":"value"}
request = urllib2.Request(url)
request = urllib2.Request(xml_output)
for key,value in headers.items():
    request.add_header(key,value)
#response = urllib2.urlopen(request)
response_status = response.read()
print "response_status=", response_status
r_content = json.load(urllib2.urlopen(request))['state']
print "r_content=", r_content
now = datetime.datetime.now()
print "Start time ==>>>>", now.strftime("%H_%M_%S")
#checking when the server is up. 
while not (r_content == "COMPLETED") :
    time.sleep( 9 )
    r_content = json.load(urllib2.urlopen(request))['state']
    #check if the server failed
    if (r_content == "FAILED"):
        STATUS = "FAILED"
        print "STATUS=", STATUS
        print "r_content=", r_content
        break
    #print "r_content=", r_content
    print "Still processing the request. It may take 4-5 minutes. Please wait."
now = datetime.datetime.now()
if ( r_content == "COMPLETED" ):
    print "Server started at ==>>>>", now.strftime("%H_%M_%S")
else:
    print "Server FAILED to start"

if __name__ == '__main__':
    pass
  评论这张
 
阅读(13)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017