1
1
package gitlab
2
2
3
3
import (
4
- "fmt "
4
+ "context "
5
5
"log"
6
6
7
+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7
8
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8
9
gitlab "github.com/xanzy/go-gitlab"
9
10
)
@@ -15,9 +16,9 @@ func resourceGitlabTagProtection() *schema.Resource {
15
16
acceptedAccessLevels = append (acceptedAccessLevels , k )
16
17
}
17
18
return & schema.Resource {
18
- Create : resourceGitlabTagProtectionCreate ,
19
- Read : resourceGitlabTagProtectionRead ,
20
- Delete : resourceGitlabTagProtectionDelete ,
19
+ CreateContext : resourceGitlabTagProtectionCreate ,
20
+ ReadContext : resourceGitlabTagProtectionRead ,
21
+ DeleteContext : resourceGitlabTagProtectionDelete ,
21
22
Importer : & schema.ResourceImporter {
22
23
StateContext : schema .ImportStatePassthroughContext ,
23
24
},
@@ -43,7 +44,7 @@ func resourceGitlabTagProtection() *schema.Resource {
43
44
}
44
45
}
45
46
46
- func resourceGitlabTagProtectionCreate (d * schema.ResourceData , meta interface {}) error {
47
+ func resourceGitlabTagProtectionCreate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
47
48
client := meta .(* gitlab.Client )
48
49
project := d .Get ("project" ).(string )
49
50
tag := gitlab .String (d .Get ("tag" ).(string ))
@@ -56,47 +57,47 @@ func resourceGitlabTagProtectionCreate(d *schema.ResourceData, meta interface{})
56
57
57
58
log .Printf ("[DEBUG] create gitlab tag protection on %v for project %s" , options .Name , project )
58
59
59
- tp , _ , err := client .ProtectedTags .ProtectRepositoryTags (project , options )
60
+ tp , _ , err := client .ProtectedTags .ProtectRepositoryTags (project , options , gitlab . WithContext ( ctx ) )
60
61
if err != nil {
61
62
// Remove existing tag protection
62
- _ , err = client .ProtectedTags .UnprotectRepositoryTags (project , * tag )
63
+ _ , err = client .ProtectedTags .UnprotectRepositoryTags (project , * tag , gitlab . WithContext ( ctx ) )
63
64
if err != nil {
64
- return err
65
+ return diag . FromErr ( err )
65
66
}
66
67
// Reprotect tag with updated values
67
- tp , _ , err = client .ProtectedTags .ProtectRepositoryTags (project , options )
68
+ tp , _ , err = client .ProtectedTags .ProtectRepositoryTags (project , options , gitlab . WithContext ( ctx ) )
68
69
if err != nil {
69
- return err
70
+ return diag . FromErr ( err )
70
71
}
71
72
}
72
73
73
74
d .SetId (buildTwoPartID (& project , & tp .Name ))
74
75
75
- return resourceGitlabTagProtectionRead (d , meta )
76
+ return resourceGitlabTagProtectionRead (ctx , d , meta )
76
77
}
77
78
78
- func resourceGitlabTagProtectionRead (d * schema.ResourceData , meta interface {}) error {
79
+ func resourceGitlabTagProtectionRead (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
79
80
client := meta .(* gitlab.Client )
80
81
project , tag , err := projectAndTagFromID (d .Id ())
81
82
if err != nil {
82
- return err
83
+ return diag . FromErr ( err )
83
84
}
84
85
85
86
log .Printf ("[DEBUG] read gitlab tag protection for project %s, tag %s" , project , tag )
86
87
87
- pt , _ , err := client .ProtectedTags .GetProtectedTag (project , tag )
88
+ pt , _ , err := client .ProtectedTags .GetProtectedTag (project , tag , gitlab . WithContext ( ctx ) )
88
89
if err != nil {
89
90
if is404 (err ) {
90
91
log .Printf ("[DEBUG] gitlab tag protection not found %s/%s" , project , tag )
91
92
d .SetId ("" )
92
93
return nil
93
94
}
94
- return err
95
+ return diag . FromErr ( err )
95
96
}
96
97
97
98
accessLevel , ok := tagProtectionAccessLevelNames [pt .CreateAccessLevels [0 ].AccessLevel ]
98
99
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 )
100
101
}
101
102
102
103
d .Set ("project" , project )
@@ -108,15 +109,19 @@ func resourceGitlabTagProtectionRead(d *schema.ResourceData, meta interface{}) e
108
109
return nil
109
110
}
110
111
111
- func resourceGitlabTagProtectionDelete (d * schema.ResourceData , meta interface {}) error {
112
+ func resourceGitlabTagProtectionDelete (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
112
113
client := meta .(* gitlab.Client )
113
114
project := d .Get ("project" ).(string )
114
115
tag := d .Get ("tag" ).(string )
115
116
116
117
log .Printf ("[DEBUG] Delete gitlab protected tag %s for project %s" , tag , project )
117
118
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
120
125
}
121
126
122
127
func projectAndTagFromID (id string ) (string , string , error ) {
0 commit comments