Skip to content

Commit 009d942

Browse files
committed
feat(function_cron): add name attribute
1 parent 538c279 commit 009d942

File tree

6 files changed

+1537
-739
lines changed

6 files changed

+1537
-739
lines changed

docs/resources/function_cron.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ resource scaleway_function main {
2929
}
3030
3131
resource scaleway_function_cron main {
32+
name = "test-cron"
3233
function_id = scaleway_function.main.id
3334
schedule = "0 0 * * *"
3435
args = jsonencode({test = "scw"})
@@ -50,6 +51,7 @@ The following arguments are required:
5051
- `function_id` - (Required) The function ID to link with your cron.
5152
- `args` - (Required) The key-value mapping to define arguments that will be passed to your function’s event object
5253
during
54+
- `name` - (Optional) The name of the cron. If not provided, the name is generated.
5355

5456
## Attributes Reference
5557

scaleway/resource_function_cron.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ func resourceScalewayFunctionCron() *schema.Resource {
4343
Required: true,
4444
Description: "Functions arguments as json object to pass through during execution.",
4545
},
46+
"name": {
47+
Type: schema.TypeString,
48+
Optional: true,
49+
Computed: true,
50+
Description: "The name of the cron job.",
51+
},
4652
"status": {
4753
Type: schema.TypeString,
4854
Computed: true,
@@ -70,6 +76,7 @@ func resourceScalewayFunctionCronCreate(ctx context.Context, d *schema.ResourceD
7076
FunctionID: f.ID,
7177
Schedule: d.Get("schedule").(string),
7278
Region: region,
79+
Name: expandStringPtr(d.Get("name")),
7380
}
7481

7582
if args, ok := d.GetOk("args"); ok {
@@ -112,7 +119,7 @@ func resourceScalewayFunctionCronRead(ctx context.Context, d *schema.ResourceDat
112119

113120
_ = d.Set("function_id", newRegionalID(region, cron.FunctionID).String())
114121
_ = d.Set("schedule", cron.Schedule)
115-
122+
_ = d.Set("name", cron.Name)
116123
args, err := scw.EncodeJSONObject(*cron.Args, scw.NoEscape)
117124
if err != nil {
118125
return diag.FromErr(err)
@@ -139,8 +146,11 @@ func resourceScalewayFunctionCronUpdate(ctx context.Context, d *schema.ResourceD
139146
Region: region,
140147
CronID: cron.ID,
141148
}
142-
143149
shouldUpdate := false
150+
if d.HasChange("name") {
151+
req.Name = expandStringPtr(d.Get("name").(string))
152+
shouldUpdate = true
153+
}
144154
if d.HasChange("schedule") {
145155
req.Schedule = expandStringPtr(d.Get("schedule").(string))
146156
shouldUpdate = true

scaleway/resource_function_cron_test.go

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,47 @@ func TestAccScalewayFunctionCron_Basic(t *testing.T) {
6969
}
7070
7171
resource scaleway_function_cron main {
72+
name = "tf-tests-cron-basic"
73+
function_id = scaleway_function.main.id
74+
schedule = "0 0 * * *"
75+
args = jsonencode({})
76+
}
77+
`,
78+
Check: resource.ComposeTestCheckFunc(
79+
testAccCheckScalewayFunctionCronExists(tt, "scaleway_function_cron.main"),
80+
resource.TestCheckResourceAttr("scaleway_function_cron.main", "schedule", "0 0 * * *"),
81+
resource.TestCheckResourceAttr("scaleway_function_cron.main", "name", "tf-tests-cron-basic"),
82+
),
83+
},
84+
},
85+
})
86+
}
87+
88+
func TestAccScalewayFunctionCron_NameUpdate(t *testing.T) {
89+
tt := NewTestTools(t)
90+
defer tt.Cleanup()
91+
92+
resource.ParallelTest(t, resource.TestCase{
93+
PreCheck: func() { testAccPreCheck(t) },
94+
ProviderFactories: tt.ProviderFactories,
95+
CheckDestroy: testAccCheckScalewayFunctionCronDestroy(tt),
96+
Steps: []resource.TestStep{
97+
{
98+
Config: `
99+
resource scaleway_function_namespace main {
100+
name = "tf-tests-function-cron-name-update"
101+
}
102+
103+
resource scaleway_function main {
104+
name = "tf-tests-function-cron-name-update"
105+
namespace_id = scaleway_function_namespace.main.id
106+
runtime = "node14"
107+
privacy = "private"
108+
handler = "handler.handle"
109+
}
110+
111+
resource scaleway_function_cron main {
112+
name = "tf-tests-function-cron-name-update"
72113
function_id = scaleway_function.main.id
73114
schedule = "0 0 * * *"
74115
args = jsonencode({})
@@ -77,6 +118,33 @@ func TestAccScalewayFunctionCron_Basic(t *testing.T) {
77118
Check: resource.ComposeTestCheckFunc(
78119
testAccCheckScalewayFunctionCronExists(tt, "scaleway_function_cron.main"),
79120
resource.TestCheckResourceAttr("scaleway_function_cron.main", "schedule", "0 0 * * *"),
121+
resource.TestCheckResourceAttr("scaleway_function_cron.main", "name", "tf-tests-function-cron-name-update"),
122+
),
123+
},
124+
{
125+
Config: `
126+
resource scaleway_function_namespace main {
127+
name = "tf-tests-function-cron-name-update"
128+
}
129+
130+
resource scaleway_function main {
131+
name = "tf-tests-function-cron-name-update"
132+
namespace_id = scaleway_function_namespace.main.id
133+
runtime = "node14"
134+
privacy = "private"
135+
handler = "handler.handle"
136+
}
137+
138+
resource scaleway_function_cron main {
139+
name = "name-changed"
140+
function_id = scaleway_function.main.id
141+
schedule = "0 0 * * *"
142+
args = jsonencode({test = "scw"})
143+
}
144+
`,
145+
Check: resource.ComposeTestCheckFunc(
146+
testAccCheckScalewayFunctionCronExists(tt, "scaleway_function_cron.main"),
147+
resource.TestCheckResourceAttr("scaleway_function_cron.main", "name", "name-changed"),
80148
),
81149
},
82150
},
@@ -107,15 +175,15 @@ func TestAccScalewayFunctionCron_WithArgs(t *testing.T) {
107175
}
108176
109177
resource scaleway_function_cron main {
178+
name = "tf-tests-cron-with-args"
110179
function_id = scaleway_function.main.id
111180
schedule = "0 0 * * *"
112181
args = jsonencode({test = "scw"})
113182
}
114183
`,
115184
Check: resource.ComposeTestCheckFunc(
116185
testAccCheckScalewayFunctionCronExists(tt, "scaleway_function_cron.main"),
117-
resource.TestCheckResourceAttr("scaleway_function_cron.main", "schedule", "0 0 * * *"),
118-
resource.TestCheckResourceAttr("scaleway_function_cron.main", "args", "{\"test\":\"scw\"}"),
186+
resource.TestCheckResourceAttr("scaleway_function_cron.main", "name", "tf-tests-cron-with-args"),
119187
),
120188
},
121189
},

0 commit comments

Comments
 (0)