From ac7606ecc4c8ae666fd06d6f8445947c6e92fbfa Mon Sep 17 00:00:00 2001 From: ponbiki Date: Tue, 10 Oct 2023 12:40:02 -0400 Subject: [PATCH] zone tags --- ns1/data_source_zone.go | 4 ++++ ns1/resource_record_test.go | 6 ++++++ ns1/resource_zone.go | 23 +++++++++++++++++++++++ ns1/resource_zone_test.go | 12 ++++++++++++ 4 files changed, 45 insertions(+) diff --git a/ns1/data_source_zone.go b/ns1/data_source_zone.go index 225b8db8..68a8fac7 100644 --- a/ns1/data_source_zone.go +++ b/ns1/data_source_zone.go @@ -99,6 +99,10 @@ func dataSourceZone() *schema.Resource { Type: schema.TypeBool, Computed: true, }, + "tags": { + Type: schema.TypeMap, + Computed: true, + }, }, Read: zoneRead, } diff --git a/ns1/resource_record_test.go b/ns1/resource_record_test.go index 68a67051..13c30fc9 100644 --- a/ns1/resource_record_test.go +++ b/ns1/resource_record_test.go @@ -90,6 +90,10 @@ func TestAccRecord_updated(t *testing.T) { testAccCheckRecordAnswerRdata( t, &record, 0, []string{fmt.Sprintf("test2.%s", zoneName)}, ), + testAccCheckRecordTagData( + map[string]string{"tag1": "location1", "tag2": "location2"}, + &record, + ), ), }, { @@ -1198,6 +1202,8 @@ resource "ns1_record" "it" { filters { filter = "geotarget_country" } + + tags = {tag1: "location1", tag2: "location2"} } resource "ns1_zone" "test" { diff --git a/ns1/resource_zone.go b/ns1/resource_zone.go index 6c4de06b..5e2dafbf 100644 --- a/ns1/resource_zone.go +++ b/ns1/resource_zone.go @@ -151,6 +151,13 @@ func resourceZone() *schema.Resource { Type: schema.TypeString, }, }, + "tags": { + Type: schema.TypeMap, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, }, Create: zoneCreate, Read: zoneRead, @@ -208,6 +215,14 @@ func resourceZoneToResourceData(d *schema.ResourceData, z *dns.Zone) error { if z.Link != nil && *z.Link != "" { d.Set("link", *z.Link) } + if len(z.Tags) > 0 { + terraformTags := make(map[string]interface{}, len(z.Tags)) + for k, v := range z.Tags { + terraformTags[k] = v + } + d.Set("tags", terraformTags) + } + return nil } @@ -289,6 +304,14 @@ func resourceDataToZone(z *dns.Zone, d *schema.ResourceData) { z.Secondary.OtherPorts[i] = otherPort.(int) } } + + if v, ok := d.GetOk("tags"); ok { + tagsRaw := v.(map[string]interface{}) + z.Tags = make(map[string]string, len(tagsRaw)) + for t, val := range tagsRaw { + z.Tags[t] = val.(string) + } + } // TODO: support OtherNetworks after ns1-go supports it if v, ok := d.GetOk("secondaries"); ok { secondariesSet := v.(*schema.Set) diff --git a/ns1/resource_zone_test.go b/ns1/resource_zone_test.go index a3726a7c..eb6a489a 100644 --- a/ns1/resource_zone_test.go +++ b/ns1/resource_zone_test.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "reflect" "strconv" "strings" "testing" @@ -83,6 +84,7 @@ func TestAccZone_updated(t *testing.T) { testAccCheckZoneExpiry(&zone, 2592000), testAccCheckZoneNxTTL(&zone, 3601), testAccCheckZoneDNSSEC(&zone, false), + testAccCheckZoneTags(&zone, map[string]string{"tag1": "location1"}), ), }, { @@ -727,6 +729,15 @@ func testAccCheckZoneDNSSEC(zone *dns.Zone, expected bool) resource.TestCheckFun } } +func testAccCheckZoneTags(zone *dns.Zone, expected map[string]string) resource.TestCheckFunc { + return func(s *terraform.State) error { + if !reflect.DeepEqual(zone.Tags, expected) { + return fmt.Errorf("Tags: got: %v want: %v", zone.Tags, expected) + } + return nil + } +} + func testAccCheckZoneHostmaster(zone *dns.Zone, expected string) resource.TestCheckFunc { return func(s *terraform.State) error { if zone.Hostmaster != expected { @@ -773,6 +784,7 @@ resource "ns1_zone" "it" { expiry = 2592000 nx_ttl = 3601 dnssec = false + tags = {tag1 = "location1"} # link = "1.2.3.4.in-addr.arpa" # TODO # primary = "1.2.3.4.in-addr.arpa" # TODO }