diff --git a/package-lock.json b/package-lock.json index df8988b..04eaf7f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "vscode-tencentcloud-terraform", - "version": "0.0.25", + "version": "0.0.28", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "vscode-tencentcloud-terraform", - "version": "0.0.25", + "version": "0.0.28", "license": "MIT", "dependencies": { "@types/jsdom": "^21.1.4", diff --git a/package.json b/package.json index 28e38f9..387a81b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/commons/tencent/user/index.ts b/src/commons/tencent/user/index.ts index 0b91656..c1b7b89 100644 --- a/src/commons/tencent/user/index.ts +++ b/src/commons/tencent/user/index.ts @@ -1,5 +1,5 @@ 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"; @@ -7,7 +7,7 @@ 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"; @@ -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"; @@ -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.'); } @@ -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.'); } @@ -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( diff --git a/src/connectivity/client.ts b/src/connectivity/client.ts index 2a0b240..f724487 100644 --- a/src/connectivity/client.ts +++ b/src/connectivity/client.ts @@ -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 { const [secretId, secretKey, region] = settingUtils.getAKSKandRegion(); @@ -32,6 +33,7 @@ export async function getTkeClient(): Promise { httpProfile: { reqMethod: "POST", // 请求方法 reqTimeout: 30, // 请求超时时间,默认60s + headers: DefaultReqCliHeader, }, }, }); @@ -60,12 +62,13 @@ export async function getCvmClient(): Promise { reqMethod: "POST", // 请求方法 // reqTimeout: 60, // 请求超时时间,默认60s endpoint: "cvm.tencentcloudapi.com", + headers: DefaultReqCliHeader, }, }, }); } -export async function getCommonClient(): Promise { +export async function getCommonClient(ep?, version?, reqCli?: string): Promise { const [secretId, secretKey, region] = settingUtils.getAKSKandRegion(); if (secretId === undefined || secretKey === undefined || secretId === null || secretKey === null || secretId === '' || secretKey === '') { @@ -75,20 +78,24 @@ export async function getCommonClient(): Promise { } 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; } diff --git a/src/import/cvm.ts b/src/import/cvm.ts index 0a6089d..31796a4 100644 --- a/src/import/cvm.ts +++ b/src/import/cvm.ts @@ -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; }, diff --git a/src/views/resources/resExplorer.ts b/src/views/resources/resExplorer.ts index 4fe650d..d0adf8c 100644 --- a/src/views/resources/resExplorer.ts +++ b/src/views/resources/resExplorer.ts @@ -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, @@ -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 {