@@ -14,6 +14,9 @@ func resourceGitlabProjectPushRules() *schema.Resource {
14
14
Read : resourceGitlabProjectPushRulesRead ,
15
15
Update : resourceGitlabProjectPushRulesUpdate ,
16
16
Delete : resourceGitlabProjectPushRulesDelete ,
17
+ Importer : & schema.ResourceImporter {
18
+ State : resourceGitlabProjectPushRulesImporter ,
19
+ },
17
20
Schema : map [string ]* schema.Schema {
18
21
"project" : {
19
22
Type : schema .TypeString ,
@@ -107,7 +110,7 @@ func resourceGitlabProjectPushRulesCreate(d *schema.ResourceData, meta interface
107
110
func resourceGitlabProjectPushRulesRead (d * schema.ResourceData , meta interface {}) error {
108
111
client := meta .(* gitlab.Client )
109
112
project := d .Get ("project" ).(string )
110
- log .Printf ("[DEBUG] read gitlab project %s" , project )
113
+ log .Printf ("[DEBUG] read gitlab project %s push rules " , project )
111
114
pushRules , _ , err := client .Projects .GetProjectPushRules (project )
112
115
if err != nil {
113
116
return err
@@ -126,8 +129,30 @@ func resourceGitlabProjectPushRulesRead(d *schema.ResourceData, meta interface{}
126
129
func resourceGitlabProjectPushRulesDelete (d * schema.ResourceData , meta interface {}) error {
127
130
client := meta .(* gitlab.Client )
128
131
project := d .Get ("project" ).(string )
129
- log .Printf ("[DEBUG] Delete gitlab project push rules %s " , project )
132
+ log .Printf ("[DEBUG] Delete gitlab project %s push rules" , project )
130
133
log .Println (project )
131
134
_ , err := client .Projects .DeleteProjectPushRule (project )
132
135
return err
133
136
}
137
+
138
+ func resourceGitlabProjectPushRulesImporter (d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
139
+ // Push rule IDs in GitLab are internal identifiers. For ease of use, we allow importing using the project ID instead.
140
+ // This means we need to lookup the push rule ID during import.
141
+
142
+ client := meta .(* gitlab.Client )
143
+ project := d .Id ()
144
+
145
+ log .Printf ("[DEBUG] read gitlab project %s push rules" , project )
146
+
147
+ pushRules , _ , err := client .Projects .GetProjectPushRules (project )
148
+ if err != nil {
149
+ return nil , err
150
+ }
151
+
152
+ d .SetId (fmt .Sprintf ("%d" , pushRules .ID ))
153
+
154
+ // Since project is used as a primary key in the Read function, we set that too.
155
+ d .Set ("project" , project )
156
+
157
+ return []* schema.ResourceData {d }, nil
158
+ }
0 commit comments