Skip to content

Commit 8f25bb4

Browse files
committed
Use context-aware CRUD functions for Tag Protection resource
1 parent 969eaa5 commit 8f25bb4

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

gitlab/resource_gitlab_tag_protection.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package gitlab
22

33
import (
4-
"fmt"
4+
"context"
55
"log"
66

7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
89
gitlab "github.com/xanzy/go-gitlab"
910
)
@@ -15,9 +16,9 @@ func resourceGitlabTagProtection() *schema.Resource {
1516
acceptedAccessLevels = append(acceptedAccessLevels, k)
1617
}
1718
return &schema.Resource{
18-
Create: resourceGitlabTagProtectionCreate,
19-
Read: resourceGitlabTagProtectionRead,
20-
Delete: resourceGitlabTagProtectionDelete,
19+
CreateContext: resourceGitlabTagProtectionCreate,
20+
ReadContext: resourceGitlabTagProtectionRead,
21+
DeleteContext: resourceGitlabTagProtectionDelete,
2122
Importer: &schema.ResourceImporter{
2223
StateContext: schema.ImportStatePassthroughContext,
2324
},
@@ -43,7 +44,7 @@ func resourceGitlabTagProtection() *schema.Resource {
4344
}
4445
}
4546

46-
func resourceGitlabTagProtectionCreate(d *schema.ResourceData, meta interface{}) error {
47+
func resourceGitlabTagProtectionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
4748
client := meta.(*gitlab.Client)
4849
project := d.Get("project").(string)
4950
tag := gitlab.String(d.Get("tag").(string))
@@ -56,47 +57,47 @@ func resourceGitlabTagProtectionCreate(d *schema.ResourceData, meta interface{})
5657

5758
log.Printf("[DEBUG] create gitlab tag protection on %v for project %s", options.Name, project)
5859

59-
tp, _, err := client.ProtectedTags.ProtectRepositoryTags(project, options)
60+
tp, _, err := client.ProtectedTags.ProtectRepositoryTags(project, options, gitlab.WithContext(ctx))
6061
if err != nil {
6162
// Remove existing tag protection
62-
_, err = client.ProtectedTags.UnprotectRepositoryTags(project, *tag)
63+
_, err = client.ProtectedTags.UnprotectRepositoryTags(project, *tag, gitlab.WithContext(ctx))
6364
if err != nil {
64-
return err
65+
return diag.FromErr(err)
6566
}
6667
// Reprotect tag with updated values
67-
tp, _, err = client.ProtectedTags.ProtectRepositoryTags(project, options)
68+
tp, _, err = client.ProtectedTags.ProtectRepositoryTags(project, options, gitlab.WithContext(ctx))
6869
if err != nil {
69-
return err
70+
return diag.FromErr(err)
7071
}
7172
}
7273

7374
d.SetId(buildTwoPartID(&project, &tp.Name))
7475

75-
return resourceGitlabTagProtectionRead(d, meta)
76+
return resourceGitlabTagProtectionRead(ctx, d, meta)
7677
}
7778

78-
func resourceGitlabTagProtectionRead(d *schema.ResourceData, meta interface{}) error {
79+
func resourceGitlabTagProtectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
7980
client := meta.(*gitlab.Client)
8081
project, tag, err := projectAndTagFromID(d.Id())
8182
if err != nil {
82-
return err
83+
return diag.FromErr(err)
8384
}
8485

8586
log.Printf("[DEBUG] read gitlab tag protection for project %s, tag %s", project, tag)
8687

87-
pt, _, err := client.ProtectedTags.GetProtectedTag(project, tag)
88+
pt, _, err := client.ProtectedTags.GetProtectedTag(project, tag, gitlab.WithContext(ctx))
8889
if err != nil {
8990
if is404(err) {
9091
log.Printf("[DEBUG] gitlab tag protection not found %s/%s", project, tag)
9192
d.SetId("")
9293
return nil
9394
}
94-
return err
95+
return diag.FromErr(err)
9596
}
9697

9798
accessLevel, ok := tagProtectionAccessLevelNames[pt.CreateAccessLevels[0].AccessLevel]
9899
if !ok {
99-
return fmt.Errorf("tag protection access level %d is not supported. Supported are: %v", pt.CreateAccessLevels[0].AccessLevel, tagProtectionAccessLevelNames)
100+
return diag.Errorf("tag protection access level %d is not supported. Supported are: %v", pt.CreateAccessLevels[0].AccessLevel, tagProtectionAccessLevelNames)
100101
}
101102

102103
d.Set("project", project)
@@ -108,15 +109,19 @@ func resourceGitlabTagProtectionRead(d *schema.ResourceData, meta interface{}) e
108109
return nil
109110
}
110111

111-
func resourceGitlabTagProtectionDelete(d *schema.ResourceData, meta interface{}) error {
112+
func resourceGitlabTagProtectionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
112113
client := meta.(*gitlab.Client)
113114
project := d.Get("project").(string)
114115
tag := d.Get("tag").(string)
115116

116117
log.Printf("[DEBUG] Delete gitlab protected tag %s for project %s", tag, project)
117118

118-
_, err := client.ProtectedTags.UnprotectRepositoryTags(project, tag)
119-
return err
119+
_, err := client.ProtectedTags.UnprotectRepositoryTags(project, tag, gitlab.WithContext(ctx))
120+
if err != nil {
121+
return diag.FromErr(err)
122+
}
123+
124+
return nil
120125
}
121126

122127
func projectAndTagFromID(id string) (string, string, error) {

0 commit comments

Comments
 (0)