Skip to content

Commit

Permalink
release 3.0.14-beta source code for nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
Huaweicloud-SDK committed Sep 10, 2021
1 parent 7253b22 commit 0a57624
Show file tree
Hide file tree
Showing 299 changed files with 9,053 additions and 4,310 deletions.
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
# 3.0.14-beta 2021-09-10

### HuaweiCloud SDK Core

- _Features_
- Support Region Management
- _Bug Fix_
- None
- _Change_
- None

### HuaweiCloud SDK CCE
- _Features_
- Support the interfaces `AddNode` and `ResetNode`.
- _Bug Fix_
- None
- _Change_
- None

### HuaweiCloud SDK EVS
- _Features_
- None
- _Bug Fix_
- None
- _Change_
- Set the request parameter `size` of the interface `CreateVolume` to `required`.


# 3.0.13-beta 2021-08-25

### HuaweiCloud SDK ECS
Expand Down
27 changes: 27 additions & 0 deletions CHANGELOG_CN.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
# 3.0.14-beta 2021-09-10

### HuaweiCloud SDK Core

- _新增特性_
- 支持Region管理
- _解决问题_
-
- _特性变更_
-

### HuaweiCloud SDK CCE
- _新增特性_
- 支持接口`AddNode``ResetNode`
- _解决问题_
-
- _特性变更_
-

### HuaweiCloud SDK EVS
- _新增特性_
-
- _解决问题_
-
- _特性变更_
- 接口`CreateVolume`的请求参数`size`改为必填

# 3.0.13-beta 2021-08-25

### HuaweiCloud SDK ECS
Expand Down
41 changes: 29 additions & 12 deletions core/ClientBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { DefaultHttpClient } from "./http/DefaultHttpClient";
import { RequiredError } from "./auth/AKSKSigner";
import { BasicCredentials } from "./auth/BasicCredentials";
import { GlobalCredentials } from "./auth/GlobalCredentials";
import { SdkException } from "./exception/SdkException";
import { Region } from "./region/region";
const path = require('path');

interface CredParams {
Expand All @@ -35,15 +37,16 @@ interface CredParams {
}
export class ClientBuilder<T> {
private init: Function;
private endpoint: string | undefined;
private credential: ICredential | undefined;
private proxyAgent: string | undefined;
public credentialType: string[] = ["BasicCredentials", "GlobalCredentials"];
public envParams: CredParams = process.env;
private endpoint?: string;
private credential?: ICredential;
private proxyAgent?: string;
private credentialType: string[] = ["BasicCredentials", "GlobalCredentials"];
private envParams: CredParams = process.env;
private region?: Region;

public constructor(init: (hcClient: HcClient) => T, credentialType?: string) {
this.init = init;
if (credentialType !== null && credentialType !== undefined) {
if (credentialType) {
this.credentialType = credentialType.split(",");
}
}
Expand All @@ -63,6 +66,11 @@ export class ClientBuilder<T> {
return this;
}

public withRegion(region: Region) {
this.region = region;
return this;
}

public build(): T {
const axiosOptions = {
disableSslVerification: true
Expand All @@ -71,25 +79,34 @@ export class ClientBuilder<T> {
Object.assign(axiosOptions, { proxyAgent: this.proxyAgent });
}

if (this.credential === null || this.credential === undefined) {
if (!this.credential) {
this.credential = this.getCredentialFromEnvironment();
}

if (!this.credential) {
throw new SdkException(`credential can not be null, ${this.credentialType}Credential objects are required`);
}

const client = new DefaultHttpClient(axiosOptions);
const hcClient = new HcClient(client);
hcClient.withEndpoint(this.endpoint).withCredential(this.credential);
if (this.region) {
hcClient.withRegion(this.region);
}
return this.init(hcClient);
}

/**
* 从环境变量获取 HUAWEICLOUD_SDK_TYPE
* 环境变量里没有则使用 credentialType[0]
* 生成credential实体
* 从环境变量获取 AK SK projectId/domainId 进行赋值, 如果环境变量是GlobalCredentials,则赋值domainId
* @returns Credentials
*/
public getCredentialFromEnvironment(): ICredential {
const sdkType: any = process.env.HUAWEICLOUD_SDK_TYPE;
const credentialTYPE = this.whichCredential(sdkType)
return this.getInputParamCredential(credentialTYPE, this.envParams);
// 从环境变量获取 HUAWEICLOUD_SDK_TYPE
// 环境变量里没有则使用 credentialType[0]
// 生成credential实体
// 从环境变量获取 AK SK projectId/domainId 进行赋值, 如果环境变量是GlobalCredentials,则赋值domainId
// 返回Credentials
}

public whichCredential(sdkType: string) {
Expand Down
23 changes: 17 additions & 6 deletions core/HcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { SdkResponse } from "./SdkResponse";
import { ExceptionUtil } from "./exception/ExceptionUtil";
import { getLogger, Logger, LogLevel } from './logger';
import { DefaultHttpResponse } from "./http/DefaultHttpResponse";
import { Region } from "./region/region";

export class HcClient {
private httpClient: HttpClient;
Expand All @@ -35,6 +36,7 @@ export class HcClient {
private proxyAgent: string = '';
private static loggerName = 'HcClient';
private logger: Logger;
region?: Region;

public constructor(client: HttpClient) {
this.httpClient = client;
Expand All @@ -44,25 +46,29 @@ export class HcClient {
this.logger.debug('initialized');
}

public withEndpoint(endpoint: string | undefined): HcClient {
public withEndpoint(endpoint?: string): HcClient {
this.endpoint = endpoint;
return this;
}

public withCredential(credential: ICredential | undefined): HcClient {
public withCredential(credential?: ICredential): HcClient {
this.credential = credential;
return this;
}

public withRegion(region?: Region) {
this.region = region;
return this;
}

public withHttpsAgent(proxyAgent: string) {
this.proxyAgent = proxyAgent;
return this;
}

public sendRequest<T extends SdkResponse>(options: any): Promise<T> | Promise<any> {
public async sendRequest<T extends SdkResponse>(options: any): Promise<any> {
this.logger.debug('send request');

const request = this.buildRequest(options);
const request = await this.buildRequest(options);
// @ts-ignore
return this.httpClient.sendRequest<T>(request).then(res => {
return this.extractResponse<T>(res);
Expand All @@ -71,7 +77,12 @@ export class HcClient {
});
}

private buildRequest(options: any): IHttpRequest {
private async buildRequest(options: any): Promise<IHttpRequest> {
if (this.region) {
this.endpoint = this.region.endpoint;
this.credential = await this.credential!.processAuthParams(this, this.region.id);
}

let url = this.endpoint + options.url;
const pathParams = options.pathParams;
Object.keys(pathParams).forEach(x => {
Expand Down
83 changes: 61 additions & 22 deletions core/auth/BasicCredentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,56 +21,77 @@

import { ICredential } from "./ICredential";
import { IHttpRequest } from "../http/IHttpRequest";
import { AKSKSigner } from "./AKSKSigner";
import { AKSKSigner } from "./AKSKSigner";
import { HttpRequestBuilder } from "../http/IHttpRequestBuilder";
import { RequiredError } from "./AKSKSigner";

import { HcClient } from "../HcClient";
import { IamService } from "../internal/services/iam.service";
import { AuthCache } from "../internal/services/authcache";
export class BasicCredentials implements ICredential {
private ak: string | undefined;
private sk: string | undefined;
private securityToken: string | undefined;
private projectId: string | undefined;
ak?: string;
sk?: string;
securityToken?: string;
projectId?: string;
iamEndpoint?: string;

constructor(data?: Partial<BasicCredentials>) {
this.ak = data?.ak;
this.sk = data?.sk;
this.securityToken = data?.securityToken;
this.projectId = data?.projectId;
this.iamEndpoint = data?.iamEndpoint;
}

public withAk(ak: string | undefined): BasicCredentials {
public withAk(ak?: string): BasicCredentials {
this.ak = ak;
return this;
}

public withSk(sk: string | undefined): BasicCredentials {
public withSk(sk?: string): BasicCredentials {
this.sk = sk;
return this;
}

public withProjectId(projectId: string | undefined): BasicCredentials {
public withProjectId(projectId?: string): BasicCredentials {
this.projectId = projectId;
return this;
}

public withSecurityToken(securityToken: string | undefined): BasicCredentials {
public withSecurityToken(securityToken?: string): BasicCredentials {
this.securityToken = securityToken;
return this;
}

public withIamEndpoint(iamEndpoint: string) {
this.iamEndpoint = iamEndpoint;
return this;
}

public getAk(): string | undefined {
return this.ak;
}
public getSk(): string | undefined {
return this.sk;
}

public setProjectId(value: string) {
this.projectId = value;
}

public getPathParams() {
const pathParams = {};
if (this.projectId) {
Object.assign(pathParams, { project_id: this.projectId });
}
return pathParams;
}

public processAuthRequest(httpRequest: IHttpRequest): IHttpRequest {

if (this.ak === null || this.ak === undefined) {
if (!this.ak) {
throw new RequiredError('AK cannot be empty or undefined.');
}
if (this.sk === null || this.sk === undefined) {
if (!this.sk) {
throw new RequiredError('SK cannot be empty or undefined.');
}

Expand All @@ -79,7 +100,7 @@ export class BasicCredentials implements ICredential {

// 替换所有的path参数
if (this.projectId) {
let url = parsePath(httpRequest.endpoint, this.getPathParams());
let url = this.parsePath(httpRequest.endpoint, this.getPathParams());
builder.withEndpoint(url);
}

Expand All @@ -91,7 +112,6 @@ export class BasicCredentials implements ICredential {
builder.addHeaders("X-Security-Token", this.securityToken);
}

// builder.addHeaders("Content-Type", "application/json");
builder.addAllHeaders(httpRequest.headers);
Object.assign(httpRequest, builder.build());
const headers = AKSKSigner.sign(httpRequest, this);
Expand All @@ -100,14 +120,33 @@ export class BasicCredentials implements ICredential {

return Object.assign(httpRequest, builder.build());
}
}

function parsePath(path: string | undefined, params: any): string {
if (!path || !params) {
return <string>path;
processAuthParams(hcClient: HcClient, region: string): Promise<ICredential> {
if (this.projectId) {
return Promise.resolve(this);
}

const authCacheInstance = AuthCache.instance();
const akWithName = this.getAk() + region;
if (authCacheInstance.getCache(akWithName)) {
this.projectId = authCacheInstance.getCache(akWithName);
return Promise.resolve(this);
}

return new IamService(hcClient, this.iamEndpoint).getProjecId(region).then(projectId => {
authCacheInstance.putCache(akWithName, projectId);
this.projectId = projectId;
return this;
});
}

parsePath(path: string | undefined, params: any): string {
if (!path || !params) {
return <string>path;
}
return Object.keys(params).reduce((parsedPath, param) => {
const value = encodeURIComponent(params[param]);
return parsedPath.replace(new RegExp(`{${param}}`), value);
}, path);
}
return Object.keys(params).reduce((parsedPath, param) => {
const value = encodeURIComponent(params[param]);
return parsedPath.replace(new RegExp(`{${param}}`), value);
}, path);
}
Loading

0 comments on commit 0a57624

Please sign in to comment.