Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dns and orgs build out #260

Merged
merged 7 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
208 changes: 208 additions & 0 deletions api/resource_dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package api

func (c *Client) GetDNSZones(slug string) ([]*DNSZone, error) {
query := `
query($slug: String!) {
organization(slug: $slug) {
dnsZones {
nodes {
id
domain
createdAt
}
}
}
}
`

req := c.NewRequest(query)

req.Var("slug", slug)

data, err := c.Run(req)
if err != nil {
return nil, err
}

return *data.Organization.DNSZones.Nodes, nil
}

func (c *Client) FindDNSZone(organizationSlug string, domain string) (*DNSZone, error) {
query := `
query($slug: String!, $domain: String!) {
organization(slug: $slug) {
dnsZone(domain: $domain) {
id
domain
createdAt
organization {
id
slug
name
}
}
}
}
`

req := c.NewRequest(query)

req.Var("slug", organizationSlug)
req.Var("domain", domain)

data, err := c.Run(req)
if err != nil {
return nil, err
}

if data.Organization == nil || data.Organization.DNSZone == nil {
return nil, ErrNotFound
}

return data.Organization.DNSZone, nil
}

func (c *Client) CreateDNSZone(organizationID string, domain string) (*DNSZone, error) {
query := `
mutation($input: CreateDnsZoneInput!) {
createDnsZone(input: $input) {
zone {
id
domain
createdAt
}
}
}
`

req := c.NewRequest(query)

req.Var("input", map[string]interface{}{
"organizationId": organizationID,
"domain": domain,
})

data, err := c.Run(req)
if err != nil {
return nil, err
}

return data.CreateDnsZone.Zone, nil
}

func (c *Client) DeleteDNSZone(zoneID string) error {
query := `
mutation($input: DeleteDnsZoneInput!) {
deleteDnsZone(input: $input) {
clientMutationId
}
}
`

req := c.NewRequest(query)

req.Var("input", map[string]interface{}{
"dnsZoneId": zoneID,
})

_, err := c.Run(req)
if err != nil {
return err
}

return nil
}

func (c *Client) GetDNSRecords(zoneID string) ([]*DNSRecord, error) {
query := `
query($zoneId: ID!) {
dnsZone: node(id: $zoneId) {
... on DnsZone {
records {
nodes {
id
fqdn
name
type
ttl
values
isApex
isWildcard
isSystem
createdAt
updatedAt
}
}
}
}
}
`

req := c.NewRequest(query)

req.Var("zoneId", zoneID)

data, err := c.Run(req)
if err != nil {
return nil, err
}

if data.DNSZone == nil {
return nil, ErrNotFound
}

return *data.DNSZone.Records.Nodes, nil
}

func (c *Client) ExportDNSRecords(zoneID string) (string, error) {
query := `
mutation($input: ExportDnsZoneInput!) {
exportDnsZone(input: $input) {
contents
}
}
`

req := c.NewRequest(query)

req.Var("input", map[string]interface{}{
"dnsZoneId": zoneID,
})

data, err := c.Run(req)
if err != nil {
return "", err
}

return data.ExportDnsZone.Contents, nil
}

func (c *Client) ImportDNSRecords(zoneID string, zonefile string) ([]ImportDnsRecordTypeResult, error) {
query := `
mutation($input: ImportDnsZoneInput!) {
importDnsZone(input: $input) {
results {
created
deleted
updated
skipped
type
}
}
}
`

req := c.NewRequest(query)

req.Var("input", map[string]interface{}{
"dnsZoneId": zoneID,
"zonefile": zonefile,
})

data, err := c.Run(req)
if err != nil {
return nil, err
}

return data.ImportDnsZone.Results, nil
}