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 2 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
183 changes: 183 additions & 0 deletions ovh/resource_ovh_dns_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
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.TypeString,
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.TypeString,
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),
}

newRecord.Ttl, _ = strconv.Atoi(d.Get("ttl").(string))
Copy link
Contributor

Choose a reason for hiding this comment

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

If ttl has to be a string, dropping the error here.


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.SetId(strconv.Itoa(resultRecord.Id))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can use fmt.Sprintf("%d", resultRecord.Id)) here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is there really a difference between using Sprintf and strconv ?

d.Set("id", strconv.Itoa(resultRecord.Id))
Copy link
Contributor

Choose a reason for hiding this comment

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

duplicate set to id

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@grubernaut as i understand terraform generate internally an ID for every ressource,
the OVH api return a ID for each DNS entry so I force the internal terraform ID to be the ID return by the API
as this id is also part of the resource i'm setting it in the resource data schema

I don't know if i'm clear here and if this right way to do it

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

return resourceOVHRecordRead(d, meta)
}

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

recordID, err := strconv.Atoi(d.Id())
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't believe you actually need this conversion back to an int here.

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

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

Choose a reason for hiding this comment

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

As you can just use %s for the id here as you're building a string.

&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", strconv.Itoa(record.Ttl))
Copy link
Contributor

Choose a reason for hiding this comment

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

Same with ttl here

d.Set("target", record.Target)

return nil
}

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

recordID, err := strconv.Atoi(d.Id())
Copy link
Contributor

Choose a reason for hiding this comment

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

Can leave this as a string

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

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, _ = strconv.Atoi(attr.(string))
}

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

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

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())

recordID, err := strconv.Atoi(d.Id())
Copy link
Contributor

Choose a reason for hiding this comment

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

Can skip type conversion here, and continue using as a string on line 174

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

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

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

return nil
}
Loading