The G42 Cloud Node.js SDK allows you to easily work withG42 Cloud services such as Elastic Compute Service (ECS) and Virtual Private Cloud (VPC) without the need to handle API related tasks.
This document introduces how to obtain and use G42 Cloud Node.js SDK.
-
To use G42 Cloud Node.js SDK, you must have G42 Cloud account as well as the Access Key (AK) and Secret key (SK) of the G42 Cloud account. You can create an Access Key in the G42 Cloud console.
-
To use G42 Cloud Node.js SDK to access the APIs of specific service, please make sure you do have activated the service in G42 Cloud console if needed.
-
G42 Cloud Node.js SDK requires Node 10.16.1 or later.
The recommended way to install SDK is with npm.
You must depended on @g42cloud/g42cloud-sdk-core
library no matter which product/service development kit you
need to use. Take using VPC SDK for example, you need to install @g42cloud/g42cloud-sdk-vpc
library:
npm install @g42cloud/g42cloud-sdk-core
npm install @g42cloud/g42cloud-sdk-vpc
- The following example shows how to query VPCs, you need to substitute your real
{Service}Client
forVpcClient
in actual use. - Substitute the values for
{your ak string}
,{your sk string}
,{your endpoint}
,{your project id}
.
// index.js
const core = require('@g42cloud/g42cloud-sdk-core');
const vpc = require('@g42cloud/g42cloud-sdk-vpc');
const ak = '<YOUR AK>';
const sk = '<YOUR SK>';
const endpoint = 'https://vpc.ae-ad-1.g42cloud.com';
const project_id = '<YOUR_PROJECT_ID>';
const credentials = new core.BasicCredentials()
.withAk(ak)
.withSk(sk)
.withProjectId(project_id);
const client = vpc.VpcClient.newBuilder()
.withCredential(credentials)
.withEndpoint(endpoint)
.build();
const request = new vpc.ListVpcsRequest();
const result = await client.listVpcs(request);
result
.then((result) => {
console.log('JSON.stringify(result)::' + JSON.stringify(result));
})
.catch((ex) => {
console.log('exception:' + JSON.stringify(ex));
});
- Debug the example above
# Run commandοΌ
node index.js
API Explorer provides api retrieval and online debugging, supports full fast retrieval, visual debugging, help document viewing, and online consultation.
Detailed changes for each released version are documented in the CHANGELOG.md.
User Manual π
- 1. Client Configuration
- 2. Credentials Configuration
- 3. Client Initialization
- 4. Send Request and Handle response
- 5. Troubleshooting
1. Client Configuration π
1.1 Default Configuration π
// Use default configuration
const client = VpcClient.newBuilder()
1.2 Network Proxy π
// Use proxy if needed
client.withProxyAgent("http://username:[email protected]:8080")
1.3 SSL Certification π
// Skip SSL certification checking while using https protocol if needed
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
2. Credentials Configuration π
There are two types of G42 Cloud services, regional
services and global
services.
For Regional
services' authentication, projectId is required.
For global
services' authentication, domainId is required.
Parameter description
:
ak
is the access key ID for your account.sk
is the secret access key for your account.projectId
is the ID of your project depending on your region which you want to operate.domainId
is the account ID of G42 CLOUD.securityToken
is the security token when using temporary AK/SK.
You could use permanent AK plus SK or use temporary AK plus SK plus SecurityToken to complete credentials' configuration.
2.1 Use Permanent AK&SK π
// Regional Services
const basicCredentials = new BasicCredentials()
.withAk(ak)
.withSk(sk)
.withProjectId(projectId)
// Global Services
const globalCredentials = new GlobalCredentials()
.withAk(ak)
.withSk(sk)
.withDomainId(domainId)
2.2 Use Temporary AK&SK π
It's required to obtain temporary AK&SK and security token first, which could be obtained through permanent AK&SK or through an agency.
- Obtaining a temporary access key and security token through token, you could refer to document: https://docs.g42cloud.com/api/iam/en-us_topic_0097949518.html. The API mentioned in the document above
corresponds to the method of
CreateTemporaryAccessKeyByToken
in IAM SDK.
// Regional Services
const basicCredentials = new BasicCredentials()
.withAk(ak)
.withSk(sk)
.withSecurityToken(securityToken)
.withProjectId(projectId)
// Global Services
const globalCredentials = new GlobalCredentials()
.withAk(ak)
.withSk(sk)
.withSecurityToken(securityToken)
.withDomainId(domainId)
3. Client Initialization π
3.1 Initialize the {Service}Client with specified Endpoint π
const client = VpcClient.newBuilder()
.withCredential(globalCredentials)
.withEndpoint(endpoint)
.withProxyAgent(proxy)
.build()
where:
endpoint
is the service specific endpoints, see Regions and Endpoints.
4. Send Requests and Handle Responses π
const result = client.showJobDetail(new ShowJobDetailRequest("id"));
result.then(result => {
res.send("JSON.stringify(result)::" + JSON.stringify(result))
}).catch(ex => {
res.send("exception:" + JSON.stringify(ex))
});
5. Troubleshooting π
5.1 Original HTTP Listener π
In some situation, you may need to debug your http requests, original http request and response information will be needed. The SDK provides a listener function to obtain the original encrypted http request and response information.
β οΈ Warning: The original http log information is used in debugging stage only, please do not print the original http header or body in the production environment. This log information is not encrypted and contains sensitive data such as the password of your ECS virtual machine, or the password of your IAM user account, etc. When the response body is binary content, the body will be printed as "***" without detailed information.
Set the environment variable process.env.DEBUG to enable debug log printing.