|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 12 | + gitlab "github.com/xanzy/go-gitlab" |
| 13 | +) |
| 14 | + |
| 15 | +var validBranchesToBeNotified = []string{ |
| 16 | + "all", "default", "protected", "default_and_protected", |
| 17 | +} |
| 18 | + |
| 19 | +var _ = registerResource("gitlab_service_emails_on_push", func() *schema.Resource { |
| 20 | + return &schema.Resource{ |
| 21 | + Description: `The ` + "`gitlab_service_emails_on_push`" + ` resource allows to manage the lifecycle of a project integration with Emails on Push Service. |
| 22 | +
|
| 23 | +**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/integrations.html#emails-on-push)`, |
| 24 | + |
| 25 | + CreateContext: resourceGitlabServiceEmailsOnPushCreate, |
| 26 | + ReadContext: resourceGitlabServiceEmailsOnPushRead, |
| 27 | + UpdateContext: resourceGitlabServiceEmailsOnPushCreate, |
| 28 | + DeleteContext: resourceGitlabServiceEmailsOnPushDelete, |
| 29 | + Importer: &schema.ResourceImporter{ |
| 30 | + StateContext: schema.ImportStatePassthroughContext, |
| 31 | + }, |
| 32 | + |
| 33 | + Schema: map[string]*schema.Schema{ |
| 34 | + "project": { |
| 35 | + Description: "ID or full-path of the project you want to activate integration on.", |
| 36 | + Type: schema.TypeString, |
| 37 | + Required: true, |
| 38 | + ForceNew: true, |
| 39 | + ValidateFunc: validation.StringIsNotEmpty, |
| 40 | + }, |
| 41 | + "recipients": { |
| 42 | + Description: "Emails separated by whitespace.", |
| 43 | + Type: schema.TypeString, |
| 44 | + Required: true, |
| 45 | + ValidateFunc: validation.StringIsNotEmpty, |
| 46 | + }, |
| 47 | + "disable_diffs": { |
| 48 | + Description: "Disable code diffs.", |
| 49 | + Type: schema.TypeBool, |
| 50 | + Optional: true, |
| 51 | + }, |
| 52 | + "send_from_committer_email": { |
| 53 | + Description: "Send from committer.", |
| 54 | + Type: schema.TypeBool, |
| 55 | + Optional: true, |
| 56 | + }, |
| 57 | + "push_events": { |
| 58 | + Description: "Enable notifications for push events.", |
| 59 | + Type: schema.TypeBool, |
| 60 | + Optional: true, |
| 61 | + }, |
| 62 | + "tag_push_events": { |
| 63 | + Description: "Enable notifications for tag push events.", |
| 64 | + Type: schema.TypeBool, |
| 65 | + Optional: true, |
| 66 | + }, |
| 67 | + "branches_to_be_notified": { |
| 68 | + Description: fmt.Sprintf("Branches to send notifications for. Valid options are %s. Notifications are always fired for tag pushes.", renderValueListForDocs(validBranchesToBeNotified)), |
| 69 | + Type: schema.TypeString, |
| 70 | + Optional: true, |
| 71 | + Default: "all", |
| 72 | + ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validBranchesToBeNotified, false)), |
| 73 | + }, |
| 74 | + "title": { |
| 75 | + Description: "Title of the integration.", |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + }, |
| 79 | + "created_at": { |
| 80 | + Description: "The ISO8601 date/time that this integration was activated at in UTC.", |
| 81 | + Type: schema.TypeString, |
| 82 | + Computed: true, |
| 83 | + }, |
| 84 | + "updated_at": { |
| 85 | + Description: "The ISO8601 date/time that this integration was last updated at in UTC.", |
| 86 | + Type: schema.TypeString, |
| 87 | + Computed: true, |
| 88 | + }, |
| 89 | + "slug": { |
| 90 | + Description: "The name of the integration in lowercase, shortened to 63 bytes, and with everything except 0-9 and a-z replaced with -. No leading / trailing -. Use in URLs, host names and domain names.", |
| 91 | + Type: schema.TypeString, |
| 92 | + Computed: true, |
| 93 | + }, |
| 94 | + "active": { |
| 95 | + Description: "Whether the integration is active.", |
| 96 | + Type: schema.TypeBool, |
| 97 | + Computed: true, |
| 98 | + }, |
| 99 | + }, |
| 100 | + } |
| 101 | +}) |
| 102 | + |
| 103 | +func resourceGitlabServiceEmailsOnPushCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 104 | + client := meta.(*gitlab.Client) |
| 105 | + |
| 106 | + options := &gitlab.SetEmailsOnPushServiceOptions{ |
| 107 | + Recipients: gitlab.String(d.Get("recipients").(string)), |
| 108 | + } |
| 109 | + if v, ok := d.GetOk("disable_diffs"); ok { |
| 110 | + options.DisableDiffs = gitlab.Bool(v.(bool)) |
| 111 | + } |
| 112 | + if v, ok := d.GetOk("send_from_committer_email"); ok { |
| 113 | + options.SendFromCommitterEmail = gitlab.Bool(v.(bool)) |
| 114 | + } |
| 115 | + if v, ok := d.GetOk("push_events"); ok { |
| 116 | + options.PushEvents = gitlab.Bool(v.(bool)) |
| 117 | + } |
| 118 | + if v, ok := d.GetOk("tag_push_events"); ok { |
| 119 | + options.TagPushEvents = gitlab.Bool(v.(bool)) |
| 120 | + } |
| 121 | + if v, ok := d.GetOk("branches_to_be_notified"); ok { |
| 122 | + options.BranchesToBeNotified = gitlab.String(v.(string)) |
| 123 | + } |
| 124 | + |
| 125 | + project := d.Get("project").(string) |
| 126 | + log.Printf("[DEBUG] create gitlab emails on push service for project %s", project) |
| 127 | + |
| 128 | + _, err := client.Services.SetEmailsOnPushService(project, options, gitlab.WithContext(ctx)) |
| 129 | + if err != nil { |
| 130 | + return diag.FromErr(err) |
| 131 | + } |
| 132 | + d.SetId(project) |
| 133 | + |
| 134 | + return resourceGitlabServiceEmailsOnPushRead(ctx, d, meta) |
| 135 | +} |
| 136 | + |
| 137 | +func resourceGitlabServiceEmailsOnPushRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 138 | + client := meta.(*gitlab.Client) |
| 139 | + project := d.Id() |
| 140 | + |
| 141 | + log.Printf("[DEBUG] read gitlab emails on push service for project %s", project) |
| 142 | + |
| 143 | + service, _, err := client.Services.GetEmailsOnPushService(project, gitlab.WithContext(ctx)) |
| 144 | + if err != nil { |
| 145 | + if is404(err) { |
| 146 | + log.Printf("[DEBUG] gitlab emails on push service not found for project %s, removing from state", project) |
| 147 | + d.SetId("") |
| 148 | + return nil |
| 149 | + } |
| 150 | + return diag.FromErr(err) |
| 151 | + } |
| 152 | + |
| 153 | + d.Set("project", project) |
| 154 | + d.Set("recipients", service.Properties.Recipients) |
| 155 | + d.Set("branches_to_be_notified", service.Properties.BranchesToBeNotified) |
| 156 | + d.Set("disable_diffs", service.Properties.DisableDiffs) |
| 157 | + d.Set("push_events", service.Properties.PushEvents) |
| 158 | + d.Set("send_from_committer_email", service.Properties.SendFromCommitterEmail) |
| 159 | + d.Set("tag_push_events", service.Properties.TagPushEvents) |
| 160 | + d.Set("active", service.Active) |
| 161 | + d.Set("slug", service.Slug) |
| 162 | + d.Set("title", service.Title) |
| 163 | + d.Set("created_at", service.CreatedAt.Format(time.RFC3339)) |
| 164 | + if service.UpdatedAt != nil { |
| 165 | + d.Set("updated_at", service.UpdatedAt.Format(time.RFC3339)) |
| 166 | + } |
| 167 | + |
| 168 | + return nil |
| 169 | +} |
| 170 | + |
| 171 | +func resourceGitlabServiceEmailsOnPushDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 172 | + client := meta.(*gitlab.Client) |
| 173 | + project := d.Id() |
| 174 | + |
| 175 | + log.Printf("[DEBUG] delete gitlab emails on push service for project %s", project) |
| 176 | + |
| 177 | + _, err := client.Services.DeleteEmailsOnPushService(project, gitlab.WithContext(ctx)) |
| 178 | + if err != nil { |
| 179 | + return diag.FromErr(err) |
| 180 | + } |
| 181 | + |
| 182 | + return nil |
| 183 | +} |
0 commit comments