Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

adds findRecord to cloudflare only #606

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/platform/src/components/aws/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
36 changes: 36 additions & 0 deletions pkg/platform/src/components/cloudflare/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions pkg/platform/src/components/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { ComponentResourceOptions, Output, Resource } from "@pulumi/pulumi";
import { Input } from "./input";
import { GetRecordResult } from "@pulumi/cloudflare/getRecord";

export interface Record {
/**
Expand Down Expand Up @@ -43,25 +44,36 @@ type CreateRecord = (
opts: ComponentResourceOptions,
) => Output<Resource>;

type FindRecord = (
namePrefix: string,
record: Record,
opts: ComponentResourceOptions,
) => Promise<Output<GetRecordResult>>;

type CreateAliasRecord = (
namePrefix: string,
record: AliasRecord,
opts: ComponentResourceOptions,
) => Output<Resource>[];

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;
7 changes: 7 additions & 0 deletions pkg/platform/src/components/vercel/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export function dns(args: DnsArgs) {
return {
provider: "vercel",
createRecord,
findRecord
} satisfies Dns;

function useCAARecord(namePrefix: string, opts: ComponentResourceOptions) {
Expand All @@ -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,
Expand Down