Skip to content

Commit c05d5e1

Browse files
committed
feat(container): add vpc integration to namespace resource
1 parent e454232 commit c05d5e1

File tree

4 files changed

+2993
-0
lines changed

4 files changed

+2993
-0
lines changed

docs/resources/container_namespace.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ The following arguments are supported:
4040

4141
- `secret_environment_variables` - (Optional) The secret environment variables of the namespace.
4242

43+
- `activate_vpc_integration` - (Optional) Activates VPC integration for the namespace. Containers of a namespace with VPC integration activated will be able to connect to a Private Network.
44+
45+
~> **Important** Updates to `activate_vpc_integration` will recreate the namespace.
46+
4347
## Attributes Reference
4448

4549
The `scaleway_container_namespace` resource exports certain attributes once the Containers namespace has been created. These attributes can be referenced in other parts of your Terraform configuration.

internal/services/container/namespace.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ func ResourceNamespace() *schema.Resource {
9595
Description: "Destroy registry on deletion",
9696
Deprecated: "Registry namespace is automatically destroyed with namespace",
9797
},
98+
"activate_vpc_integration": {
99+
Type: schema.TypeBool,
100+
ForceNew: true,
101+
Optional: true,
102+
Default: false,
103+
Description: "Activate VPC integration for the namespace",
104+
},
98105
"region": regional.Schema(),
99106
"organization_id": account.OrganizationIDSchema(),
100107
"project_id": account.ProjectIDSchema(),
@@ -122,6 +129,10 @@ func ResourceContainerNamespaceCreate(ctx context.Context, d *schema.ResourceDat
122129
createReq.Tags = types.ExpandStrings(rawTag)
123130
}
124131

132+
if activateVPC, ok := d.GetOk("activate_vpc_integration"); ok {
133+
createReq.ActivateVpcIntegration = activateVPC.(bool)
134+
}
135+
125136
ns, err := api.CreateNamespace(createReq, scw.WithContext(ctx))
126137
if err != nil {
127138
return diag.FromErr(err)
@@ -164,6 +175,7 @@ func ResourceContainerNamespaceRead(ctx context.Context, d *schema.ResourceData,
164175
_ = d.Set("registry_endpoint", ns.RegistryEndpoint)
165176
_ = d.Set("registry_namespace_id", ns.RegistryNamespaceID)
166177
_ = d.Set("secret_environment_variables", flattenContainerSecrets(ns.SecretEnvironmentVariables))
178+
_ = d.Set("activate_vpc_integration", types.FlattenBoolPtr(ns.VpcIntegrationActivated))
167179

168180
return nil
169181
}

internal/services/container/namespace_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package container_test
22

33
import (
44
"fmt"
5+
vpcchecks "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/vpc/testfuncs"
6+
"regexp"
57
"testing"
68

79
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -264,6 +266,77 @@ func TestAccNamespace_DestroyRegistry(t *testing.T) {
264266
},
265267
})
266268
}
269+
func TestAccNamespace_VPCIntegration(t *testing.T) {
270+
tt := acctest.NewTestTools(t)
271+
defer tt.Cleanup()
272+
273+
namespaceID := ""
274+
275+
resource.ParallelTest(t, resource.TestCase{
276+
PreCheck: func() { acctest.PreCheck(t) },
277+
ProviderFactories: tt.ProviderFactories,
278+
CheckDestroy: resource.ComposeTestCheckFunc(
279+
isNamespaceDestroyed(tt),
280+
isContainerDestroyed(tt),
281+
vpcchecks.CheckPrivateNetworkDestroy(tt),
282+
),
283+
Steps: []resource.TestStep{
284+
{
285+
Config: `
286+
resource scaleway_vpc_private_network main {}
287+
288+
resource scaleway_container_namespace main {}
289+
290+
resource scaleway_container main {
291+
namespace_id = scaleway_container_namespace.main.id
292+
sandbox = "v1"
293+
}
294+
`,
295+
Check: resource.ComposeTestCheckFunc(
296+
isNamespacePresent(tt, "scaleway_container_namespace.main"),
297+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "activate_vpc_integration", "false"),
298+
acctest.CheckResourceIDPersisted("scaleway_container_namespace.main", &namespaceID),
299+
),
300+
},
301+
{
302+
Config: `
303+
resource scaleway_vpc_private_network main {}
304+
305+
resource scaleway_container_namespace main {}
306+
307+
resource scaleway_container main {
308+
namespace_id = scaleway_container_namespace.main.id
309+
private_network_id = scaleway_vpc_private_network.main.id
310+
sandbox = "v1"
311+
}
312+
`,
313+
ExpectError: regexp.MustCompile("Application can't be attached to private network, vpc integration must be activated on its parent namespace"),
314+
},
315+
{
316+
Config: `
317+
resource scaleway_vpc_private_network main {}
318+
319+
resource scaleway_container_namespace main {
320+
activate_vpc_integration = true
321+
}
322+
323+
resource scaleway_container main {
324+
namespace_id = scaleway_container_namespace.main.id
325+
private_network_id = scaleway_vpc_private_network.main.id
326+
sandbox = "v1"
327+
}
328+
`,
329+
Check: resource.ComposeTestCheckFunc(
330+
isNamespacePresent(tt, "scaleway_container_namespace.main"),
331+
isContainerPresent(tt, "scaleway_container.main"),
332+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "activate_vpc_integration", "true"),
333+
resource.TestCheckResourceAttrPair("scaleway_container.main", "private_network_id", "scaleway_vpc_private_network.main", "id"),
334+
acctest.CheckResourceIDChanged("scaleway_container_namespace.main", &namespaceID),
335+
),
336+
},
337+
},
338+
})
339+
}
267340

268341
func isNamespacePresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
269342
return func(state *terraform.State) error {

0 commit comments

Comments
 (0)