-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a5ea680
rebase from previous pull request
remijouannet 30702d6
documentation for the resource
remijouannet e54372a
Merge remote-tracking branch 'upstream/master' into r-ovh-domains-zone
remijouannet ee39f35
fix type for id and ttl
remijouannet 8ef7ebe
lowercase fieldtype and subdomain
remijouannet 8e5da69
Update ovh_domain_zone_record.html.markdown
remijouannet 2ec3a65
add refresh DNS Zone
outscale-rjt 6f82249
forgot to fmt
outscale-rjt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { | ||
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) | ||
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) | ||
} | ||
|
||
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
😄