Skip to content

Commit

Permalink
providers/dnsimple: init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pearkes committed Jul 23, 2014
1 parent 5fea301 commit d8c83a1
Show file tree
Hide file tree
Showing 9 changed files with 491 additions and 0 deletions.
10 changes: 10 additions & 0 deletions builtin/bins/provider-dnsimple/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

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

func main() {
plugin.Serve(new(dnsimple.ResourceProvider))
}
1 change: 1 addition & 0 deletions builtin/bins/provider-dnsimple/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package main
33 changes: 33 additions & 0 deletions builtin/providers/dnsimple/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dnsimple

import (
"log"
"os"

"github.com/rubyist/go-dnsimple"
)

type Config struct {
Token string `mapstructure:"token"`
Email string `mapstructure:"email"`
}

// Client() returns a new client for accessing heroku.
//
func (c *Config) Client() (*dnsimple.DNSimpleClient, error) {

// If we have env vars set (like in the acc) tests,
// we need to override the values passed in here.
if v := os.Getenv("DNSIMPLE_EMAIL"); v != "" {
c.Email = v
}
if v := os.Getenv("DNSIMPLE_TOKEN"); v != "" {
c.Token = v
}

client := dnsimple.NewClient(c.Token, c.Email)

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

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

import (
"fmt"
"log"
"strconv"

"github.com/hashicorp/terraform/helper/config"
"github.com/hashicorp/terraform/helper/diff"
"github.com/hashicorp/terraform/terraform"
"github.com/rubyist/go-dnsimple"
)

func resource_dnsimple_record_create(
s *terraform.ResourceState,
d *terraform.ResourceDiff,
meta interface{}) (*terraform.ResourceState, error) {
p := meta.(*ResourceProvider)
client := p.client

// Merge the diff into the state so that we have all the attributes
// properly.
rs := s.MergeDiff(d)

var err error

newRecord := dnsimple.Record{
Name: rs.Attributes["name"],
Content: rs.Attributes["value"],
RecordType: rs.Attributes["type"],
}

if attr, ok := rs.Attributes["ttl"]; ok {
newRecord.TTL, err = strconv.Atoi(attr)
if err != nil {
return nil, err
}
}

log.Printf("[DEBUG] record create configuration: %#v", newRecord)

rec, err := client.CreateRecord(rs.Attributes["domain"], newRecord)

if err != nil {
return nil, fmt.Errorf("Failed to create record: %s", err)
}

rs.ID = strconv.Itoa(rec.Id)

log.Printf("[INFO] record ID: %s", rs.ID)

return resource_dnsimple_record_update_state(rs, &rec)
}

func resource_dnsimple_record_update(
s *terraform.ResourceState,
d *terraform.ResourceDiff,
meta interface{}) (*terraform.ResourceState, error) {

panic("Cannot update record")

return nil, nil
}

func resource_dnsimple_record_destroy(
s *terraform.ResourceState,
meta interface{}) error {
p := meta.(*ResourceProvider)
client := p.client

log.Printf("[INFO] Deleting record: %s", s.ID)

rec, err := resource_dnsimple_record_retrieve(s.Attributes["domain"], s.ID, client)
if err != nil {
return err
}

err = rec.Delete(client)
if err != nil {
return fmt.Errorf("Error deleting record: %s", err)
}

return nil
}

func resource_dnsimple_record_refresh(
s *terraform.ResourceState,
meta interface{}) (*terraform.ResourceState, error) {
p := meta.(*ResourceProvider)
client := p.client

rec, err := resource_dnsimple_record_retrieve(s.Attributes["app"], s.ID, client)
if err != nil {
return nil, err
}

return resource_dnsimple_record_update_state(s, rec)
}

func resource_dnsimple_record_diff(
s *terraform.ResourceState,
c *terraform.ResourceConfig,
meta interface{}) (*terraform.ResourceDiff, error) {

b := &diff.ResourceBuilder{
Attrs: map[string]diff.AttrType{
"domain": diff.AttrTypeCreate,
"name": diff.AttrTypeCreate,
"value": diff.AttrTypeUpdate,
"ttl": diff.AttrTypeCreate,
"type": diff.AttrTypeUpdate,
},

ComputedAttrs: []string{
"priority",
"domain_id",
},
}

return b.Diff(s, c)
}

func resource_dnsimple_record_update_state(
s *terraform.ResourceState,
rec *dnsimple.Record) (*terraform.ResourceState, error) {

s.Attributes["name"] = rec.Name
s.Attributes["value"] = rec.Content
s.Attributes["type"] = rec.RecordType
s.Attributes["ttl"] = strconv.Itoa(rec.TTL)
s.Attributes["priority"] = strconv.Itoa(rec.Priority)
s.Attributes["domain_id"] = strconv.Itoa(rec.DomainId)

return s, nil
}

func resource_dnsimple_record_retrieve(domain string, id string, client *dnsimple.DNSimpleClient) (*dnsimple.Record, error) {
intId, err := strconv.Atoi(id)
if err != nil {
return nil, err
}

record, err := client.RetrieveRecord(domain, intId)
if err != nil {
return nil, fmt.Errorf("Error retrieving record: %s", err)
}

return &record, nil
}

func resource_dnsimple_record_validation() *config.Validator {
return &config.Validator{
Required: []string{
"domain",
"name",
"value",
"type",
},
Optional: []string{
"ttl",
},
}
}
115 changes: 115 additions & 0 deletions builtin/providers/dnsimple/resource_dnsimple_record_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package dnsimple

import (
"fmt"
"strconv"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/rubyist/go-dnsimple"
)

func TestAccDNSimpleRecord_Basic(t *testing.T) {
var record dnsimple.Record

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDNSimpleRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckDNSimpleRecordConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckDNSimpleRecordExists("dnsimple_record.foobar", &record),
testAccCheckDNSimpleRecordAttributes(&record),
resource.TestCheckResourceAttr(
"dnsimple_record.foobar", "name", "terraform"),
resource.TestCheckResourceAttr(
"dnsimple_record.foobar", "domain", "jack.ly"),
resource.TestCheckResourceAttr(
"dnsimple_record.foobar", "value", "192.168.0.10"),
),
},
},
})
}

func testAccCheckDNSimpleRecordDestroy(s *terraform.State) error {
client := testAccProvider.client

for _, rs := range s.Resources {
if rs.Type != "dnsimple_record" {
continue
}

intId, err := strconv.Atoi(rs.ID)
if err != nil {
return err
}

_, err = client.RetrieveRecord(rs.Attributes["domain"], intId)

if err == nil {
return fmt.Errorf("Record still exists")
}
}

return nil
}

func testAccCheckDNSimpleRecordAttributes(record *dnsimple.Record) resource.TestCheckFunc {
return func(s *terraform.State) error {

if record.Content != "192.168.0.10" {
return fmt.Errorf("Bad content: %s", record.Content)
}

return nil
}
}

func testAccCheckDNSimpleRecordExists(n string, record *dnsimple.Record) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.Resources[n]

if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.ID == "" {
return fmt.Errorf("No Record ID is set")
}

client := testAccProvider.client

intId, err := strconv.Atoi(rs.ID)
if err != nil {
return err
}

foundRecord, err := client.RetrieveRecord(rs.Attributes["domain"], intId)

if err != nil {
return err
}

if strconv.Itoa(foundRecord.Id) != rs.ID {
return fmt.Errorf("Record not found")
}

*record = foundRecord

return nil
}
}

const testAccCheckDNSimpleRecordConfig_basic = `
resource "dnsimple_record" "foobar" {
domain = "jack.ly"
name = "terraform"
value = "192.168.0.10"
type = "A"
ttl = 3600
}`
68 changes: 68 additions & 0 deletions builtin/providers/dnsimple/resource_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dnsimple

import (
"log"

"github.com/hashicorp/terraform/helper/config"
"github.com/hashicorp/terraform/terraform"
"github.com/rubyist/go-dnsimple"
)

type ResourceProvider struct {
Config Config

client *dnsimple.DNSimpleClient
}

func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) {
v := &config.Validator{
Required: []string{
"token",
"email",
},
}

return v.Validate(c)
}

func (p *ResourceProvider) ValidateResource(
t string, c *terraform.ResourceConfig) ([]string, []error) {
return resourceMap.Validate(t, c)
}

func (p *ResourceProvider) Configure(c *terraform.ResourceConfig) error {
if _, err := config.Decode(&p.Config, c.Config); err != nil {
return err
}

log.Println("[INFO] Initializing DNSimple client")
var err error
p.client, err = p.Config.Client()

if err != nil {
return err
}

return nil
}

func (p *ResourceProvider) Apply(
s *terraform.ResourceState,
d *terraform.ResourceDiff) (*terraform.ResourceState, error) {
return resourceMap.Apply(s, d, p)
}

func (p *ResourceProvider) Diff(
s *terraform.ResourceState,
c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) {
return resourceMap.Diff(s, c, p)
}

func (p *ResourceProvider) Refresh(
s *terraform.ResourceState) (*terraform.ResourceState, error) {
return resourceMap.Refresh(s, p)
}

func (p *ResourceProvider) Resources() []terraform.ResourceType {
return resourceMap.Resources()
}
Loading

0 comments on commit d8c83a1

Please sign in to comment.