Skip to content

Commit 5bc59f0

Browse files
authored
Merge pull request #969 from timofurrer/feature/ctx
Use context for all GitLab API calls
2 parents 4321fad + 6cbb830 commit 5bc59f0

16 files changed

+58
-52
lines changed

internal/provider/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package provider
22

33
import (
4+
"context"
45
"crypto/tls"
56
"crypto/x509"
67
"io/ioutil"
@@ -22,7 +23,7 @@ type Config struct {
2223
}
2324

2425
// Client returns a *gitlab.Client to interact with the configured gitlab instance
25-
func (c *Config) Client() (*gitlab.Client, error) {
26+
func (c *Config) Client(ctx context.Context) (*gitlab.Client, error) {
2627
// Configure TLS/SSL
2728
tlsConfig := &tls.Config{}
2829

@@ -78,7 +79,7 @@ func (c *Config) Client() (*gitlab.Client, error) {
7879

7980
// Test the credentials by checking we can get information about the authenticated user.
8081
if c.EarlyAuthFail {
81-
_, _, err = client.Users.CurrentUser()
82+
_, _, err = client.Users.CurrentUser(gitlab.WithContext(ctx))
8283
}
8384

8485
return client, err

internal/provider/custom_attribute_helper.go

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

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"strconv"
78
"strings"
89

10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
911
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1012
gitlab "github.com/xanzy/go-gitlab"
1113
)
@@ -26,26 +28,26 @@ func CreateCustomAttributeResource(idName string, createGetter CreateGetter, cre
2628
d.Set("value", customAttribute.Value)
2729
}
2830

29-
readFunc := func(d *schema.ResourceData, meta interface{}) error {
31+
readFunc := func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
3032
client := meta.(*gitlab.Client)
3133
getter := createGetter(client)
3234
log.Printf("[DEBUG] read Custom Attribute %s", d.Id())
3335

3436
id, key, err := parseId(d.Id())
3537
if err != nil {
36-
return err
38+
return diag.FromErr(err)
3739
}
3840

3941
customAttribute, _, err := getter(id, key)
4042
if err != nil {
41-
return err
43+
return diag.FromErr(err)
4244
}
4345

4446
setToState(d, id, customAttribute)
4547
return nil
4648
}
4749

48-
setFunc := func(d *schema.ResourceData, meta interface{}) error {
50+
setFunc := func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
4951
client := meta.(*gitlab.Client)
5052
setter := createSetter(client)
5153

@@ -57,39 +59,39 @@ func CreateCustomAttributeResource(idName string, createGetter CreateGetter, cre
5759

5860
log.Printf("[DEBUG] set (create or update) Custom Attribute %s with value %s for %s %d", options.Key, options.Value, idName, id)
5961

60-
customAttribute, _, err := setter(id, *options)
62+
customAttribute, _, err := setter(id, *options, gitlab.WithContext(ctx))
6163
if err != nil {
62-
return err
64+
return diag.FromErr(err)
6365
}
6466

6567
d.SetId(buildId(id, customAttribute.Key))
66-
return readFunc(d, meta)
68+
return readFunc(ctx, d, meta)
6769
}
6870

69-
deleteFunc := func(d *schema.ResourceData, meta interface{}) error {
71+
deleteFunc := func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
7072
client := meta.(*gitlab.Client)
7173
deleter := createDeleter(client)
7274
log.Printf("[DEBUG] delete Custom Attribute %s", d.Id())
7375

7476
id, key, err := parseId(d.Id())
7577
if err != nil {
76-
return err
78+
return diag.FromErr(err)
7779
}
7880

79-
_, err = deleter(id, key)
81+
_, err = deleter(id, key, gitlab.WithContext(ctx))
8082
if err != nil {
81-
return err
83+
return diag.FromErr(err)
8284
}
8385

8486
return nil
8587
}
8688

8789
return &schema.Resource{
88-
Description: description,
89-
Create: setFunc,
90-
Read: readFunc,
91-
Update: setFunc,
92-
Delete: deleteFunc,
90+
Description: description,
91+
CreateContext: setFunc,
92+
ReadContext: readFunc,
93+
UpdateContext: setFunc,
94+
DeleteContext: deleteFunc,
9395
Importer: &schema.ResourceImporter{
9496
StateContext: schema.ImportStatePassthroughContext,
9597
},

internal/provider/data_source_gitlab_group_membership.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func dataSourceGitlabGroupMembershipRead(ctx context.Context, d *schema.Resource
118118
}
119119
} else if fullPathOk {
120120
// Get group by full path
121-
group, _, err = client.Groups.GetGroup(fullPathData.(string), nil)
121+
group, _, err = client.Groups.GetGroup(fullPathData.(string), nil, gitlab.WithContext(ctx))
122122
if err != nil {
123123
return diag.FromErr(err)
124124
}

internal/provider/data_source_gitlab_project_issues.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ func dataSourceGitlabProjectIssuesRead(ctx context.Context, d *schema.ResourceDa
348348

349349
var issues []*gitlab.Issue
350350
for options.Page != 0 {
351-
paginatedIssues, resp, err := client.Issues.ListProjectIssues(project, &options)
351+
paginatedIssues, resp, err := client.Issues.ListProjectIssues(project, &options, gitlab.WithContext(ctx))
352352
if err != nil {
353353
return diag.FromErr(err)
354354
}

internal/provider/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func configure(version string, p *schema.Provider) func(context.Context, *schema
105105
EarlyAuthFail: d.Get("early_auth_check").(bool),
106106
}
107107

108-
client, err := config.Client()
108+
client, err := config.Client(ctx)
109109
if err != nil {
110110
return nil, diag.FromErr(err)
111111
}

internal/provider/provider_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package provider
22

33
import (
4+
"context"
45
"os"
56
"testing"
67

@@ -35,7 +36,7 @@ var testGitlabClient *gitlab.Client
3536

3637
func init() {
3738
if os.Getenv(resource.EnvTfAcc) != "" {
38-
client, err := testGitlabConfig.Client()
39+
client, err := testGitlabConfig.Client(context.Background())
3940
if err != nil {
4041
panic("failed to create test client: " + err.Error()) // lintignore: R009 // TODO: Resolve this tfproviderlint issue
4142
}

internal/provider/resource_gitlab_group_label.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func resourceGitlabGroupLabelImporter(ctx context.Context, d *schema.ResourceDat
138138
}
139139

140140
d.SetId(parts[1])
141-
group, _, err := client.Groups.GetGroup(parts[0], nil)
141+
group, _, err := client.Groups.GetGroup(parts[0], nil, gitlab.WithContext(ctx))
142142
if err != nil {
143143
return nil, err
144144
}

internal/provider/resource_gitlab_label.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func resourceGitlabLabelImporter(ctx context.Context, d *schema.ResourceData, me
147147
}
148148

149149
d.SetId(parts[1])
150-
project, _, err := client.Projects.GetProject(parts[0], nil)
150+
project, _, err := client.Projects.GetProject(parts[0], nil, gitlab.WithContext(ctx))
151151
if err != nil {
152152
return nil, err
153153
}

internal/provider/resource_gitlab_managed_license_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package provider
22

33
import (
4+
"context"
45
"fmt"
56
"strconv"
67
"testing"
@@ -60,7 +61,7 @@ func TestAccGitlabManagedLicense_deprecatedConfigValues(t *testing.T) {
6061
// Create a managed license with an "approved" state
6162
SkipFunc: orSkipFunc(
6263
isRunningInCE,
63-
isGitLabVersionLessThan(testGitlabClient, "15.0"),
64+
isGitLabVersionLessThan(context.Background(), testGitlabClient, "15.0"),
6465
),
6566
Config: testManagedLicenseConfig(rInt, "approved"),
6667
Check: resource.ComposeTestCheckFunc(
@@ -71,7 +72,7 @@ func TestAccGitlabManagedLicense_deprecatedConfigValues(t *testing.T) {
7172
// Update the managed license to have a blacklisted state
7273
SkipFunc: orSkipFunc(
7374
isRunningInCE,
74-
isGitLabVersionLessThan(testGitlabClient, "15.0"),
75+
isGitLabVersionLessThan(context.Background(), testGitlabClient, "15.0"),
7576
),
7677
Config: testManagedLicenseConfig(rInt, "blacklisted"),
7778
Check: resource.ComposeTestCheckFunc(
@@ -81,7 +82,7 @@ func TestAccGitlabManagedLicense_deprecatedConfigValues(t *testing.T) {
8182
{
8283
SkipFunc: orSkipFunc(
8384
isRunningInCE,
84-
isGitLabVersionLessThan(testGitlabClient, "15.0"),
85+
isGitLabVersionLessThan(context.Background(), testGitlabClient, "15.0"),
8586
),
8687
ResourceName: "gitlab_managed_license.fixme",
8788
ImportState: true,

internal/provider/resource_gitlab_project.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ A project can either be created in a group or user namespace.
663663
}
664664
})
665665

666-
func resourceGitlabProjectSetToState(client *gitlab.Client, d *schema.ResourceData, project *gitlab.Project) error {
666+
func resourceGitlabProjectSetToState(ctx context.Context, client *gitlab.Client, d *schema.ResourceData, project *gitlab.Project) error {
667667
d.SetId(fmt.Sprintf("%d", project.ID))
668668
d.Set("name", project.Name)
669669
d.Set("path", project.Path)
@@ -694,7 +694,7 @@ func resourceGitlabProjectSetToState(client *gitlab.Client, d *schema.ResourceDa
694694
return err
695695
}
696696
d.Set("archived", project.Archived)
697-
if supportsSquashOption, err := isGitLabVersionAtLeast(client, "14.1")(); err != nil {
697+
if supportsSquashOption, err := isGitLabVersionAtLeast(ctx, client, "14.1")(); err != nil {
698698
return err
699699
} else if supportsSquashOption {
700700
d.Set("squash_option", project.SquashOption)
@@ -938,7 +938,7 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
938938
options.MergeCommitTemplate = gitlab.String(v.(string))
939939
}
940940

941-
if supportsSquashOption, err := isGitLabVersionAtLeast(client, "14.1")(); err != nil {
941+
if supportsSquashOption, err := isGitLabVersionAtLeast(ctx, client, "14.1")(); err != nil {
942942
return diag.FromErr(err)
943943
} else if supportsSquashOption {
944944
if v, ok := d.GetOk("squash_option"); ok {
@@ -1153,7 +1153,7 @@ func resourceGitlabProjectRead(ctx context.Context, d *schema.ResourceData, meta
11531153
return nil
11541154
}
11551155

1156-
if err := resourceGitlabProjectSetToState(client, d, project); err != nil {
1156+
if err := resourceGitlabProjectSetToState(ctx, client, d, project); err != nil {
11571157
return diag.FromErr(err)
11581158
}
11591159

@@ -1262,7 +1262,7 @@ func resourceGitlabProjectUpdate(ctx context.Context, d *schema.ResourceData, me
12621262
options.LFSEnabled = gitlab.Bool(d.Get("lfs_enabled").(bool))
12631263
}
12641264

1265-
if supportsSquashOption, err := isGitLabVersionAtLeast(client, "14.1")(); err != nil {
1265+
if supportsSquashOption, err := isGitLabVersionAtLeast(ctx, client, "14.1")(); err != nil {
12661266
return diag.FromErr(err)
12671267
} else if supportsSquashOption && d.HasChange("squash_option") {
12681268
options.SquashOption = stringToSquashOptionValue(d.Get("squash_option").(string))

0 commit comments

Comments
 (0)