-
Notifications
You must be signed in to change notification settings - Fork 144
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
Changes from 5 commits
a5ea680
30702d6
e54372a
ee39f35
8ef7ebe
8e5da69
2ec3a65
6f82249
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
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": { | ||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there really a difference between using Sprintf and strconv ? |
||
|
||
log.Printf("[INFO] OVH Record ID: %s", d.Id()) | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
||
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) | ||
} | ||
|
||
return nil | ||
} |
There was a problem hiding this comment.
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 aTypeString
and not aTypeInt
? Would save you from having to callstrconv
during Create.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@remijouannet ping ^
There was a problem hiding this comment.
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
😄