Skip to content

Commit 594b1c2

Browse files
authored
Add importer to resource_gitlab_project_push_rules (#360)
* Add importer to resource_gitlab_project_push_rules * Fix apparent copy-paste problem and inconsistency in push rules debug logs
1 parent 840a3fb commit 594b1c2

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

gitlab/resource_gitlab_project_push_rules.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
107110
func 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{}
126129
func 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+
}

gitlab/resource_gitlab_project_push_rules_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,29 @@ func TestAccGitlabProjectPushRules_basic(t *testing.T) {
7878
})
7979
}
8080

81+
func TestAccGitlabProjectPushRules_import(t *testing.T) {
82+
rInt := acctest.RandInt()
83+
84+
resource.Test(t, resource.TestCase{
85+
PreCheck: func() { testAccPreCheck(t) },
86+
Providers: testAccProviders,
87+
CheckDestroy: testAccCheckGitlabProjectPushRulesDestroy,
88+
Steps: []resource.TestStep{
89+
{
90+
SkipFunc: isRunningInCE,
91+
Config: testAccGitlabProjectPushRulesConfig(rInt),
92+
},
93+
{
94+
SkipFunc: isRunningInCE,
95+
ResourceName: "gitlab_project_push_rules.foo",
96+
ImportStateId: fmt.Sprintf("foo-%d", rInt),
97+
ImportState: true,
98+
ImportStateVerify: true,
99+
},
100+
},
101+
})
102+
}
103+
81104
func testAccCheckGitlabProjectPushRulesExists(n string, pushRules *gitlab.ProjectPushRules) resource.TestCheckFunc {
82105
return func(s *terraform.State) error {
83106
rs, ok := s.RootModule().Resources[n]

website/docs/r/project_push_rules.html.markdown

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,11 @@ The following arguments are supported:
5353
The resource exports the following attributes:
5454

5555
* `id` - The unique id assigned to the push rules by the GitLab server.
56+
57+
## Import
58+
59+
Project push rules can be imported using the ID of the project or NAMESPACE/PROJECT_NAME, e.g.
60+
61+
```
62+
$ terraform import gitlab_project_push_rules.example richardc/example
63+
```

0 commit comments

Comments
 (0)