mirror of
https://github.com/certd/certd.git
synced 2025-11-25 09:10:11 +08:00
perf: 优化华为dns解析记录创建和删除问题
This commit is contained in:
@@ -60,3 +60,9 @@
|
||||
footer{
|
||||
background-color: hsl(var(--card)) !important;
|
||||
}
|
||||
|
||||
|
||||
.ant-select-multiple .ant-select-selection-item-remove{
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
@@ -1,37 +1,38 @@
|
||||
import * as _ from 'lodash-es';
|
||||
import { AbstractDnsProvider, CreateRecordOptions, IsDnsProvider, RemoveRecordOptions } from '@certd/plugin-cert';
|
||||
import { Autowire } from '@certd/pipeline';
|
||||
import * as _ from "lodash-es";
|
||||
import { AbstractDnsProvider, CreateRecordOptions, IsDnsProvider, RemoveRecordOptions } from "@certd/plugin-cert";
|
||||
import { Autowire } from "@certd/pipeline";
|
||||
|
||||
import { HuaweiAccess } from '../access/index.js';
|
||||
import { ApiRequestOptions, HuaweiYunClient } from '@certd/lib-huawei';
|
||||
import { HuaweiAccess } from "../access/index.js";
|
||||
import { ApiRequestOptions, HuaweiYunClient } from "@certd/lib-huawei";
|
||||
|
||||
export type SearchRecordOptions = {
|
||||
zoneId: string;
|
||||
} & CreateRecordOptions;
|
||||
|
||||
@IsDnsProvider({
|
||||
name: 'huawei',
|
||||
title: '华为云',
|
||||
desc: '华为云DNS解析提供商',
|
||||
accessType: 'huawei',
|
||||
icon: 'svg:icon-huawei',
|
||||
name: "huawei",
|
||||
title: "华为云",
|
||||
desc: "华为云DNS解析提供商",
|
||||
accessType: "huawei",
|
||||
icon: "svg:icon-huawei"
|
||||
})
|
||||
export class HuaweiDnsProvider extends AbstractDnsProvider {
|
||||
client!: HuaweiYunClient;
|
||||
@Autowire()
|
||||
access!: HuaweiAccess;
|
||||
domainEndpoint = 'https://domains-external.myhuaweicloud.com';
|
||||
dnsEndpoint = 'https://dns.cn-south-1.myhuaweicloud.com';
|
||||
domainEndpoint = "https://domains-external.myhuaweicloud.com";
|
||||
dnsEndpoint = "https://dns.cn-south-1.myhuaweicloud.com";
|
||||
|
||||
async onInstance() {
|
||||
const access: any = this.access;
|
||||
this.client = new HuaweiYunClient(access,this.logger);
|
||||
this.client = new HuaweiYunClient(access, this.logger);
|
||||
}
|
||||
|
||||
async getDomainList() {
|
||||
const url = `${this.dnsEndpoint}/v2/zones`;
|
||||
const ret = await this.client.request({
|
||||
url,
|
||||
method: 'GET',
|
||||
method: "GET"
|
||||
});
|
||||
return ret.zones;
|
||||
}
|
||||
@@ -40,21 +41,21 @@ export class HuaweiDnsProvider extends AbstractDnsProvider {
|
||||
const zoneList = await this.getDomainList();
|
||||
let zoneRecord = null;
|
||||
for (const item of zoneList) {
|
||||
if (_.endsWith(dnsRecord + '.', item.name)) {
|
||||
if (_.endsWith(dnsRecord + ".", item.name)) {
|
||||
zoneRecord = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!zoneRecord) {
|
||||
throw new Error('can not find Domain ,' + dnsRecord);
|
||||
throw new Error("can not find Domain ," + dnsRecord);
|
||||
}
|
||||
return zoneRecord;
|
||||
}
|
||||
|
||||
async searchRecord(options: SearchRecordOptions): Promise<any> {
|
||||
const req: ApiRequestOptions = {
|
||||
url: `${this.dnsEndpoint}/v2/zones/${options.zoneId}/recordsets?name=${options.fullRecord}.`,
|
||||
method: 'GET',
|
||||
url: `${this.dnsEndpoint}/v2/zones/${options.zoneId}/recordsets?search_mode=equal&name=${options.fullRecord}.&type=${options.type}`,
|
||||
method: "GET"
|
||||
};
|
||||
const ret = await this.client.request(req);
|
||||
return ret.recordsets;
|
||||
@@ -62,59 +63,120 @@ export class HuaweiDnsProvider extends AbstractDnsProvider {
|
||||
|
||||
async createRecord(options: CreateRecordOptions): Promise<any> {
|
||||
const { fullRecord, value, type } = options;
|
||||
this.logger.info('添加域名解析:', fullRecord, value);
|
||||
this.logger.info("添加域名解析:", fullRecord, value);
|
||||
this.logger.info("查询是否有重复记录");
|
||||
const zoneRecord = await this.matchDomain(fullRecord);
|
||||
const zoneId = zoneRecord.id;
|
||||
|
||||
const records: any = await this.searchRecord({
|
||||
zoneId,
|
||||
...options,
|
||||
...options
|
||||
});
|
||||
this.logger.info(`查询${options.type}数量:${records.length}`);
|
||||
let found = null;
|
||||
const hwRecordValue = `"${value}"`;
|
||||
if (records && records.length > 0) {
|
||||
for (const record of records) {
|
||||
await this.removeRecord({
|
||||
recordRes: record,
|
||||
recordReq: options,
|
||||
});
|
||||
found = records[0];
|
||||
this.logger.info(`记录:${found.id},${found.records}`);
|
||||
if (found.records.includes(hwRecordValue)) {
|
||||
// this.logger.info(`删除重复记录:${record.id}`)
|
||||
// await this.removeRecord({
|
||||
// recordRes: record,
|
||||
// recordReq: options,
|
||||
// });
|
||||
this.logger.info(`无需重复添加:${found.records}`);
|
||||
return found;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (found) {
|
||||
//修改
|
||||
const req: ApiRequestOptions = {
|
||||
url: `${this.dnsEndpoint}/v2/zones/${zoneId}/recordsets`,
|
||||
method: 'POST',
|
||||
url: `${this.dnsEndpoint}/v2/zones/${zoneId}/recordsets/${found.id}`,
|
||||
method: "PUT",
|
||||
data: {
|
||||
name: fullRecord + '.',
|
||||
name: fullRecord + ".",
|
||||
type,
|
||||
records: [`"${value}"`],
|
||||
},
|
||||
records: [hwRecordValue, ...found.records]
|
||||
}
|
||||
};
|
||||
const ret = await this.client.request(req);
|
||||
this.logger.info('添加域名解析成功:', value, ret);
|
||||
this.logger.info("添加域名解析成功:", value, ret);
|
||||
return ret;
|
||||
} catch (e: any) {
|
||||
if (e.code === 'DNS.0312') {
|
||||
return;
|
||||
} else {
|
||||
//创建
|
||||
try {
|
||||
const req: ApiRequestOptions = {
|
||||
url: `${this.dnsEndpoint}/v2/zones/${zoneId}/recordsets`,
|
||||
method: "POST",
|
||||
data: {
|
||||
name: fullRecord + ".",
|
||||
type,
|
||||
records: [hwRecordValue]
|
||||
}
|
||||
};
|
||||
const ret = await this.client.request(req);
|
||||
this.logger.info("添加域名解析成功:", value, ret);
|
||||
return ret;
|
||||
} catch (e: any) {
|
||||
if (e.code === "DNS.0312") {
|
||||
return;
|
||||
}
|
||||
this.logger.info("添加域名解析出错", e);
|
||||
throw e;
|
||||
}
|
||||
this.logger.info('添加域名解析出错', e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async removeRecord(options: RemoveRecordOptions<any>): Promise<any> {
|
||||
const { fullRecord, value } = options.recordReq;
|
||||
const record = options.recordRes;
|
||||
if (!record) {
|
||||
this.logger.info('解析记录recordId为空,不执行删除', fullRecord, value);
|
||||
this.logger.info("解析记录recordId为空,不执行删除", fullRecord, value);
|
||||
return;
|
||||
}
|
||||
const req: ApiRequestOptions = {
|
||||
url: `${this.dnsEndpoint}/v2/zones/${record.zone_id}/recordsets/${record.id}`,
|
||||
method: 'DELETE',
|
||||
};
|
||||
const zoneId = record.zone_id;
|
||||
|
||||
const ret = await this.client.request(req);
|
||||
this.logger.info('删除域名解析成功:', fullRecord, value, ret.RecordId);
|
||||
return ret.RecordId;
|
||||
//查询原来的记录
|
||||
const records: any = await this.searchRecord({
|
||||
zoneId,
|
||||
...options.recordReq
|
||||
});
|
||||
const hwRecordValue = `"${value}"`;
|
||||
|
||||
if (records && records.length > 0) {
|
||||
//找到记录
|
||||
const found = records[0];
|
||||
if (found.records.includes(hwRecordValue)) {
|
||||
if (found.records.length > 1) {
|
||||
//修改
|
||||
|
||||
const req: ApiRequestOptions = {
|
||||
url: `${this.dnsEndpoint}/v2/zones/${zoneId}/recordsets/${found.id}`,
|
||||
method: "PUT",
|
||||
data: {
|
||||
name: fullRecord + ".",
|
||||
type: found.type,
|
||||
records: found.records.filter((item: string) => item !== hwRecordValue)
|
||||
}
|
||||
};
|
||||
const ret = await this.client.request(req);
|
||||
this.logger.info("修改域名解析成功[put]:", value, ret);
|
||||
} else {
|
||||
//删除
|
||||
const req: ApiRequestOptions = {
|
||||
url: `${this.dnsEndpoint}/v2/zones/${zoneId}/recordsets/${found.id}`,
|
||||
method: "DELETE"
|
||||
};
|
||||
const ret = await this.client.request(req);
|
||||
this.logger.info("删除域名解析成功[delete]:", fullRecord, value, ret.RecordId);
|
||||
}
|
||||
}else{
|
||||
this.logger.info("没有找到records无需删除", fullRecord, value,found);
|
||||
}
|
||||
}else{
|
||||
this.logger.info("删除域名解析失败,没有找到解析记录", fullRecord, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user