Skip to content

Commit d02facf

Browse files
committed
feat(serverless): add support for tags on functions/containers namespaces
1 parent 37aea84 commit d02facf

File tree

6 files changed

+79
-7
lines changed

6 files changed

+79
-7
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require (
2323
github.com/nats-io/jwt/v2 v2.7.2
2424
github.com/nats-io/nats.go v1.37.0
2525
github.com/robfig/cron/v3 v3.0.1
26-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30.0.20241021115642-2d127a2d76c7
26+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30.0.20241028153617-2a48843b5fcb
2727
github.com/stretchr/testify v1.9.0
2828
golang.org/x/crypto v0.28.0
2929
gopkg.in/dnaeon/go-vcr.v3 v3.2.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXq
242242
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
243243
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
244244
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
245-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30.0.20241021115642-2d127a2d76c7 h1:mWi3yS37Lhf73OP2Z4CboKtXJM4mWDAUWFHKSx2WK7k=
246-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30.0.20241021115642-2d127a2d76c7/go.mod h1:3jrRJM7638J+P33hKy9MBvfOBxNo8pEGNQQoIv65Ihg=
245+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30.0.20241028153617-2a48843b5fcb h1:OsRpbw60numCy/+3FS7UhZzkdiTu6OZwq29bb4b3gNo=
246+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30.0.20241028153617-2a48843b5fcb/go.mod h1:3jrRJM7638J+P33hKy9MBvfOBxNo8pEGNQQoIv65Ihg=
247247
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
248248
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
249249
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=

internal/services/container/namespace.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ func ResourceNamespace() *schema.Resource {
4646
Optional: true,
4747
Description: "The description of the container namespace",
4848
},
49+
"tags": {
50+
Type: schema.TypeList,
51+
Elem: &schema.Schema{
52+
Type: schema.TypeString,
53+
},
54+
Optional: true,
55+
Description: "List of tags [\"tag1\", \"tag2\", ...] attached to the container namespace",
56+
},
4957
"environment_variables": {
5058
Type: schema.TypeMap,
5159
Optional: true,
@@ -97,14 +105,21 @@ func ResourceContainerNamespaceCreate(ctx context.Context, d *schema.ResourceDat
97105
return diag.FromErr(err)
98106
}
99107

100-
ns, err := api.CreateNamespace(&container.CreateNamespaceRequest{
108+
createReq := &container.CreateNamespaceRequest{
101109
Description: types.ExpandStringPtr(d.Get("description").(string)),
102110
EnvironmentVariables: types.ExpandMapPtrStringString(d.Get("environment_variables")),
103111
SecretEnvironmentVariables: expandContainerSecrets(d.Get("secret_environment_variables")),
104112
Name: types.ExpandOrGenerateString(d.Get("name").(string), "ns"),
105113
ProjectID: d.Get("project_id").(string),
106114
Region: region,
107-
}, scw.WithContext(ctx))
115+
}
116+
117+
rawTag, tagExist := d.GetOk("tags")
118+
if tagExist {
119+
createReq.Tags = types.ExpandStrings(rawTag)
120+
}
121+
122+
ns, err := api.CreateNamespace(createReq, scw.WithContext(ctx))
108123
if err != nil {
109124
return diag.FromErr(err)
110125
}
@@ -135,6 +150,7 @@ func ResourceContainerNamespaceRead(ctx context.Context, d *schema.ResourceData,
135150
}
136151

137152
_ = d.Set("description", types.FlattenStringPtr(ns.Description))
153+
_ = d.Set("tags", types.FlattenSliceString(ns.Tags))
138154
_ = d.Set("environment_variables", ns.EnvironmentVariables)
139155
_ = d.Set("name", ns.Name)
140156
_ = d.Set("organization_id", ns.OrganizationID)
@@ -166,6 +182,10 @@ func ResourceContainerNamespaceUpdate(ctx context.Context, d *schema.ResourceDat
166182
req.Description = types.ExpandUpdatedStringPtr(d.Get("description"))
167183
}
168184

185+
if d.HasChange("tags") {
186+
req.Tags = types.ExpandUpdatedStringsPtr(d.Get("tags"))
187+
}
188+
169189
if d.HasChanges("environment_variables") {
170190
req.EnvironmentVariables = types.ExpandMapPtrStringString(d.Get("environment_variables"))
171191
}

internal/services/container/namespace_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,22 @@ func TestAccNamespace_Basic(t *testing.T) {
121121
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "environment_variables.foo", "bar"),
122122
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "secret_environment_variables.foo_secret", "bar_secret"),
123123

124+
acctest.CheckResourceAttrUUID("scaleway_container_namespace.main", "id"),
125+
),
126+
},
127+
{
128+
Config: `
129+
resource scaleway_container_namespace main {
130+
name = "tf-tags-test"
131+
tags = ["tag1", "tag2"]
132+
}
133+
`,
134+
Check: resource.ComposeTestCheckFunc(
135+
isNamespacePresent(tt, "scaleway_container_namespace.main"),
136+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "name", "tf-tags-test"),
137+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "tags.0", "tag1"),
138+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "tags.1", "tag2"),
139+
124140
acctest.CheckResourceAttrUUID("scaleway_container_namespace.main", "id"),
125141
),
126142
},

internal/services/function/namespace.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ func ResourceNamespace() *schema.Resource {
4444
Optional: true,
4545
Description: "The description of the function namespace",
4646
},
47+
"tags": {
48+
Type: schema.TypeList,
49+
Elem: &schema.Schema{
50+
Type: schema.TypeString,
51+
},
52+
Optional: true,
53+
Description: "List of tags [\"tag1\", \"tag2\", ...] attached to the function namespace",
54+
},
4755
"environment_variables": {
4856
Type: schema.TypeMap,
4957
Optional: true,
@@ -88,14 +96,21 @@ func ResourceFunctionNamespaceCreate(ctx context.Context, d *schema.ResourceData
8896
return diag.FromErr(err)
8997
}
9098

91-
ns, err := api.CreateNamespace(&function.CreateNamespaceRequest{
99+
createReq := &function.CreateNamespaceRequest{
92100
Description: types.ExpandStringPtr(d.Get("description").(string)),
93101
EnvironmentVariables: types.ExpandMapPtrStringString(d.Get("environment_variables")),
94102
SecretEnvironmentVariables: expandFunctionsSecrets(d.Get("secret_environment_variables")),
95103
Name: types.ExpandOrGenerateString(d.Get("name").(string), "func"),
96104
ProjectID: d.Get("project_id").(string),
97105
Region: region,
98-
}, scw.WithContext(ctx))
106+
}
107+
108+
rawTag, tagExist := d.GetOk("tags")
109+
if tagExist {
110+
createReq.Tags = types.ExpandStrings(rawTag)
111+
}
112+
113+
ns, err := api.CreateNamespace(createReq, scw.WithContext(ctx))
99114
if err != nil {
100115
return diag.FromErr(err)
101116
}
@@ -126,6 +141,7 @@ func ResourceFunctionNamespaceRead(ctx context.Context, d *schema.ResourceData,
126141
}
127142

128143
_ = d.Set("description", ns.Description)
144+
_ = d.Set("tags", types.FlattenSliceString(ns.Tags))
129145
_ = d.Set("environment_variables", ns.EnvironmentVariables)
130146
_ = d.Set("name", ns.Name)
131147
_ = d.Set("organization_id", ns.OrganizationID)
@@ -161,6 +177,10 @@ func ResourceFunctionNamespaceUpdate(ctx context.Context, d *schema.ResourceData
161177
req.Description = types.ExpandUpdatedStringPtr(d.Get("description"))
162178
}
163179

180+
if d.HasChange("tags") {
181+
req.Tags = types.ExpandUpdatedStringsPtr(d.Get("tags"))
182+
}
183+
164184
if d.HasChanges("environment_variables") {
165185
req.EnvironmentVariables = types.ExpandMapPtrStringString(d.Get("environment_variables"))
166186
}

internal/services/function/namespace_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ func TestAccFunctionNamespace_Basic(t *testing.T) {
6565
resource.TestCheckResourceAttr("scaleway_function_namespace.main", "environment_variables.test", "test"),
6666
resource.TestCheckResourceAttr("scaleway_function_namespace.main", "secret_environment_variables.test_secret", "test_secret"),
6767

68+
acctest.CheckResourceAttrUUID("scaleway_function_namespace.main", "id"),
69+
),
70+
},
71+
{
72+
Config: `
73+
resource scaleway_function_namespace main {
74+
name = "tf-tags-test"
75+
tags = ["tag1", "tag2"]
76+
}
77+
`,
78+
Check: resource.ComposeTestCheckFunc(
79+
isNamespacePresent(tt, "scaleway_function_namespace.main"),
80+
resource.TestCheckResourceAttr("scaleway_function_namespace.main", "name", "tf-tags-test"),
81+
resource.TestCheckResourceAttr("scaleway_function_namespace.main", "tags.0", "tag1"),
82+
resource.TestCheckResourceAttr("scaleway_function_namespace.main", "tags.1", "tag2"),
83+
6884
acctest.CheckResourceAttrUUID("scaleway_function_namespace.main", "id"),
6985
),
7086
},

0 commit comments

Comments
 (0)