Skip to content

Commit 0d2359f

Browse files
committed
feat: Use read context for data sources
Signed-off-by: Steve Hipwell <steve.hipwell@gmail.com>
1 parent 46682cf commit 0d2359f

59 files changed

Lines changed: 458 additions & 445 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

github/data_source_github_actions_organization_oidc_subject_claim_customization_template.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package github
22

33
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
47
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
58
)
69

710
func dataSourceGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplate() *schema.Resource {
811
return &schema.Resource{
9-
Read: dataSourceGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplateRead,
12+
ReadContext: dataSourceGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplateRead,
1013

1114
Schema: map[string]*schema.Schema{
1215
"include_claim_keys": {
@@ -20,25 +23,23 @@ func dataSourceGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplate()
2023
}
2124
}
2225

23-
func dataSourceGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplateRead(d *schema.ResourceData, meta any) error {
26+
func dataSourceGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplateRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
2427
client := meta.(*Owner).v3client
2528
orgName := meta.(*Owner).name
26-
ctx := meta.(*Owner).StopContext
2729

2830
err := checkOrganization(meta)
2931
if err != nil {
30-
return err
32+
return diag.FromErr(err)
3133
}
3234

3335
template, _, err := client.Actions.GetOrgOIDCSubjectClaimCustomTemplate(ctx, orgName)
3436
if err != nil {
35-
return err
37+
return diag.FromErr(err)
3638
}
3739

3840
d.SetId(orgName)
39-
err = d.Set("include_claim_keys", template.IncludeClaimKeys)
40-
if err != nil {
41-
return err
41+
if err = d.Set("include_claim_keys", template.IncludeClaimKeys); err != nil {
42+
return diag.FromErr(err)
4243
}
4344

4445
return nil

github/data_source_github_actions_organization_public_key.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package github
33
import (
44
"context"
55

6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
78
)
89

910
func dataSourceGithubActionsOrganizationPublicKey() *schema.Resource {
1011
return &schema.Resource{
11-
Read: dataSourceGithubActionsOrganizationPublicKeyRead,
12+
ReadContext: dataSourceGithubActionsOrganizationPublicKeyRead,
1213

1314
Schema: map[string]*schema.Schema{
1415
"key_id": {
@@ -23,30 +24,28 @@ func dataSourceGithubActionsOrganizationPublicKey() *schema.Resource {
2324
}
2425
}
2526

26-
func dataSourceGithubActionsOrganizationPublicKeyRead(d *schema.ResourceData, meta any) error {
27+
func dataSourceGithubActionsOrganizationPublicKeyRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
2728
err := checkOrganization(meta)
2829
if err != nil {
29-
return err
30+
return diag.FromErr(err)
3031
}
3132

3233
client := meta.(*Owner).v3client
3334
owner := meta.(*Owner).name
3435

35-
ctx := context.Background()
36-
3736
publicKey, _, err := client.Actions.GetOrgPublicKey(ctx, owner)
3837
if err != nil {
39-
return err
38+
return diag.FromErr(err)
4039
}
4140

4241
d.SetId(publicKey.GetKeyID())
4342
err = d.Set("key_id", publicKey.GetKeyID())
4443
if err != nil {
45-
return err
44+
return diag.FromErr(err)
4645
}
4746
err = d.Set("key", publicKey.GetKey())
4847
if err != nil {
49-
return err
48+
return diag.FromErr(err)
5049
}
5150

5251
return nil

github/data_source_github_actions_organization_registration_token.go

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

33
import (
44
"context"
5-
"fmt"
65
"log"
76

7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
99
)
1010

1111
func dataSourceGithubActionsOrganizationRegistrationToken() *schema.Resource {
1212
return &schema.Resource{
13-
Read: dataSourceGithubActionsOrganizationRegistrationTokenRead,
13+
ReadContext: dataSourceGithubActionsOrganizationRegistrationTokenRead,
1414

1515
Schema: map[string]*schema.Schema{
1616
"token": {
@@ -25,25 +25,24 @@ func dataSourceGithubActionsOrganizationRegistrationToken() *schema.Resource {
2525
}
2626
}
2727

28-
func dataSourceGithubActionsOrganizationRegistrationTokenRead(d *schema.ResourceData, meta any) error {
29-
ctx := context.Background()
28+
func dataSourceGithubActionsOrganizationRegistrationTokenRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
3029
client := meta.(*Owner).v3client
3130
owner := meta.(*Owner).name
3231

3332
log.Printf("[DEBUG] Creating a GitHub Actions organization registration token for %s", owner)
3433
token, _, err := client.Actions.CreateOrganizationRegistrationToken(ctx, owner)
3534
if err != nil {
36-
return fmt.Errorf("error creating a GitHub Actions organization registration token for %s: %w", owner, err)
35+
return diag.Errorf("error creating a GitHub Actions organization registration token for %s: %v", owner, err)
3736
}
3837

3938
d.SetId(owner)
4039
err = d.Set("token", token.Token)
4140
if err != nil {
42-
return err
41+
return diag.FromErr(err)
4342
}
4443
err = d.Set("expires_at", token.ExpiresAt.Unix())
4544
if err != nil {
46-
return err
45+
return diag.FromErr(err)
4746
}
4847

4948
return nil

github/data_source_github_actions_organization_secrets.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55

66
"github.com/google/go-github/v82/github"
77

8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
)
1011

1112
func dataSourceGithubActionsOrganizationSecrets() *schema.Resource {
1213
return &schema.Resource{
13-
Read: dataSourceGithubActionsOrganizationSecretsRead,
14+
ReadContext: dataSourceGithubActionsOrganizationSecretsRead,
1415

1516
Schema: map[string]*schema.Schema{
1617
"secrets": {
@@ -41,20 +42,19 @@ func dataSourceGithubActionsOrganizationSecrets() *schema.Resource {
4142
}
4243
}
4344

44-
func dataSourceGithubActionsOrganizationSecretsRead(d *schema.ResourceData, meta any) error {
45-
ctx := context.Background()
45+
func dataSourceGithubActionsOrganizationSecretsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
4646
client := meta.(*Owner).v3client
4747
owner := meta.(*Owner).name
4848

4949
options := github.ListOptions{
5050
PerPage: 100,
5151
}
5252

53-
var all_secrets []map[string]string
53+
var allSecrets []map[string]string
5454
for {
5555
secrets, resp, err := client.Actions.ListOrgSecrets(ctx, owner, &options)
5656
if err != nil {
57-
return err
57+
return diag.FromErr(err)
5858
}
5959
for _, secret := range secrets.Secrets {
6060
new_secret := map[string]string{
@@ -63,7 +63,7 @@ func dataSourceGithubActionsOrganizationSecretsRead(d *schema.ResourceData, meta
6363
"updated_at": secret.UpdatedAt.String(),
6464
"visibility": secret.Visibility,
6565
}
66-
all_secrets = append(all_secrets, new_secret)
66+
allSecrets = append(allSecrets, new_secret)
6767

6868
}
6969
if resp.NextPage == 0 {
@@ -73,9 +73,9 @@ func dataSourceGithubActionsOrganizationSecretsRead(d *schema.ResourceData, meta
7373
}
7474

7575
d.SetId(owner)
76-
err := d.Set("secrets", all_secrets)
76+
err := d.Set("secrets", allSecrets)
7777
if err != nil {
78-
return err
78+
return diag.FromErr(err)
7979
}
8080

8181
return nil

github/data_source_github_actions_organization_variables.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55

66
"github.com/google/go-github/v82/github"
77

8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
)
1011

1112
func dataSourceGithubActionsOrganizationVariables() *schema.Resource {
1213
return &schema.Resource{
13-
Read: dataSourceGithubActionsOrganizationVariablesRead,
14+
ReadContext: dataSourceGithubActionsOrganizationVariablesRead,
1415

1516
Schema: map[string]*schema.Schema{
1617
"variables": {
@@ -45,8 +46,7 @@ func dataSourceGithubActionsOrganizationVariables() *schema.Resource {
4546
}
4647
}
4748

48-
func dataSourceGithubActionsOrganizationVariablesRead(d *schema.ResourceData, meta any) error {
49-
ctx := context.Background()
49+
func dataSourceGithubActionsOrganizationVariablesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
5050
client := meta.(*Owner).v3client
5151
owner := meta.(*Owner).name
5252

@@ -58,7 +58,7 @@ func dataSourceGithubActionsOrganizationVariablesRead(d *schema.ResourceData, me
5858
for {
5959
variables, resp, err := client.Actions.ListOrgVariables(ctx, owner, &options)
6060
if err != nil {
61-
return err
61+
return diag.FromErr(err)
6262
}
6363
for _, variable := range variables.Variables {
6464
new_variable := map[string]string{
@@ -79,7 +79,7 @@ func dataSourceGithubActionsOrganizationVariablesRead(d *schema.ResourceData, me
7979
d.SetId(owner)
8080
err := d.Set("variables", all_variables)
8181
if err != nil {
82-
return err
82+
return diag.FromErr(err)
8383
}
8484

8585
return nil

github/data_source_github_actions_public_key.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package github
33
import (
44
"context"
55

6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
78
)
89

910
func dataSourceGithubActionsPublicKey() *schema.Resource {
1011
return &schema.Resource{
11-
Read: dataSourceGithubActionsPublicKeyRead,
12+
ReadContext: dataSourceGithubActionsPublicKeyRead,
1213

1314
Schema: map[string]*schema.Schema{
1415
"repository": {
@@ -27,26 +28,25 @@ func dataSourceGithubActionsPublicKey() *schema.Resource {
2728
}
2829
}
2930

30-
func dataSourceGithubActionsPublicKeyRead(d *schema.ResourceData, meta any) error {
31+
func dataSourceGithubActionsPublicKeyRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
3132
repository := d.Get("repository").(string)
3233
owner := meta.(*Owner).name
3334

3435
client := meta.(*Owner).v3client
35-
ctx := context.Background()
3636

3737
publicKey, _, err := client.Actions.GetRepoPublicKey(ctx, owner, repository)
3838
if err != nil {
39-
return err
39+
return diag.FromErr(err)
4040
}
4141

4242
d.SetId(publicKey.GetKeyID())
4343
err = d.Set("key_id", publicKey.GetKeyID())
4444
if err != nil {
45-
return err
45+
return diag.FromErr(err)
4646
}
4747
err = d.Set("key", publicKey.GetKey())
4848
if err != nil {
49-
return err
49+
return diag.FromErr(err)
5050
}
5151

5252
return nil

github/data_source_github_actions_registration_token.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"fmt"
66
"log"
77

8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
)
1011

1112
func dataSourceGithubActionsRegistrationToken() *schema.Resource {
1213
return &schema.Resource{
13-
Read: dataSourceGithubActionsRegistrationTokenRead,
14+
ReadContext: dataSourceGithubActionsRegistrationTokenRead,
1415

1516
Schema: map[string]*schema.Schema{
1617
"repository": {
@@ -30,26 +31,25 @@ func dataSourceGithubActionsRegistrationToken() *schema.Resource {
3031
}
3132
}
3233

33-
func dataSourceGithubActionsRegistrationTokenRead(d *schema.ResourceData, meta any) error {
34-
ctx := context.Background()
34+
func dataSourceGithubActionsRegistrationTokenRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
3535
client := meta.(*Owner).v3client
3636
owner := meta.(*Owner).name
3737
repoName := d.Get("repository").(string)
3838

3939
log.Printf("[DEBUG] Creating a GitHub Actions repository registration token for %s/%s", owner, repoName)
4040
token, _, err := client.Actions.CreateRegistrationToken(ctx, owner, repoName)
4141
if err != nil {
42-
return fmt.Errorf("error creating a GitHub Actions repository registration token for %s/%s: %w", owner, repoName, err)
42+
return diag.Errorf("error creating a GitHub Actions repository registration token for %s/%s: %v", owner, repoName, err)
4343
}
4444

4545
d.SetId(fmt.Sprintf("%s/%s", owner, repoName))
4646
err = d.Set("token", token.Token)
4747
if err != nil {
48-
return err
48+
return diag.FromErr(err)
4949
}
5050
err = d.Set("expires_at", token.ExpiresAt.Unix())
5151
if err != nil {
52-
return err
52+
return diag.FromErr(err)
5353
}
5454

5555
return nil

github/data_source_github_actions_repository_oidc_subject_claim_customization_template.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package github
22

3-
import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
49

510
func dataSourceGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplate() *schema.Resource {
611
return &schema.Resource{
7-
Read: dataSourceGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplateRead,
12+
ReadContext: dataSourceGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplateRead,
813

914
Schema: map[string]*schema.Schema{
1015
"name": {
@@ -26,26 +31,25 @@ func dataSourceGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplate() *s
2631
}
2732
}
2833

29-
func dataSourceGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplateRead(d *schema.ResourceData, meta any) error {
34+
func dataSourceGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplateRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
3035
client := meta.(*Owner).v3client
3136

3237
repository := d.Get("name").(string)
3338
owner := meta.(*Owner).name
34-
ctx := meta.(*Owner).StopContext
3539

3640
template, _, err := client.Actions.GetRepoOIDCSubjectClaimCustomTemplate(ctx, owner, repository)
3741
if err != nil {
38-
return err
42+
return diag.FromErr(err)
3943
}
4044

4145
d.SetId(repository)
4246
err = d.Set("use_default", template.UseDefault)
4347
if err != nil {
44-
return err
48+
return diag.FromErr(err)
4549
}
4650
err = d.Set("include_claim_keys", template.IncludeClaimKeys)
4751
if err != nil {
48-
return err
52+
return diag.FromErr(err)
4953
}
5054

5155
return nil

0 commit comments

Comments
 (0)