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

UltraDNS provider #5716

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions builtin/bins/provider-ultradns/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"github.com/hashicorp/terraform/builtin/providers/ultradns"
"github.com/hashicorp/terraform/plugin"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: ultradns.Provider,
})
}
28 changes: 28 additions & 0 deletions builtin/providers/ultradns/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ultradns

import (
"fmt"
"log"

"github.com/Ensighten/udnssdk"
)

// Config collects the connection service-endpoint and credentials
type Config struct {
Username string
Password string
BaseURL string
}

// Client returns a new client for accessing UltraDNS.
func (c *Config) Client() (*udnssdk.Client, error) {
client, err := udnssdk.NewClient(c.Username, c.Password, c.BaseURL)

if err != nil {
return nil, fmt.Errorf("Error setting up client: %s", err)
}

log.Printf("[INFO] UltraDNS Client configured for user: %s", client.Username)

return client, nil
}
49 changes: 49 additions & 0 deletions builtin/providers/ultradns/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ultradns

import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

// Provider returns a terraform.ResourceProvider.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"username": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("ULTRADNS_USERNAME", nil),
Description: "UltraDNS Username.",
},

"password": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("ULTRADNS_PASSWORD", nil),
Description: "UltraDNS User Password",
},
"baseurl": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("ULTRADNS_BASEURL", nil),
Description: "UltraDNS Base Url(defaults to testing)",
},
},

ResourcesMap: map[string]*schema.Resource{
"ultradns_record": resourceUltraDNSRecord(),
},

ConfigureFunc: providerConfigure,
}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{
Username: d.Get("username").(string),
Password: d.Get("password").(string),
BaseURL: d.Get("baseurl").(string),
}

return config.Client()
}
43 changes: 43 additions & 0 deletions builtin/providers/ultradns/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ultradns

import (
"os"
"testing"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider

func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"ultradns": testAccProvider,
}
}

func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("ULTRADNS_USERNAME"); v == "" {
t.Fatal("ULTRADNS_USERNAME must be set for acceptance tests")
}

if v := os.Getenv("ULTRADNS_PASSWORD"); v == "" {
t.Fatal("ULTRADNS_PASSWORD must be set for acceptance tests")
}

if v := os.Getenv("ULTRADNS_DOMAIN"); v == "" {
t.Fatal("ULTRADNS_DOMAIN must be set for acceptance tests. The domain is used to create and destroy record against.")
}
}
218 changes: 218 additions & 0 deletions builtin/providers/ultradns/resource_ultradns_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package ultradns

import (
"fmt"
"log"
"strconv"
"strings"

"github.com/Ensighten/udnssdk"
"github.com/hashicorp/terraform/helper/schema"
)

type rRSetResource struct {
OwnerName string
RRType string
RData []string
TTL int
Profile *udnssdk.StringProfile
Zone string
}

func newRRSetResource(d *schema.ResourceData) (rRSetResource, error) {
r := rRSetResource{}

if attr, ok := d.GetOk("name"); ok {
r.OwnerName = attr.(string)
}

if attr, ok := d.GetOk("type"); ok {
r.RRType = attr.(string)
}

if attr, ok := d.GetOk("zone"); ok {
r.Zone = attr.(string)
}

if attr, ok := d.GetOk("rdata"); ok {
rdata := attr.([]interface{})
r.RData = make([]string, len(rdata))
for i, j := range rdata {
r.RData[i] = j.(string)
}
}

if attr, ok := d.GetOk("ttl"); ok {
r.TTL, _ = strconv.Atoi(attr.(string))
}

return r, nil
}

func (r rRSetResource) RRSetKey() udnssdk.RRSetKey {
return udnssdk.RRSetKey{
Zone: r.Zone,
Type: r.RRType,
Name: r.OwnerName,
}
}

func (r rRSetResource) RRSet() udnssdk.RRSet {
return udnssdk.RRSet{
OwnerName: r.OwnerName,
RRType: r.RRType,
RData: r.RData,
TTL: r.TTL,
}
}

func (r rRSetResource) ID() string {
return fmt.Sprintf("%s.%s", r.OwnerName, r.Zone)
}

func populateResourceDataFromRRSet(r udnssdk.RRSet, d *schema.ResourceData) error {
zone := d.Get("zone")
// ttl
d.Set("ttl", r.TTL)
// rdata
err := d.Set("rdata", r.RData)
if err != nil {
return fmt.Errorf("ultradns_record.rdata set failed: %#v", err)
}
// hostname
if r.OwnerName == "" {
d.Set("hostname", zone)
} else {
if strings.HasSuffix(r.OwnerName, ".") {
d.Set("hostname", r.OwnerName)
} else {
d.Set("hostname", fmt.Sprintf("%s.%s", r.OwnerName, zone))
}
}
return nil
}

func resourceUltraDNSRecord() *schema.Resource {
return &schema.Resource{
Create: resourceUltraDNSRecordCreate,
Read: resourceUltraDNSRecordRead,
Update: resourceUltraDNSRecordUpdate,
Delete: resourceUltraDNSRecordDelete,

Schema: map[string]*schema.Schema{
// Required
"zone": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"rdata": &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
// Optional
"ttl": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "3600",
},
// Computed
"hostname": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceUltraDNSRecordCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*udnssdk.Client)

r, err := newRRSetResource(d)
if err != nil {
return err
}

log.Printf("[INFO] ultradns_record create: %#v", r.RRSet())
_, err = client.RRSets.Create(r.RRSetKey(), r.RRSet())
if err != nil {
return fmt.Errorf("Failed to create UltraDNS RRSet: %s", err)
}

d.SetId(r.ID())
log.Printf("[INFO] ultradns_record.id: %s", d.Id())

return resourceUltraDNSRecordRead(d, meta)
}

func resourceUltraDNSRecordRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*udnssdk.Client)

r, err := newRRSetResource(d)
if err != nil {
return err
}

rrsets, err := client.RRSets.Select(r.RRSetKey())
if err != nil {
uderr, ok := err.(*udnssdk.ErrorResponseList)
if ok {
for _, r := range uderr.Responses {
// 70002 means Records Not Found
if r.ErrorCode == 70002 {
d.SetId("")
return nil
}
return fmt.Errorf("ultradns_record not found: %s", err)
}
}
return fmt.Errorf("ultradns_record not found: %s", err)
}
rec := rrsets[0]
return populateResourceDataFromRRSet(rec, d)
}

func resourceUltraDNSRecordUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*udnssdk.Client)

r, err := newRRSetResource(d)
if err != nil {
return err
}

log.Printf("[INFO] ultradns_record update: %#v", r.RRSet())
_, err = client.RRSets.Update(r.RRSetKey(), r.RRSet())
if err != nil {
return fmt.Errorf("ultradns_record update failed: %s", err)
}

return resourceUltraDNSRecordRead(d, meta)
}

func resourceUltraDNSRecordDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*udnssdk.Client)

r, err := newRRSetResource(d)
if err != nil {
return err
}

log.Printf("[INFO] ultradns_record delete: %#v", r.RRSet())
_, err = client.RRSets.Delete(r.RRSetKey())
if err != nil {
return fmt.Errorf("ultradns_record delete failed: %s", err)
}

return nil
}
Loading