mirror of
https://github.com/certd/certd.git
synced 2026-04-23 11:37:23 +08:00
perf: aws route53
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import { AccessInput, BaseAccess, IsAccess } from '@certd/pipeline';
|
||||
|
||||
export const AwsRegions = [
|
||||
{ label: 'cn-north-1', value: 'cn-north-1' },
|
||||
{ label: 'cn-northwest-1', value: 'cn-northwest-1' },
|
||||
{ label: '---------------', value: '--',disabled: true },
|
||||
{ label: '------中国区------', value: 'cn',disabled: true },
|
||||
{ label: '北京', value: 'cn-north-1' },
|
||||
{ label: '宁夏', value: 'cn-northwest-1' },
|
||||
{ label: '------海外-----', value: 'out',disabled: true },
|
||||
{ label: 'us-east-1', value: 'us-east-1' },
|
||||
{ label: 'us-east-2', value: 'us-east-2' },
|
||||
{ label: 'us-west-1', value: 'us-west-1' },
|
||||
@@ -72,7 +73,7 @@ export class AwsAccess extends BaseAccess {
|
||||
options: AwsRegions,
|
||||
},
|
||||
required: true,
|
||||
helper: '请选择您的默认AWS区域,默认us-east-1',
|
||||
helper: '请选择您的默认AWS区域,主要区分中国区还是海外区即可',
|
||||
options: AwsRegions,
|
||||
})
|
||||
region = '';
|
||||
|
||||
@@ -32,7 +32,7 @@ export class AwsRoute53Provider extends AbstractDnsProvider {
|
||||
fullRecord: fullRecord,
|
||||
type: type,
|
||||
value: value,
|
||||
action: 'CREATE',
|
||||
action: 'UPSERT',
|
||||
});
|
||||
return {
|
||||
hostedZoneId: ZoneId,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// 导入所需的 SDK 模块
|
||||
import { AwsAccess } from '../access.js';
|
||||
import { CertInfo } from '@certd/plugin-cert';
|
||||
import {ILogger, utils} from '@certd/basic';
|
||||
type AwsClientOptions = { access: AwsAccess; region: string, logger:ILogger };
|
||||
import { ILogger, utils } from '@certd/basic';
|
||||
type AwsClientOptions = { access: AwsAccess; region: string, logger: ILogger };
|
||||
|
||||
export class AwsClient {
|
||||
options: AwsClientOptions;
|
||||
@@ -52,24 +52,24 @@ export class AwsClient {
|
||||
});
|
||||
}
|
||||
|
||||
async route53GetHostedZoneId(name:string) :Promise<{ZoneId:string,ZoneName:string}> {
|
||||
async route53GetHostedZoneId(name: string): Promise<{ ZoneId: string, ZoneName: string }> {
|
||||
const hostedZones = await this.route53ListHostedZones(name);
|
||||
const zoneId = hostedZones[0].Id.replace('/hostedzone/','');
|
||||
const zoneId = hostedZones[0].Id.replace('/hostedzone/', '');
|
||||
this.logger.info(`获取到hostedZoneId:${zoneId},name:${hostedZones[0].Name}`);
|
||||
return {
|
||||
ZoneId: zoneId,
|
||||
ZoneName: hostedZones[0].Name,
|
||||
};
|
||||
}
|
||||
async route53ListHostedZones(name:string) :Promise<{Id:string,Name:string}[]> {
|
||||
const { ListHostedZonesByNameCommand } =await import("@aws-sdk/client-route-53"); // ES Modules import
|
||||
async route53ListHostedZones(name: string): Promise<{ Id: string, Name: string }[]> {
|
||||
const { ListHostedZonesByNameCommand } = await import("@aws-sdk/client-route-53"); // ES Modules import
|
||||
|
||||
const client = await this.route53ClientGet();
|
||||
const input = { // ListHostedZonesByNameRequest
|
||||
DNSName: name,
|
||||
};
|
||||
const command = new ListHostedZonesByNameCommand(input);
|
||||
const response = await this.doRequest(()=>client.send(command));
|
||||
const response = await this.doRequest(() => client.send(command));
|
||||
if (response.HostedZones.length === 0) {
|
||||
throw new Error(`找不到 HostedZone ${name}`);
|
||||
}
|
||||
@@ -77,9 +77,10 @@ export class AwsClient {
|
||||
return response.HostedZones;
|
||||
}
|
||||
|
||||
async route53ChangeRecord(req:{
|
||||
hostedZoneId:string,fullRecord:string,type:string, value:string, action:"CREATE"|"DELETE"}){
|
||||
const { ChangeResourceRecordSetsCommand} =await import("@aws-sdk/client-route-53"); // ES Modules import
|
||||
async route53ChangeRecord(req: {
|
||||
hostedZoneId: string, fullRecord: string, type: string, value: string, action: "UPSERT" | "DELETE"
|
||||
}) {
|
||||
const { ChangeResourceRecordSetsCommand } = await import("@aws-sdk/client-route-53"); // ES Modules import
|
||||
// const { Route53Client, ChangeResourceRecordSetsCommand } = require("@aws-sdk/client-route-53"); // CommonJS import
|
||||
// import type { Route53ClientConfig } from "@aws-sdk/client-route-53";
|
||||
const client = await this.route53ClientGet();
|
||||
@@ -88,9 +89,9 @@ export class AwsClient {
|
||||
ChangeBatch: { // ChangeBatch
|
||||
Changes: [ // Changes // required
|
||||
{ // Change
|
||||
Action: req.action as any , // required
|
||||
Action: req.action as any, // required
|
||||
ResourceRecordSet: { // ResourceRecordSet
|
||||
Name: req.fullRecord+".", // required
|
||||
Name: req.fullRecord, // required
|
||||
Type: req.type.toUpperCase() as any,
|
||||
ResourceRecords: [ // ResourceRecords
|
||||
{ // ResourceRecord
|
||||
@@ -103,9 +104,9 @@ export class AwsClient {
|
||||
],
|
||||
},
|
||||
};
|
||||
this.logger.info(`添加域名解析参数:${JSON.stringify(input)}`);
|
||||
this.logger.info(`设置域名解析参数:${JSON.stringify(input)}`);
|
||||
const command = new ChangeResourceRecordSetsCommand(input);
|
||||
const response = await this.doRequest(()=>client.send(command));
|
||||
const response = await this.doRequest(() => client.send(command));
|
||||
console.log('Add record successful:', JSON.stringify(response));
|
||||
await utils.sleep(3000);
|
||||
return response;
|
||||
@@ -120,10 +121,10 @@ export class AwsClient {
|
||||
// };*/
|
||||
}
|
||||
|
||||
async doRequest<T>(call:()=>Promise<T>):Promise<T>{
|
||||
try{
|
||||
async doRequest<T>(call: () => Promise<T>): Promise<T> {
|
||||
try {
|
||||
return await call();
|
||||
}catch(err){
|
||||
} catch (err) {
|
||||
this.logger.error(`调用接口失败:${err.Error?.Message || err.message},requestId:${err.requestId}`);
|
||||
throw err;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user