Skip to content

Commit 6f4b341

Browse files
committed
resource/gitlab_project_hook: Refactor schema into separate file
1 parent d2f69cc commit 6f4b341

File tree

3 files changed

+151
-118
lines changed

3 files changed

+151
-118
lines changed

docs/resources/project_hook.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ resource "gitlab_project_hook" "example" {
5151

5252
### Read-Only
5353

54+
- `hook_id` (Number) The id of the project hook.
5455
- `id` (String) The ID of this resource.
56+
- `project_id` (Number) The id of the project for the hook.
5557

5658
## Import
5759

internal/provider/resource_gitlab_project_hook.go

Lines changed: 6 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -25,108 +25,7 @@ var _ = registerResource("gitlab_project_hook", func() *schema.Resource {
2525
Importer: &schema.ResourceImporter{
2626
StateContext: resourceGitlabProjectHookStateImporter,
2727
},
28-
29-
Schema: map[string]*schema.Schema{
30-
"project": {
31-
Description: "The name or id of the project to add the hook to.",
32-
Type: schema.TypeString,
33-
Required: true,
34-
},
35-
"url": {
36-
Description: "The url of the hook to invoke.",
37-
Type: schema.TypeString,
38-
Required: true,
39-
},
40-
"token": {
41-
Description: "A token to present when invoking the hook. The token is not available for imported resources.",
42-
Type: schema.TypeString,
43-
Optional: true,
44-
Sensitive: true,
45-
},
46-
"push_events": {
47-
Description: "Invoke the hook for push events.",
48-
Type: schema.TypeBool,
49-
Optional: true,
50-
Default: true,
51-
},
52-
"push_events_branch_filter": {
53-
Description: "Invoke the hook for push events on matching branches only.",
54-
Type: schema.TypeString,
55-
Optional: true,
56-
},
57-
"issues_events": {
58-
Description: "Invoke the hook for issues events.",
59-
Type: schema.TypeBool,
60-
Optional: true,
61-
Default: false,
62-
},
63-
"confidential_issues_events": {
64-
Description: "Invoke the hook for confidential issues events.",
65-
Type: schema.TypeBool,
66-
Optional: true,
67-
Default: false,
68-
},
69-
"merge_requests_events": {
70-
Description: "Invoke the hook for merge requests.",
71-
Type: schema.TypeBool,
72-
Optional: true,
73-
Default: false,
74-
},
75-
"tag_push_events": {
76-
Description: "Invoke the hook for tag push events.",
77-
Type: schema.TypeBool,
78-
Optional: true,
79-
Default: false,
80-
},
81-
"note_events": {
82-
Description: "Invoke the hook for notes events.",
83-
Type: schema.TypeBool,
84-
Optional: true,
85-
Default: false,
86-
},
87-
"confidential_note_events": {
88-
Description: "Invoke the hook for confidential notes events.",
89-
Type: schema.TypeBool,
90-
Optional: true,
91-
Default: false,
92-
},
93-
"job_events": {
94-
Description: "Invoke the hook for job events.",
95-
Type: schema.TypeBool,
96-
Optional: true,
97-
Default: false,
98-
},
99-
"pipeline_events": {
100-
Description: "Invoke the hook for pipeline events.",
101-
Type: schema.TypeBool,
102-
Optional: true,
103-
Default: false,
104-
},
105-
"wiki_page_events": {
106-
Description: "Invoke the hook for wiki page events.",
107-
Type: schema.TypeBool,
108-
Optional: true,
109-
Default: false,
110-
},
111-
"deployment_events": {
112-
Description: "Invoke the hook for deployment events.",
113-
Type: schema.TypeBool,
114-
Optional: true,
115-
Default: false,
116-
},
117-
"releases_events": {
118-
Description: "Invoke the hook for releases events.",
119-
Type: schema.TypeBool,
120-
Optional: true,
121-
Default: false,
122-
},
123-
"enable_ssl_verification": {
124-
Description: "Enable ssl verification when invoking the hook.",
125-
Type: schema.TypeBool,
126-
Optional: true,
127-
Default: true,
128-
},
129-
},
28+
Schema: gitlabProjectHookSchema(),
13029
}
13130
})
13231

@@ -180,28 +79,17 @@ func resourceGitlabProjectHookRead(ctx context.Context, d *schema.ResourceData,
18079
hook, _, err := client.Projects.GetProjectHook(project, hookId, gitlab.WithContext(ctx))
18180
if err != nil {
18281
if is404(err) {
183-
log.Printf("[DEBUG] gitlab project hook not found %s/%d", project, hookId)
82+
log.Printf("[DEBUG] gitlab project hook not found %s/%d, removing from state", project, hookId)
18483
d.SetId("")
18584
return nil
18685
}
18786
return diag.FromErr(err)
18887
}
18988

190-
d.Set("url", hook.URL)
191-
d.Set("push_events", hook.PushEvents)
192-
d.Set("push_events_branch_filter", hook.PushEventsBranchFilter)
193-
d.Set("issues_events", hook.IssuesEvents)
194-
d.Set("confidential_issues_events", hook.ConfidentialIssuesEvents)
195-
d.Set("merge_requests_events", hook.MergeRequestsEvents)
196-
d.Set("tag_push_events", hook.TagPushEvents)
197-
d.Set("note_events", hook.NoteEvents)
198-
d.Set("confidential_note_events", hook.ConfidentialNoteEvents)
199-
d.Set("job_events", hook.JobEvents)
200-
d.Set("pipeline_events", hook.PipelineEvents)
201-
d.Set("wiki_page_events", hook.WikiPageEvents)
202-
d.Set("deployment_events", hook.DeploymentEvents)
203-
d.Set("releases_events", hook.ReleasesEvents)
204-
d.Set("enable_ssl_verification", hook.EnableSSLVerification)
89+
stateMap := gitlabProjectHookToStateMap(project, hook)
90+
if err = setStateMapInResourceData(stateMap, d); err != nil {
91+
return diag.FromErr(err)
92+
}
20593
return nil
20694
}
20795

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package provider
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
5+
"github.com/xanzy/go-gitlab"
6+
)
7+
8+
func gitlabProjectHookSchema() map[string]*schema.Schema {
9+
return map[string]*schema.Schema{
10+
"project": {
11+
Description: "The name or id of the project to add the hook to.",
12+
Type: schema.TypeString,
13+
Required: true,
14+
},
15+
"project_id": {
16+
Description: "The id of the project for the hook.",
17+
Type: schema.TypeInt,
18+
Computed: true,
19+
},
20+
"hook_id": {
21+
Description: "The id of the project hook",
22+
Type: schema.TypeInt,
23+
Computed: true,
24+
},
25+
"url": {
26+
Description: "The url of the hook to invoke.",
27+
Type: schema.TypeString,
28+
Required: true,
29+
},
30+
"token": {
31+
Description: "A token to present when invoking the hook. The token is not available for imported resources.",
32+
Type: schema.TypeString,
33+
Optional: true,
34+
Sensitive: true,
35+
},
36+
"push_events": {
37+
Description: "Invoke the hook for push events.",
38+
Type: schema.TypeBool,
39+
Optional: true,
40+
Default: true,
41+
},
42+
"push_events_branch_filter": {
43+
Description: "Invoke the hook for push events on matching branches only.",
44+
Type: schema.TypeString,
45+
Optional: true,
46+
},
47+
"issues_events": {
48+
Description: "Invoke the hook for issues events.",
49+
Type: schema.TypeBool,
50+
Optional: true,
51+
Default: false,
52+
},
53+
"confidential_issues_events": {
54+
Description: "Invoke the hook for confidential issues events.",
55+
Type: schema.TypeBool,
56+
Optional: true,
57+
Default: false,
58+
},
59+
"merge_requests_events": {
60+
Description: "Invoke the hook for merge requests.",
61+
Type: schema.TypeBool,
62+
Optional: true,
63+
Default: false,
64+
},
65+
"tag_push_events": {
66+
Description: "Invoke the hook for tag push events.",
67+
Type: schema.TypeBool,
68+
Optional: true,
69+
Default: false,
70+
},
71+
"note_events": {
72+
Description: "Invoke the hook for notes events.",
73+
Type: schema.TypeBool,
74+
Optional: true,
75+
Default: false,
76+
},
77+
"confidential_note_events": {
78+
Description: "Invoke the hook for confidential notes events.",
79+
Type: schema.TypeBool,
80+
Optional: true,
81+
Default: false,
82+
},
83+
"job_events": {
84+
Description: "Invoke the hook for job events.",
85+
Type: schema.TypeBool,
86+
Optional: true,
87+
Default: false,
88+
},
89+
"pipeline_events": {
90+
Description: "Invoke the hook for pipeline events.",
91+
Type: schema.TypeBool,
92+
Optional: true,
93+
Default: false,
94+
},
95+
"wiki_page_events": {
96+
Description: "Invoke the hook for wiki page events.",
97+
Type: schema.TypeBool,
98+
Optional: true,
99+
Default: false,
100+
},
101+
"deployment_events": {
102+
Description: "Invoke the hook for deployment events.",
103+
Type: schema.TypeBool,
104+
Optional: true,
105+
Default: false,
106+
},
107+
"releases_events": {
108+
Description: "Invoke the hook for releases events.",
109+
Type: schema.TypeBool,
110+
Optional: true,
111+
Default: false,
112+
},
113+
"enable_ssl_verification": {
114+
Description: "Enable ssl verification when invoking the hook.",
115+
Type: schema.TypeBool,
116+
Optional: true,
117+
Default: true,
118+
},
119+
}
120+
}
121+
122+
func gitlabProjectHookToStateMap(project string, hook *gitlab.ProjectHook) map[string]interface{} {
123+
stateMap := make(map[string]interface{})
124+
stateMap["project"] = project
125+
stateMap["project_id"] = hook.ProjectID
126+
stateMap["hook_id"] = hook.ID
127+
stateMap["url"] = hook.URL
128+
stateMap["push_events"] = hook.PushEvents
129+
stateMap["push_events_branch_filter"] = hook.PushEventsBranchFilter
130+
stateMap["issues_events"] = hook.IssuesEvents
131+
stateMap["confidential_issues_events"] = hook.ConfidentialIssuesEvents
132+
stateMap["merge_requests_events"] = hook.MergeRequestsEvents
133+
stateMap["tag_push_events"] = hook.TagPushEvents
134+
stateMap["note_events"] = hook.NoteEvents
135+
stateMap["confidential_note_events"] = hook.ConfidentialNoteEvents
136+
stateMap["job_events"] = hook.JobEvents
137+
stateMap["pipeline_events"] = hook.PipelineEvents
138+
stateMap["wiki_page_events"] = hook.WikiPageEvents
139+
stateMap["deployment_events"] = hook.DeploymentEvents
140+
stateMap["releases_events"] = hook.ReleasesEvents
141+
stateMap["enable_ssl_verification"] = hook.EnableSSLVerification
142+
return stateMap
143+
}

0 commit comments

Comments
 (0)