-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
ab844db
commit e855b9f
Showing
9 changed files
with
204 additions
and
29 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
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
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
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
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,89 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/cloudflare/cloudflare-go" | ||
"github.com/porter-dev/porter/internal/integrations/dns" | ||
) | ||
|
||
// RecordType strongly types cloudflare dns entry types | ||
type RecordType string | ||
|
||
const ( | ||
// RecordType_A declares an A record type for cloudflare | ||
RecordType_A RecordType = "A" | ||
|
||
// RecordType_CNAME declares an CNME record type for cloudflare | ||
RecordType_CNAME = "CNAME" | ||
) | ||
|
||
// TTL sets the TTL for Cloudflare DNS records | ||
const TTL = 300 | ||
|
||
// Client is a struct wrapper around the cloudflare client | ||
type Client struct { | ||
zoneID string | ||
|
||
client *cloudflare.API | ||
} | ||
|
||
// NewClient creates a new cloudflare API client | ||
func NewClient(apiToken string, runDomain string) (Client, error) { | ||
client, err := cloudflare.NewWithAPIToken(apiToken) | ||
if err != nil { | ||
return Client{}, err | ||
} | ||
|
||
zoneID, err := client.ZoneIDByName(runDomain) | ||
if err != nil { | ||
return Client{}, err | ||
} | ||
|
||
return Client{client: client, zoneID: zoneID}, nil | ||
} | ||
|
||
// CreateCNAMERecord creates a new CNAME record for the nameserver | ||
// | ||
// The method ignores record.RootDomain in favor of the zoneID derived from c.runDomain | ||
func (c Client) CreateCNAMERecord(record dns.Record) error { | ||
proxy := false | ||
|
||
cloudflareRecord := cloudflare.CreateDNSRecordParams{ | ||
Name: record.Name, | ||
Type: string(RecordType_CNAME), | ||
Content: record.Value, | ||
TTL: TTL, | ||
Proxied: &proxy, | ||
} | ||
|
||
_, err := c.client.CreateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(c.zoneID), cloudflareRecord) | ||
if err != nil { | ||
return fmt.Errorf("failed to create CNAME dns record: %w", err) | ||
} | ||
|
||
return err | ||
} | ||
|
||
// CreateARecord creates a new A record for the nameserver | ||
// | ||
// The method ignores record.RootDomain in favor of the zoneID derived from c.runDomain | ||
func (c Client) CreateARecord(record dns.Record) error { | ||
proxy := false | ||
|
||
cloudflareRecord := cloudflare.CreateDNSRecordParams{ | ||
Name: record.Name, | ||
Type: string(RecordType_A), | ||
Content: record.Value, | ||
TTL: TTL, | ||
Proxied: &proxy, | ||
} | ||
|
||
_, err := c.client.CreateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(c.zoneID), cloudflareRecord) | ||
if err != nil { | ||
return fmt.Errorf("failed to create A dns record: %w", err) | ||
} | ||
|
||
return nil | ||
} |
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 |
---|---|---|
@@ -1,19 +1,43 @@ | ||
package dns | ||
|
||
import "github.com/porter-dev/porter/internal/integrations/powerdns" | ||
// RecordType strongly types dns record types | ||
type RecordType int | ||
|
||
const ( | ||
// RecordType_A represents a DNS RecordType_A record | ||
RecordType_A RecordType = iota | ||
|
||
// RecordType_CNAME represents a DNS RecordType_CNAME record | ||
RecordType_CNAME | ||
) | ||
|
||
// WrappedClient is an interface describing a wrapper | ||
// around a particular dns implementation | ||
type WrappedClient interface { | ||
CreateARecord(record Record) error | ||
CreateCNAMERecord(record Record) error | ||
} | ||
|
||
// Client wraps the underlying powerdns client | ||
// providing a stable api around interacting with DNS | ||
type Client struct { | ||
Client *powerdns.Client | ||
Client WrappedClient | ||
} | ||
|
||
// CreateARecord creates a new A record | ||
func (c Client) CreateARecord(value, hostname string) error { | ||
return c.Client.CreateARecord(value, hostname) | ||
// Record describes a specific DNS record to create | ||
// and can include implementation-specific attributes | ||
type Record struct { | ||
Type RecordType | ||
Name string | ||
RootDomain string | ||
Value string | ||
} | ||
|
||
// CreateCNAMERecord creates a new CNAME record | ||
func (c Client) CreateCNAMERecord(value, hostname string) error { | ||
return c.Client.CreateCNAMERecord(value, hostname) | ||
// CreateRecord creates a new dns record | ||
func (c Client) CreateRecord(record Record) error { | ||
if record.Type == RecordType_A { | ||
return c.Client.CreateARecord(record) | ||
} | ||
|
||
return c.Client.CreateCNAMERecord(record) | ||
} |
Oops, something went wrong.