From 6c09c955be3154f73ca108ed6b37daec79484b07 Mon Sep 17 00:00:00 2001 From: shichangkuo Date: Wed, 21 Oct 2020 16:35:58 +0800 Subject: [PATCH] support tags for dns and vpn resource (#599) * support tags for dns ptr record resource * support tags for dns record set resource * support tags for vpn site connection resource --- docs/resources/dns_recordset.md | 7 +- docs/resources/vpnaas_site_connection_v2.md | 3 + .../resource_huaweicloud_dns_ptrrecord_v2.go | 92 ++++----- ...ource_huaweicloud_dns_ptrrecord_v2_test.go | 57 +++--- .../resource_huaweicloud_dns_recordset_v2.go | 65 ++++++- ...ource_huaweicloud_dns_recordset_v2_test.go | 178 +++++++++--------- ...urce_huaweicloud_vpnaas_site_connection.go | 30 ++- ...huaweicloud_vpnaas_site_connection_test.go | 52 ++--- 8 files changed, 297 insertions(+), 187 deletions(-) diff --git a/docs/resources/dns_recordset.md b/docs/resources/dns_recordset.md index 53a3ccf3b9..7ba687a01b 100644 --- a/docs/resources/dns_recordset.md +++ b/docs/resources/dns_recordset.md @@ -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. @@ -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. @@ -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 @@ -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 / +$ terraform import huaweicloud_dns_recordset.recordset_1 < zone_id >/< recordset_id > ``` diff --git a/docs/resources/vpnaas_site_connection_v2.md b/docs/resources/vpnaas_site_connection_v2.md index e4f19814f0..5ff110e64e 100644 --- a/docs/resources/vpnaas_site_connection_v2.md +++ b/docs/resources/vpnaas_site_connection_v2.md @@ -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: @@ -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 diff --git a/huaweicloud/resource_huaweicloud_dns_ptrrecord_v2.go b/huaweicloud/resource_huaweicloud_dns_ptrrecord_v2.go index 73c0485727..b538f823d9 100644 --- a/huaweicloud/resource_huaweicloud_dns_ptrrecord_v2.go +++ b/huaweicloud/resource_huaweicloud_dns_ptrrecord_v2.go @@ -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 { @@ -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, @@ -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 } @@ -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) } diff --git a/huaweicloud/resource_huaweicloud_dns_ptrrecord_v2_test.go b/huaweicloud/resource_huaweicloud_dns_ptrrecord_v2_test.go index 9e65b1b0fc..c7ca9d13ce 100644 --- a/huaweicloud/resource_huaweicloud_dns_ptrrecord_v2_test.go +++ b/huaweicloud/resource_huaweicloud_dns_ptrrecord_v2_test.go @@ -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) }, @@ -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"), ), }, }, @@ -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 } @@ -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) } diff --git a/huaweicloud/resource_huaweicloud_dns_recordset_v2.go b/huaweicloud/resource_huaweicloud_dns_recordset_v2.go index cfbec9f074..ce94cf6f80 100644 --- a/huaweicloud/resource_huaweicloud_dns_recordset_v2.go +++ b/huaweicloud/resource_huaweicloud_dns_recordset_v2.go @@ -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 { @@ -78,6 +80,7 @@ func ResourceDNSRecordSetV2() *schema.Resource { ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, }, + "tags": tagsSchema(), }, } } @@ -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) } @@ -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 } @@ -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) } @@ -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) +} diff --git a/huaweicloud/resource_huaweicloud_dns_recordset_v2_test.go b/huaweicloud/resource_huaweicloud_dns_recordset_v2_test.go index f74bb76642..8a54c25b7d 100644 --- a/huaweicloud/resource_huaweicloud_dns_recordset_v2_test.go +++ b/huaweicloud/resource_huaweicloud_dns_recordset_v2_test.go @@ -13,13 +13,13 @@ import ( ) func randomZoneName() string { - // TODO: why does back-end convert name to lowercase? return fmt.Sprintf("acpttest-zone-%s.com.", acctest.RandString(5)) } func TestAccDNSV2RecordSet_basic(t *testing.T) { var recordset recordsets.RecordSet zoneName := randomZoneName() + resourceName := "huaweicloud_dns_recordset.recordset_1" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheckDNS(t) }, @@ -29,28 +29,27 @@ func TestAccDNSV2RecordSet_basic(t *testing.T) { { Config: testAccDNSV2RecordSet_basic(zoneName), Check: resource.ComposeTestCheckFunc( - testAccCheckDNSV2RecordSetExists("huaweicloud_dns_recordset_v2.recordset_1", &recordset), - resource.TestCheckResourceAttr( - "huaweicloud_dns_recordset_v2.recordset_1", "description", "a record set"), - resource.TestCheckResourceAttr( - "huaweicloud_dns_recordset_v2.recordset_1", "records.0", "10.1.0.0"), + testAccCheckDNSV2RecordSetExists(resourceName, &recordset), + resource.TestCheckResourceAttr(resourceName, "name", zoneName), + resource.TestCheckResourceAttr(resourceName, "description", "a record set"), + resource.TestCheckResourceAttr(resourceName, "type", "A"), + resource.TestCheckResourceAttr(resourceName, "ttl", "3000"), + resource.TestCheckResourceAttr(resourceName, "tags.key", "value"), + resource.TestCheckResourceAttr(resourceName, "records.0", "10.1.0.0"), ), }, { - ResourceName: "huaweicloud_dns_recordset_v2.recordset_1", + ResourceName: resourceName, ImportState: true, ImportStateVerify: true, }, { Config: testAccDNSV2RecordSet_update(zoneName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("huaweicloud_dns_recordset_v2.recordset_1", "name", zoneName), - resource.TestCheckResourceAttr("huaweicloud_dns_recordset_v2.recordset_1", "ttl", "6000"), - resource.TestCheckResourceAttr("huaweicloud_dns_recordset_v2.recordset_1", "type", "A"), - resource.TestCheckResourceAttr( - "huaweicloud_dns_recordset_v2.recordset_1", "description", "an updated record set"), - resource.TestCheckResourceAttr( - "huaweicloud_dns_recordset_v2.recordset_1", "records.0", "10.1.0.1"), + resource.TestCheckResourceAttr(resourceName, "description", "an updated record set"), + resource.TestCheckResourceAttr(resourceName, "ttl", "6000"), + resource.TestCheckResourceAttr(resourceName, "tags.key", "value_updated"), + resource.TestCheckResourceAttr(resourceName, "records.0", "10.1.0.1"), ), }, }, @@ -60,6 +59,7 @@ func TestAccDNSV2RecordSet_basic(t *testing.T) { func TestAccDNSV2RecordSet_readTTL(t *testing.T) { var recordset recordsets.RecordSet zoneName := randomZoneName() + resourceName := "huaweicloud_dns_recordset.recordset_1" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheckDNS(t) }, @@ -69,9 +69,8 @@ func TestAccDNSV2RecordSet_readTTL(t *testing.T) { { Config: testAccDNSV2RecordSet_readTTL(zoneName), Check: resource.ComposeTestCheckFunc( - testAccCheckDNSV2RecordSetExists("huaweicloud_dns_recordset_v2.recordset_1", &recordset), - resource.TestMatchResourceAttr( - "huaweicloud_dns_recordset_v2.recordset_1", "ttl", regexp.MustCompile("^[0-9]+$")), + testAccCheckDNSV2RecordSetExists(resourceName, &recordset), + resource.TestMatchResourceAttr(resourceName, "ttl", regexp.MustCompile("^[0-9]+$")), ), }, }, @@ -81,6 +80,7 @@ func TestAccDNSV2RecordSet_readTTL(t *testing.T) { func TestAccDNSV2RecordSet_timeout(t *testing.T) { var recordset recordsets.RecordSet zoneName := randomZoneName() + resourceName := "huaweicloud_dns_recordset.recordset_1" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheckDNS(t) }, @@ -90,7 +90,7 @@ func TestAccDNSV2RecordSet_timeout(t *testing.T) { { Config: testAccDNSV2RecordSet_timeout(zoneName), Check: resource.ComposeTestCheckFunc( - testAccCheckDNSV2RecordSetExists("huaweicloud_dns_recordset_v2.recordset_1", &recordset), + testAccCheckDNSV2RecordSetExists(resourceName, &recordset), ), }, }, @@ -105,7 +105,7 @@ func testAccCheckDNSV2RecordSetDestroy(s *terraform.State) error { } for _, rs := range s.RootModule().Resources { - if rs.Type != "huaweicloud_dns_recordset_v2" { + if rs.Type != "huaweicloud_dns_recordset" { continue } @@ -162,87 +162,93 @@ func testAccCheckDNSV2RecordSetExists(n string, recordset *recordsets.RecordSet) func testAccDNSV2RecordSet_basic(zoneName string) string { return fmt.Sprintf(` - resource "huaweicloud_dns_zone_v2" "zone_1" { - name = "%s" - email = "email2@example.com" - description = "a zone" - ttl = 6000 - #type = "PRIMARY" - } +resource "huaweicloud_dns_zone" "zone_1" { + name = "%s" + email = "email2@example.com" + description = "a zone" + ttl = 6000 +} - resource "huaweicloud_dns_recordset_v2" "recordset_1" { - zone_id = "${huaweicloud_dns_zone_v2.zone_1.id}" - name = "%s" - type = "A" - description = "a record set" - ttl = 3000 - records = ["10.1.0.0"] - } - `, zoneName, zoneName) +resource "huaweicloud_dns_recordset" "recordset_1" { + zone_id = huaweicloud_dns_zone.zone_1.id + name = "%s" + type = "A" + description = "a record set" + ttl = 3000 + records = ["10.1.0.0"] + + tags = { + foo = "bar" + key = "value" + } +} +`, zoneName, zoneName) } func testAccDNSV2RecordSet_update(zoneName string) string { return fmt.Sprintf(` - resource "huaweicloud_dns_zone_v2" "zone_1" { - name = "%s" - email = "email2@example.com" - description = "an updated zone" - ttl = 6000 - #type = "PRIMARY" - } +resource "huaweicloud_dns_zone" "zone_1" { + name = "%s" + email = "email2@example.com" + description = "an updated zone" + ttl = 6000 +} - resource "huaweicloud_dns_recordset_v2" "recordset_1" { - zone_id = "${huaweicloud_dns_zone_v2.zone_1.id}" - name = "%s" - type = "A" - description = "an updated record set" - ttl = 6000 - records = ["10.1.0.1"] - } - `, zoneName, zoneName) +resource "huaweicloud_dns_recordset" "recordset_1" { + zone_id = huaweicloud_dns_zone.zone_1.id + name = "%s" + type = "A" + description = "an updated record set" + ttl = 6000 + records = ["10.1.0.1"] + + tags = { + foo = "bar" + key = "value_updated" + } +} +`, zoneName, zoneName) } func testAccDNSV2RecordSet_readTTL(zoneName string) string { return fmt.Sprintf(` - resource "huaweicloud_dns_zone_v2" "zone_1" { - name = "%s" - email = "email2@example.com" - description = "an updated zone" - ttl = 6000 - #type = "PRIMARY" - } +resource "huaweicloud_dns_zone" "zone_1" { + name = "%s" + email = "email2@example.com" + description = "a zone" + ttl = 6000 +} - resource "huaweicloud_dns_recordset_v2" "recordset_1" { - zone_id = "${huaweicloud_dns_zone_v2.zone_1.id}" - name = "%s" - type = "A" - records = ["10.1.0.2"] - } - `, zoneName, zoneName) +resource "huaweicloud_dns_recordset" "recordset_1" { + zone_id = huaweicloud_dns_zone.zone_1.id + name = "%s" + type = "A" + records = ["10.1.0.2"] +} +`, zoneName, zoneName) } func testAccDNSV2RecordSet_timeout(zoneName string) string { return fmt.Sprintf(` - resource "huaweicloud_dns_zone_v2" "zone_1" { - name = "%s" - email = "email2@example.com" - description = "an updated zone" - ttl = 6000 - #type = "PRIMARY" - } +resource "huaweicloud_dns_zone" "zone_1" { + name = "%s" + email = "email2@example.com" + description = "a zone" + ttl = 6000 +} - resource "huaweicloud_dns_recordset_v2" "recordset_1" { - zone_id = "${huaweicloud_dns_zone_v2.zone_1.id}" - name = "%s" - type = "A" - ttl = 3000 - records = ["10.1.0.3"] - - timeouts { - create = "5m" - update = "5m" - delete = "5m" - } - } - `, zoneName, zoneName) +resource "huaweicloud_dns_recordset" "recordset_1" { + zone_id = huaweicloud_dns_zone.zone_1.id + name = "%s" + type = "A" + ttl = 3000 + records = ["10.1.0.3"] + + timeouts { + create = "5m" + update = "5m" + delete = "5m" + } +} +`, zoneName, zoneName) } diff --git a/huaweicloud/resource_huaweicloud_vpnaas_site_connection.go b/huaweicloud/resource_huaweicloud_vpnaas_site_connection.go index e9150b6c63..aa4f0c2f5b 100644 --- a/huaweicloud/resource_huaweicloud_vpnaas_site_connection.go +++ b/huaweicloud/resource_huaweicloud_vpnaas_site_connection.go @@ -8,6 +8,7 @@ import ( "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/networking/v2/extensions/vpnaas/siteconnections" ) @@ -137,6 +138,7 @@ func resourceVpnSiteConnectionV2() *schema.Resource { ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, }, + "tags": tagsSchema(), }, } } @@ -201,15 +203,22 @@ func resourceVpnSiteConnectionV2Create(d *schema.ResourceData, meta interface{}) MinTimeout: 2 * time.Second, } _, err = stateConf.WaitForState() - if err != nil { return err } log.Printf("[DEBUG] SiteConnection created: %#v", conn) - d.SetId(conn.ID) + // create tags + tagRaw := d.Get("tags").(map[string]interface{}) + if len(tagRaw) > 0 { + taglist := expandResourceTags(tagRaw) + if tagErr := tags.Create(networkingClient, "ipsec-site-connections", d.Id(), taglist).ExtractErr(); tagErr != nil { + return fmt.Errorf("Error setting tags of VPN site connection %s: %s", d.Id(), tagErr) + } + } + return resourceVpnSiteConnectionV2Read(d, meta) } @@ -260,6 +269,17 @@ func resourceVpnSiteConnectionV2Read(d *schema.ResourceData, meta interface{}) e log.Printf("[WARN] unable to set Site connection DPD") } + // Set tags + resourceTags, err := tags.Get(networkingClient, "ipsec-site-connections", d.Id()).Extract() + if err != nil { + return fmt.Errorf("Error fetching VPN site connection tags: %s", err) + } + + tagmap := tagsToMap(resourceTags.Tags) + if err := d.Set("tags", tagmap); err != nil { + return fmt.Errorf("Error saving tags for VPN site connection %s: %s", d.Id(), err) + } + return nil } @@ -372,6 +392,12 @@ func resourceVpnSiteConnectionV2Update(d *schema.ResourceData, meta interface{}) log.Printf("[DEBUG] Updated connection with id %s", d.Id()) } + // update tags + tagErr := UpdateResourceTags(networkingClient, d, "ipsec-site-connections", d.Id()) + if tagErr != nil { + return fmt.Errorf("Error updating tags of VPN site connection %s: %s", d.Id(), tagErr) + } + return resourceVpnSiteConnectionV2Read(d, meta) } diff --git a/huaweicloud/resource_huaweicloud_vpnaas_site_connection_test.go b/huaweicloud/resource_huaweicloud_vpnaas_site_connection_test.go index c9e7026a8a..20530baa3f 100644 --- a/huaweicloud/resource_huaweicloud_vpnaas_site_connection_test.go +++ b/huaweicloud/resource_huaweicloud_vpnaas_site_connection_test.go @@ -12,6 +12,8 @@ import ( func TestAccVpnSiteConnectionV2_basic(t *testing.T) { var conn siteconnections.Connection + resourceName := "huaweicloud_vpnaas_site_connection_v2.conn_1" + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -20,16 +22,18 @@ func TestAccVpnSiteConnectionV2_basic(t *testing.T) { { Config: testAccSiteConnectionV2_basic, Check: resource.ComposeTestCheckFunc( - testAccCheckSiteConnectionV2Exists( - "huaweicloud_vpnaas_site_connection_v2.conn_1", &conn), - resource.TestCheckResourceAttrPtr("huaweicloud_vpnaas_site_connection_v2.conn_1", "ikepolicy_id", &conn.IKEPolicyID), - resource.TestCheckResourceAttr("huaweicloud_vpnaas_site_connection_v2.conn_1", "admin_state_up", "true"), - resource.TestCheckResourceAttrPtr("huaweicloud_vpnaas_site_connection_v2.conn_1", "ipsecpolicy_id", &conn.IPSecPolicyID), - resource.TestCheckResourceAttrPtr("huaweicloud_vpnaas_site_connection_v2.conn_1", "vpnservice_id", &conn.VPNServiceID), - resource.TestCheckResourceAttrPtr("huaweicloud_vpnaas_site_connection_v2.conn_1", "local_ep_group_id", &conn.LocalEPGroupID), - resource.TestCheckResourceAttrPtr("huaweicloud_vpnaas_site_connection_v2.conn_1", "local_id", &conn.LocalID), - resource.TestCheckResourceAttrPtr("huaweicloud_vpnaas_site_connection_v2.conn_1", "peer_ep_group_id", &conn.PeerEPGroupID), - resource.TestCheckResourceAttrPtr("huaweicloud_vpnaas_site_connection_v2.conn_1", "name", &conn.Name), + testAccCheckSiteConnectionV2Exists(resourceName, &conn), + resource.TestCheckResourceAttrPtr(resourceName, "name", &conn.Name), + resource.TestCheckResourceAttrPtr(resourceName, "vpnservice_id", &conn.VPNServiceID), + resource.TestCheckResourceAttrPtr(resourceName, "ikepolicy_id", &conn.IKEPolicyID), + resource.TestCheckResourceAttrPtr(resourceName, "ipsecpolicy_id", &conn.IPSecPolicyID), + resource.TestCheckResourceAttrPtr(resourceName, "peer_ep_group_id", &conn.PeerEPGroupID), + resource.TestCheckResourceAttrPtr(resourceName, "local_ep_group_id", &conn.LocalEPGroupID), + resource.TestCheckResourceAttrPtr(resourceName, "local_id", &conn.LocalID), + + resource.TestCheckResourceAttr(resourceName, "admin_state_up", "true"), + resource.TestCheckResourceAttr(resourceName, "tags.foo", "bar"), + resource.TestCheckResourceAttr(resourceName, "tags.key", "value"), ), }, }, @@ -43,7 +47,7 @@ func testAccCheckSiteConnectionV2Destroy(s *terraform.State) error { return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err) } for _, rs := range s.RootModule().Resources { - if rs.Type != "huaweicloud_vpnaas_site_connection" { + if rs.Type != "huaweicloud_vpnaas_site_connection_v2" { continue } _, err = siteconnections.Get(networkingClient, rs.Primary.ID).Extract() @@ -93,7 +97,7 @@ var testAccSiteConnectionV2_basic = fmt.Sprintf(` } resource "huaweicloud_networking_subnet_v2" "subnet_1" { - network_id = "${huaweicloud_networking_network_v2.network_1.id}" + network_id = huaweicloud_networking_network_v2.network_1.id cidr = "192.168.199.0/24" ip_version = 4 } @@ -104,13 +108,13 @@ var testAccSiteConnectionV2_basic = fmt.Sprintf(` } resource "huaweicloud_networking_router_interface_v2" "router_interface_1" { - router_id = "${huaweicloud_networking_router_v2.router_1.id}" - subnet_id = "${huaweicloud_networking_subnet_v2.subnet_1.id}" + router_id = huaweicloud_networking_router_v2.router_1.id + subnet_id = huaweicloud_networking_subnet_v2.subnet_1.id } resource "huaweicloud_vpnaas_service_v2" "service_1" { name = "vpngw-acctest" - router_id = "${huaweicloud_networking_router_v2.router_1.id}" + router_id = huaweicloud_networking_router_v2.router_1.id } resource "huaweicloud_vpnaas_ipsec_policy_v2" "policy_1" { @@ -125,19 +129,25 @@ var testAccSiteConnectionV2_basic = fmt.Sprintf(` } resource "huaweicloud_vpnaas_endpoint_group_v2" "group_2" { type = "subnet" - endpoints = [ "${huaweicloud_networking_subnet_v2.subnet_1.id}" ] + endpoints = [huaweicloud_networking_subnet_v2.subnet_1.id] } resource "huaweicloud_vpnaas_site_connection_v2" "conn_1" { name = "connection_1" - ikepolicy_id = "${huaweicloud_vpnaas_ike_policy_v2.policy_2.id}" - ipsecpolicy_id = "${huaweicloud_vpnaas_ipsec_policy_v2.policy_1.id}" - vpnservice_id = "${huaweicloud_vpnaas_service_v2.service_1.id}" + ikepolicy_id = huaweicloud_vpnaas_ike_policy_v2.policy_2.id + ipsecpolicy_id = huaweicloud_vpnaas_ipsec_policy_v2.policy_1.id + vpnservice_id = huaweicloud_vpnaas_service_v2.service_1.id psk = "secret" peer_address = "192.168.10.1" peer_id = "192.168.10.1" - local_ep_group_id = "${huaweicloud_vpnaas_endpoint_group_v2.group_2.id}" - peer_ep_group_id = "${huaweicloud_vpnaas_endpoint_group_v2.group_1.id}" + local_ep_group_id = huaweicloud_vpnaas_endpoint_group_v2.group_2.id + peer_ep_group_id = huaweicloud_vpnaas_endpoint_group_v2.group_1.id + + tags = { + foo = "bar" + key = "value" + } + depends_on = ["huaweicloud_networking_router_interface_v2.router_interface_1"] } `, OS_EXTGW_ID)