diff --git a/pkg/platform/src/components/aws/dns.ts b/pkg/platform/src/components/aws/dns.ts index 156555db1..5699db4ce 100644 --- a/pkg/platform/src/components/aws/dns.ts +++ b/pkg/platform/src/components/aws/dns.ts @@ -71,8 +71,15 @@ export function dns(args: DnsArgs = {}) { provider: "aws", createRecord, createAliasRecords, + findRecord } satisfies Dns; + function findRecord() { + throw new Error( + `Current version of the AWS adapter does not support finding records.` + ); + } + /** * Creates a DNS record in the hosted zone. * diff --git a/pkg/platform/src/components/cloudflare/dns.ts b/pkg/platform/src/components/cloudflare/dns.ts index f4ab606ad..862861c29 100644 --- a/pkg/platform/src/components/cloudflare/dns.ts +++ b/pkg/platform/src/components/cloudflare/dns.ts @@ -70,8 +70,44 @@ export function dns(args: DnsArgs = {}) { return { provider: "cloudflare", createRecord, + findRecord, } satisfies Dns; + async function findRecord( + namePrefix: string, + record: Record, + opts: ComponentResourceOptions, + ) { + return output(record).apply((record) => { + const nameSuffix = sanitizeToPascalCase(record.name); + const zoneId = lookupZone(); + const dnsRecord = findRecord(); + return dnsRecord; + + function lookupZone() { + if (args.zone) return args.zone; + return new ZoneLookup( + `${namePrefix}${record.type}ZoneLookup${nameSuffix}`, + { + accountId: sst.cloudflare.DEFAULT_ACCOUNT_ID, + domain: output(record.name).apply((name) => + name.replace(/\.$/, ""), + ), + }, + opts, + ).id; + } + + function findRecord() { + return cloudflare.getRecord({ + zoneId: zoneId.toString(), + type: record.type, + hostname: record.name, + }) + } + }); + } + function createRecord( namePrefix: string, record: Record, diff --git a/pkg/platform/src/components/dns.ts b/pkg/platform/src/components/dns.ts index 739f5bc1a..7ee898293 100644 --- a/pkg/platform/src/components/dns.ts +++ b/pkg/platform/src/components/dns.ts @@ -6,6 +6,7 @@ import { ComponentResourceOptions, Output, Resource } from "@pulumi/pulumi"; import { Input } from "./input"; +import { GetRecordResult } from "@pulumi/cloudflare/getRecord"; export interface Record { /** @@ -43,25 +44,36 @@ type CreateRecord = ( opts: ComponentResourceOptions, ) => Output; +type FindRecord = ( + namePrefix: string, + record: Record, + opts: ComponentResourceOptions, +) => Promise>; + type CreateAliasRecord = ( namePrefix: string, record: AliasRecord, opts: ComponentResourceOptions, ) => Output[]; +type UnSupportedFindRecord = () => void; + type AwsDns = { provider: "aws"; createRecord: CreateRecord; createAliasRecords: CreateAliasRecord; + findRecord: UnSupportedFindRecord; }; type CloudflareDns = { provider: "cloudflare"; createRecord: CreateRecord; + findRecord: FindRecord; }; type VercelDns = { provider: "vercel"; createRecord: CreateRecord; + findRecord: UnSupportedFindRecord; }; export type Dns = AwsDns | CloudflareDns | VercelDns; diff --git a/pkg/platform/src/components/vercel/dns.ts b/pkg/platform/src/components/vercel/dns.ts index d33909b73..487d29b2a 100644 --- a/pkg/platform/src/components/vercel/dns.ts +++ b/pkg/platform/src/components/vercel/dns.ts @@ -70,6 +70,7 @@ export function dns(args: DnsArgs) { return { provider: "vercel", createRecord, + findRecord } satisfies Dns; function useCAARecord(namePrefix: string, opts: ComponentResourceOptions) { @@ -89,6 +90,12 @@ export function dns(args: DnsArgs) { return caaRecord; } + function findRecord() { + throw new Error( + `Current version of the Vercel adapter does not support finding records.` + ); + } + function createRecord( namePrefix: string, record: Record,