准备环境
第三方软件
Java Development Kit:7u10,推荐1.7及以上版本
idea/Eclipes 编辑器
服务器和帐户信息
使用SDK调用FusionCompute的接口之前,要先获取FusionCompute服务器的端口号和帐户信息。
这个是我本地搭建的,也可以使用华为的远程实验室。
获取软件及文档
可以在华为开发者社区资源中心获取最新的SDK和接口参考文档,以及二次开发时候需要的开源软件jar包
鉴权测试
创建Java项目,并引入SDK等相关资源包。
example code package com.huawei.esdk.fusioncompute.demo.servlet; import com.huawei.esdk.fusioncompute.local.ServiceFactory; import com.huawei.esdk.fusioncompute.local.model.ClientProviderBean; import com.huawei.esdk.fusioncompute.local.model.FCSDKResponse; import com.huawei.esdk.fusioncompute.local.model.common.LoginResp; import com.huawei.esdk.fusioncompute.local.resources.common.AuthenticateResource; /** * Created with IntelliJ IDEA. * User: WangNing * Date: 2018/10/19 * Time: 14:24 * To change this template use File | Settings | File Templates. * Description: */ public class Login { public static void main(String[] args) { //设定服务器配置 ClientProviderBean clientProvider = new ClientProviderBean(); //设定服务器配置_设定服务器IP clientProvider.setServerIp("192.168.5.185"); //设定服务器配置_设定服务器端口号 clientProvider.setServerPort("7443"); //初始化用户资源实例 AuthenticateResource auth = ServiceFactory.getService(AuthenticateResource.class, clientProvider); //以用户名,用户密码作为传入参数,调用AuthenticateResource提供的login方法,完成用户的登录 FCSDKResponse<LoginResp> resp = auth.login("jiekou", "Huawei@123!"); System.out.println(resp.getResult()); } }
运行结果
网络异常,图片无法展示
|
服务器默认端口为74443
调用FC的eSDK接口的用户类型必须是接口用户类型
。所以通过SDK鉴权时需要在FC页面中创建接口对接用户,然后用这个用户来调试相关代码。
出现
"errorCode":"10000022","errorDes":"版本号错误"
:
检查FC版本和SDK版本
获取站点和虚机信息
在某些场景下,有时我们需要先获取到所有的虚拟机列表。
然后根据特定条件找到我们需要的特定虚拟机,并对其进行操作。
所以这时便需要用到 FCSDKResponse<PageList> queryVMs(QueryVmsReq queryVmsReq,java.lang.String siteUri)
所以在获取虚拟机信息列表前,需要先获取站点信息,得到siteuri参数值.
1 获取站点信息
package site; import com.huawei.esdk.fusioncompute.local.ServiceFactory; import com.huawei.esdk.fusioncompute.local.model.ClientProviderBean; import com.huawei.esdk.fusioncompute.local.model.FCSDKResponse; import com.huawei.esdk.fusioncompute.local.model.SDKCommonResp; import com.huawei.esdk.fusioncompute.local.model.common.LoginResp; import com.huawei.esdk.fusioncompute.local.model.site.SiteBasicInfo; import com.huawei.esdk.fusioncompute.local.resources.common.AuthenticateResource; import com.huawei.esdk.fusioncompute.local.resources.site.SiteResource; import org.junit.Test; import java.util.List; public class test_site { @Test public void querySites(){ //设定服务器配置 ClientProviderBean clientProvider = new ClientProviderBean(); //设定服务器配置_设定服务器IP clientProvider.setServerIp("192.168.5.185"); //设定服务器配置_设定服务器端口号 clientProvider.setServerPort("7443"); //初始化用户资源实例 AuthenticateResource auth = ServiceFactory.getService(AuthenticateResource.class, clientProvider); //以用户名,用户密码作为传入参数,调用AuthenticateResource提供的login方法,完成用户的登录 FCSDKResponse<LoginResp> resp = auth.login("jiekou", "Huawei@123!"); System.out.println(resp.getResult()); System.out.println("---------------------------------"); //获取SiteResource接口的实现 SiteResource site = ServiceFactory.getService(SiteResource.class,clientProvider); FCSDKResponse<List<SiteBasicInfo>> resps = site.querySites(); System.out.println(resps); //登出FC系统 SDKCommonResp logout = auth.logout(); } }
返回的结果:
/*** * { * "sites": [{ * "name": "site", * "status": "normal", * "ip": "192.168.5.185", * "uri": "/service/sites/4BAD086A", * "urn": "urn:sites:4BAD086A", * "isSelf": true, * "isDC": false * }] * } */
2. 获取虚拟机信息
package vm; import com.huawei.esdk.fusioncompute.local.ServiceFactory; import com.huawei.esdk.fusioncompute.local.model.ClientProviderBean; import com.huawei.esdk.fusioncompute.local.model.FCSDKResponse; import com.huawei.esdk.fusioncompute.local.model.PageList; import com.huawei.esdk.fusioncompute.local.model.SDKCommonResp; import com.huawei.esdk.fusioncompute.local.model.common.LoginResp; import com.huawei.esdk.fusioncompute.local.model.vm.QueryVmsReq; import com.huawei.esdk.fusioncompute.local.model.vm.VmInfo; import com.huawei.esdk.fusioncompute.local.resources.common.AuthenticateResource; import com.huawei.esdk.fusioncompute.local.resources.vm.VmResource; import org.junit.Test; public class queryVMs { // 获取虚拟机信息 @Test public void queryVMs(){ //设定服务器配置 ClientProviderBean clientProvider = new ClientProviderBean(); //设定服务器配置_设定服务器IP clientProvider.setServerIp("192.168.5.185"); //设定服务器配置_设定服务器端口号 clientProvider.setServerPort("7443"); //初始化用户资源实例 AuthenticateResource auth = ServiceFactory.getService(AuthenticateResource.class, clientProvider); //以用户名,用户密码作为传入参数,调用AuthenticateResource提供的login方法,完成用户的登录 FCSDKResponse<LoginResp> resp = auth.login("jiekou", "Huawei@123!"); System.out.println(resp.getResult()); //之前获取的站点信息 String siteUri ="/service/sites/4BAD086A"; VmResource instance = ServiceFactory.getService(VmResource.class,clientProvider); QueryVmsReq req = new QueryVmsReq(); req.setLimit(5); req.setOffset(0); FCSDKResponse<PageList<VmInfo>> fcsdkResponse = instance.queryVMs(req, siteUri); System.out.println(fcsdkResponse); SDKCommonResp sdkCommonResp = auth.logout(); } }
返回结果:
/** * { * "total": 2, * "vms": [{ * "name": "test-linux01", * "location": "urn:sites:4BAD086A:clusters:10", * "hostName": "linux-IcjKyx", * "description": null, * "group": null, * "status": "running", * "clusterName": "ManagementCluster", * "hostUrn": "urn:sites:4BAD086A:hosts:62", * "isBindingHost": false, * "isTemplate": false, * "uri": "/service/sites/4BAD086A/vms/i-00000003", * "urn": "urn:sites:4BAD086A:vms:i-00000003", * "uuid": "e6479704-8f5a-40e7-a376-b320d022e706", * "clusterUrn": "urn:sites:4BAD086A:clusters:10", * "cdRomStatus": "empty", * "createTime": "2018-10-18 07:16:44", * "isLinkClone": false, * "locationName": "ManagementCluster", * "pvDriverStatus": "notRunning", * "toolInstallStatus": "empty", * "imcSetting": null, * "idle": 0, * "toolsVersion": "" * }, { * "name": "VRM01", * "location": "urn:sites:4BAD086A:hosts:62", * "hostName": "linux-IcjKyx", * "description": "", * "group": "VRMGroup", * "status": "running", * "clusterName": "ManagementCluster", * "hostUrn": "urn:sites:4BAD086A:hosts:62", * "isBindingHost": true, * "isTemplate": false, * "uri": "/service/sites/4BAD086A/vms/i-00000001", * "urn": "urn:sites:4BAD086A:vms:i-00000001", * "uuid": "57bf58b6-a716-4037-81ac-0d49d506460b", * "clusterUrn": "urn:sites:4BAD086A:clusters:10", * "cdRomStatus": "empty", * "createTime": "2018-10-17 22:25:34", * "isLinkClone": false, * "locationName": "linux-IcjKyx", * "pvDriverStatus": "running", * "toolInstallStatus": "empty", * "imcSetting": null, * "idle": 0, * "toolsVersion": "1.3.0.23" * }] * } */
总结
以上就是使用FusionCompute eSDK 进行的一个简单尝试,我们可以利用SDK显示更多针对虚拟机的操作。