Skip to content

Commit

Permalink
zone tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ponbiki committed Oct 10, 2023
1 parent 84a8d7a commit ac7606e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ns1/data_source_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func dataSourceZone() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"tags": {
Type: schema.TypeMap,
Computed: true,
},
},
Read: zoneRead,
}
Expand Down
6 changes: 6 additions & 0 deletions ns1/resource_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
),
},
{
Expand Down Expand Up @@ -1198,6 +1202,8 @@ resource "ns1_record" "it" {
filters {
filter = "geotarget_country"
}
tags = {tag1: "location1", tag2: "location2"}
}
resource "ns1_zone" "test" {
Expand Down
23 changes: 23 additions & 0 deletions ns1/resource_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions ns1/resource_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"reflect"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -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"}),
),
},
{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit ac7606e

Please sign in to comment.