Last active
September 4, 2022 19:00
-
-
Save avinashupadhya99/263d00972275a74ebb3495e4f45442f2 to your computer and use it in GitHub Desktop.
Pulumi Name.com Dynamic Provider
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
import * as pulumi from "@pulumi/pulumi"; | |
let config = new pulumi.Config(); | |
const nameComUserName = config.requireSecret("name-com-user-name"); | |
const nameComToken = config.requireSecret("name-com-token"); | |
const domainNameRE = /{domainName}/gi; | |
const nameComUrl: string = | |
"https://api.name.com/v4/domains/{domainName}:setNameservers"; | |
interface NameComContact { | |
firstName: string; | |
lastName: string; | |
companyName: string; | |
address1: string; | |
address2: string; | |
city: string; | |
state: string; | |
zip: string; | |
country: string; | |
phone: string; | |
fax: string; | |
email: string; | |
} | |
interface NameComContacts { | |
registrant: NameComContact; | |
admin: NameComContact; | |
tech: NameComContact; | |
billing: NameComContact; | |
} | |
interface SetNSInputs { | |
domainName: string; | |
nameservers: string[]; | |
} | |
interface SetNSResponse extends SetNSInputs { | |
contacts: NameComContacts; | |
privacyEnabled: boolean; | |
locked: boolean; | |
autorenewEnabled: boolean; | |
expireDate: string; | |
createDate: string; | |
renewalPrice: number; | |
} | |
class NameComProvider implements pulumi.dynamic.ResourceProvider { | |
private name: string; | |
constructor(name: string) { | |
this.name = name; | |
} | |
async create(props: SetNSInputs): Promise<pulumi.dynamic.CreateResult> { | |
const got = (await import("got")).default; | |
const requestData = { | |
nameServers: props.nameservers | |
} | |
console.log(requestData); | |
let data = await got | |
.post(nameComUrl.replace(domainNameRE, props.domainName), { | |
headers: { | |
"Content-Type": "application/json", | |
"Authorization": `Basic ${Buffer.from(String(nameComUserName) +":"+String(nameComToken), 'binary').toString('base64')}` | |
}, | |
json: requestData, | |
}) | |
.json<SetNSResponse>(); | |
console.log(data); | |
return { id: data.domainName, outs: data }; | |
} | |
} | |
export class NameComNameServer extends pulumi.dynamic.Resource { | |
constructor( | |
name: string, | |
props: SetNSInputs, | |
opts?: pulumi.CustomResourceOptions | |
) { | |
super(new NameComProvider(name), name, props, opts); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment