Skip to content

Commit ff8cc42

Browse files
authored
feat(container): deploy container after creation (#3204)
1 parent b786eda commit ff8cc42

File tree

6 files changed

+323
-0
lines changed

6 files changed

+323
-0
lines changed

cmd/scw/testdata/test-all-usage-container-container-create-usage.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ ARGS:
2424
[secret-environment-variables.{index}.key]
2525
[secret-environment-variables.{index}.value]
2626
[http-option=enabled] Configure how HTTP and HTTPS requests are handled (unknown_http_option | enabled | redirected)
27+
[deploy=true] Deploy container after creation
2728
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw)
2829

2930
FLAGS:

docs/commands/container.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ scw container container create [arg=value ...]
7373
| secret-environment-variables.{index}.key | | |
7474
| secret-environment-variables.{index}.value | | |
7575
| http-option | Default: `enabled`<br />One of: `unknown_http_option`, `enabled`, `redirected` | Configure how HTTP and HTTPS requests are handled |
76+
| deploy | Default: `true` | Deploy container after creation |
7677
| region | Default: `fr-par`<br />One of: `fr-par`, `nl-ams`, `pl-waw` | Region to target. If none is passed will use default region from the config |
7778

7879

internal/namespaces/container/v1beta1/custom_container.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package container
22

33
import (
44
"context"
5+
"reflect"
56
"time"
67

78
"github.com/fatih/color"
@@ -45,6 +46,37 @@ func containerContainerDeployBuilder(command *core.Command) *core.Command {
4546
}
4647

4748
func containerContainerCreateBuilder(command *core.Command) *core.Command {
49+
// Add an interceptor that will deploy container after it was created
50+
type CustomCreateContainerRequest struct {
51+
*container.CreateContainerRequest
52+
Deploy bool `json:"deploy"`
53+
}
54+
55+
command.ArgSpecs.AddBefore("region", &core.ArgSpec{
56+
Name: "deploy",
57+
Short: "Deploy container after creation",
58+
Required: false,
59+
Default: core.DefaultValueSetter("true"),
60+
})
61+
62+
command.ArgsType = reflect.TypeOf(CustomCreateContainerRequest{})
63+
64+
command.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
65+
args := argsI.(*CustomCreateContainerRequest)
66+
resI, err := runner(ctx, args.CreateContainerRequest)
67+
if err != nil {
68+
return resI, err
69+
}
70+
71+
c := resI.(*container.Container)
72+
containerAPI := container.NewAPI(core.ExtractClient(ctx))
73+
74+
return containerAPI.DeployContainer(&container.DeployContainerRequest{
75+
Region: c.Region,
76+
ContainerID: c.ID,
77+
}, scw.WithContext(ctx))
78+
})
79+
4880
command.WaitFunc = waitForContainer
4981
return command
5082
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package container
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/alecthomas/assert"
8+
"github.com/scaleway/scaleway-cli/v2/internal/core"
9+
container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
10+
)
11+
12+
func createNamespace(metaKey string) core.BeforeFunc {
13+
return core.ExecStoreBeforeCmd(metaKey, "scw container namespace create -w")
14+
}
15+
16+
func deleteNamespace(metaKey string) core.AfterFunc {
17+
return func(ctx *core.AfterFuncCtx) error {
18+
return core.ExecAfterCmd("scw container namespace delete {{ ." + metaKey + ".ID }}")(ctx)
19+
}
20+
}
21+
22+
func Test_Create(t *testing.T) {
23+
commands := GetCommands()
24+
25+
t.Run("Simple", core.Test(&core.TestConfig{
26+
Commands: commands,
27+
BeforeFunc: core.BeforeFuncCombine(
28+
createNamespace("Namespace"),
29+
),
30+
Cmd: fmt.Sprintf("scw container container create namespace-id={{ .Namespace.ID }} name=%s deploy=true", core.GetRandomName("test")),
31+
Check: core.TestCheckCombine(
32+
func(t *testing.T, ctx *core.CheckFuncCtx) {
33+
c := ctx.Result.(*container.Container)
34+
assert.Equal(t, container.ContainerStatusPending, c.Status)
35+
},
36+
core.TestCheckExitCode(0),
37+
core.TestCheckGolden(),
38+
),
39+
AfterFunc: core.AfterFuncCombine(
40+
deleteNamespace("Namespace"),
41+
),
42+
DisableParallel: true,
43+
}))
44+
}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: '{"id":"550f7065-ee73-4399-a32b-49b179950b57","name":"cli-cns-silly-shirley","environment_variables":{},"organization_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","project_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","status":"pending","registry_namespace_id":"","error_message":null,"registry_endpoint":"","description":"","secret_environment_variables":[],"region":"fr-par"}'
6+
form: {}
7+
headers:
8+
Content-Type:
9+
- application/json
10+
User-Agent:
11+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
12+
url: https://api.scaleway.com/containers/v1beta1/regions/fr-par/namespaces
13+
method: POST
14+
response:
15+
body: '{"id":"550f7065-ee73-4399-a32b-49b179950b57","name":"cli-cns-silly-shirley","environment_variables":{},"organization_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","project_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","status":"pending","registry_namespace_id":"","error_message":null,"registry_endpoint":"","description":"","secret_environment_variables":[],"region":"fr-par"}'
16+
headers:
17+
Content-Length:
18+
- "371"
19+
Content-Security-Policy:
20+
- default-src 'none'; frame-ancestors 'none'
21+
Content-Type:
22+
- application/json
23+
Date:
24+
- Tue, 13 Jun 2023 13:04:17 GMT
25+
Server:
26+
- Scaleway API-Gateway
27+
Strict-Transport-Security:
28+
- max-age=63072000
29+
X-Content-Type-Options:
30+
- nosniff
31+
X-Frame-Options:
32+
- DENY
33+
X-Request-Id:
34+
- deffd039-a6a2-4f09-ace8-0ffd84edd7e9
35+
status: 200 OK
36+
code: 200
37+
duration: ""
38+
- request:
39+
body: '{"id":"550f7065-ee73-4399-a32b-49b179950b57","name":"cli-cns-silly-shirley","environment_variables":{},"organization_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","project_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","status":"pending","registry_namespace_id":"","error_message":null,"registry_endpoint":"","description":"","secret_environment_variables":[],"region":"fr-par"}'
40+
form: {}
41+
headers:
42+
User-Agent:
43+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
44+
url: https://api.scaleway.com/containers/v1beta1/regions/fr-par/namespaces/550f7065-ee73-4399-a32b-49b179950b57
45+
method: GET
46+
response:
47+
body: '{"id":"550f7065-ee73-4399-a32b-49b179950b57","name":"cli-cns-silly-shirley","environment_variables":{},"organization_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","project_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","status":"pending","registry_namespace_id":"","error_message":null,"registry_endpoint":"","description":"","secret_environment_variables":[],"region":"fr-par"}'
48+
headers:
49+
Content-Length:
50+
- "371"
51+
Content-Security-Policy:
52+
- default-src 'none'; frame-ancestors 'none'
53+
Content-Type:
54+
- application/json
55+
Date:
56+
- Tue, 13 Jun 2023 13:04:17 GMT
57+
Server:
58+
- Scaleway API-Gateway
59+
Strict-Transport-Security:
60+
- max-age=63072000
61+
X-Content-Type-Options:
62+
- nosniff
63+
X-Frame-Options:
64+
- DENY
65+
X-Request-Id:
66+
- 0a4495de-a6a4-4758-a1d1-4e719ad70440
67+
status: 200 OK
68+
code: 200
69+
duration: ""
70+
- request:
71+
body: '{"id":"550f7065-ee73-4399-a32b-49b179950b57","name":"cli-cns-silly-shirley","environment_variables":{},"organization_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","project_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","status":"ready","registry_namespace_id":"27885dea-fa5e-455b-b90b-2c67553a344b","error_message":null,"registry_endpoint":"rg.fr-par.scw.cloud/funcscwclicnssillyshirleyusnp0hk3","description":"","secret_environment_variables":[],"region":"fr-par"}'
72+
form: {}
73+
headers:
74+
User-Agent:
75+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
76+
url: https://api.scaleway.com/containers/v1beta1/regions/fr-par/namespaces/550f7065-ee73-4399-a32b-49b179950b57
77+
method: GET
78+
response:
79+
body: '{"id":"550f7065-ee73-4399-a32b-49b179950b57","name":"cli-cns-silly-shirley","environment_variables":{},"organization_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","project_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","status":"ready","registry_namespace_id":"27885dea-fa5e-455b-b90b-2c67553a344b","error_message":null,"registry_endpoint":"rg.fr-par.scw.cloud/funcscwclicnssillyshirleyusnp0hk3","description":"","secret_environment_variables":[],"region":"fr-par"}'
80+
headers:
81+
Content-Length:
82+
- "458"
83+
Content-Security-Policy:
84+
- default-src 'none'; frame-ancestors 'none'
85+
Content-Type:
86+
- application/json
87+
Date:
88+
- Tue, 13 Jun 2023 13:04:23 GMT
89+
Server:
90+
- Scaleway API-Gateway
91+
Strict-Transport-Security:
92+
- max-age=63072000
93+
X-Content-Type-Options:
94+
- nosniff
95+
X-Frame-Options:
96+
- DENY
97+
X-Request-Id:
98+
- 989d3d6a-2cd2-4ac9-843c-7935b5b90cef
99+
status: 200 OK
100+
code: 200
101+
duration: ""
102+
- request:
103+
body: '{"id":"eff8830f-5f3b-4e42-a2f0-8e4a77ec4cb0","name":"cli-test-kind-herschel","namespace_id":"550f7065-ee73-4399-a32b-49b179950b57","status":"created","environment_variables":{},"min_scale":0,"max_scale":5,"memory_limit":256,"cpu_limit":140,"timeout":"300s","error_message":null,"privacy":"public","description":"","registry_image":"rg.fr-par.scw.cloud/funcscwclicnssillyshirleyusnp0hk3/cli-test-kind-herschel:latest","max_concurrency":50,"domain_name":"clicnssillyshirleyusnp0hk3-cli-test-kind-herschel.functions.fnc.fr-par.scw.cloud","protocol":"http1","port":8080,"secret_environment_variables":[],"http_option":"enabled","region":"fr-par"}'
104+
form: {}
105+
headers:
106+
Content-Type:
107+
- application/json
108+
User-Agent:
109+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
110+
url: https://api.scaleway.com/containers/v1beta1/regions/fr-par/containers
111+
method: POST
112+
response:
113+
body: '{"id":"eff8830f-5f3b-4e42-a2f0-8e4a77ec4cb0","name":"cli-test-kind-herschel","namespace_id":"550f7065-ee73-4399-a32b-49b179950b57","status":"created","environment_variables":{},"min_scale":0,"max_scale":5,"memory_limit":256,"cpu_limit":140,"timeout":"300s","error_message":null,"privacy":"public","description":"","registry_image":"rg.fr-par.scw.cloud/funcscwclicnssillyshirleyusnp0hk3/cli-test-kind-herschel:latest","max_concurrency":50,"domain_name":"clicnssillyshirleyusnp0hk3-cli-test-kind-herschel.functions.fnc.fr-par.scw.cloud","protocol":"http1","port":8080,"secret_environment_variables":[],"http_option":"enabled","region":"fr-par"}'
114+
headers:
115+
Content-Length:
116+
- "642"
117+
Content-Security-Policy:
118+
- default-src 'none'; frame-ancestors 'none'
119+
Content-Type:
120+
- application/json
121+
Date:
122+
- Tue, 13 Jun 2023 13:04:23 GMT
123+
Server:
124+
- Scaleway API-Gateway
125+
Strict-Transport-Security:
126+
- max-age=63072000
127+
X-Content-Type-Options:
128+
- nosniff
129+
X-Frame-Options:
130+
- DENY
131+
X-Request-Id:
132+
- 7eb50852-2517-423d-adb5-5ed6f925945e
133+
status: 200 OK
134+
code: 200
135+
duration: ""
136+
- request:
137+
body: '{"id":"eff8830f-5f3b-4e42-a2f0-8e4a77ec4cb0","name":"cli-test-kind-herschel","namespace_id":"550f7065-ee73-4399-a32b-49b179950b57","status":"pending","environment_variables":{},"min_scale":0,"max_scale":5,"memory_limit":256,"cpu_limit":140,"timeout":"300s","error_message":null,"privacy":"public","description":"","registry_image":"rg.fr-par.scw.cloud/funcscwclicnssillyshirleyusnp0hk3/cli-test-kind-herschel:latest","max_concurrency":50,"domain_name":"clicnssillyshirleyusnp0hk3-cli-test-kind-herschel.functions.fnc.fr-par.scw.cloud","protocol":"http1","port":8080,"secret_environment_variables":[],"http_option":"enabled","region":"fr-par"}'
138+
form: {}
139+
headers:
140+
Content-Type:
141+
- application/json
142+
User-Agent:
143+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
144+
url: https://api.scaleway.com/containers/v1beta1/regions/fr-par/containers/eff8830f-5f3b-4e42-a2f0-8e4a77ec4cb0/deploy
145+
method: POST
146+
response:
147+
body: '{"id":"eff8830f-5f3b-4e42-a2f0-8e4a77ec4cb0","name":"cli-test-kind-herschel","namespace_id":"550f7065-ee73-4399-a32b-49b179950b57","status":"pending","environment_variables":{},"min_scale":0,"max_scale":5,"memory_limit":256,"cpu_limit":140,"timeout":"300s","error_message":null,"privacy":"public","description":"","registry_image":"rg.fr-par.scw.cloud/funcscwclicnssillyshirleyusnp0hk3/cli-test-kind-herschel:latest","max_concurrency":50,"domain_name":"clicnssillyshirleyusnp0hk3-cli-test-kind-herschel.functions.fnc.fr-par.scw.cloud","protocol":"http1","port":8080,"secret_environment_variables":[],"http_option":"enabled","region":"fr-par"}'
148+
headers:
149+
Content-Length:
150+
- "642"
151+
Content-Security-Policy:
152+
- default-src 'none'; frame-ancestors 'none'
153+
Content-Type:
154+
- application/json
155+
Date:
156+
- Tue, 13 Jun 2023 13:04:23 GMT
157+
Server:
158+
- Scaleway API-Gateway
159+
Strict-Transport-Security:
160+
- max-age=63072000
161+
X-Content-Type-Options:
162+
- nosniff
163+
X-Frame-Options:
164+
- DENY
165+
X-Request-Id:
166+
- 46c4743b-6f0b-4e4f-8ce6-e96863efce7e
167+
status: 200 OK
168+
code: 200
169+
duration: ""
170+
- request:
171+
body: '{"id":"550f7065-ee73-4399-a32b-49b179950b57","name":"cli-cns-silly-shirley","environment_variables":{},"organization_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","project_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","status":"deleting","registry_namespace_id":"27885dea-fa5e-455b-b90b-2c67553a344b","error_message":null,"registry_endpoint":"rg.fr-par.scw.cloud/funcscwclicnssillyshirleyusnp0hk3","description":"","secret_environment_variables":[],"region":"fr-par"}'
172+
form: {}
173+
headers:
174+
User-Agent:
175+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
176+
url: https://api.scaleway.com/containers/v1beta1/regions/fr-par/namespaces/550f7065-ee73-4399-a32b-49b179950b57
177+
method: DELETE
178+
response:
179+
body: '{"id":"550f7065-ee73-4399-a32b-49b179950b57","name":"cli-cns-silly-shirley","environment_variables":{},"organization_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","project_id":"ee7bd9e1-9cbd-4724-b2f4-19e50f3cf38b","status":"deleting","registry_namespace_id":"27885dea-fa5e-455b-b90b-2c67553a344b","error_message":null,"registry_endpoint":"rg.fr-par.scw.cloud/funcscwclicnssillyshirleyusnp0hk3","description":"","secret_environment_variables":[],"region":"fr-par"}'
180+
headers:
181+
Content-Length:
182+
- "461"
183+
Content-Security-Policy:
184+
- default-src 'none'; frame-ancestors 'none'
185+
Content-Type:
186+
- application/json
187+
Date:
188+
- Tue, 13 Jun 2023 13:04:23 GMT
189+
Server:
190+
- Scaleway API-Gateway
191+
Strict-Transport-Security:
192+
- max-age=63072000
193+
X-Content-Type-Options:
194+
- nosniff
195+
X-Frame-Options:
196+
- DENY
197+
X-Request-Id:
198+
- 0beec01d-efd0-4ce3-ae32-2ffa2692bf26
199+
status: 200 OK
200+
code: 200
201+
duration: ""
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3+
ID eff8830f-5f3b-4e42-a2f0-8e4a77ec4cb0
4+
Name cli-test-kind-herschel
5+
NamespaceID 550f7065-ee73-4399-a32b-49b179950b57
6+
Status pending
7+
MinScale 0
8+
MaxScale 5
9+
MemoryLimit 256
10+
CPULimit 140
11+
Timeout 5 minutes
12+
Privacy public
13+
Description -
14+
RegistryImage rg.fr-par.scw.cloud/funcscwclicnssillyshirleyusnp0hk3/cli-test-kind-herschel:latest
15+
MaxConcurrency 50
16+
DomainName clicnssillyshirleyusnp0hk3-cli-test-kind-herschel.functions.fnc.fr-par.scw.cloud
17+
Protocol http1
18+
Port 8080
19+
HTTPOption enabled
20+
Region fr-par
21+
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
22+
{
23+
"id": "eff8830f-5f3b-4e42-a2f0-8e4a77ec4cb0",
24+
"name": "cli-test-kind-herschel",
25+
"namespace_id": "550f7065-ee73-4399-a32b-49b179950b57",
26+
"status": "pending",
27+
"environment_variables": {},
28+
"min_scale": 0,
29+
"max_scale": 5,
30+
"memory_limit": 256,
31+
"cpu_limit": 140,
32+
"timeout": "300.000000000s",
33+
"error_message": null,
34+
"privacy": "public",
35+
"description": "",
36+
"registry_image": "rg.fr-par.scw.cloud/funcscwclicnssillyshirleyusnp0hk3/cli-test-kind-herschel:latest",
37+
"max_concurrency": 50,
38+
"domain_name": "clicnssillyshirleyusnp0hk3-cli-test-kind-herschel.functions.fnc.fr-par.scw.cloud",
39+
"protocol": "http1",
40+
"port": 8080,
41+
"secret_environment_variables": [],
42+
"http_option": "enabled",
43+
"region": "fr-par"
44+
}

0 commit comments

Comments
 (0)