Skip to content

Commit fd09346

Browse files
authored
Feat/add request client version and fix cvm explorer issue (#15)
* 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
1 parent c7e9487 commit fd09346

File tree

6 files changed

+44
-17
lines changed

6 files changed

+44
-17
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-tencentcloud-terraform",
33
"displayName": "Tencent Cloud Terraform",
44
"description": "VS Code extension for developing with Terraform on Tencent Cloud",
5-
"version": "0.0.25",
5+
"version": "0.0.28",
66
"license": "MIT",
77
"publisher": "Tencent-Cloud",
88
"icon": "images/tc-tf-logo.png",

src/commons/tencent/user/index.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { localize } from "vscode-nls-i18n";
2-
import { ExtensionContext, workspace, ConfigurationTarget, window, ProgressLocation, MessageItem } from "vscode";
2+
import { ExtensionContext, workspace, ConfigurationTarget, window, ProgressLocation, MessageItem, extensions } from "vscode";
33

44
import { container } from "../../container";
55
import { Context } from "../../context";
66
import { tree } from "../treeDataProvider";
77
import { getCredentailByInput } from "./auth";
88
import { AbstractClient } from "tencentcloud-sdk-nodejs/tencentcloud/common/abstract_client";
99
import { Credential } from "tencentcloud-sdk-nodejs/tencentcloud/common/interface";
10-
import { getCamClient, getStsClient } from "@/connectivity/client";
10+
import { getCamClient, getCommonClient, getStsClient } from "@/connectivity/client";
1111
import * as loginMgt from "../../../views/login/loginMgt";
1212
import * as settingUtils from "../../../utils/settingUtils";
1313

@@ -24,6 +24,7 @@ export namespace user {
2424
arn?: string;
2525
}
2626

27+
export const REQUEST_CLIENT_PREFIX = "Terraform-Vscode-";//Terraform-1.81.61@vscode";
2728
export const AKSK_TITLE = "TcTerraform.pickup.aksk";
2829
export const OAUTH_TITLE = "TcTerraform.pickup.oauth";
2930
export const AKSK_PLACEHOLD = "TcTerraform.pickup.aksk.placeholder";
@@ -71,10 +72,16 @@ export namespace user {
7172
try {
7273
// query user info
7374
const stsClient = await getStsClient();
74-
const stsResp = await stsClient?.GetCallerIdentity().
75+
const currentVersion = getExtensionVersion();
76+
const reqCli = `${REQUEST_CLIENT_PREFIX}v${currentVersion}`;
77+
stsClient.sdkVersion = reqCli;
78+
console.log('[DEBUG]--------------------getStsClient:', stsClient);
79+
// const stsClient = await getCommonClient("sts.tencentcloudapi.com", "2018-08-13");
80+
// const stsResp = await stsClient.request("GetCallerIdentity", req).
81+
const stsResp = await stsClient?.GetCallerIdentity(null).
7582
then(
7683
(result) => {
77-
console.debug('[DEBUG]--------------------------------result:', result);
84+
console.debug('[DEBUG]--------------------------------GetCallerIdentity result:', result);
7885
if (!result) {
7986
throw new Error('[Warn] GetCallerIdentity result.TotalCount is 0.');
8087
}
@@ -84,12 +91,15 @@ export namespace user {
8491
throw new Error(err);
8592
}
8693
);
94+
// ) as stsModels.GetCallerIdentityResponse;
8795

8896
const camClient = await getCamClient();
89-
const camResp = await camClient?.GetUserAppId().
97+
camClient.sdkVersion = reqCli;
98+
console.log('[DEBUG]--------------------getCamClient:', camClient);
99+
const camResp = await camClient?.GetUserAppId(null).
90100
then(
91101
(result) => {
92-
console.debug('[DEBUG]--------------------------------result:', result);
102+
console.debug('[DEBUG]--------------------------------GetUserAppId result:', result);
93103
if (!result) {
94104
throw new Error('[Warn] GetUserAppId result.TotalCount is 0.');
95105
}
@@ -122,6 +132,12 @@ export namespace user {
122132
}
123133
}
124134

135+
function getExtensionVersion(): string {
136+
let extension = extensions.getExtension('Tencent-Cloud.vscode-tencentcloud-terraform');
137+
let currentVersion = extension.packageJSON.version;
138+
return currentVersion;
139+
}
140+
125141
export async function loginOut() {
126142
const yesBtn: MessageItem = { title: localize("TcTerraform.common.yes") };
127143
const action = await window.showWarningMessage(

src/connectivity/client.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as tencentcloud from "tencentcloud-sdk-nodejs";
1111
import { localize } from "vscode-nls-i18n";
1212
import * as settingUtils from "../utils/settingUtils";
1313

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

1516
export async function getTkeClient(): Promise<TkeClient> {
1617
const [secretId, secretKey, region] = settingUtils.getAKSKandRegion();
@@ -32,6 +33,7 @@ export async function getTkeClient(): Promise<TkeClient> {
3233
httpProfile: {
3334
reqMethod: "POST", // 请求方法
3435
reqTimeout: 30, // 请求超时时间,默认60s
36+
headers: DefaultReqCliHeader,
3537
},
3638
},
3739
});
@@ -60,12 +62,13 @@ export async function getCvmClient(): Promise<CvmClient> {
6062
reqMethod: "POST", // 请求方法
6163
// reqTimeout: 60, // 请求超时时间,默认60s
6264
endpoint: "cvm.tencentcloudapi.com",
65+
headers: DefaultReqCliHeader,
6366
},
6467
},
6568
});
6669
}
6770

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

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

7780
const client = new AbstractClient(
78-
"open.test.tencentcloudapi.com",
79-
"2018-12-25",
81+
ep ?? "open.test.tencentcloudapi.com",
82+
version ?? "2018-12-25",
8083
{
8184
credential: {
8285
secretId: secretId,
8386
secretKey: secretKey,
8487
},
88+
region: region ?? "ap-guangzhou",
8589
profile: {
8690
httpProfile: {
87-
proxy: "http://9.135.97.58:8899",
91+
reqMethod: "POST", // 请求方法
92+
endpoint: ep ?? "open.test.tencentcloudapi.com",
93+
headers: DefaultReqCliHeader,
8894
},
8995
},
9096
}
9197
);
98+
client.sdkVersion = reqCli ?? "Terraform-1.81.61@vscode";
9299

93100
return client;
94101
}

src/import/cvm.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class CvmService implements ITencentCloudAPI {
2727
(result) => {
2828
// console.debug('[DEBUG]--------------------------------result:', result);
2929
if (result.TotalCount === 0) {
30-
throw new Error('[Warn] DescribeInstances result.TotalCount is 0.');
30+
console.warn('[Warn] DescribeInstances result.TotalCount is 0.');
3131
}
3232
return result.InstanceSet;
3333
},

src/views/resources/resExplorer.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ export class CvmResProvider extends tencent.tree.TreeDataProvider {
2121
const service = new CvmService();
2222
const instances = await service.describeInstances();
2323
const config = await service.getConfig();
24+
let items: tencent.tree.TreeItem[] = [];
25+
if (!Array.isArray(instances)) {
26+
return items;// return [] if instances nil
27+
}
2428

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

4751
return items;
4852
} catch (error) {
4953
console.error('[Error]#### getChildren got a error:[%s] from CvmService. stack:%s', error.message, error.stack);
50-
return error;
54+
return [];
5155
}
5256

5357
} else {

0 commit comments

Comments
 (0)