Skip to content

Commit

Permalink
Merge pull request #257 from superfly/dns
Browse files Browse the repository at this point in the history
dns commands
  • Loading branch information
codepope committed Aug 6, 2020
2 parents 12ceb4b + 6abb446 commit a25ab43
Show file tree
Hide file tree
Showing 6 changed files with 519 additions and 0 deletions.
208 changes: 208 additions & 0 deletions api/resource_dns.go
@@ -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
}
24 changes: 24 additions & 0 deletions api/resource_organizations.go
Expand Up @@ -24,6 +24,30 @@ func (client *Client) GetOrganizations() ([]Organization, error) {
return data.Organizations.Nodes, nil
}

func (client *Client) FindOrganizationBySlug(slug string) (*Organization, error) {
q := `
query($slug: String!) {
organization(slug: $slug) {
id
slug
name
type
}
}
`

req := client.NewRequest(q)

req.Var("slug", slug)

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

return data.Organization, nil
}

func (client *Client) GetCurrentOrganizations() (Organization, []Organization, error) {
query := `
query {
Expand Down
61 changes: 61 additions & 0 deletions api/types.go
Expand Up @@ -19,15 +19,23 @@ type Query struct {
Organizations struct {
Nodes []Organization
}

Organization *Organization
UserOrganizations UserOrganizations
OrganizationDetails OrganizationDetails
Build Build

Node interface{}
Nodes []interface{}

Platform struct {
Regions []Region
VMSizes []VMSize
}

// hack to let us alias node to a type
DNSZone *DNSZone

// mutations
CreateApp struct {
App App
Expand Down Expand Up @@ -108,6 +116,17 @@ type Query struct {
App App
}

CreateDnsZone struct {
Zone *DNSZone
}

ExportDnsZone struct {
Contents string
}

ImportDnsZone struct {
Results []ImportDnsRecordTypeResult
}
CreateOrganization CreateOrganizationPayload
DeleteOrganization DeleteOrganizationPayload
}
Expand Down Expand Up @@ -208,6 +227,16 @@ type Organization struct {
Name string
Slug string
Type string

DNSZones struct {
Nodes *[]*DNSZone
Edges *[]*struct {
Cursor *string
Node *DNSZone
}
}

DNSZone *DNSZone
}

type OrganizationDetails struct {
Expand Down Expand Up @@ -635,3 +664,35 @@ type Extensions struct {
Query string
Variables map[string]string
}

type DNSZone struct {
ID string
Domain string
CreatedAt time.Time
Organization *Organization
Records *struct {
Nodes *[]*DNSRecord
}
}

type DNSRecord struct {
ID string
Name string
FQDN string
IsApex bool
IsWildcard bool
IsSystem bool
TTL int
Type string
Values []string
CreatedAt time.Time
UpdatedAt time.Time
}

type ImportDnsRecordTypeResult struct {
Created int
Deleted int
Skipped int
Updated int
Type string
}

0 comments on commit a25ab43

Please sign in to comment.