使用SoapUI调用Vsphere Web Service

时间:2023-03-09 18:13:35
使用SoapUI调用Vsphere Web Service

项目中经常需要调用Webservice进行验证测试,下面就介绍下如何使用测试工具SoapUI调用Vsphere vcenter的 Web Service

VSphere的Webservice地址默认为 https://vcenterip/sdk/vimService

1 首先要进行认证。使用Login方法,获取合法Cookie

Request内容如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:vim25">
   <soapenv:Header/>

   <soapenv:Body>
      <urn:Login>
         <urn:_this type="SessionManager">SessionManager</urn:_this>
         <urn:userName>administrator@vsphere.local</urn:userName>
         <urn:password>密码</urn:password>
         <!--Optional:-->
         <urn:locale>?</urn:locale>
      </urn:Login>
   </soapenv:Body>
</soapenv:Envelope>

Response内容如下:

<soapenv:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <LoginResponse xmlns="urn:vim25">
         <returnval>
            <key>52c8ab58-eb03-e526-c733-5051d62652b6</key>
            <userName>VSPHERE.LOCAL\Administrator</userName>
            <fullName>Administrator vsphere.local</fullName>
            <loginTime>2016-08-15T09:19:39.526713Z</loginTime>
            <lastActiveTime>2016-08-15T09:19:39.526713Z</lastActiveTime>
            <locale>?</locale>
            <messageLocale>zh_CN</messageLocale>
            <extensionSession>false</extensionSession>
            <ipAddress>10.200.108.92</ipAddress>
            <userAgent>Apache-HttpClient/4.1.1 (java 1.5)</userAgent>
            <callCount>0</callCount>
         </returnval>
      </LoginResponse>
   </soapenv:Body>
</soapenv:Envelope>

注意:需要记录response中的header信息,用于后面设置header,我这里的header信息如下

Set-Cookie        vmware_soap_session="52f2ce6a-8ce9-94cc-bd62-3f750d61cd4f"; Path=/; HttpOnly; Secure;

2 然后我使用FindByDnsName方法,获取一台机器的ManagedObjectID

Request内容如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:vim25">
<soapenv:Header>
</soapenv:Header>
   <soapenv:Body>

      <urn:FindByDnsName>
         <urn:_this type="SearchIndex">SearchIndex</urn:_this>
         <!--Optional:-->
         <urn:dnsName>testupgrade</urn:dnsName>
         <urn:vmSearch>true</urn:vmSearch>
      </urn:FindByDnsName>

   </soapenv:Body>
</soapenv:Envelope>

注意上面xml中<urn:_this type="SearchIndex">SearchIndex</urn:_this>红色字体是根据文档中的定义填写的

然后添加header,作为认证信息,此处的cookie中填写的是,认证时response中的Set-Cookie ,具体如下图

使用SoapUI调用Vsphere Web Service

执行成功,Response的内容如下:

<soapenv:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <FindByDnsNameResponse xmlns="urn:vim25">
         <returnval type="VirtualMachine">vm-2823</returnval>
      </FindByDnsNameResponse>
   </soapenv:Body>
</soapenv:Envelope>

3 调用VSphere 其他的Webservice的方式也一样

如果你设置的Header不对,那么系统将会报错:

faultstring:The session is not authenticated

参考:

soapUI: faultstring: The session is not authenticated