Skip to content

Commit

Permalink
support tags for dns and vpn resource (#599)
Browse files Browse the repository at this point in the history
* support tags for dns ptr record resource

* support tags for dns record set resource

* support tags for vpn site connection resource
  • Loading branch information
ShiChangkuo authored Oct 21, 2020
1 parent b9fb182 commit 6c09c95
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 187 deletions.
7 changes: 5 additions & 2 deletions docs/resources/dns_recordset.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ resource "huaweicloud_dns_recordset" "rs_example_com" {

The following arguments are supported:

* `region` - (Optional) The region in which to obtain the V2 DNS client.
* `region` - (Optional) The region in which to obtain the DNS client.
If omitted, the `region` argument of the provider is used.
Changing this creates a new DNS record set.

Expand All @@ -54,6 +54,8 @@ The following arguments are supported:

* `records` - (Required) An array of DNS records.

* `tags` - (Optional) The key/value pairs to associate with the zone.

* `value_specs` - (Optional) Map of additional options. Changing this creates a
new record set.

Expand All @@ -68,6 +70,7 @@ The following attributes are exported:
* `description` - See Argument Reference above.
* `records` - See Argument Reference above.
* `zone_id` - See Argument Reference above.
* `tags` - See Argument Reference above.
* `value_specs` - See Argument Reference above.

## Import
Expand All @@ -76,5 +79,5 @@ This resource can be imported by specifying the zone ID and recordset ID,
separated by a forward slash.

```
$ terraform import huaweicloud_dns_recordset.recordset_1 <zone_id>/<recordset_id>
$ terraform import huaweicloud_dns_recordset.recordset_1 < zone_id >/< recordset_id >
```
3 changes: 3 additions & 0 deletions docs/resources/vpnaas_site_connection_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ The following arguments are supported:

* `value_specs` - (Optional) Map of additional options.

* `tags` - (Optional) The key/value pairs to associate with the connection.

## Attributes Reference

The following attributes are exported:
Expand All @@ -114,6 +116,7 @@ The following attributes are exported:
* `vpnservice_id` - See Argument Reference above.
* `ikepolicy_id` - See Argument Reference above.
* `value_specs` - See Argument Reference above.
* `tags` - See Argument Reference above.

## Import

Expand Down
92 changes: 47 additions & 45 deletions huaweicloud/resource_huaweicloud_dns_ptrrecord_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"strings"
"time"

"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/dns/v2/ptrrecords"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/common/tags"
"github.com/huaweicloud/golangsdk/openstack/dns/v2/ptrrecords"
)

func ResourceDNSPtrRecordV2() *schema.Resource {
Expand Down Expand Up @@ -51,13 +52,7 @@ func ResourceDNSPtrRecordV2() *schema.Resource {
ForceNew: false,
ValidateFunc: resourceValidateTTL,
},
"tags": {
Type: schema.TypeMap,
Optional: true,
ForceNew: false,
ValidateFunc: validateECSTagValue,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tagsSchema(),
"address": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -147,6 +142,16 @@ func resourceDNSPtrRecordV2Read(d *schema.ResourceData, meta interface{}) error
d.Set("ttl", n.TTL)
d.Set("address", n.Address)

// save tags
resourceTags, err := tags.Get(dnsClient, "DNS-ptr_record", d.Id()).Extract()
if err != nil {
return fmt.Errorf("Error fetching HuaweiCloud DNS ptr record tags: %s", err)
}

tagmap := tagsToMap(resourceTags.Tags)
if err := d.Set("tags", tagmap); err != nil {
return fmt.Errorf("Error saving tags for HuaweiCloud DNS ptr record %s: %s", d.Id(), err)
}
return nil
}

Expand All @@ -158,49 +163,46 @@ func resourceDNSPtrRecordV2Update(d *schema.ResourceData, meta interface{}) erro
return fmt.Errorf("Error creating HuaweiCloud DNS client: %s", err)
}

tagmap := d.Get("tags").(map[string]interface{})
taglist := []ptrrecords.Tag{}
for k, v := range tagmap {
tag := ptrrecords.Tag{
Key: k,
Value: v.(string),
if d.HasChanges("name", "description", "ttl") {
updateOpts := ptrrecords.CreateOpts{
PtrName: d.Get("name").(string),
Description: d.Get("description").(string),
TTL: d.Get("ttl").(int),
}
taglist = append(taglist, tag)
}

createOpts := ptrrecords.CreateOpts{
PtrName: d.Get("name").(string),
Description: d.Get("description").(string),
TTL: d.Get("ttl").(int),
Tags: taglist,
}
log.Printf("[DEBUG] Update Options: %#v", updateOpts)
fip_id := d.Get("floatingip_id").(string)
n, err := ptrrecords.Create(dnsClient, region, fip_id, updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating HuaweiCloud DNS PTR record: %s", err)
}

log.Printf("[DEBUG] Update Options: %#v", createOpts)
fip_id := d.Get("floatingip_id").(string)
n, err := ptrrecords.Create(dnsClient, region, fip_id, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating HuaweiCloud DNS PTR record: %s", err)
}
log.Printf("[DEBUG] Waiting for DNS PTR record (%s) to become available", n.ID)
stateConf := &resource.StateChangeConf{
Target: []string{"ACTIVE"},
Pending: []string{"PENDING_CREATE"},
Refresh: waitForDNSPtrRecord(dnsClient, n.ID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}

log.Printf("[DEBUG] Waiting for DNS PTR record (%s) to become available", n.ID)
stateConf := &resource.StateChangeConf{
Target: []string{"ACTIVE"},
Pending: []string{"PENDING_CREATE"},
Refresh: waitForDNSPtrRecord(dnsClient, n.ID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf(
"Error waiting for PTR record (%s) to become ACTIVE for update: %s",
n.ID, err)
}

_, err = stateConf.WaitForState()
log.Printf("[DEBUG] Updated HuaweiCloud DNS PTR record %s: %#v", n.ID, n)
}

if err != nil {
return fmt.Errorf(
"Error waiting for PTR record (%s) to become ACTIVE for update: %s",
n.ID, err)
// update tags
tagErr := UpdateResourceTags(dnsClient, d, "DNS-ptr_record", d.Id())
if tagErr != nil {
return fmt.Errorf("Error updating tags of DNS PTR record %s: %s", d.Id(), tagErr)
}

log.Printf("[DEBUG] Updated HuaweiCloud DNS PTR record %s: %#v", n.ID, n)
return resourceDNSPtrRecordV2Read(d, meta)

}
Expand Down
57 changes: 29 additions & 28 deletions huaweicloud/resource_huaweicloud_dns_ptrrecord_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func randomPtrName() string {
func TestAccDNSV2PtrRecord_basic(t *testing.T) {
var ptrrecord ptrrecords.Ptr
ptrName := randomPtrName()
resourceName := "huaweicloud_dns_ptrrecord.ptr_1"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckDNS(t) },
Expand All @@ -27,17 +28,16 @@ func TestAccDNSV2PtrRecord_basic(t *testing.T) {
{
Config: testAccDNSV2PtrRecord_basic(ptrName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDNSV2PtrRecordExists("huaweicloud_dns_ptrrecord_v2.ptr_1", &ptrrecord),
resource.TestCheckResourceAttr(
"huaweicloud_dns_ptrrecord_v2.ptr_1", "description", "a ptr record"),
testAccCheckDNSV2PtrRecordExists(resourceName, &ptrrecord),
resource.TestCheckResourceAttr(resourceName, "description", "a ptr record"),
),
},
{
Config: testAccDNSV2PtrRecord_update(ptrName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDNSV2PtrRecordExists("huaweicloud_dns_ptrrecord_v2.ptr_1", &ptrrecord),
resource.TestCheckResourceAttr(
"huaweicloud_dns_ptrrecord_v2.ptr_1", "description", "ptr record updated"),
testAccCheckDNSV2PtrRecordExists(resourceName, &ptrrecord),
resource.TestCheckResourceAttr(resourceName, "description", "ptr record updated"),
resource.TestCheckResourceAttr(resourceName, "tags.foo", "bar"),
),
},
},
Expand All @@ -52,7 +52,7 @@ func testAccCheckDNSV2PtrRecordDestroy(s *terraform.State) error {
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "huaweicloud_dns_ptrrecord_v2" {
if rs.Type != "huaweicloud_dns_ptrrecord" {
continue
}

Expand Down Expand Up @@ -99,31 +99,32 @@ func testAccCheckDNSV2PtrRecordExists(n string, ptrrecord *ptrrecords.Ptr) resou

func testAccDNSV2PtrRecord_basic(ptrName string) string {
return fmt.Sprintf(`
resource "huaweicloud_networking_floatingip_v2" "fip_1" {
}
resource "huaweicloud_networking_floatingip_v2" "fip_1" {
}
resource "huaweicloud_dns_ptrrecord_v2" "ptr_1" {
name = "%s"
description = "a ptr record"
floatingip_id = "${huaweicloud_networking_floatingip_v2.fip_1.id}"
ttl = 6000
}
`, ptrName)
resource "huaweicloud_dns_ptrrecord" "ptr_1" {
name = "%s"
description = "a ptr record"
floatingip_id = huaweicloud_networking_floatingip_v2.fip_1.id
ttl = 6000
}
`, ptrName)
}

func testAccDNSV2PtrRecord_update(ptrName string) string {
return fmt.Sprintf(`
resource "huaweicloud_networking_floatingip_v2" "fip_1" {
}
resource "huaweicloud_networking_floatingip_v2" "fip_1" {
}
resource "huaweicloud_dns_ptrrecord_v2" "ptr_1" {
name = "%s"
description = "ptr record updated"
floatingip_id = "${huaweicloud_networking_floatingip_v2.fip_1.id}"
ttl = 6000
tags = {
foo = "bar"
}
}
`, ptrName)
resource "huaweicloud_dns_ptrrecord" "ptr_1" {
name = "%s"
description = "ptr record updated"
floatingip_id = huaweicloud_networking_floatingip_v2.fip_1.id
ttl = 6000
tags = {
foo = "bar"
}
}
`, ptrName)
}
65 changes: 62 additions & 3 deletions huaweicloud/resource_huaweicloud_dns_recordset_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"strings"
"time"

"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/dns/v2/recordsets"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/common/tags"
"github.com/huaweicloud/golangsdk/openstack/dns/v2/recordsets"
"github.com/huaweicloud/golangsdk/openstack/dns/v2/zones"
)

func ResourceDNSRecordSetV2() *schema.Resource {
Expand Down Expand Up @@ -78,6 +80,7 @@ func ResourceDNSRecordSetV2() *schema.Resource {
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tagsSchema(),
},
}
}
Expand Down Expand Up @@ -135,6 +138,20 @@ func resourceDNSRecordSetV2Create(d *schema.ResourceData, meta interface{}) erro
id := fmt.Sprintf("%s/%s", zoneID, n.ID)
d.SetId(id)

// set tags
tagRaw := d.Get("tags").(map[string]interface{})
if len(tagRaw) > 0 {
resourceType, err := getDNSRecordSetResourceType(dnsClient, zoneID)
if err != nil {
return fmt.Errorf("Error getting resource type of DNS record set %s: %s", n.ID, err)
}

taglist := expandResourceTags(tagRaw)
if tagErr := tags.Create(dnsClient, resourceType, n.ID, taglist).ExtractErr(); tagErr != nil {
return fmt.Errorf("Error setting tags of DNS record set %s: %s", n.ID, tagErr)
}
}

log.Printf("[DEBUG] Created HuaweiCloud DNS record set %s: %#v", n.ID, n)
return resourceDNSRecordSetV2Read(d, meta)
}
Expand Down Expand Up @@ -170,6 +187,21 @@ func resourceDNSRecordSetV2Read(d *schema.ResourceData, meta interface{}) error
d.Set("region", GetRegion(d, config))
d.Set("zone_id", zoneID)

// save tags
resourceType, err := getDNSRecordSetResourceType(dnsClient, zoneID)
if err != nil {
return fmt.Errorf("Error getting resource type of DNS record set %s: %s", recordsetID, err)
}
resourceTags, err := tags.Get(dnsClient, resourceType, recordsetID).Extract()
if err != nil {
return fmt.Errorf("Error fetching OpenTelekomCloud DNS record set tags: %s", err)
}

tagmap := tagsToMap(resourceTags.Tags)
if err := d.Set("tags", tagmap); err != nil {
return fmt.Errorf("Error saving tags for OpenTelekomCloud DNS record set %s: %s", recordsetID, err)
}

return nil
}

Expand Down Expand Up @@ -228,6 +260,17 @@ func resourceDNSRecordSetV2Update(d *schema.ResourceData, meta interface{}) erro
recordsetID, err)
}

// update tags
resourceType, err := getDNSRecordSetResourceType(dnsClient, zoneID)
if err != nil {
return fmt.Errorf("Error getting resource type of DNS record set %s: %s", d.Id(), err)
}

tagErr := UpdateResourceTags(dnsClient, d, resourceType, recordsetID)
if tagErr != nil {
return fmt.Errorf("Error updating tags of DNS record set %s: %s", d.Id(), tagErr)
}

return resourceDNSRecordSetV2Read(d, meta)
}

Expand Down Expand Up @@ -335,3 +378,19 @@ func resourceValidateTTL(v interface{}, k string) (ws []string, errors []error)
errors = append(errors, fmt.Errorf("%q must be [300, 2147483647]", k))
return
}

// get resource type of DNS record set from zone_id
func getDNSRecordSetResourceType(client *golangsdk.ServiceClient, zone_id string) (string, error) {
zone, err := zones.Get(client, zone_id).Extract()
if err != nil {
return "", err
}

zoneType := zone.ZoneType
if zoneType == "public" {
return "DNS-public_recordset", nil
} else if zoneType == "private" {
return "DNS-private_recordset", nil
}
return "", fmt.Errorf("invalid zone type: %s", zoneType)
}
Loading

0 comments on commit 6c09c95

Please sign in to comment.