Using the SDK to Query Resource Details, Relationships, and Change Records
Overview
This example shows how to use the Java SDK to query resource details, relationships, and change records.
- Resource List only displays some resource attributes. The following example shows how to query more details about a resource.
- Associated Resources displays relationships between your Huawei Cloud resources.
- Resource Timeline records resource changes. A record will be added to the resource timeline when a service reports a resource attribute or relationship change to Config. Config retains resource change records for seven years by default.
Prerequisites
- You have obtained the Huawei Cloud SDK and installed the Java SDK.
- You have a Huawei Cloud account and an access key ID (AK) and a secret access key. You can view or create an AK/SK pair in My Credentials > Access Keys on the Huawei Cloud console. For details, see Access Keys.
- Config SDK supports Java JDK 1.8 or later.
Installing the SDK
You can obtain and install the SDK using Maven. To use Maven, add dependencies to the pom.xml file. For details about SDK versions, see SDK Center.
<dependency>
<groupId>com.huaweicloud.sdk</groupId>
<artifactId>huaweicloud-sdk-config</artifactId>
<version>{sdk-version}</version>
</dependency>
Example Code
public class ShowResourceRelationDemo {
public static void main(String[] args) {
// There will be security risks if the AK and SK used for authentication is written into code. Encrypt the AK/SK and store them into the configuration file or environment variables.
// In this example, the AK and SK are stored in environment variables. Before running this example, set environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK.
String ak = System.getenv("HUAWEICLOUD_SDK_AK");
String sk = System.getenv("HUAWEICLOUD_SDK_SK");
String regionId = "<region id>";
HttpConfig config = HttpConfig.getDefaultHttpConfig();
config.withIgnoreSSLVerification(true);
ICredential auth = new GlobalCredentials().withAk(ak).withSk(sk);
ConfigClient client = ConfigClient.newBuilder().withHttpConfig(config).withCredential(auth)
.withRegion(ConfigRegion.valueOf(regionId)).build();
try {
String resourceId = "<resource id>";
// Querying resource details
ShowResourceDetailRequest resourceDetailRequest = new ShowResourceDetailRequest()
.withResourceId(resourceId);
System.out.println(client.showResourceDetail(resourceDetailRequest));
// Querying resource relationships
ShowResourceRelationsRequest resourceRelationsRequest = new ShowResourceRelationsRequest()
.withResourceId(resourceId)
.withDirection(ShowResourceRelationsRequest.DirectionEnum.IN);
System.out.println(client.showResourceRelations(resourceRelationsRequest).toString());
// Querying resource change records
ShowResourceHistoryRequest resourceHistoryRequest = new ShowResourceHistoryRequest()
.withResourceId(resourceId);
System.out.println(client.showResourceHistory(resourceHistoryRequest).toString());
} catch (ConnectionException | RequestTimeoutException | ServiceResponseException ex) {
System.out.println(ex);
}
}
}
Response
class ShowResourceDetailResponse {
id: 81fi****a864
name: zh****ng
provider: iam
type: users
regionId: global
projectId:
projectName:
epId: 0
epName: default
checksum: 522u****e689
created: 2023-09-18T12:56:30.000Z
updated: 2023-09-18T12:56:30.000Z
provisioningState: Succeeded
state: Normal
tags: {}
properties: {pwd_status=false, pwd_strength=high, group_list=[f588****54c5], role_list=[], last_login_time=2023-09-18T12:57:45Z, virtual_mfa_device=false, login_protect={enabled=false}, credentials=[], policy_list=[], access_mode=default, is_root_user=false, enabled=true}
}
class ShowResourceRelationsResponse {
relations: [class ResourceRelation {
relationType: contains
fromResourceType: iam.groups
toResourceType: iam.users
fromResourceId: f587****54c5
toResourceId: 81fa****a864
}]
pageInfo: class PageInfo {
currentCount: 1
nextMarker: null
}
}
class ShowResourceHistoryResponse {
items: [class HistoryItem {
domainId: 39f4****ea39
resourceId: 81fa****a864
resourceType: iam.users
captureTime: 2023-09-21T15:39:27.632Z
status: ResourceChanged.CREATE
relations: [class ResourceRelation {
relationType: isContainedIn
fromResourceType: iam.users
toResourceType: iam.groups
fromResourceId: 81fa****a864
toResourceId: b04e****8dd2
}]
resource: class ResourceEntity {
id: 81fa****a864
name: zh****ng
provider: iam
type: users
regionId: global
projectId:
projectName:
epId: 0
epName: default
checksum: 00ce****f053
created: 2023-09-18T12:56:30Z
updated: 2023-09-18T12:56:30Z
provisioningState: Succeeded
state: null
tags: {}
properties: {pwd_status=false, pwd_strength=high, group_list=[b04e****8dd2], role_list=[], virtual_mfa_device=false, login_protect={enabled=false}, credentials=[], policy_list=[], access_mode=default, enabled=true}
}
}]
pageInfo: class PageInfo {
currentCount: 1
nextMarker: null
}
}
Reference
For more details, see Viewing Resource Changes.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot