Skip to content

Commit 95b3dc6

Browse files
committed
wip container resource
1 parent e5db261 commit 95b3dc6

File tree

5 files changed

+217
-1
lines changed

5 files changed

+217
-1
lines changed

docs/resources/container.md

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

112112
- `args` - (Optional) Arguments passed to the command specified in the "command" field. These override the default arguments from the container image, and behave like command-line parameters.
113113

114+
- `private_network_id` (Optional) The ID of the Private Network the container is connected to.
115+
116+
~> **Important** This feature is currently in beta and requires a namespace with VPC integration activated by setting the `activate_vpc_integration` attribute to `true`.
117+
114118
Note that if you want to use your own configuration, you must consult our configuration [restrictions](https://www.scaleway.com/en/docs/serverless-containers/reference-content/containers-limitations/#configuration-restrictions) section.
115119

116120
## Attributes Reference

internal/services/container/container.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func ResourceContainer() *schema.Resource {
5353
"namespace_id": {
5454
Type: schema.TypeString,
5555
Required: true,
56+
ForceNew: true,
5657
Description: "The container namespace associated",
5758
},
5859
"tags": {
@@ -263,6 +264,11 @@ func ResourceContainer() *schema.Resource {
263264
Optional: true,
264265
Description: "Arguments passed to the command from the command \"field\". Overrides the arguments from the container image.",
265266
},
267+
"private_network_id": {
268+
Type: schema.TypeString,
269+
Optional: true,
270+
Description: "ID of the Private Network the container is connected to",
271+
},
266272
// computed
267273
"status": {
268274
Type: schema.TypeString,
@@ -385,6 +391,10 @@ func ResourceContainerRead(ctx context.Context, d *schema.ResourceData, m interf
385391
_ = d.Set("command", types.FlattenSliceString(co.Command))
386392
_ = d.Set("args", types.FlattenSliceString(co.Args))
387393

394+
if co.PrivateNetworkID != nil {
395+
_ = d.Set("private_network_id", regional.NewID(region, types.FlattenStringPtr(co.PrivateNetworkID).(string)).String())
396+
}
397+
388398
return nil
389399
}
390400

internal/services/container/container_test.go

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1414
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/container"
1515
containerchecks "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/container/testfuncs"
16+
vpcchecks "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/vpc/testfuncs"
1617
)
1718

1819
func TestAccContainer_Basic(t *testing.T) {
@@ -645,6 +646,195 @@ func TestAccContainer_CommandAndArgs(t *testing.T) {
645646
})
646647
}
647648

649+
func TestAccContainer_PrivateNetwork(t *testing.T) {
650+
tt := acctest.NewTestTools(t)
651+
defer tt.Cleanup()
652+
resource.ParallelTest(t, resource.TestCase{
653+
PreCheck: func() { acctest.PreCheck(t) },
654+
ProviderFactories: tt.ProviderFactories,
655+
CheckDestroy: resource.ComposeTestCheckFunc(
656+
isNamespaceDestroyed(tt),
657+
isContainerDestroyed(tt),
658+
vpcchecks.CheckPrivateNetworkDestroy(tt),
659+
),
660+
Steps: []resource.TestStep{
661+
{
662+
Config: `
663+
resource scaleway_vpc_private_network pn00 {}
664+
resource scaleway_vpc_private_network pn01 {}
665+
666+
resource scaleway_container_namespace main {
667+
activate_vpc_integration = true
668+
}
669+
670+
resource scaleway_container c00 {
671+
namespace_id = scaleway_container_namespace.main.id
672+
private_network_id = scaleway_vpc_private_network.pn00.id
673+
sandbox = "v1"
674+
}
675+
`,
676+
Check: resource.ComposeTestCheckFunc(
677+
isContainerPresent(tt, "scaleway_container.c00"),
678+
resource.TestCheckResourceAttr("scaleway_container_namespace.main", "activate_vpc_integration", "true"),
679+
resource.TestCheckResourceAttr("scaleway_container.c00", "sandbox", "v1"),
680+
resource.TestCheckResourceAttrPair("scaleway_container.c00", "private_network_id", "scaleway_vpc_private_network.pn00", "id"),
681+
),
682+
},
683+
//{
684+
// Config: `
685+
// resource scaleway_vpc_private_network pn00 {}
686+
// resource scaleway_vpc_private_network pn01 {}
687+
//
688+
// resource scaleway_container_namespace main {
689+
// activate_vpc_integration = true
690+
// }
691+
//
692+
// resource scaleway_container c00 {
693+
// namespace_id = scaleway_container_namespace.main.id
694+
// private_network_id = scaleway_vpc_private_network.pn01.id
695+
// sandbox = "v1"
696+
// }
697+
// `,
698+
// Check: resource.ComposeTestCheckFunc(
699+
// isContainerPresent(tt, "scaleway_container.c00"),
700+
// resource.TestCheckResourceAttr("scaleway_container.c00", "sandbox", "v1"),
701+
// resource.TestCheckResourceAttrPair("scaleway_container.c00", "private_network_id", "scaleway_vpc_private_network.pn01", "id"),
702+
// ),
703+
//},
704+
//{
705+
// Config: `
706+
// resource scaleway_vpc_private_network pn00 {}
707+
// resource scaleway_vpc_private_network pn01 {}
708+
//
709+
// resource scaleway_container_namespace main {
710+
// activate_vpc_integration = true
711+
// }
712+
//
713+
// resource scaleway_container c00 {
714+
// namespace_id = scaleway_container_namespace.main.id
715+
// sandbox = "v1"
716+
// }
717+
// `,
718+
// Check: resource.ComposeTestCheckFunc(
719+
// isContainerPresent(tt, "scaleway_container.c00"),
720+
// resource.TestCheckResourceAttr("scaleway_container.c00", "private_network_id", ""),
721+
// ),
722+
//},
723+
},
724+
})
725+
}
726+
727+
// func TestAccContainer_PrivateNetwork(t *testing.T) {
728+
// tt := acctest.NewTestTools(t)
729+
// defer tt.Cleanup()
730+
// resource.ParallelTest(t, resource.TestCase{
731+
// PreCheck: func() { acctest.PreCheck(t) },
732+
// ProviderFactories: tt.ProviderFactories,
733+
// CheckDestroy: resource.ComposeTestCheckFunc(
734+
// isNamespaceDestroyed(tt),
735+
// isContainerDestroyed(tt),
736+
// vpcchecks.CheckPrivateNetworkDestroy(tt),
737+
// ),
738+
// Steps: []resource.TestStep{
739+
// {
740+
// Config: `
741+
// resource scaleway_vpc_private_network pn00 {}
742+
// resource scaleway_vpc_private_network pn01 {}
743+
//
744+
// resource scaleway_container_namespace main {
745+
// activate_vpc_integration = true
746+
// }
747+
//
748+
// resource scaleway_container c00 {
749+
// namespace_id = scaleway_container_namespace.main.id
750+
// private_network_id = scaleway_vpc_private_network.pn00.id
751+
// sandbox = "v1"
752+
// }
753+
// `,
754+
// Check: resource.ComposeTestCheckFunc(
755+
// isContainerPresent(tt, "scaleway_container.c00"),
756+
// resource.TestCheckResourceAttr("scaleway_container_namespace.main", "activate_vpc_integration", "true"),
757+
// resource.TestCheckResourceAttr("scaleway_container.c00", "sandbox", "v1"),
758+
// resource.TestCheckResourceAttrPair("scaleway_container.c00", "private_network_id", "scaleway_vpc_private_network.pn00", "id"),
759+
// ),
760+
// },
761+
// {
762+
// Config: `
763+
// resource scaleway_vpc_private_network pn00 {}
764+
// resource scaleway_vpc_private_network pn01 {}
765+
//
766+
// resource scaleway_container_namespace main {
767+
// activate_vpc_integration = true
768+
// }
769+
//
770+
// resource scaleway_container c00 {
771+
// namespace_id = scaleway_container_namespace.main.id
772+
// private_network_id = scaleway_vpc_private_network.pn00.id
773+
// sandbox = "v1"
774+
// }
775+
//
776+
// resource scaleway_container c01 {
777+
// namespace_id = scaleway_container_namespace.main.id
778+
// private_network_id = scaleway_vpc_private_network.pn00.id
779+
// sandbox = "v1"
780+
// }
781+
//
782+
// resource scaleway_container c02 {
783+
// namespace_id = scaleway_container_namespace.main.id
784+
// private_network_id = scaleway_vpc_private_network.pn00.id
785+
// sandbox = "v1"
786+
// }
787+
// `,
788+
// Check: resource.ComposeTestCheckFunc(
789+
// isContainerPresent(tt, "scaleway_container.c00"),
790+
// isContainerPresent(tt, "scaleway_container.c01"),
791+
// isContainerPresent(tt, "scaleway_container.c02"),
792+
// resource.TestCheckResourceAttr("scaleway_container.c00", "sandbox", "v1"),
793+
// resource.TestCheckResourceAttr("scaleway_container.c01", "sandbox", "v1"),
794+
// resource.TestCheckResourceAttr("scaleway_container.c02", "sandbox", "v1"),
795+
// resource.TestCheckResourceAttrPair("scaleway_container.c00", "private_network_id", "scaleway_vpc_private_network.pn00", "id"),
796+
// resource.TestCheckResourceAttrPair("scaleway_container.c01", "private_network_id", "scaleway_vpc_private_network.pn00", "id"),
797+
// resource.TestCheckResourceAttrPair("scaleway_container.c02", "private_network_id", "scaleway_vpc_private_network.pn00", "id"),
798+
// ),
799+
// }, {
800+
// Config: `
801+
// resource scaleway_vpc_private_network pn00 {}
802+
// resource scaleway_vpc_private_network pn01 {}
803+
//
804+
// resource scaleway_container_namespace main {
805+
// activate_vpc_integration = true
806+
// }
807+
//
808+
// resource scaleway_container c00 {
809+
// namespace_id = scaleway_container_namespace.main.id
810+
// sandbox = "v1"
811+
// }
812+
//
813+
// resource scaleway_container c01 {
814+
// namespace_id = scaleway_container_namespace.main.id
815+
// private_network_id = scaleway_vpc_private_network.pn01.id
816+
// sandbox = "v1"
817+
// }
818+
//
819+
// resource scaleway_container c02 {
820+
// namespace_id = scaleway_container_namespace.main.id
821+
// private_network_id = scaleway_vpc_private_network.pn00.id
822+
// sandbox = "v1"
823+
// }
824+
// `,
825+
// Check: resource.ComposeTestCheckFunc(
826+
// isContainerPresent(tt, "scaleway_container.c00"),
827+
// isContainerPresent(tt, "scaleway_container.c01"),
828+
// isContainerPresent(tt, "scaleway_container.c02"),
829+
// resource.TestCheckResourceAttr("scaleway_container.c00", "private_network_id", ""),
830+
// resource.TestCheckResourceAttrPair("scaleway_container.c01", "private_network_id", "scaleway_vpc_private_network.pn01", "id"),
831+
// resource.TestCheckResourceAttrPair("scaleway_container.c02", "private_network_id", "scaleway_vpc_private_network.pn00", "id"),
832+
// ),
833+
// },
834+
// },
835+
// })
836+
// }
837+
648838
func isContainerPresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
649839
return func(state *terraform.State) error {
650840
rs, ok := state.RootModule().Resources[n]

internal/services/container/helpers_container.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ func setCreateContainerRequest(d *schema.ResourceData, region scw.Region) (*cont
152152
req.Args = types.ExpandStrings(args)
153153
}
154154

155+
if pnID, ok := d.GetOk("private_network_id"); ok {
156+
req.PrivateNetworkID = types.ExpandStringPtr(locality.ExpandID(pnID.(string)))
157+
}
158+
155159
return req, nil
156160
}
157161

@@ -270,6 +274,14 @@ func setUpdateContainerRequest(d *schema.ResourceData, region scw.Region, contai
270274
req.Args = types.ExpandUpdatedStringsPtr(d.Get("args"))
271275
}
272276

277+
if d.HasChanges("private_network_id") {
278+
if _, newPNID := d.GetChange("private_network_id"); newPNID != nil && newPNID.(string) != "" {
279+
req.PrivateNetworkID = types.ExpandUpdatedStringPtr(locality.ExpandID(newPNID.(string)))
280+
} else {
281+
req.PrivateNetworkID = nil
282+
}
283+
}
284+
273285
return req, nil
274286
}
275287

internal/services/container/namespace_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package container_test
22

33
import (
44
"fmt"
5-
vpcchecks "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/vpc/testfuncs"
65
"regexp"
76
"testing"
87

@@ -14,6 +13,7 @@ import (
1413
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1514
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/container"
1615
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/registry"
16+
vpcchecks "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/vpc/testfuncs"
1717
)
1818

1919
const containerNamespaceResource = "scaleway_container_namespace"

0 commit comments

Comments
 (0)