Skip to content

Commit

Permalink
Feat/add request client version and fix cvm explorer issue (#15)
Browse files Browse the repository at this point in the history
* enable json load

* fix multiple json issue

* 1.fix the logic of JSON files match. 2.use the extensionPath instead of __dirname. 3.load json files with sync(await). 4.rm pre-release trigger when PR merged.

* 0.0.26

* add request client

* 0.0.27

* fix cvm explorer issue when instances are null

* 0.0.28
  • Loading branch information
lyu571 authored Jan 3, 2024
1 parent c7e9487 commit fd09346
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-tencentcloud-terraform",
"displayName": "Tencent Cloud Terraform",
"description": "VS Code extension for developing with Terraform on Tencent Cloud",
"version": "0.0.25",
"version": "0.0.28",
"license": "MIT",
"publisher": "Tencent-Cloud",
"icon": "images/tc-tf-logo.png",
Expand Down
28 changes: 22 additions & 6 deletions src/commons/tencent/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { localize } from "vscode-nls-i18n";
import { ExtensionContext, workspace, ConfigurationTarget, window, ProgressLocation, MessageItem } from "vscode";
import { ExtensionContext, workspace, ConfigurationTarget, window, ProgressLocation, MessageItem, extensions } from "vscode";

import { container } from "../../container";
import { Context } from "../../context";
import { tree } from "../treeDataProvider";
import { getCredentailByInput } from "./auth";
import { AbstractClient } from "tencentcloud-sdk-nodejs/tencentcloud/common/abstract_client";
import { Credential } from "tencentcloud-sdk-nodejs/tencentcloud/common/interface";
import { getCamClient, getStsClient } from "@/connectivity/client";
import { getCamClient, getCommonClient, getStsClient } from "@/connectivity/client";
import * as loginMgt from "../../../views/login/loginMgt";
import * as settingUtils from "../../../utils/settingUtils";

Expand All @@ -24,6 +24,7 @@ export namespace user {
arn?: string;
}

export const REQUEST_CLIENT_PREFIX = "Terraform-Vscode-";//Terraform-1.81.61@vscode";
export const AKSK_TITLE = "TcTerraform.pickup.aksk";
export const OAUTH_TITLE = "TcTerraform.pickup.oauth";
export const AKSK_PLACEHOLD = "TcTerraform.pickup.aksk.placeholder";
Expand Down Expand Up @@ -71,10 +72,16 @@ export namespace user {
try {
// query user info
const stsClient = await getStsClient();
const stsResp = await stsClient?.GetCallerIdentity().
const currentVersion = getExtensionVersion();
const reqCli = `${REQUEST_CLIENT_PREFIX}v${currentVersion}`;
stsClient.sdkVersion = reqCli;
console.log('[DEBUG]--------------------getStsClient:', stsClient);
// const stsClient = await getCommonClient("sts.tencentcloudapi.com", "2018-08-13");
// const stsResp = await stsClient.request("GetCallerIdentity", req).
const stsResp = await stsClient?.GetCallerIdentity(null).
then(
(result) => {
console.debug('[DEBUG]--------------------------------result:', result);
console.debug('[DEBUG]--------------------------------GetCallerIdentity result:', result);
if (!result) {
throw new Error('[Warn] GetCallerIdentity result.TotalCount is 0.');
}
Expand All @@ -84,12 +91,15 @@ export namespace user {
throw new Error(err);
}
);
// ) as stsModels.GetCallerIdentityResponse;

const camClient = await getCamClient();
const camResp = await camClient?.GetUserAppId().
camClient.sdkVersion = reqCli;
console.log('[DEBUG]--------------------getCamClient:', camClient);
const camResp = await camClient?.GetUserAppId(null).
then(
(result) => {
console.debug('[DEBUG]--------------------------------result:', result);
console.debug('[DEBUG]--------------------------------GetUserAppId result:', result);
if (!result) {
throw new Error('[Warn] GetUserAppId result.TotalCount is 0.');
}
Expand Down Expand Up @@ -122,6 +132,12 @@ export namespace user {
}
}

function getExtensionVersion(): string {
let extension = extensions.getExtension('Tencent-Cloud.vscode-tencentcloud-terraform');
let currentVersion = extension.packageJSON.version;
return currentVersion;
}

export async function loginOut() {
const yesBtn: MessageItem = { title: localize("TcTerraform.common.yes") };
const action = await window.showWarningMessage(
Expand Down
15 changes: 11 additions & 4 deletions src/connectivity/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as tencentcloud from "tencentcloud-sdk-nodejs";
import { localize } from "vscode-nls-i18n";
import * as settingUtils from "../utils/settingUtils";

const DefaultReqCliHeader = { "X-TC-RequestClient": "Terraform-Vscode-v0.0.26" };

export async function getTkeClient(): Promise<TkeClient> {
const [secretId, secretKey, region] = settingUtils.getAKSKandRegion();
Expand All @@ -32,6 +33,7 @@ export async function getTkeClient(): Promise<TkeClient> {
httpProfile: {
reqMethod: "POST", // 请求方法
reqTimeout: 30, // 请求超时时间,默认60s
headers: DefaultReqCliHeader,
},
},
});
Expand Down Expand Up @@ -60,12 +62,13 @@ export async function getCvmClient(): Promise<CvmClient> {
reqMethod: "POST", // 请求方法
// reqTimeout: 60, // 请求超时时间,默认60s
endpoint: "cvm.tencentcloudapi.com",
headers: DefaultReqCliHeader,
},
},
});
}

export async function getCommonClient(): Promise<AbstractClient> {
export async function getCommonClient(ep?, version?, reqCli?: string): Promise<AbstractClient> {
const [secretId, secretKey, region] = settingUtils.getAKSKandRegion();

if (secretId === undefined || secretKey === undefined || secretId === null || secretKey === null || secretId === '' || secretKey === '') {
Expand All @@ -75,20 +78,24 @@ export async function getCommonClient(): Promise<AbstractClient> {
}

const client = new AbstractClient(
"open.test.tencentcloudapi.com",
"2018-12-25",
ep ?? "open.test.tencentcloudapi.com",
version ?? "2018-12-25",
{
credential: {
secretId: secretId,
secretKey: secretKey,
},
region: region ?? "ap-guangzhou",
profile: {
httpProfile: {
proxy: "http://9.135.97.58:8899",
reqMethod: "POST", // 请求方法
endpoint: ep ?? "open.test.tencentcloudapi.com",
headers: DefaultReqCliHeader,
},
},
}
);
client.sdkVersion = reqCli ?? "Terraform-1.81.61@vscode";

return client;
}
Expand Down
2 changes: 1 addition & 1 deletion src/import/cvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class CvmService implements ITencentCloudAPI {
(result) => {
// console.debug('[DEBUG]--------------------------------result:', result);
if (result.TotalCount === 0) {
throw new Error('[Warn] DescribeInstances result.TotalCount is 0.');
console.warn('[Warn] DescribeInstances result.TotalCount is 0.');
}
return result.InstanceSet;
},
Expand Down
10 changes: 7 additions & 3 deletions src/views/resources/resExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ export class CvmResProvider extends tencent.tree.TreeDataProvider {
const service = new CvmService();
const instances = await service.describeInstances();
const config = await service.getConfig();
let items: tencent.tree.TreeItem[] = [];
if (!Array.isArray(instances)) {
return items;// return [] if instances nil
}

const items: tencent.tree.TreeItem[] = Array.isArray(instances)
items = instances.length > 0
? instances.map(instance => ({
label: `${instance.InstanceName}(${instance.InstanceId})`,
id: instance.InstanceId,
Expand All @@ -42,12 +46,12 @@ export class CvmResProvider extends tencent.tree.TreeDataProvider {
}],
},
}))
: [];// return [] if instances nil
: [{ label: "No instance." }]; // return none tips if instance is empty

return items;
} catch (error) {
console.error('[Error]#### getChildren got a error:[%s] from CvmService. stack:%s', error.message, error.stack);
return error;
return [];
}

} else {
Expand Down

0 comments on commit fd09346

Please sign in to comment.