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

R ovh domains zone #3

Merged
merged 8 commits into from
Dec 5, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ovh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func Provider() terraform.ResourceProvider {
"ovh_publiccloud_private_network_subnet": resourcePublicCloudPrivateNetworkSubnet(),
"ovh_publiccloud_user": resourcePublicCloudUser(),
"ovh_vrack_publiccloud_attachment": resourceVRackPublicCloudAttachment(),
"ovh_domain_zone_record": resourceOVHDomainZoneRecord(),
},

ConfigureFunc: configureProvider,
Expand Down
5 changes: 5 additions & 0 deletions ovh/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func testAccPreCheck(t *testing.T) {
t.Fatal("OVH_PUBLIC_CLOUD must be set for acceptance tests")
}

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

if testAccOVHClient == nil {
config := Config{
Endpoint: os.Getenv("OVH_ENDPOINT"),
Expand Down
192 changes: 192 additions & 0 deletions ovh/resource_ovh_dns_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package ovh

import (
"fmt"
"log"
"strconv"

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

type NewRecord struct {
Target string `json:"target"`
Ttl int `json:"ttl"`
FieldType string `json:"fieldType"`
SubDomain string `json:"subDomain"`
}

type Record struct {
Id int `json:"id"`
Zone string `json:"zone"`
Target string `json:"target"`
Ttl int `json:"ttl"`
FieldType string `json:"fieldType"`
SubDomain string `json:"subDomain"`
}

func resourceOVHDomainZoneRecord() *schema.Resource {
return &schema.Resource{
Create: resourceOVHRecordCreate,
Read: resourceOVHRecordRead,
Update: resourceOVHRecordUpdate,
Delete: resourceOVHRecordDelete,

Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"zone": {
Type: schema.TypeString,
Required: true,
},
"target": {
Type: schema.TypeString,
Required: true,
},
"ttl": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason ttl needs to be a TypeString and not a TypeInt? Would save you from having to call strconv during Create.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@remijouannet ping ^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, saw you changed it to TypeInt 😄

Type: schema.TypeInt,
Optional: true,
Default: 3600,
},
"fieldtype": {
Type: schema.TypeString,
Required: true,
},
"subdomain": {
Type: schema.TypeString,
Optional: true,
},
},
}
}

func resourceOVHRecordCreate(d *schema.ResourceData, meta interface{}) error {
provider := meta.(*Config)

// Create the new record
newRecord := &NewRecord{
FieldType: d.Get("fieldtype").(string),
SubDomain: d.Get("subdomain").(string),
Target: d.Get("target").(string),
Ttl: d.Get("ttl").(int),
}

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

resultRecord := Record{}

err := provider.OVHClient.Post(
fmt.Sprintf("/domain/zone/%s/record", d.Get("zone").(string)),
newRecord,
&resultRecord,
)

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

d.Set("id", resultRecord.Id)
d.SetId(strconv.Itoa(resultRecord.Id))

log.Printf("[INFO] OVH Record ID: %s", d.Id())

OVHZoneRefresh(d, meta)

return resourceOVHRecordRead(d, meta)
}

func resourceOVHRecordRead(d *schema.ResourceData, meta interface{}) error {
provider := meta.(*Config)

record := Record{}
err := provider.OVHClient.Get(
fmt.Sprintf("/domain/zone/%s/record/%s", d.Get("zone").(string), d.Id()),
&record,
)

if err != nil {
d.SetId("")
return nil
}

d.Set("id", record.Id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

record.Id is an int, can't use it for d.Set(). Need to use fmt.Sprintf("%d", record.Id) or strconv

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've changed the type of id in the data schema

d.Set("zone", record.Zone)
d.Set("fieldtype", record.FieldType)
d.Set("subdomain", record.SubDomain)
d.Set("ttl", record.Ttl)
d.Set("target", record.Target)

return nil
}

func resourceOVHRecordUpdate(d *schema.ResourceData, meta interface{}) error {
provider := meta.(*Config)

record := NewRecord{}

if attr, ok := d.GetOk("subdomain"); ok {
record.SubDomain = attr.(string)
}
if attr, ok := d.GetOk("fieldtype"); ok {
record.FieldType = attr.(string)
}
if attr, ok := d.GetOk("target"); ok {
record.Target = attr.(string)
}
if attr, ok := d.GetOk("ttl"); ok {
record.Ttl, _ = attr.(int)
}

log.Printf("[DEBUG] OVH Record update configuration: %#v", record)

err := provider.OVHClient.Put(
fmt.Sprintf("/domain/zone/%s/record/%s", d.Get("zone").(string), d.Id()),
record,
nil,
)
if err != nil {
return fmt.Errorf("Failed to update OVH Record: %s", err)
}

OVHZoneRefresh(d, meta)

return resourceOVHRecordRead(d, meta)
}

func resourceOVHRecordDelete(d *schema.ResourceData, meta interface{}) error {
provider := meta.(*Config)

log.Printf("[INFO] Deleting OVH Record: %s.%s, %s", d.Get("zone").(string), d.Get("subdomain").(string), d.Id())

err := provider.OVHClient.Delete(
fmt.Sprintf("/domain/zone/%s/record/%s", d.Get("zone").(string), d.Id()),
nil,
)

if err != nil {
return fmt.Errorf("Error deleting OVH Record: %s", err)
}

OVHZoneRefresh(d, meta)

return nil
}

func OVHZoneRefresh(d *schema.ResourceData, meta interface{}) error {
provider := meta.(*Config)

log.Printf("[INFO] Refresh OVH Zone: %s", d.Get("zone").(string))

err := provider.OVHClient.Post(
fmt.Sprintf("/domain/zone/%s/refresh", d.Get("zone").(string)),
nil,
nil,
)

if err != nil {
return fmt.Errorf("Error refresh OVH Zone: %s", err)
}

return nil
}
Loading