@@ -14,6 +14,9 @@ func resourceGitlabProjectPushRules() *schema.Resource {
1414 Read : resourceGitlabProjectPushRulesRead ,
1515 Update : resourceGitlabProjectPushRulesUpdate ,
1616 Delete : resourceGitlabProjectPushRulesDelete ,
17+ Importer : & schema.ResourceImporter {
18+ State : resourceGitlabProjectPushRulesImporter ,
19+ },
1720 Schema : map [string ]* schema.Schema {
1821 "project" : {
1922 Type : schema .TypeString ,
@@ -107,7 +110,7 @@ func resourceGitlabProjectPushRulesCreate(d *schema.ResourceData, meta interface
107110func resourceGitlabProjectPushRulesRead (d * schema.ResourceData , meta interface {}) error {
108111 client := meta .(* gitlab.Client )
109112 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 )
111114 pushRules , _ , err := client .Projects .GetProjectPushRules (project )
112115 if err != nil {
113116 return err
@@ -126,8 +129,30 @@ func resourceGitlabProjectPushRulesRead(d *schema.ResourceData, meta interface{}
126129func resourceGitlabProjectPushRulesDelete (d * schema.ResourceData , meta interface {}) error {
127130 client := meta .(* gitlab.Client )
128131 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 )
130133 log .Println (project )
131134 _ , err := client .Projects .DeleteProjectPushRule (project )
132135 return err
133136}
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