|
| 1 | +import * as pulumi from "@pulumi/pulumi"; |
| 2 | +import * as NetlifyAPI from "netlify"; |
| 3 | +import { |
| 4 | + CheckFailure, |
| 5 | + CheckResult, |
| 6 | + CreateResult, |
| 7 | + DiffResult, |
| 8 | + ReadResult, |
| 9 | + Resource, |
| 10 | + ResourceProvider, |
| 11 | +} from "@pulumi/pulumi/dynamic"; |
| 12 | + |
| 13 | +export type NetlifyDnsInputs = { |
| 14 | + apiKey: string | pulumi.Input<string>; |
| 15 | + zoneId: string | pulumi.Input<string>; |
| 16 | + type: string | pulumi.Input<string>; |
| 17 | + hostname: string | pulumi.Input<string>; |
| 18 | + value: string | pulumi.Input<string>; |
| 19 | + ttl?: number | pulumi.Input<number>; |
| 20 | + priority?: number | pulumi.Input<number>; |
| 21 | + secondsToWaitAfter?: number | pulumi.Input<number>; |
| 22 | +}; |
| 23 | + |
| 24 | +type ProviderInputs = { |
| 25 | + apiKey: string; |
| 26 | + zoneId: string; |
| 27 | + type: string; |
| 28 | + hostname: string; |
| 29 | + value: string; |
| 30 | + ttl?: number; |
| 31 | + priority?: number; |
| 32 | + secondsToWaitAfter?: number; |
| 33 | + [key: string]: string | number | undefined; |
| 34 | +}; |
| 35 | + |
| 36 | +const validKeys = [ |
| 37 | + "apiKey", |
| 38 | + "zoneId", |
| 39 | + "type", |
| 40 | + "hostname", |
| 41 | + "value", |
| 42 | + "ttl", |
| 43 | + "priority", |
| 44 | +]; |
| 45 | +const filterValidKeys = (key: string) => validKeys.includes(key); |
| 46 | + |
| 47 | +const shallowCompare = (obj1: ProviderInputs, obj2: ProviderInputs) => { |
| 48 | + const obj1Keys = Object.keys(obj1).filter(filterValidKeys); |
| 49 | + const obj2Keys = Object.keys(obj2).filter(filterValidKeys); |
| 50 | + return ( |
| 51 | + obj1Keys.length === obj2Keys.length && |
| 52 | + obj1Keys.every((key) => obj1[key] === obj2[key]) |
| 53 | + ); |
| 54 | +}; |
| 55 | + |
| 56 | +// https://gist.github.com/joepie91/2664c85a744e6bd0629c |
| 57 | +const sleep = (duration: number) => |
| 58 | + new Promise((resolve) => setTimeout(resolve, duration * 1000)); |
| 59 | + |
| 60 | +const netlifyDnsProvider: ResourceProvider = { |
| 61 | + async check( |
| 62 | + olds: NetlifyDnsInputs, |
| 63 | + news: NetlifyDnsInputs, |
| 64 | + ): Promise<CheckResult> { |
| 65 | + const failures: CheckFailure[] = []; |
| 66 | + if (failures.length > 0) { |
| 67 | + return {failures: failures}; |
| 68 | + } |
| 69 | + return {inputs: news}; |
| 70 | + }, |
| 71 | + |
| 72 | + async create(props: ProviderInputs): Promise<CreateResult> { |
| 73 | + const netlify = new NetlifyAPI(props.apiKey); |
| 74 | + |
| 75 | + try { |
| 76 | + const {__provider, secondsToWaitAfter, ...outs} = props; |
| 77 | + |
| 78 | + const body = { |
| 79 | + type: props.type, |
| 80 | + hostname: props.hostname, |
| 81 | + value: props.value, |
| 82 | + ttl: props.ttl, |
| 83 | + priority: props.priority, |
| 84 | + }; |
| 85 | + |
| 86 | + const {id} = await netlify.createDnsRecord({zone_id: props.zoneId, body}); |
| 87 | + |
| 88 | + if (typeof secondsToWaitAfter === "number" && secondsToWaitAfter > 0) { |
| 89 | + await sleep(secondsToWaitAfter); |
| 90 | + } |
| 91 | + |
| 92 | + return {id, outs}; |
| 93 | + } catch (error) { |
| 94 | + console.log(error.message); |
| 95 | + throw error; |
| 96 | + } |
| 97 | + }, |
| 98 | + |
| 99 | + async diff( |
| 100 | + id: string, |
| 101 | + olds: ProviderInputs, |
| 102 | + news: ProviderInputs, |
| 103 | + ): Promise<DiffResult> { |
| 104 | + const replaces: string[] = []; |
| 105 | + let changes = false; |
| 106 | + |
| 107 | + if (!shallowCompare(olds, news)) { |
| 108 | + changes = true; |
| 109 | + replaces.push(...Object.keys(news).filter(filterValidKeys)); |
| 110 | + } |
| 111 | + |
| 112 | + return {changes, replaces}; |
| 113 | + }, |
| 114 | + |
| 115 | + async delete(id: string, {apiKey, zoneId}: ProviderInputs) { |
| 116 | + const netlify = new NetlifyAPI(apiKey); |
| 117 | + await netlify.deleteDnsRecord({zone_id: zoneId, dns_record_id: id}); |
| 118 | + }, |
| 119 | + |
| 120 | + async read(id: string, props: ProviderInputs): Promise<ReadResult> { |
| 121 | + const netlify = new NetlifyAPI(props.apiKey); |
| 122 | + const response = await netlify.getIndividualDnsRecord({ |
| 123 | + zone_id: props.zoneId, |
| 124 | + dns_record_id: id, |
| 125 | + }); |
| 126 | + return {id: response.id, props: {...props, ...response}}; |
| 127 | + }, |
| 128 | +}; |
| 129 | + |
| 130 | +export class NetlifyDnsRecord extends Resource { |
| 131 | + /** |
| 132 | + * Creates a new DNS record on Netlify |
| 133 | + * |
| 134 | + * @param apiKey - Netlify API key |
| 135 | + * @param zoneId - Netlify DNS zone ID |
| 136 | + * @param type - DNS record type |
| 137 | + * @param hostname - DNS record hostname |
| 138 | + * @param value - DNS record value |
| 139 | + * @param ttl - Time to live in seconds |
| 140 | + * @param priority - The priority of the target host, lower value means more preferred. Only for records of type MX. |
| 141 | + * @param secondsToWaitAfter - How many seconds to wait after the record has been created |
| 142 | + */ |
| 143 | + constructor( |
| 144 | + name: string, |
| 145 | + props: NetlifyDnsInputs, |
| 146 | + opts?: pulumi.CustomResourceOptions, |
| 147 | + ) { |
| 148 | + super(netlifyDnsProvider, name, props, opts); |
| 149 | + } |
| 150 | +} |
0 commit comments