Call an API operation by using an SDK to query ApsaraDB for MongoDB instances in a specific region.
View API documentation
Refer to List of operations by function to find an API operation that queries instances in a specific region. For example, you can call the DescribeDBInstances operation. See the DescribeDBInstances topic for the required request parameters and permissions.
Create a RAM user and grant permissions to the RAM user
Identities
You can call the operation by using an Alibaba Cloud account, a RAM role, or a RAM user. For more information, see Identity, credential, and authorization. In this example, a RAM user is used to call the DescribeDBInstances operation. Log on to the RAM console, create a RAM user, and record the AccessKey pair of the RAM user. For more information, see Create a RAM user.
Authorization
-
Go to the Users page, find the target RAM user, and click Add Permissions in the Actions column.
-
In the Policies section, search for the keyword
MongoDBand select the policy that you want to attach.NoteAliyunMongoDBFullAccess: provides full access to ApsaraDB for MongoDB.
AliyunMongoDBReadOnlyAccess: provides read-only access to ApsaraDB for MongoDB.
For information about how to create a custom policy, see RAM authorization.
-
Click OK.
Credential
A default AccessKey is generated when you create a RAM user. You can also go to the details page of the RAM user, navigate to the Certification management tab, and click Create AccessKey. For more information, see Create an AccessKey.
Call the DescribeDBInstances operation
The following example uses SDK for Java to call the DescribeDBInstances operation.
Configure environment variables
Configure the AccessKey pair in environment variables to avoid hard-coding credentials in your code.
For more information, see Configure environment variables in Linux, macOS, and Windows.
Download the sample code
-
Go to the DescribeDBInstances page.
-
On the Parameter Configuration tab, set the required parameter.
-
RegionId: the ID of the region in which you want to query instances.
-
-
On the right side, on the SDK Sample tab, select v2.0 for the SDK version and Java for the language. Then, click Download Project.
-
Decompress the sample code package on your computer and access the alibabacloud_sample directory.
Open and run the project
Use IntelliJ IDEA to load the project. After the dependencies are loaded, open the src/main/java/com/aliyun/sample/Sample.java file and add code to print the returned values based on the comments in the code.
Click the Run button in the upper-right corner of the IDE to view the output log. The following example shows a sample response:
{
"headers": {
"access-control-allow-origin": "*",
"date": "Wed, 26 Jul 2023 05:45:58 GMT",
"keep-alive": "timeout=25",
"transfer-encoding": "chunked",
"vary": "Accept-Encoding",
"x-acs-request-id": "CF00C412-4BB5-5D02-803C-46D0AF71DC23",
"connection": "keep-alive",
"content-type": "application/json;charset=utf-8",
"access-control-expose-headers": "*",
"x-acs-trace-id": "48df4a01d258338ee5327a6a48628426"
},
"statusCode": 200,
"body": {
"DBInstances": {
"DBInstance": [
{
"capacityUnit": "",
"chargeType": "PostPaid",
"creationTime": "2023-07-25T06:32:25Z",
"DBInstanceClass": "mdb.shard.2x.xlarge.d",
"DBInstanceId": "dds-***",
"DBInstanceStatus": "Running",
"DBInstanceStorage": 500,
"DBInstanceType": "replicate",
"engine": "MongoDB",
"engineVersion": "5.0",
"expireTime": "2999-09-08T16:00Z",
"kindCode": "18",
"lockMode": "Unlock",
"mongosList": { "mongosAttribute": [] },
"networkType": "VPC",
"regionId": "cn-shanghai",
"replicationFactor": "3",
"resourceGroupId": "rg-***",
"shardList": { "shardAttribute": [] },
"storageType": "cloud_essd1",
"tags": { "tag": [] },
"zoneId": "cn-shanghai-m"
}
]
},
"pageNumber": 1,
"pageSize": 30,
"requestId": "CF00C412-4BB5-5D02-803C-***",
"totalCount": 3
}
}