Skip to content

Commit 316c164

Browse files
authored
chore(account): use context for workflow (#618)
1 parent 2c10620 commit 316c164

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

scaleway/data_source_account_ssh_key.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package scaleway
22

33
import (
4+
"context"
45
"fmt"
56

7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
68
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
79
account "github.com/scaleway/scaleway-sdk-go/api/account/v2alpha1"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
811
)
912

1013
func dataSourceScalewayAccountSSHKey() *schema.Resource {
1114
return &schema.Resource{
12-
Read: dataSourceScalewayAccountSSHKeyRead,
13-
15+
ReadContext: dataSourceScalewayAccountSSHKeyRead,
1416
Schema: map[string]*schema.Schema{
1517
"name": {
1618
Type: schema.TypeString,
@@ -35,29 +37,29 @@ func dataSourceScalewayAccountSSHKey() *schema.Resource {
3537
}
3638
}
3739

38-
func dataSourceScalewayAccountSSHKeyRead(d *schema.ResourceData, m interface{}) error {
40+
func dataSourceScalewayAccountSSHKeyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
3941
accountAPI := accountAPI(m)
4042

4143
var sshKey *account.SSHKey
4244
sshKeyID, ok := d.GetOk("ssh_key_id")
4345
if ok {
44-
res, err := accountAPI.GetSSHKey(&account.GetSSHKeyRequest{SSHKeyID: expandID(sshKeyID)})
46+
res, err := accountAPI.GetSSHKey(&account.GetSSHKeyRequest{SSHKeyID: expandID(sshKeyID)}, scw.WithContext(ctx))
4547
if err != nil {
46-
return err
48+
return diag.FromErr(err)
4749
}
4850
sshKey = res
4951
} else {
5052
res, err := accountAPI.ListSSHKeys(&account.ListSSHKeysRequest{
5153
Name: expandStringPtr(d.Get("name")),
52-
})
54+
}, scw.WithContext(ctx))
5355
if err != nil {
54-
return err
56+
return diag.FromErr(err)
5557
}
5658
if len(res.SSHKeys) == 0 {
57-
return fmt.Errorf("no SSH Key found with the name %s", d.Get("name"))
59+
return diag.FromErr(fmt.Errorf("no SSH Key found with the name %s", d.Get("name")))
5860
}
5961
if len(res.SSHKeys) > 1 {
60-
return fmt.Errorf("%d SSH Keys found with the same name %s", len(res.SSHKeys), d.Get("name"))
62+
return diag.FromErr(fmt.Errorf("%d SSH Keys found with the same name %s", len(res.SSHKeys), d.Get("name")))
6163
}
6264
sshKey = res.SSHKeys[0]
6365
}

scaleway/resource_account_ssh_key.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package scaleway
22

33
import (
4+
"context"
45
"strings"
56

7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
68
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
79
account "github.com/scaleway/scaleway-sdk-go/api/account/v2alpha1"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
811
)
912

1013
func resourceScalewayAccountSSKKey() *schema.Resource {
1114
return &schema.Resource{
12-
Create: resourceScalewayAccountSSHKeyCreate,
13-
Read: resourceScalewayAccountSSHKeyRead,
14-
Update: resourceScalewayAccountSSHKeyUpdate,
15-
Delete: resourceScalewayAccountSSHKeyDelete,
15+
CreateContext: resourceScalewayAccountSSHKeyCreate,
16+
ReadContext: resourceScalewayAccountSSHKeyRead,
17+
UpdateContext: resourceScalewayAccountSSHKeyUpdate,
18+
DeleteContext: resourceScalewayAccountSSHKeyDelete,
1619
Importer: &schema.ResourceImporter{
17-
State: schema.ImportStatePassthrough,
20+
StateContext: schema.ImportStatePassthroughContext,
1821
},
1922
Schema: map[string]*schema.Schema{
2023
"name": {
@@ -37,35 +40,35 @@ func resourceScalewayAccountSSKKey() *schema.Resource {
3740
}
3841
}
3942

40-
func resourceScalewayAccountSSHKeyCreate(d *schema.ResourceData, m interface{}) error {
43+
func resourceScalewayAccountSSHKeyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
4144
accountAPI := accountAPI(m)
4245

4346
res, err := accountAPI.CreateSSHKey(&account.CreateSSHKeyRequest{
4447
Name: d.Get("name").(string),
4548
PublicKey: strings.Trim(d.Get("public_key").(string), "\n"),
4649
OrganizationID: expandStringPtr(d.Get("organization_id")),
47-
})
50+
}, scw.WithContext(ctx))
4851
if err != nil {
49-
return err
52+
return diag.FromErr(err)
5053
}
5154

5255
d.SetId(res.ID)
5356

54-
return resourceScalewayAccountSSHKeyRead(d, m)
57+
return resourceScalewayAccountSSHKeyRead(ctx, d, m)
5558
}
5659

57-
func resourceScalewayAccountSSHKeyRead(d *schema.ResourceData, m interface{}) error {
60+
func resourceScalewayAccountSSHKeyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
5861
accountAPI := accountAPI(m)
5962

6063
res, err := accountAPI.GetSSHKey(&account.GetSSHKeyRequest{
6164
SSHKeyID: d.Id(),
62-
})
65+
}, scw.WithContext(ctx))
6366
if err != nil {
6467
if is404Error(err) {
6568
d.SetId("")
6669
return nil
6770
}
68-
return err
71+
return diag.FromErr(err)
6972
}
7073

7174
_ = d.Set("name", res.Name)
@@ -75,30 +78,30 @@ func resourceScalewayAccountSSHKeyRead(d *schema.ResourceData, m interface{}) er
7578
return nil
7679
}
7780

78-
func resourceScalewayAccountSSHKeyUpdate(d *schema.ResourceData, m interface{}) error {
81+
func resourceScalewayAccountSSHKeyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
7982
accountAPI := accountAPI(m)
8083

8184
if d.HasChange("name") {
8285
_, err := accountAPI.UpdateSSHKey(&account.UpdateSSHKeyRequest{
8386
SSHKeyID: d.Id(),
8487
Name: expandStringPtr(d.Get("name")),
85-
})
88+
}, scw.WithContext(ctx))
8689
if err != nil {
87-
return err
90+
return diag.FromErr(err)
8891
}
8992
}
9093

91-
return resourceScalewayAccountSSHKeyRead(d, m)
94+
return resourceScalewayAccountSSHKeyRead(ctx, d, m)
9295
}
9396

94-
func resourceScalewayAccountSSHKeyDelete(d *schema.ResourceData, m interface{}) error {
97+
func resourceScalewayAccountSSHKeyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
9598
accountAPI := accountAPI(m)
9699

97100
err := accountAPI.DeleteSSHKey(&account.DeleteSSHKeyRequest{
98101
SSHKeyID: d.Id(),
99-
})
102+
}, scw.WithContext(ctx))
100103
if err != nil && !is404Error(err) {
101-
return err
104+
return diag.FromErr(err)
102105
}
103106

104107
return nil

0 commit comments

Comments
 (0)