Skip to content

Commit 149790e

Browse files
Fix Network Resource group order (#94)
1 parent b5c0241 commit 149790e

File tree

4 files changed

+61
-22
lines changed

4 files changed

+61
-22
lines changed

docs/resources/network_resource.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ resource "netbird_network_resource" "example" {
3838
### Required
3939

4040
- `address` (String) Network resource address (either a direct host like 1.1.1.1 or 1.1.1.1/32, or a subnet like 192.168.178.0/24, or domains like example.com and *.example.com)
41-
- `groups` (List of String) Group IDs containing the resource
41+
- `groups` (Set of String) Group IDs containing the resource
4242
- `name` (String) NetworkResource Name
4343
- `network_id` (String) The unique identifier of a network
4444

internal/provider/network_resource_resource.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ package provider
66
import (
77
"context"
88
"fmt"
9-
"sort"
109
"strings"
1110

12-
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
11+
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
1312
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1413
"github.com/hashicorp/terraform-plugin-framework/diag"
1514
"github.com/hashicorp/terraform-plugin-framework/path"
@@ -47,7 +46,7 @@ type NetworkResourceModel struct {
4746
Description types.String `tfsdk:"description"`
4847
Address types.String `tfsdk:"address"`
4948
Enabled types.Bool `tfsdk:"enabled"`
50-
Groups types.List `tfsdk:"groups"`
49+
Groups types.Set `tfsdk:"groups"`
5150
}
5251

5352
func (r *NetworkResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
@@ -93,11 +92,11 @@ func (r *NetworkResource) Schema(ctx context.Context, req resource.SchemaRequest
9392
Computed: true,
9493
Default: booldefault.StaticBool(true),
9594
},
96-
"groups": schema.ListAttribute{
95+
"groups": schema.SetAttribute{
9796
MarkdownDescription: "Group IDs containing the resource",
9897
Required: true,
9998
ElementType: types.StringType,
100-
Validators: []validator.List{listvalidator.ValueStringsAre(stringvalidator.LengthAtLeast(1)), listvalidator.SizeAtLeast(1)},
99+
Validators: []validator.Set{setvalidator.ValueStringsAre(stringvalidator.LengthAtLeast(1)), setvalidator.SizeAtLeast(1)},
101100
},
102101
},
103102
}
@@ -135,8 +134,7 @@ func networkResourceAPIToTerraform(ctx context.Context, networkResource *api.Net
135134
for i, k := range networkResource.Groups {
136135
groups[i] = k.Id
137136
}
138-
sort.Strings(groups)
139-
data.Groups, d = types.ListValueFrom(ctx, types.StringType, groups)
137+
data.Groups, d = types.SetValueFrom(ctx, types.StringType, groups)
140138
ret.Append(d...)
141139
return ret
142140
}
@@ -156,7 +154,7 @@ func (r *NetworkResource) Create(ctx context.Context, req resource.CreateRequest
156154
Description: data.Description.ValueStringPointer(),
157155
Address: data.Address.ValueString(),
158156
Enabled: data.Enabled.ValueBool(),
159-
Groups: stringListDefault(ctx, data.Groups, []string{}),
157+
Groups: stringSetDefault(ctx, data.Groups, []string{}),
160158
}
161159

162160
networkResource, err := r.client.Networks.Resources(data.NetworkId.ValueString()).Create(ctx, networkResourceReq)
@@ -226,7 +224,7 @@ func (r *NetworkResource) Update(ctx context.Context, req resource.UpdateRequest
226224
Description: data.Description.ValueStringPointer(),
227225
Address: data.Address.ValueString(),
228226
Enabled: data.Enabled.ValueBool(),
229-
Groups: stringListDefault(ctx, data.Groups, []string{}),
227+
Groups: stringSetDefault(ctx, data.Groups, []string{}),
230228
}
231229

232230
networkResource, err := r.client.Networks.Resources(data.NetworkId.ValueString()).Update(ctx, data.Id.ValueString(), networkResourceReq)

internal/provider/network_resource_test.go

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func Test_networkResourceAPIToTerraform(t *testing.T) {
3535
Description: types.StringNull(),
3636
Address: types.StringValue("1.1.1.1/32"),
3737
Enabled: types.BoolValue(false),
38-
Groups: types.ListValueMust(types.StringType, []attr.Value{}),
38+
Groups: types.SetValueMust(types.StringType, []attr.Value{}),
3939
},
4040
},
4141
{
@@ -62,7 +62,41 @@ func Test_networkResourceAPIToTerraform(t *testing.T) {
6262
Description: types.StringValue("Test"),
6363
Address: types.StringValue("example.com"),
6464
Enabled: types.BoolValue(true),
65-
Groups: types.ListValueMust(types.StringType, []attr.Value{types.StringValue("g1")}),
65+
Groups: types.SetValueMust(types.StringType, []attr.Value{types.StringValue("g1")}),
66+
},
67+
},
68+
{
69+
resource: &api.NetworkResource{
70+
Address: "example.com",
71+
Description: valPtr("Test"),
72+
Enabled: true,
73+
Groups: []api.GroupMinimum{
74+
{
75+
Id: "g1",
76+
Name: "not important",
77+
Issued: nil,
78+
PeersCount: 0,
79+
ResourcesCount: 0,
80+
},
81+
{
82+
Id: "g2",
83+
Name: "doesn't matter",
84+
Issued: nil,
85+
PeersCount: 0,
86+
ResourcesCount: 0,
87+
},
88+
},
89+
Id: "r2",
90+
Name: "test2",
91+
Type: api.NetworkResourceTypeDomain,
92+
},
93+
expected: NetworkResourceModel{
94+
Id: types.StringValue("r2"),
95+
Name: types.StringValue("test2"),
96+
Description: types.StringValue("Test"),
97+
Address: types.StringValue("example.com"),
98+
Enabled: types.BoolValue(true),
99+
Groups: types.SetValueMust(types.StringType, []attr.Value{types.StringValue("g1"), types.StringValue("g2")}),
66100
},
67101
},
68102
}
@@ -89,12 +123,11 @@ func Test_NetworkResource_Create(t *testing.T) {
89123
Steps: []resource.TestStep{
90124
{
91125
ResourceName: rName,
92-
Config: testNetworkResourceResource(rName, "network1", `example.com`, `["group-notall"]`, rName),
126+
Config: testNetworkResourceResource(rName, "network1", `example.com`, `["group-notall", "group-all"]`, rName),
93127
Check: resource.ComposeAggregateTestCheckFunc(
94128
resource.TestCheckResourceAttrSet(rNameFull, "id"),
95129
resource.TestCheckResourceAttr(rNameFull, "address", "example.com"),
96-
resource.TestCheckResourceAttr(rNameFull, "groups.#", "1"),
97-
resource.TestCheckResourceAttr(rNameFull, "groups.0", "group-notall"),
130+
resource.TestCheckResourceAttr(rNameFull, "groups.#", "2"),
98131
resource.TestCheckResourceAttr(rNameFull, "name", rName),
99132
func(s *terraform.State) error {
100133
nreID := s.RootModule().Resources[rNameFull].Primary.Attributes["id"]
@@ -107,8 +140,8 @@ func Test_NetworkResource_Create(t *testing.T) {
107140
return fmt.Errorf("NetworkResource Address mismatch, expected example.com, found %s on management server", resource.Address)
108141
}
109142

110-
if len(resource.Groups) != 1 || resource.Groups[0].Id != "group-notall" {
111-
return fmt.Errorf("NetworkResource Groups mismatch, expected [group-notall], found %#v on management server", resource.Groups)
143+
if len(resource.Groups) != 2 || (resource.Groups[0].Id != "group-notall" && resource.Groups[0].Id != "group-all") && (resource.Groups[1].Id != "group-notall" && resource.Groups[1].Id != "group-all") {
144+
return fmt.Errorf("NetworkResource Groups mismatch, expected [group-notall, group-all], found %#v on management server", resource.Groups)
112145
}
113146

114147
if resource.Name != rName {
@@ -139,11 +172,10 @@ func Test_NetworkResource_Update(t *testing.T) {
139172
},
140173
{
141174
ResourceName: rName,
142-
Config: testNetworkResourceResource(rName, "network1", `google.com`, `["group-all"]`, rName+"Updated"),
175+
Config: testNetworkResourceResource(rName, "network1", `google.com`, `["group-all", "group-notall"]`, rName+"Updated"),
143176
Check: resource.ComposeAggregateTestCheckFunc(
144177
resource.TestCheckResourceAttr(rNameFull, "address", "google.com"),
145-
resource.TestCheckResourceAttr(rNameFull, "groups.#", "1"),
146-
resource.TestCheckResourceAttr(rNameFull, "groups.0", "group-all"),
178+
resource.TestCheckResourceAttr(rNameFull, "groups.#", "2"),
147179
resource.TestCheckResourceAttr(rNameFull, "name", rName+"Updated"),
148180
func(s *terraform.State) error {
149181
nreID := s.RootModule().Resources[rNameFull].Primary.Attributes["id"]
@@ -156,8 +188,8 @@ func Test_NetworkResource_Update(t *testing.T) {
156188
return fmt.Errorf("NetworkResource Address mismatch, expected google.com, found %s on management server", resource.Address)
157189
}
158190

159-
if len(resource.Groups) != 1 || resource.Groups[0].Id != "group-all" {
160-
return fmt.Errorf("NetworkResource Groups mismatch, expected [group-all], found %#v on management server", resource.Groups)
191+
if len(resource.Groups) != 2 || (resource.Groups[0].Id != "group-notall" && resource.Groups[0].Id != "group-all") && (resource.Groups[1].Id != "group-notall" && resource.Groups[1].Id != "group-all") {
192+
return fmt.Errorf("NetworkResource Groups mismatch, expected [group-notall, group-all], found %#v on management server", resource.Groups)
161193
}
162194

163195
if resource.Name != rName+"Updated" {

internal/provider/util.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ func stringListDefault(ctx context.Context, a types.List, b []string) []string {
4141
return ret
4242
}
4343

44+
func stringSetDefault(ctx context.Context, a types.Set, b []string) []string {
45+
if a.IsUnknown() || a.IsNull() {
46+
return b
47+
}
48+
var ret []string
49+
a.ElementsAs(ctx, &ret, false)
50+
return ret
51+
}
52+
4453
func int32Default(a types.Int32, b int32) int32 {
4554
if a.IsUnknown() || a.IsNull() {
4655
return b

0 commit comments

Comments
 (0)