Skip to content

Commit ed21204

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

File tree

6 files changed

+1538
-740
lines changed

6 files changed

+1538
-740
lines changed

docs/resources/function_cron.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---
1+
---
22
subcategory: "Functions"
33
page_title: "Scaleway: scaleway_function_cron"
44
---
@@ -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 empty.
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,11 @@ 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+
Description: "The name of the cron job.",
50+
},
4651
"status": {
4752
Type: schema.TypeString,
4853
Computed: true,
@@ -70,6 +75,7 @@ func resourceScalewayFunctionCronCreate(ctx context.Context, d *schema.ResourceD
7075
FunctionID: f.ID,
7176
Schedule: d.Get("schedule").(string),
7277
Region: region,
78+
Name: expandStringPtr(d.Get("name")),
7379
}
7480

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

113119
_ = d.Set("function_id", newRegionalID(region, cron.FunctionID).String())
114120
_ = d.Set("schedule", cron.Schedule)
115-
121+
_ = d.Set("name", cron.Name)
116122
args, err := scw.EncodeJSONObject(*cron.Args, scw.NoEscape)
117123
if err != nil {
118124
return diag.FromErr(err)
@@ -139,8 +145,12 @@ func resourceScalewayFunctionCronUpdate(ctx context.Context, d *schema.ResourceD
139145
Region: region,
140146
CronID: cron.ID,
141147
}
142-
143148
shouldUpdate := false
149+
if d.HasChange("name") {
150+
req.Name = expandStringPtr(d.Get("name").(string))
151+
shouldUpdate = true
152+
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)