Skip to content

Commit 6f6860e

Browse files
norbjdCodelax
andauthored
feat(serverless): add support for tags on functions/containers namespaces (#2807)
* feat(serverless): add support for tags on functions/containers namespaces * fix function namespace test * change acceptance tests * improve tests * record cassettes --------- Co-authored-by: Jules Casteran <[email protected]>
1 parent e7e721d commit 6f6860e

File tree

8 files changed

+5146
-2214
lines changed

8 files changed

+5146
-2214
lines changed

go.mod

+1-1
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

+2-2
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

+22-2
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

+45-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,32 @@ func TestAccNamespace_Basic(t *testing.T) {
7070
acctest.CheckResourceAttrUUID("scaleway_container_namespace.main", "id"),
7171
),
7272
},
73+
{
74+
Config: `
75+
resource scaleway_container_namespace main {
76+
name = "test-cr-ns-01"
77+
environment_variables = {
78+
"test" = "test"
79+
}
80+
secret_environment_variables = {
81+
"test_secret" = "test_secret"
82+
}
83+
tags = ["tag1", "tag2"]
84+
}
85+
`,
86+
Check: resource.ComposeTestCheckFunc(
87+
isNamespacePresent(tt, "scaleway_container_namespace.main"),
88+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "description", ""),
89+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "name", "test-cr-ns-01"),
90+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "environment_variables.test", "test"),
91+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "secret_environment_variables.test_secret", "test_secret"),
92+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "tags.#", "2"),
93+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "tags.0", "tag1"),
94+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "tags.1", "tag2"),
95+
96+
acctest.CheckResourceAttrUUID("scaleway_container_namespace.main", "id"),
97+
),
98+
},
7399
{
74100
Config: `
75101
resource scaleway_container_namespace main {
@@ -80,6 +106,7 @@ func TestAccNamespace_Basic(t *testing.T) {
80106
resource.TestCheckResourceAttrSet("scaleway_container_namespace.main", "name"),
81107
resource.TestCheckResourceAttrSet("scaleway_container_namespace.main", "registry_endpoint"),
82108
resource.TestCheckResourceAttrSet("scaleway_container_namespace.main", "registry_namespace_id"),
109+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "tags.#", "0"),
83110
),
84111
},
85112
{
@@ -99,7 +126,7 @@ func TestAccNamespace_Basic(t *testing.T) {
99126
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "name", "tf-env-test"),
100127
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "environment_variables.test", "test"),
101128
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "secret_environment_variables.test_secret", "test_secret"),
102-
129+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "tags.#", "0"),
103130
acctest.CheckResourceAttrUUID("scaleway_container_namespace.main", "id"),
104131
),
105132
},
@@ -120,6 +147,23 @@ func TestAccNamespace_Basic(t *testing.T) {
120147
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "name", "tf-env-test"),
121148
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "environment_variables.foo", "bar"),
122149
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "secret_environment_variables.foo_secret", "bar_secret"),
150+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "tags.#", "0"),
151+
acctest.CheckResourceAttrUUID("scaleway_container_namespace.main", "id"),
152+
),
153+
},
154+
{
155+
Config: `
156+
resource scaleway_container_namespace main {
157+
name = "tf-tags-test"
158+
tags = ["tag1", "tag2"]
159+
}
160+
`,
161+
Check: resource.ComposeTestCheckFunc(
162+
isNamespacePresent(tt, "scaleway_container_namespace.main"),
163+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "name", "tf-tags-test"),
164+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "tags.#", "2"),
165+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "tags.0", "tag1"),
166+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "tags.1", "tag2"),
123167

124168
acctest.CheckResourceAttrUUID("scaleway_container_namespace.main", "id"),
125169
),

0 commit comments

Comments
 (0)