Skip to content

Commit ef2debf

Browse files
committed
Refactor to use Context-aware functions
Signed-off-by: Timo Sand <[email protected]>
1 parent b39c5e1 commit ef2debf

1 file changed

Lines changed: 34 additions & 33 deletions

File tree

github/resource_github_emu_group_mapping.go

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@ import (
77
"strings"
88

99
"github.com/google/go-github/v67/github"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1112
)
1213

1314
func resourceGithubEMUGroupMapping() *schema.Resource {
1415
return &schema.Resource{
15-
Create: resourceGithubEMUGroupMappingCreate,
16-
Read: resourceGithubEMUGroupMappingRead,
17-
Update: resourceGithubEMUGroupMappingUpdate,
18-
Delete: resourceGithubEMUGroupMappingDelete,
16+
CreateContext: resourceGithubEMUGroupMappingCreate,
17+
ReadContext: resourceGithubEMUGroupMappingRead,
18+
UpdateContext: resourceGithubEMUGroupMappingUpdate,
19+
DeleteContext: resourceGithubEMUGroupMappingDelete,
1920
Importer: &schema.ResourceImporter{
20-
State: func(d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
21+
StateContext: func(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
2122
isTwoPartID := strings.Contains(d.Id(), ":")
2223
if isTwoPartID {
23-
return importWithTwoPartID(d, meta)
24+
return importWithTwoPartID(ctx, d, meta)
2425
}
25-
return importWithIntegerID(d, meta)
26+
return importWithIntegerID(ctx, d, meta)
2627
},
2728
},
2829
Schema: map[string]*schema.Schema{
@@ -44,28 +45,28 @@ func resourceGithubEMUGroupMapping() *schema.Resource {
4445
}
4546
}
4647

47-
func resourceGithubEMUGroupMappingCreate(d *schema.ResourceData, meta any) error {
48-
return resourceGithubEMUGroupMappingUpdate(d, meta)
48+
func resourceGithubEMUGroupMappingCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
49+
return resourceGithubEMUGroupMappingUpdate(ctx, d, meta)
4950
}
5051

51-
func resourceGithubEMUGroupMappingRead(d *schema.ResourceData, meta any) error {
52+
func resourceGithubEMUGroupMappingRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
5253
err := checkOrganization(meta)
5354
if err != nil {
54-
return err
55+
return diag.FromErr(err)
5556
}
5657
client := meta.(*Owner).v3client
5758
orgName := meta.(*Owner).name
5859

5960
id, ok := d.GetOk("group_id")
6061
if !ok {
61-
return fmt.Errorf("could not get group id from provided value")
62+
return diag.Errorf("could not get group id from provided value")
6263
}
6364
id64, err := getInt64FromInterface(id)
6465
if err != nil {
65-
return err
66+
return diag.FromErr(err)
6667
}
6768

68-
ctx := context.WithValue(context.Background(), ctxId, d.Id())
69+
ctx = context.WithValue(ctx, ctxId, d.Id())
6970

7071
group, resp, err := client.Teams.GetExternalGroup(ctx, orgName, id64)
7172
if err != nil {
@@ -74,7 +75,7 @@ func resourceGithubEMUGroupMappingRead(d *schema.ResourceData, meta any) error {
7475
d.SetId("")
7576
return nil
7677
}
77-
return err
78+
return diag.FromErr(err)
7879
}
7980

8081
if len(group.Teams) < 1 {
@@ -85,35 +86,35 @@ func resourceGithubEMUGroupMappingRead(d *schema.ResourceData, meta any) error {
8586
}
8687

8788
if err = d.Set("etag", resp.Header.Get("ETag")); err != nil {
88-
return err
89+
return diag.FromErr(err)
8990
}
9091
if err = d.Set("group_id", int(*group.GroupID)); err != nil {
91-
return err
92+
return diag.FromErr(err)
9293
}
9394
return nil
9495
}
9596

96-
func resourceGithubEMUGroupMappingUpdate(d *schema.ResourceData, meta any) error {
97+
func resourceGithubEMUGroupMappingUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
9798
err := checkOrganization(meta)
9899
if err != nil {
99-
return err
100+
return diag.FromErr(err)
100101
}
101102
client := meta.(*Owner).v3client
102103
orgName := meta.(*Owner).name
103-
ctx := context.WithValue(context.Background(), ctxId, d.Id())
104+
ctx = context.WithValue(ctx, ctxId, d.Id())
104105

105106
teamSlug, ok := d.GetOk("team_slug")
106107
if !ok {
107-
return fmt.Errorf("could not get team slug from provided value")
108+
return diag.Errorf("could not get team slug from provided value")
108109
}
109110

110111
id, ok := d.GetOk("group_id")
111112
if !ok {
112-
return fmt.Errorf("could not get group id from provided value")
113+
return diag.Errorf("could not get group id from provided value")
113114
}
114115
id64, err := getInt64FromInterface(id)
115116
if err != nil {
116-
return err
117+
return diag.FromErr(err)
117118
}
118119

119120
eg := &github.ExternalGroup{
@@ -122,31 +123,31 @@ func resourceGithubEMUGroupMappingUpdate(d *schema.ResourceData, meta any) error
122123

123124
_, _, err = client.Teams.UpdateConnectedExternalGroup(ctx, orgName, teamSlug.(string), eg)
124125
if err != nil {
125-
return err
126+
return diag.FromErr(err)
126127
}
127128

128129
d.SetId(fmt.Sprintf("teams/%s/external-groups", teamSlug))
129-
return resourceGithubEMUGroupMappingRead(d, meta)
130+
return resourceGithubEMUGroupMappingRead(ctx, d, meta)
130131
}
131132

132-
func resourceGithubEMUGroupMappingDelete(d *schema.ResourceData, meta any) error {
133+
func resourceGithubEMUGroupMappingDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
133134
err := checkOrganization(meta)
134135
if err != nil {
135-
return err
136+
return diag.FromErr(err)
136137
}
137138
client := meta.(*Owner).v3client
138139
orgName := meta.(*Owner).name
139140

140141
teamSlug, ok := d.GetOk("team_slug")
141142
if !ok {
142-
return fmt.Errorf("could not parse team slug from provided value")
143+
return diag.Errorf("could not parse team slug from provided value")
143144
}
144145

145-
ctx := context.WithValue(context.Background(), ctxId, d.Id())
146+
ctx = context.WithValue(ctx, ctxId, d.Id())
146147

147148
_, err = client.Teams.RemoveConnectedExternalGroup(ctx, orgName, teamSlug.(string))
148149
if err != nil {
149-
return err
150+
return diag.FromErr(err)
150151
}
151152
return nil
152153
}
@@ -170,7 +171,7 @@ func getInt64FromInterface(val any) (int64, error) {
170171
return id64, nil
171172
}
172173

173-
func importWithTwoPartID(d *schema.ResourceData, _ any) ([]*schema.ResourceData, error) {
174+
func importWithTwoPartID(_ context.Context, d *schema.ResourceData, _ any) ([]*schema.ResourceData, error) {
174175
groupIDString, teamSlug, err := parseTwoPartID(d.Id(), "group_id", "team_slug")
175176
if err != nil {
176177
return nil, err
@@ -189,15 +190,15 @@ func importWithTwoPartID(d *schema.ResourceData, _ any) ([]*schema.ResourceData,
189190
return []*schema.ResourceData{d}, nil
190191
}
191192

192-
func importWithIntegerID(d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
193+
func importWithIntegerID(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
193194
groupID, err := strconv.Atoi(d.Id())
194195
if err != nil {
195196
return nil, err
196197
}
197198
if err := d.Set("group_id", groupID); err != nil {
198199
return nil, err
199200
}
200-
ctx := context.WithValue(context.Background(), ctxId, d.Id())
201+
ctx = context.WithValue(ctx, ctxId, d.Id())
201202
client := meta.(*Owner).v3client
202203
orgName := meta.(*Owner).name
203204
group, _, err := client.Teams.GetExternalGroup(ctx, orgName, int64(groupID))

0 commit comments

Comments
 (0)