-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f8dd64b
Showing
8 changed files
with
2,014 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# The MIT License | ||
|
||
Copyright 2020 Can Rau, contributors | ||
|
||
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: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Netlify DNS Record Dynamic Provider for Pulumi | ||
|
||
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). | ||
|
||
## Usage | ||
|
||
```ts | ||
// index.ts | ||
import * as pulumi from "@pulumi/pulumi"; | ||
import {NetlifyDnsRecord} from "@canrau/pulumi-netlify-dns-record"; | ||
|
||
const cfg = new pulumi.Config(); | ||
|
||
new NetlifyDnsRecord("dns-record", { | ||
apiKey: cfg.requireSecret("netlify_api_key"), | ||
zoneId: cfg.requireSecret("netlify_dns_zone_id"), | ||
type: "TXT", | ||
ttl: 10 * 60 /* 10 minutes */, | ||
hostname: "mydomain.com", | ||
value: "TXT Value", | ||
}); | ||
``` | ||
|
||
## Options | ||
|
||
```ts | ||
type NetlifyDnsInputs = { | ||
apiKey: string | pulumi.Input<string>; | ||
zoneId: string | pulumi.Input<string>; | ||
type: string | pulumi.Input<string>; | ||
hostname: string | pulumi.Input<string>; | ||
value: string | pulumi.Input<string>; | ||
ttl?: number | pulumi.Input<number>; | ||
priority?: number | pulumi.Input<number>; | ||
secondsToWaitAfter?: number | pulumi.Input<number>; | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "@canrau/pulumi-netlify-dns-record", | ||
"description": "Dynamic Pulumi provider to manage Netlify DNS Zone records", | ||
"version": "1.0.0", | ||
"author": "CanRau <cansrau@gmail.com> (https://www.canrau.com/)", | ||
"license": "MIT", | ||
"main": "dist/netlify-dns-record.js", | ||
"types": "dist/netlify-dns-record.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/CanRau/pulumi-netlify-dns-record.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/CanRau/pulumi-netlify-dns-record/issues" | ||
}, | ||
"keywords": [ | ||
"pulumi", | ||
"netlify", | ||
"dns", | ||
"infrastructure-as-code", | ||
"iac" | ||
], | ||
"files": [ | ||
"dist/**/*" | ||
], | ||
"dependencies": { | ||
"@pulumi/pulumi": "2.10.1", | ||
"netlify": "4.5.1" | ||
}, | ||
"devDependencies": { | ||
"prettier": "2.1.2", | ||
"typescript": "4.0.2" | ||
}, | ||
"prettier": { | ||
"semi": true, | ||
"singleQuote": false, | ||
"useTabs": false, | ||
"trailingComma": "all", | ||
"bracketSpacing": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import * as pulumi from "@pulumi/pulumi"; | ||
import * as NetlifyAPI from "netlify"; | ||
import { | ||
CheckFailure, | ||
CheckResult, | ||
CreateResult, | ||
DiffResult, | ||
ReadResult, | ||
Resource, | ||
ResourceProvider, | ||
} from "@pulumi/pulumi/dynamic"; | ||
|
||
export type NetlifyDnsInputs = { | ||
apiKey: string | pulumi.Input<string>; | ||
zoneId: string | pulumi.Input<string>; | ||
type: string | pulumi.Input<string>; | ||
hostname: string | pulumi.Input<string>; | ||
value: string | pulumi.Input<string>; | ||
ttl?: number | pulumi.Input<number>; | ||
priority?: number | pulumi.Input<number>; | ||
secondsToWaitAfter?: number | pulumi.Input<number>; | ||
}; | ||
|
||
type ProviderInputs = { | ||
apiKey: string; | ||
zoneId: string; | ||
type: string; | ||
hostname: string; | ||
value: string; | ||
ttl?: number; | ||
priority?: number; | ||
secondsToWaitAfter?: number; | ||
[key: string]: string | number | undefined; | ||
}; | ||
|
||
const validKeys = [ | ||
"apiKey", | ||
"zoneId", | ||
"type", | ||
"hostname", | ||
"value", | ||
"ttl", | ||
"priority", | ||
]; | ||
const filterValidKeys = (key: string) => validKeys.includes(key); | ||
|
||
const shallowCompare = (obj1: ProviderInputs, obj2: ProviderInputs) => { | ||
const obj1Keys = Object.keys(obj1).filter(filterValidKeys); | ||
const obj2Keys = Object.keys(obj2).filter(filterValidKeys); | ||
return ( | ||
obj1Keys.length === obj2Keys.length && | ||
obj1Keys.every((key) => obj1[key] === obj2[key]) | ||
); | ||
}; | ||
|
||
// https://gist.github.com/joepie91/2664c85a744e6bd0629c | ||
const sleep = (duration: number) => | ||
new Promise((resolve) => setTimeout(resolve, duration * 1000)); | ||
|
||
const netlifyDnsProvider: ResourceProvider = { | ||
async check( | ||
olds: NetlifyDnsInputs, | ||
news: NetlifyDnsInputs, | ||
): Promise<CheckResult> { | ||
const failures: CheckFailure[] = []; | ||
if (failures.length > 0) { | ||
return {failures: failures}; | ||
} | ||
return {inputs: news}; | ||
}, | ||
|
||
async create(props: ProviderInputs): Promise<CreateResult> { | ||
const netlify = new NetlifyAPI(props.apiKey); | ||
|
||
try { | ||
const {__provider, secondsToWaitAfter, ...outs} = props; | ||
|
||
const body = { | ||
type: props.type, | ||
hostname: props.hostname, | ||
value: props.value, | ||
ttl: props.ttl, | ||
priority: props.priority, | ||
}; | ||
|
||
const {id} = await netlify.createDnsRecord({zone_id: props.zoneId, body}); | ||
|
||
if (typeof secondsToWaitAfter === "number" && secondsToWaitAfter > 0) { | ||
await sleep(secondsToWaitAfter); | ||
} | ||
|
||
return {id, outs}; | ||
} catch (error) { | ||
console.log(error.message); | ||
throw error; | ||
} | ||
}, | ||
|
||
async diff( | ||
id: string, | ||
olds: ProviderInputs, | ||
news: ProviderInputs, | ||
): Promise<DiffResult> { | ||
const replaces: string[] = []; | ||
let changes = false; | ||
|
||
if (!shallowCompare(olds, news)) { | ||
changes = true; | ||
replaces.push(...Object.keys(news).filter(filterValidKeys)); | ||
} | ||
|
||
return {changes, replaces}; | ||
}, | ||
|
||
async delete(id: string, {apiKey, zoneId}: ProviderInputs) { | ||
const netlify = new NetlifyAPI(apiKey); | ||
await netlify.deleteDnsRecord({zone_id: zoneId, dns_record_id: id}); | ||
}, | ||
|
||
async read(id: string, props: ProviderInputs): Promise<ReadResult> { | ||
const netlify = new NetlifyAPI(props.apiKey); | ||
const response = await netlify.getIndividualDnsRecord({ | ||
zone_id: props.zoneId, | ||
dns_record_id: id, | ||
}); | ||
return {id: response.id, props: {...props, ...response}}; | ||
}, | ||
}; | ||
|
||
export class NetlifyDnsRecord extends Resource { | ||
/** | ||
* Creates a new DNS record on Netlify | ||
* | ||
* @param apiKey - Netlify API key | ||
* @param zoneId - Netlify DNS zone ID | ||
* @param type - DNS record type | ||
* @param hostname - DNS record hostname | ||
* @param value - DNS record value | ||
* @param ttl - Time to live in seconds | ||
* @param priority - The priority of the target host, lower value means more preferred. Only for records of type MX. | ||
* @param secondsToWaitAfter - How many seconds to wait after the record has been created | ||
*/ | ||
constructor( | ||
name: string, | ||
props: NetlifyDnsInputs, | ||
opts?: pulumi.CustomResourceOptions, | ||
) { | ||
super(netlifyDnsProvider, name, props, opts); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{"compilerOptions": { | ||
"declaration": true, | ||
"strict": true, | ||
"outDir": "dist", | ||
"target": "ESNext", | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"sourceMap": true, | ||
"experimentalDecorators": true, | ||
"pretty": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"noImplicitReturns": true, | ||
"forceConsistentCasingInFileNames": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Can be removed if https://github.com/netlify/js-client/issues/50 is resolved | ||
declare module "netlify"; |
Oops, something went wrong.