此教程演示如何通过SDK调用云控制API实现资源管理。将使用Java语言,以专有网络VPC的交换机为例,演示如何集成SDK,查询资源元数据,如何创建、查询、更新与删除资源,以及遇到云控制API采用异步操作处理任务时如何查询任务状态。
前提条件
RAM用户:
由于阿里云账号(主账号)拥有资源的所有权限,其AccessKey一旦泄露风险巨大,所以建议您使用满足最小化权限需求的RAM用户的AccessKey。具体操作方式请参见创建AccessKey。
给RAM用户授予操作专有网络VPC相关资源的权限。此教程涉及VPC的VSwitch多种操作,所以选择系统权限策略AliyunVPCFullAccess,您在使用的时候可以根据需求进行自定义授权,请参见创建自定义权限策略。
在环境变量中配置AccessKey,具体操作步骤请参见在Linux、macOS和Windows系统配置环境变量。
VPC
您需要有一个VPC资源,用于交换机的绑定,如果您没有VPC,请参见创建和管理专有网络。
资源元数据
资源管理是基于资源元数据的操作,因此您需要熟悉资源元数据。
同步操作与异步操作
此教程中创建资源和删除资源的请求,云控制API处理任务时采用异步操作,你需要了解同步操作与异步操作的概念。云控制API采用异步处理时,查询任务状态参见教程中的查询任务。
Java
最低要求Java 8
初始化项目
引入云控制API的SDK,获取环境变量中的AccessKey。
在项目中引入SDK,具体参见安装SDK,SDK能够帮助您高效的集成与使用云控制API。本例中,SDK引入如下:
<dependency> <groupId>com.aliyun</groupId> <artifactId>cloudcontrol20220830</artifactId> <version>1.1.1</version> </dependency>
新建一个ExampleDemo类,用于实现资源管理。ExampleDemo类中添加createClient方法,用于获取环境变量中的AccessKey,初始化账号Client。示例如下:
/** * 初始化账号Client * @return Client */ public static Client createClient() throws Exception { // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。 Config config = new Config() // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")) // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")); // Endpoint 请参考 https://api.aliyun.com/product/cloudcontrol config.endpoint = "cloudcontrol.aliyuncs.com"; return new Client(config); }
查询资源类型的详情
通过产品code与资源类型code,查询资源类型的详情,返回数据为资源元数据。后续的创建、查询、列举和删除等资源管理操作都是基于资源元数据。
调用GetResourceType,获取资源类型的详情。此教程的示例为专有网络VPC的交换机,专有网络VPC的产品code为VPC,交换机的资源类型code为VSwitch。调用示例如下:
产品code和资源code的获取方式,请参见:产品code和资源code哪里获取?
/** * 查询资源类型的详情 * @param productCode 产品code,此例中为"VPC" * @param resourceTypeCode 资源类型code,此例中为“VSwitch” * @param client 账号client * @return resourceTypeWithOptions 返回对象 */ public GetResourceTypeResponse getResourceType(String productCode, String resourceTypeCode, Client client) throws Exception { String requestPath = "/api/v1/providers/Aliyun/products/" + productCode + "/resourceTypes/" + resourceTypeCode; com.aliyun.cloudcontrol20220830.models.GetResourceTypeHeaders getResourceTypeHeaders = new com.aliyun.cloudcontrol20220830.models.GetResourceTypeHeaders() .setXAcsAcceptLanguage("zh_CH"); com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); // 复制代码运行请自行打印 API 的返回值 GetResourceTypeResponse resourceTypeWithOptions = client.getResourceTypeWithOptions(requestPath, getResourceTypeHeaders, runtime); return resourceTypeWithOptions; }
返回body为VSwitch的资源元数据,解读资源元数据请参见GetResourceType - 查询资源类型的详情,此次调用返回body如下:
创建资源
创建一个交换机,此交换机将用于后续的更新、查询和删除等操作。
调用CreateResource,根据资源元数据填写请求参数。调用示例如下:
/** * 创建资源 * @param regionId 地域ID * @param productCode 产品code,此例中为"VPC" * @param resourceTypeCode 资源类型code,此例中为“VSwitch” * @param client RAM用户client * @return resourceWithOptions 返回对象 */ public CreateResourceResponse createResource(String regionId, String productCode, String resourceTypeCode, Client client) throws Exception { String requestPath = "/api/v1/providers/Aliyun/products/" + productCode + "/resources/" + resourceTypeCode; // 参数集合 Map body = new HashMap(); body.put("VpcId", "vpc-m5esjf3a6b380olet****"); // VPC的ID body.put("CidrBlock", "172.16.24.0/24"); // 交换机的网段。 body.put("ZoneId", "cn-qingdao-b"); // 可用区ID 请参见:https://help.aliyun.com/document_detail/40654.html?spm=5176.28426678.J_HeJR_wZokYt378dwP-lLl.11.51145181tKif1n&scm=20140722.S_help@@%E6%96%87%E6%A1%A3@@40654.S_BB2@bl+RQW@ag0+BB1@ag0+os0.ID_40654-RL_%E5%8F%AF%E7%94%A8%E5%8C%BAID-LOC_search~UND~helpdoc~UND~item-OR_ser-PAR1_213e364e17289863905338434ea19d-V_3-P0_0 CreateResourceRequest createResourceRequest = new CreateResourceRequest() .setRegionId(regionId) .setBody(body); com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); Map<String, String> headers = new HashMap<>(); CreateResourceResponse resourceWithOptions = client.createResourceWithOptions(requestPath, createResourceRequest, headers, runtime); return resourceWithOptions; }
此次调用成功,返回statusCode=202,说明为异步操作,需要进一步查询任务状态,记录taskId,用于查询任务,请参见查询任务。同时,记录resourceId,用于后续步骤中的查询资源、更新资源与删除资源等操作。返回body如下:
列举资源
调用GetResources,列举所有VSwitch资源。调用示例如下:
/** * 列举资源 * @param regionId 地域ID * @param productCode 产品code,此例中为"VPC" * @param resourceTypeCode 资源类型code,此例中为“VSwitch” * @param client RAM用户client * @return resourcesWithOptions 返回对象 */ public GetResourcesResponse getResources(String regionId, String productCode, String resourceTypeCode, Client client) throws Exception { String requestPath = "/api/v1/providers/aliyun/products/" + productCode + "/resources/" + resourceTypeCode; GetResourcesRequest getResourcesRequest = new GetResourcesRequest() .setRegionId(regionId); com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); Map<String, String> headers = new HashMap<>(); GetResourcesResponse resourcesWithOptions = client.getResourcesWithOptions(requestPath, getResourcesRequest, headers, runtime); return resourcesWithOptions; }
此次调用成功,返回body如下:
更新资源
调用UpdateResource,更新指定资源。调用示例如下:
更新资源时,请谨慎操作,避免误操作影响其他资源。
/** * 更新资源 * @param regionId 地域ID * @param productCode 产品code,此例中为"VPC" * @param resourceTypeCode 资源类型code,此例中为“VSwitch” * @param resourceId 资源ID,传入此例中创建VSwitch的ID * @param client RAM用户client * @return updateResourceResponse 返回对象 */ public UpdateResourceResponse updateResource(String regionId, String productCode, String resourceTypeCode, String resourceId, Client client) throws Exception { String requestPath = "/api/v1/providers/Aliyun/products/" + productCode + "/resources/" + resourceTypeCode + "/" + resourceId; // 参数集合 Map body = new HashMap(); body.put("Description", "测试更新"); UpdateResourceRequest updateResourceRequest = new UpdateResourceRequest() .setRegionId(regionId) .setBody(body); com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); Map<String, String> headers = new HashMap<>(); UpdateResourceResponse updateResourceResponse = client.updateResourceWithOptions(requestPath, updateResourceRequest, headers, runtime); return updateResourceResponse; }
此次请求返回码statusCode=200,说明请求成功,返回body如下。验证更新结果可执行查询资源操作,请参见查询资源。
查询资源
调用GetResources,查询指定资源。调用示例如下:
/** * 查询资源 * @param regionId 地域ID * @param productCode 产品code,此例中为"VPC" * @param resourceTypeCode 资源类型code,此例中为“VSwitch” * @param resourceId 资源ID,传入此例中创建VSwitch的ID * @param client RAM用户client * @return resourcesWithOptions 返回对象 */ public GetResourcesResponse getResources(String regionId, String productCode, String resourceTypeCode, String resourceId, Client client) throws Exception { String requestPath = "/api/v1/providers/aliyun/products/" + productCode + "/resources/" + resourceTypeCode + "/" + resourceId; Map<String, String> body = new HashMap<>(); GetResourcesRequest getResourcesRequest = new GetResourcesRequest() .setRegionId(regionId); com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); Map<String, String> headers = new HashMap<>(); GetResourcesResponse resourcesWithOptions = client.getResourcesWithOptions(requestPath, getResourcesRequest, headers, runtime); return resourcesWithOptions; }
此次请求成功,返回body如下:
删除资源
调用DeleteResource,删除指定资源。调用示例如下:
删除资源时,请谨慎操作,避免误操作影响其他资源。
/** * 删除资源 * @param regionId 地域ID * @param productCode 产品code,此例中为"VPC" * @param resourceTypeCode 资源类型code,此例中为“VSwitch” * @param resourceId 资源ID,传入此例中创建VSwitch的ID * @param client RAM用户client * @return deleteResourceResponse 返回对象 */ public DeleteResourceResponse deleteResource(String regionId, String productCode, String resourceTypeCode, String resourceId, Client client) throws Exception { String requestPath = "/api/v1/providers/Aliyun/products/" + productCode + "/resources/" + resourceTypeCode + "/" + resourceId; DeleteResourceRequest deleteResourceRequest = new DeleteResourceRequest() .setRegionId(regionId); com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); Map<String, String> headers = new HashMap<>(); DeleteResourceResponse deleteResourceResponse = client.deleteResourceWithOptions(requestPath, deleteResourceRequest, headers, runtime); return deleteResourceResponse; }
此次调用成功,返回statusCode=202,说明为异步操作,需要进一步查询任务状态,记录taskId,用于查询任务,请参见查询任务。
查询任务
查询指定任务的任务状态。
调用GetTask,查询指定任务的状态。调用示例为查询此教程中的创建VSwitch任务,如下:
/** * 查询异步任务 * @param taskId 任务Id * @param client RAM用户client * @return taskWithOptions 返回对象 */ public GetTaskResponse getTask(String taskId, Client client) throws Exception { com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); Map<String, String> headers = new HashMap<>(); GetTaskResponse taskWithOptions = client.getTaskWithOptions(taskId, headers, runtime); return taskWithOptions; }
此次请求成功,返回body中status="Succeeded",说明任务处理完成。
完整示例代码
本教程中完整ExampleDemo如下:
- 本页导读 (1)
- 前提条件
- 初始化项目
- 查询资源类型的详情
- 创建资源
- 列举资源
- 更新资源
- 查询资源
- 删除资源
- 查询任务
- 完整示例代码