Skip to content

Commit

Permalink
Use zone in ID to get import working and write doc
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetanfl committed Sep 25, 2018
1 parent 6412376 commit 012f5a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
25 changes: 22 additions & 3 deletions ovh/resource_ovh_domain_zone_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package ovh

import (
"fmt"
"github.com/hashicorp/terraform/helper/logging"
"github.com/hashicorp/terraform/helper/schema"
"log"
"strconv"

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

type OvhDomainZoneRecord struct {
Expand All @@ -17,14 +18,29 @@ type OvhDomainZoneRecord struct {
SubDomain string `json:"subDomain,omitempty"`
}

func resourceOvhDomainZoneImportState(
d *schema.ResourceData,
meta interface{}) ([]*schema.ResourceData, error) {
givenId := d.Id()
splitId := strings.SplitN(givenId, ".", 2)
if len(splitId) != 2 {
return nil, fmt.Errorf("Import Id is not OVH_ID.zone formatted")
}
d.SetId(splitId[0])
d.Set("zone", splitId[1])
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}

func resourceOvhDomainZoneRecord() *schema.Resource {
return &schema.Resource{
Create: resourceOvhDomainZoneRecordCreate,
Read: resourceOvhDomainZoneRecordRead,
Update: resourceOvhDomainZoneRecordUpdate,
Delete: resourceOvhDomainZoneRecordDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceOvhDomainZoneImportState,
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -91,6 +107,9 @@ func resourceOvhDomainZoneRecordRead(d *schema.ResourceData, meta interface{}) e
provider := meta.(*Config)

record := OvhDomainZoneRecord{}
if logging.IsDebugOrHigher() {
log.Printf("[DEBUG] OVH Get Record ID: %s in zone %s", d.Id(), d.Get("zone").(string))
}
err := provider.OVHClient.Get(
fmt.Sprintf("/domain/zone/%s/record/%s", d.Get("zone").(string), d.Id()),
&record,
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/ovh_domain_zone_record.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ The following attributes are exported:
* `fieldType` - The type of the record
* `ttl` - The TTL of the record

## Import

OVH record can be imported using the `id` and the `zone`, eg:

```
$ terraform import aws_domain_zone_record.test 1234OVH_ID.zone.tld
```

0 comments on commit 012f5a2

Please sign in to comment.