Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(serverless): add sandbox field #2657

Merged
merged 4 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/data-sources/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ In addition to all arguments above, the following attributes are exported:

- `deploy` - Boolean indicating whether the container is on a production environment.

- `sandbox` - (Optional) Execution environment of the container.

- `status` - The container status.

- `cron_status` - The cron status of the container.
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ The following arguments are optional:

- `http_option` - (Optional) Allow both HTTP and HTTPS (enabled) or redirect HTTP to HTTPS (redirected). Defaults to enabled.

- `sandbox` - (Optional) Execution environment of the container.

- `port` - (Optional) The port to expose the container. Defaults to 8080.

- `deploy` - (Optional) Boolean controlling whether the container is on a production environment.
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/function.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ The following arguments are supported:

- `deploy` - Define if the function should be deployed, terraform will wait for function to be deployed. Function will get deployed if you change source zip

- `sandbox` - (Optional) Execution environment of the function.

- `region` - (Defaults to [provider](../index.md#region) `region`). The [region](../guides/regions_and_zones.md#regions) in which the namespace should be created.

- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the namespace is associated with.
Expand Down
12 changes: 12 additions & 0 deletions internal/services/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ func ResourceContainer() *schema.Resource {
Default: container.ContainerHTTPOptionEnabled.String(),
ValidateDiagFunc: verify.ValidateEnum[container.ContainerHTTPOption](),
},
"sandbox": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Execution environment of the container.",
ValidateDiagFunc: verify.ValidateEnum[container.ContainerSandbox](),
},
// computed
"status": {
Type: schema.TypeString,
Expand Down Expand Up @@ -272,6 +279,7 @@ func ResourceContainerRead(ctx context.Context, d *schema.ResourceData, m interf
_ = d.Set("port", int(co.Port))
_ = d.Set("deploy", scw.BoolPtr(*types.ExpandBoolPtr(d.Get("deploy"))))
_ = d.Set("http_option", co.HTTPOption)
_ = d.Set("sandbox", co.Sandbox)
_ = d.Set("region", co.Region.String())

return nil
Expand Down Expand Up @@ -363,6 +371,10 @@ func ResourceContainerUpdate(ctx context.Context, d *schema.ResourceData, m inte
req.Redeploy = types.ExpandBoolPtr(d.Get("deploy"))
}

if d.HasChanges("sandbox") {
req.Sandbox = container.ContainerSandbox(d.Get("sandbox").(string))
}

imageHasChanged := d.HasChanges("registry_sha256")
if imageHasChanged {
req.Redeploy = &imageHasChanged
Expand Down
70 changes: 70 additions & 0 deletions internal/services/container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,76 @@ func TestAccContainer_HTTPOption(t *testing.T) {
})
}

func TestAccContainer_Sandbox(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: isContainerDestroyed(tt),
Steps: []resource.TestStep{
{
Config: `
resource scaleway_container_namespace main {}

resource scaleway_container main {
namespace_id = scaleway_container_namespace.main.id
deploy = false
}
`,
Check: resource.ComposeTestCheckFunc(
isContainerPresent(tt, "scaleway_container.main"),
resource.TestCheckResourceAttrSet("scaleway_container.main", "sandbox"),
),
},
{
Config: `
resource scaleway_container_namespace main {}

resource scaleway_container main {
namespace_id = scaleway_container_namespace.main.id
deploy = false
sandbox = "v2"
}
`,
Check: resource.ComposeTestCheckFunc(
isContainerPresent(tt, "scaleway_container.main"),
resource.TestCheckResourceAttr("scaleway_container.main", "sandbox", containerSDK.ContainerSandboxV2.String()),
),
},
{
Config: `
resource scaleway_container_namespace main {}

resource scaleway_container main {
namespace_id = scaleway_container_namespace.main.id
deploy = false
sandbox = "v1"
}
`,
Check: resource.ComposeTestCheckFunc(
isContainerPresent(tt, "scaleway_container.main"),
resource.TestCheckResourceAttr("scaleway_container.main", "sandbox", containerSDK.ContainerSandboxV1.String()),
),
},
{
Config: `
resource scaleway_container_namespace main {}

resource scaleway_container main {
namespace_id = scaleway_container_namespace.main.id
sandbox = "v2"
}
`,
Check: resource.ComposeTestCheckFunc(
isContainerPresent(tt, "scaleway_container.main"),
resource.TestCheckResourceAttr("scaleway_container.main", "sandbox", containerSDK.ContainerSandboxV2.String()),
),
},
},
})
}

func isContainerPresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
return func(state *terraform.State) error {
rs, ok := state.RootModule().Resources[n]
Expand Down
4 changes: 4 additions & 0 deletions internal/services/container/helpers_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func setCreateContainerRequest(d *schema.ResourceData, region scw.Region) (*cont
req.MaxConcurrency = scw.Uint32Ptr(uint32(maxConcurrency.(int)))
}

if sandbox, ok := d.GetOk("sandbox"); ok {
req.Sandbox = container.ContainerSandbox(sandbox.(string))
}

return req, nil
}

Expand Down
Loading
Loading