Skip to content

Commit f8dd64b

Browse files
committed
initial commit
0 parents  commit f8dd64b

File tree

8 files changed

+2014
-0
lines changed

8 files changed

+2014
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

License.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# The MIT License
2+
3+
Copyright 2020 Can Rau, contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Netlify DNS Record Dynamic Provider for Pulumi
2+
3+
This dynamic [Pulumi](https://www.pulumi.com/) provider creates & destroys DNS records in a Netlify DNS zone via the [Netlify SDK](https://npmjs.com/package/netlify).
4+
5+
## Usage
6+
7+
```ts
8+
// index.ts
9+
import * as pulumi from "@pulumi/pulumi";
10+
import {NetlifyDnsRecord} from "@canrau/pulumi-netlify-dns-record";
11+
12+
const cfg = new pulumi.Config();
13+
14+
new NetlifyDnsRecord("dns-record", {
15+
apiKey: cfg.requireSecret("netlify_api_key"),
16+
zoneId: cfg.requireSecret("netlify_dns_zone_id"),
17+
type: "TXT",
18+
ttl: 10 * 60 /* 10 minutes */,
19+
hostname: "mydomain.com",
20+
value: "TXT Value",
21+
});
22+
```
23+
24+
## Options
25+
26+
```ts
27+
type NetlifyDnsInputs = {
28+
apiKey: string | pulumi.Input<string>;
29+
zoneId: string | pulumi.Input<string>;
30+
type: string | pulumi.Input<string>;
31+
hostname: string | pulumi.Input<string>;
32+
value: string | pulumi.Input<string>;
33+
ttl?: number | pulumi.Input<number>;
34+
priority?: number | pulumi.Input<number>;
35+
secondsToWaitAfter?: number | pulumi.Input<number>;
36+
};
37+
```

package.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "@canrau/pulumi-netlify-dns-record",
3+
"description": "Dynamic Pulumi provider to manage Netlify DNS Zone records",
4+
"version": "1.0.0",
5+
"author": "CanRau <cansrau@gmail.com> (https://www.canrau.com/)",
6+
"license": "MIT",
7+
"main": "dist/netlify-dns-record.js",
8+
"types": "dist/netlify-dns-record.d.ts",
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/CanRau/pulumi-netlify-dns-record.git"
12+
},
13+
"bugs": {
14+
"url": "https://github.com/CanRau/pulumi-netlify-dns-record/issues"
15+
},
16+
"keywords": [
17+
"pulumi",
18+
"netlify",
19+
"dns",
20+
"infrastructure-as-code",
21+
"iac"
22+
],
23+
"files": [
24+
"dist/**/*"
25+
],
26+
"dependencies": {
27+
"@pulumi/pulumi": "2.10.1",
28+
"netlify": "4.5.1"
29+
},
30+
"devDependencies": {
31+
"prettier": "2.1.2",
32+
"typescript": "4.0.2"
33+
},
34+
"prettier": {
35+
"semi": true,
36+
"singleQuote": false,
37+
"useTabs": false,
38+
"trailingComma": "all",
39+
"bracketSpacing": false
40+
}
41+
}

src/netlify-dns-record.ts

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
}

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{"compilerOptions": {
2+
"declaration": true,
3+
"strict": true,
4+
"outDir": "dist",
5+
"target": "ESNext",
6+
"module": "commonjs",
7+
"moduleResolution": "node",
8+
"sourceMap": true,
9+
"experimentalDecorators": true,
10+
"pretty": true,
11+
"noFallthroughCasesInSwitch": true,
12+
"noImplicitReturns": true,
13+
"forceConsistentCasingInFileNames": true
14+
}
15+
}

typings/netlify.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Can be removed if https://github.com/netlify/js-client/issues/50 is resolved
2+
declare module "netlify";

0 commit comments

Comments
 (0)