3. 安装SDK
您可以通过Maven方式获取和安装SDK,您只需要在Java项目的pom.xml文件中加入相应的依赖项即可。
具体的SDK版本号请参见 SDK开发中心 。
Copied!
<dependencies>
<dependency>
<groupId>com.huaweicloud.sdk</groupId>
<artifactId>huaweicloud-sdk-rms</artifactId>
<version>3.0.68</version>
</dependency>
</dependencies>
4.开始使用
创建预定义标签示例代码
Copied!
public class ListAllResources {
public static void main(String[] args) {
String ak = System.getenv("HUAWEICLOUD_SDK_AK");
String sk = System.getenv("HUAWEICLOUD_SDK_SK");
String regionId = "<regionId>";
String domainId = "<domainId>";
ICredential auth = new GlobalCredentials()
.withAk(ak)
.withSk(sk)
.withDomainId(domainId);
RmsClient client = RmsClient.newBuilder()
.withCredential(auth)
.withRegion(RmsRegion.valueOf(regionId))
.build();
ListAllResourcesRequest request = new ListAllResourcesRequest();
try {
ListAllResourcesResponse response = client.listAllResources(request);
System.out.println(response.toString());
} catch (ConnectionException | RequestTimeoutException | ServiceResponseException e) {
System.out.println(e);
}
}
}
5.返回示例
当前租户下所有资源
Copied!
{
"httpStatusCode": 200,
"pageInfo": {
"currentCount": 100,
"nextMarker": "******"
},
"resources": [
{
"checksum": "******",
"created": "2020-12-23T05:06:29Z",
"epId": "0",
"epName": "default",
"id": "******",
"name": "DRS-8805",
"projectId": "******",
"projectName": "cn-north-7",
"properties": {
"net_type": "eip",
"job_direction": "up",
"db_use_type": "sync",
"status": "RELEASE_RESOURCE_COMPLETE"
},
"provider": "drs",
"provisioningState": "Succeeded",
"regionId": "cn-north-7",
"tags": {},
"type": "synchronizationJob",
"updated": "2020-12-23T06:07:26Z"
}
]
}
0. 版本说明
本示例基于华为云SDK V3.0版本开发。
1. 示例简介
华为云提供了资源管理服务(RMS)的SDK,您可以直接集成SDK来调用RMS的相关API,从而实现对资源管理的快速操作。该示例展示了如何通过java版SDK返回当前租户下所有资源。
2. 前置条件
3. 安装SDK
您可以通过Maven方式获取和安装SDK,您只需要在Java项目的pom.xml文件中加入相应的依赖项即可。 具体的SDK版本号请参见 SDK开发中心 。
<dependencies> <dependency> <groupId>com.huaweicloud.sdk</groupId> <artifactId>huaweicloud-sdk-rms</artifactId> <version>3.0.68</version> </dependency> </dependencies>
4.开始使用
创建预定义标签示例代码
public class ListAllResources { public static void main(String[] args) { // 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 String ak = System.getenv("HUAWEICLOUD_SDK_AK"); String sk = System.getenv("HUAWEICLOUD_SDK_SK"); String regionId = "<regionId>"; String domainId = "<domainId>"; ICredential auth = new GlobalCredentials() // 测试环境加入此行代码 // .withIamEndpoint("<iamEndpoint>") .withAk(ak) .withSk(sk) .withDomainId(domainId); RmsClient client = RmsClient.newBuilder() .withCredential(auth) // 测试环境应改为 .withRegion(new Region(regionId, "<endpoint>")) .withRegion(RmsRegion.valueOf(regionId)) .build(); ListAllResourcesRequest request = new ListAllResourcesRequest(); try { ListAllResourcesResponse response = client.listAllResources(request); System.out.println(response.toString()); } catch (ConnectionException | RequestTimeoutException | ServiceResponseException e) { System.out.println(e); } } }
5.返回示例
当前租户下所有资源
{ "httpStatusCode": 200, "pageInfo": { "currentCount": 100, "nextMarker": "******" }, "resources": [ { "checksum": "******", "created": "2020-12-23T05:06:29Z", "epId": "0", "epName": "default", "id": "******", "name": "DRS-8805", "projectId": "******", "projectName": "cn-north-7", "properties": { "net_type": "eip", "job_direction": "up", "db_use_type": "sync", "status": "RELEASE_RESOURCE_COMPLETE" }, "provider": "drs", "provisioningState": "Succeeded", "regionId": "cn-north-7", "tags": {}, "type": "synchronizationJob", "updated": "2020-12-23T06:07:26Z" } ] }
6.接口及参数说明
参见:返回当前租户下所有资源
7. 参考
更多示例信息请参考RMS
8. 修订记录