# huaweicloud-RFS-ApplyExecutionPlanDemo-java **Repository Path**: HuaweiCloudDeveloper/huaweicloud-rfs-apply-execution-plan-demo-java ## Basic Information - **Project Name**: huaweicloud-RFS-ApplyExecutionPlanDemo-java - **Description**: 提供通过执行计划创建和删除VPC资源的代码示例,说明如何使用执行计划部署资源,并在部署资源之前获得执行计划内容的详细信息。 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master-dev - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-07-25 - **Last Updated**: 2025-06-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 资源编排服务中通过执行计划创建和删除VPC资源(java版本) ## 前置条件 - 1.参考华为云开发工具包(SDK)引入AOS Java SDK - 2.要使用华为云Java SDK,您需要拥有云账号以及该账号对应的Access Key(AK)和 Secret Access Key(SK)。请在华为云控制台“我的凭证-访问密钥”页面上创建和查看您的AK&SK。更多信息请查看[访问密钥](https://support.huaweicloud.com/usermanual-ca/zh-cn_topic_0046606340.html)。 - Aos SDK期望用户主动传入project_id,若未传入project_id,Aos SDK将选择您对应region下默认项目的project_id。更多信息请查看[IAM基本概念](https://support.huaweicloud.com/productdesc-iam/iam_01_0023.html#section8)。 - 3.获取您期望使用的目标终端节点 - 4.预先准备您的模板文件(HCL语法文本描述文件,支持tf、tf.json、zip包文件格式,用于描述您的云资源) - 当前支持的provider版本请见[华为云官网](https://support.huaweicloud.com/productdesc-aos/rf_02_0006.html) - 模板书写说明详见[huaweicloud provider官网](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest) - 5.华为云AOS Java SDK 需要运行在Java 1.8及以上版本。 ### SDK获取和安装 使用maven安装华为云Aos Java SDK: ```xml com.huaweicloud.sdk huaweicloud-sdk-aos 3.1.49 ``` ## 示例代码 声明您的Access Key、Secret Key、VPC资源的HCL语法模板、资源栈名称以及执行计划名称。 ```java // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 String ak = System.getenv("HUAWEICLOUD_SDK_AK"); String sk = System.getenv("HUAWEICLOUD_SDK_SK"); /*stack以project_id为单位进行管理*/ String projectId = ""; /*HCL语法文本描述文件,支持tf、tf.json文件格式*/ String templateBody = ""; /*stack_name在当前region下是唯一的,用于标识您的资源栈*/ String stackName = ""; /*execution_plan_name在当前资源栈下是唯一的,用于标识您的执行计划*/ String executionPlanName = ""; ``` 通过Aos SDK创建一个Client,资源编排服务为region级别服务,将在您选择的region下生成目标资源。 ```java ICredential auth = new BasicCredentials() .withProjectId(projectId) .withAk(ak) .withSk(sk); /*资源栈及其资源将创建在此region下,此处以CN_NORTH_4为例*/ AosClient client = AosClient.newBuilder() .withCredential(auth) .withRegion(AosRegion.CN_NORTH_4) .build(); ``` 首先创建一个空的资源栈,在该步骤中请不要传入templateBody或templateUri,否则将立即触发部署资源栈。 ```java public static String createStack(AosClient client, String stackName) { CreateStackRequest createStackRequest = new CreateStackRequest(); createStackRequest.setClientRequestId(UUID.randomUUID().toString()); CreateStackRequestBody createStackRequestBody = new CreateStackRequestBody(); createStackRequestBody.setStackName(stackName); createStackRequest.setBody(createStackRequestBody); CreateStackResponse response = client.createStack(createStackRequest); System.out.println("create stack return:" + response.toString()); return response.getStackId(); } ``` 在当前资源栈下调用CreateExecutionPlan,资源编排将为您异步处理解析模板,生成执行计划,此时不会直接部署资源。 ```java public static String createExecutionPlan(AosClient client, String stackName, String stackId, String executionPlanName, String templateBody) { CreateExecutionPlanRequest createExecutionPlanRequest = new CreateExecutionPlanRequest(); createExecutionPlanRequest.setClientRequestId(UUID.randomUUID().toString()); createExecutionPlanRequest.setStackName(stackName); CreateExecutionPlanRequestBody createExecutionPlanRequestBody = new CreateExecutionPlanRequestBody(); createExecutionPlanRequestBody.setExecutionPlanName(executionPlanName); createExecutionPlanRequestBody.setStackId(stackId); createExecutionPlanRequestBody.setTemplateBody(templateBody); createExecutionPlanRequest.setBody(createExecutionPlanRequestBody); CreateExecutionPlanResponse createExecutionPlanResponse = client.createExecutionPlan(createExecutionPlanRequest); System.out.println("create execution plan return: " + createExecutionPlanResponse.toString()); return createExecutionPlanResponse.getExecutionPlanId(); } ``` 您可通过GetExecutionPlan获取执行计划的详细信息。 ```java public static void getExecutionPlan(AosClient client, String stackName, String executionPlanName, String stackId, String executionPlanId) { GetExecutionPlanRequest getExecutionPlanRequest = new GetExecutionPlanRequest(); getExecutionPlanRequest.setClientRequestId(UUID.randomUUID().toString()); getExecutionPlanRequest.setExecutionPlanName(executionPlanName); getExecutionPlanRequest.setExecutionPlanId(executionPlanId); getExecutionPlanRequest.setStackName(stackName); getExecutionPlanRequest.setStackId(stackId); long startTime = System.currentTimeMillis(); while (true) { long nowTime = System.currentTimeMillis(); if (nowTime - startTime > timeoutInterval) { System.out.println("create execution plan time out and will be exit"); throw new RuntimeException("create execution plan timeout"); } System.out.println("waiting for create execution plan ..."); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println("waiting for create execution plan throw exception: " + Arrays.toString(e.getStackTrace())); throw new RuntimeException("waiting for create execution plan throw exception"); } GetExecutionPlanResponse executionPlanResponse = client.getExecutionPlan(getExecutionPlanRequest); if (executionPlanResponse.getHttpStatusCode() != 404) { System.out.println("get execution plan return: " + executionPlanResponse); break; } System.out.println("create execution plan in progress ..."); } } ``` 确认执行计划与目标资源状态一致后,可继续触发ApplyExecutionPlan,此时才会正式生成资源。 ```java public static void applyExecutionPlan(AosClient client, String stackName, String executionPlanName, String stackId, String executionPlanId) { ApplyExecutionPlanRequest applyExecutionPlanRequest = new ApplyExecutionPlanRequest(); applyExecutionPlanRequest.setClientRequestId(UUID.randomUUID().toString()); applyExecutionPlanRequest.setExecutionPlanName(executionPlanName); applyExecutionPlanRequest.setStackName(stackName); ApplyExecutionPlanRequestBody applyExecutionPlanRequestBody = new ApplyExecutionPlanRequestBody(); applyExecutionPlanRequestBody.setExecutionPlanId(executionPlanId); applyExecutionPlanRequestBody.setStackId(stackId); applyExecutionPlanRequest.setBody(applyExecutionPlanRequestBody); ApplyExecutionPlanResponse applyExecutionPlanResponse = client.applyExecutionPlan(applyExecutionPlanRequest); System.out.println("apply execution plan: " + applyExecutionPlanResponse); } ``` 通过getExecutionPlanMetadata轮询获取执行计划的状态,等待部署完成。 ```java public static void getExecutionPlanMetadata(AosClient client, String stackName, String executionPlanName, String stackId, String executionPlanId) { GetExecutionPlanMetadataRequest getExecutionPlanMetadataRequest = new GetExecutionPlanMetadataRequest(); getExecutionPlanMetadataRequest.setClientRequestId(UUID.randomUUID().toString()); getExecutionPlanMetadataRequest.setExecutionPlanName(executionPlanName); getExecutionPlanMetadataRequest.setStackName(stackName); getExecutionPlanMetadataRequest.setExecutionPlanId(executionPlanId); getExecutionPlanMetadataRequest.setStackId(stackId); long startTime = System.currentTimeMillis(); while (true) { long nowTime = System.currentTimeMillis(); if (nowTime - startTime > timeoutInterval) { System.out.println("apply execution plan time out and will be exit"); throw new RuntimeException("apply execution plan time out"); } System.out.println("waiting for apply execution plan ..."); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println("waiting for apply execution plan throw exception: " + Arrays.toString(e.getStackTrace())); throw new RuntimeException("waiting for apply execution plan throw exception"); } GetExecutionPlanMetadataResponse executionPlanResponse = client.getExecutionPlanMetadata(getExecutionPlanMetadataRequest); if (executionPlanResponse.getStatus() == GetExecutionPlanMetadataResponse.StatusEnum.APPLIED) { System.out.println("execution plan applied!"); System.out.println("get execution plan metadata return: " + executionPlanResponse.toString()); break; } System.out.println("apply execution plan in progress ..."); } } ``` 此时可通过ListStackResource来查看生成的资源。 ```java public static void listStackResource(AosClient client, String stackId, String stackName) { ListStackResourcesRequest listStackResourcesRequest = new ListStackResourcesRequest(); listStackResourcesRequest.setClientRequestId(UUID.randomUUID().toString()); listStackResourcesRequest.setStackId(stackId); listStackResourcesRequest.setStackName(stackName); ListStackResourcesResponse listStackResourcesResponse = client.listStackResources(listStackResourcesRequest); System.out.println("list stack resources return: " + listStackResourcesResponse.toString()); } ``` 最后,可以通过删除资源栈来删除资源栈、执行计划以及生成的资源。 ```java public static void deleteStack(AosClient client, String stackId, String stackName) { DeleteStackRequest deleteStackRequest = new DeleteStackRequest(); deleteStackRequest.setClientRequestId(UUID.randomUUID().toString()); deleteStackRequest.setStackId(stackId); deleteStackRequest.setStackName(stackName); client.deleteStack(deleteStackRequest); GetStackMetadataRequest getStackMetadataRequest = new GetStackMetadataRequest(); getStackMetadataRequest.setClientRequestId(UUID.randomUUID().toString()); getStackMetadataRequest.setStackId(stackId); getStackMetadataRequest.setStackName(stackName); long startTime = System.currentTimeMillis(); while (true) { long nowTime = System.currentTimeMillis(); if (nowTime - startTime > timeoutInterval) { System.out.println("delete stack time out and will be exit"); throw new RuntimeException("delete stack time out"); } System.out.println("waiting for delete stack ..."); try { Thread.sleep(5000); client.getStackMetadata(getStackMetadataRequest); } catch (ClientRequestException e) { if (e.getHttpStatusCode() == 404) { System.out.println("delete complete!"); return; } System.out.println("delete stack throw exception: " + e); throw e; } catch (InterruptedException e) { System.out.println("waiting delete stack throw exception: " + Arrays.toString(e.getStackTrace())); throw new RuntimeException("waiting delete stack throw exception"); } } } ``` ## 参考 更多信息请参考[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/AOS/doc?api=CreateStack) ## 修订记录 | 发布日期 | 文档版本 | 修订说明 | |:----------:| :------: | :----------: | | 2023-08-30 | 1.0 | 文档首次发布 |