-
Notifications
You must be signed in to change notification settings - Fork 143
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 2 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,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": { | ||
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)) | ||
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. If |
||
|
||
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)) | ||
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 ? |
||
d.Set("id", 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. duplicate set to 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. @grubernaut as i understand terraform generate internally an ID for every ressource, 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()) | ||
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. Don't believe you actually need this conversion back to an |
||
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), | ||
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. As you can just use |
||
&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", strconv.Itoa(record.Ttl)) | ||
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. Same with |
||
d.Set("target", record.Target) | ||
|
||
return nil | ||
} | ||
|
||
func resourceOVHRecordUpdate(d *schema.ResourceData, meta interface{}) error { | ||
provider := meta.(*Config) | ||
|
||
recordID, err := strconv.Atoi(d.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 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()) | ||
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 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 | ||
} |
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
😄