|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strconv" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + gitlab "github.com/xanzy/go-gitlab" |
| 13 | +) |
| 14 | + |
| 15 | +var _ = registerResource("gitlab_system_hook", func() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Description: `The ` + "`gitlab_system_hook`" + ` resource allows to manage the lifecycle of a system hook. |
| 18 | +
|
| 19 | +-> This resource requires GitLab 14.9 |
| 20 | +
|
| 21 | +**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/system_hooks.html)`, |
| 22 | + |
| 23 | + CreateContext: resourceGitlabSystemHookCreate, |
| 24 | + ReadContext: resourceGitlabSystemHookRead, |
| 25 | + DeleteContext: resourceGitlabSystemHookDelete, |
| 26 | + Importer: &schema.ResourceImporter{ |
| 27 | + StateContext: schema.ImportStatePassthroughContext, |
| 28 | + }, |
| 29 | + |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + "url": { |
| 32 | + Description: "The hook URL.", |
| 33 | + Type: schema.TypeString, |
| 34 | + Required: true, |
| 35 | + ForceNew: true, |
| 36 | + }, |
| 37 | + "token": { |
| 38 | + Description: "Secret token to validate received payloads; this isn’t returned in the response. This attribute is not available for imported resources.", |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + Sensitive: true, |
| 42 | + ForceNew: true, |
| 43 | + }, |
| 44 | + "push_events": { |
| 45 | + Description: "When true, the hook fires on push events.", |
| 46 | + Type: schema.TypeBool, |
| 47 | + Optional: true, |
| 48 | + ForceNew: true, |
| 49 | + }, |
| 50 | + "tag_push_events": { |
| 51 | + Description: "When true, the hook fires on new tags being pushed.", |
| 52 | + Type: schema.TypeBool, |
| 53 | + Optional: true, |
| 54 | + ForceNew: true, |
| 55 | + }, |
| 56 | + "merge_requests_events": { |
| 57 | + Description: "Trigger hook on merge requests events.", |
| 58 | + Type: schema.TypeBool, |
| 59 | + Optional: true, |
| 60 | + ForceNew: true, |
| 61 | + }, |
| 62 | + "repository_update_events": { |
| 63 | + Description: "Trigger hook on repository update events.", |
| 64 | + Type: schema.TypeBool, |
| 65 | + Optional: true, |
| 66 | + ForceNew: true, |
| 67 | + }, |
| 68 | + "enable_ssl_verification": { |
| 69 | + Description: "Do SSL verification when triggering the hook.", |
| 70 | + Type: schema.TypeBool, |
| 71 | + Optional: true, |
| 72 | + ForceNew: true, |
| 73 | + }, |
| 74 | + "created_at": { |
| 75 | + Description: "The date and time the hook was created in ISO8601 format.", |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | +}) |
| 82 | + |
| 83 | +func resourceGitlabSystemHookCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 84 | + client := meta.(*gitlab.Client) |
| 85 | + |
| 86 | + options := &gitlab.AddHookOptions{ |
| 87 | + URL: gitlab.String(d.Get("url").(string)), |
| 88 | + } |
| 89 | + // NOTE: `GetOkExists()` is deprecated, but until there is a replacement we need to use it. |
| 90 | + // see https://github.com/hashicorp/terraform-plugin-sdk/pull/350#issuecomment-597888969 |
| 91 | + |
| 92 | + // nolint:staticcheck // SA1019 ignore deprecated GetOkExists |
| 93 | + // lintignore: XR001 // TODO: replace with alternative for GetOkExists |
| 94 | + if v, ok := d.GetOkExists("token"); ok { |
| 95 | + options.Token = gitlab.String(v.(string)) |
| 96 | + } |
| 97 | + // nolint:staticcheck // SA1019 ignore deprecated GetOkExists |
| 98 | + // lintignore: XR001 // TODO: replace with alternative for GetOkExists |
| 99 | + if v, ok := d.GetOkExists("push_events"); ok { |
| 100 | + options.PushEvents = gitlab.Bool(v.(bool)) |
| 101 | + } |
| 102 | + // nolint:staticcheck // SA1019 ignore deprecated GetOkExists |
| 103 | + // lintignore: XR001 // TODO: replace with alternative for GetOkExists |
| 104 | + if v, ok := d.GetOkExists("tag_push_events"); ok { |
| 105 | + options.TagPushEvents = gitlab.Bool(v.(bool)) |
| 106 | + } |
| 107 | + // nolint:staticcheck // SA1019 ignore deprecated GetOkExists |
| 108 | + // lintignore: XR001 // TODO: replace with alternative for GetOkExists |
| 109 | + if v, ok := d.GetOkExists("merge_requests_events"); ok { |
| 110 | + options.MergeRequestsEvents = gitlab.Bool(v.(bool)) |
| 111 | + } |
| 112 | + // nolint:staticcheck // SA1019 ignore deprecated GetOkExists |
| 113 | + // lintignore: XR001 // TODO: replace with alternative for GetOkExists |
| 114 | + if v, ok := d.GetOkExists("repository_update_events"); ok { |
| 115 | + options.RepositoryUpdateEvents = gitlab.Bool(v.(bool)) |
| 116 | + } |
| 117 | + // nolint:staticcheck // SA1019 ignore deprecated GetOkExists |
| 118 | + // lintignore: XR001 // TODO: replace with alternative for GetOkExists |
| 119 | + if v, ok := d.GetOkExists("enable_ssl_verification"); ok { |
| 120 | + options.EnableSSLVerification = gitlab.Bool(v.(bool)) |
| 121 | + } |
| 122 | + |
| 123 | + log.Printf("[DEBUG] create gitlab system hook %q", *options.URL) |
| 124 | + |
| 125 | + hook, _, err := client.SystemHooks.AddHook(options, gitlab.WithContext(ctx)) |
| 126 | + if err != nil { |
| 127 | + return diag.FromErr(err) |
| 128 | + } |
| 129 | + |
| 130 | + d.SetId(fmt.Sprintf("%d", hook.ID)) |
| 131 | + d.Set("token", options.Token) |
| 132 | + return resourceGitlabSystemHookRead(ctx, d, meta) |
| 133 | +} |
| 134 | + |
| 135 | +func resourceGitlabSystemHookRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 136 | + client := meta.(*gitlab.Client) |
| 137 | + hookID, err := strconv.Atoi(d.Id()) |
| 138 | + if err != nil { |
| 139 | + return diag.FromErr(err) |
| 140 | + } |
| 141 | + log.Printf("[DEBUG] read gitlab system hook %d", hookID) |
| 142 | + |
| 143 | + hook, _, err := client.SystemHooks.GetHook(hookID, gitlab.WithContext(ctx)) |
| 144 | + if err != nil { |
| 145 | + if is404(err) { |
| 146 | + log.Printf("[DEBUG] gitlab system hook not found %d, removing from state", hookID) |
| 147 | + d.SetId("") |
| 148 | + return nil |
| 149 | + } |
| 150 | + return diag.FromErr(err) |
| 151 | + } |
| 152 | + |
| 153 | + d.Set("url", hook.URL) |
| 154 | + d.Set("push_events", hook.PushEvents) |
| 155 | + d.Set("tag_push_events", hook.TagPushEvents) |
| 156 | + d.Set("merge_requests_events", hook.MergeRequestsEvents) |
| 157 | + d.Set("repository_update_events", hook.RepositoryUpdateEvents) |
| 158 | + d.Set("enable_ssl_verification", hook.EnableSSLVerification) |
| 159 | + d.Set("created_at", hook.CreatedAt.Format(time.RFC3339)) |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +func resourceGitlabSystemHookDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 164 | + client := meta.(*gitlab.Client) |
| 165 | + hookID, err := strconv.Atoi(d.Id()) |
| 166 | + if err != nil { |
| 167 | + return diag.FromErr(err) |
| 168 | + } |
| 169 | + log.Printf("[DEBUG] Delete gitlab system hook %s", d.Id()) |
| 170 | + |
| 171 | + _, err = client.SystemHooks.DeleteHook(hookID, gitlab.WithContext(ctx)) |
| 172 | + if err != nil { |
| 173 | + return diag.FromErr(err) |
| 174 | + } |
| 175 | + |
| 176 | + return nil |
| 177 | +} |
0 commit comments