|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 9 | + gitlab "github.com/xanzy/go-gitlab" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceGitlabServiceMicrosoftTeams() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Create: resourceGitlabServiceMicrosoftTeamsCreate, |
| 15 | + Read: resourceGitlabServiceMicrosoftTeamsRead, |
| 16 | + Update: resourceGitlabServiceMicrosoftTeamsUpdate, |
| 17 | + Delete: resourceGitlabServiceMicrosoftTeamsDelete, |
| 18 | + Importer: &schema.ResourceImporter{ |
| 19 | + State: schema.ImportStatePassthrough, |
| 20 | + }, |
| 21 | + |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "project": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + ForceNew: true, |
| 27 | + }, |
| 28 | + "created_at": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Computed: true, |
| 31 | + }, |
| 32 | + "updated_at": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Computed: true, |
| 35 | + }, |
| 36 | + "active": { |
| 37 | + Type: schema.TypeBool, |
| 38 | + Computed: true, |
| 39 | + }, |
| 40 | + "webhook": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Required: true, |
| 43 | + ValidateFunc: validateURLFunc, |
| 44 | + }, |
| 45 | + "notify_only_broken_pipelines": { |
| 46 | + Type: schema.TypeBool, |
| 47 | + Optional: true, |
| 48 | + }, |
| 49 | + "branches_to_be_notified": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Optional: true, |
| 52 | + }, |
| 53 | + "push_events": { |
| 54 | + Type: schema.TypeBool, |
| 55 | + Optional: true, |
| 56 | + }, |
| 57 | + "issues_events": { |
| 58 | + Type: schema.TypeBool, |
| 59 | + Optional: true, |
| 60 | + }, |
| 61 | + "confidential_issues_events": { |
| 62 | + Type: schema.TypeBool, |
| 63 | + Optional: true, |
| 64 | + }, |
| 65 | + "merge_requests_events": { |
| 66 | + Type: schema.TypeBool, |
| 67 | + Optional: true, |
| 68 | + }, |
| 69 | + "tag_push_events": { |
| 70 | + Type: schema.TypeBool, |
| 71 | + Optional: true, |
| 72 | + }, |
| 73 | + "note_events": { |
| 74 | + Type: schema.TypeBool, |
| 75 | + Optional: true, |
| 76 | + }, |
| 77 | + "confidential_note_events": { |
| 78 | + Type: schema.TypeBool, |
| 79 | + Optional: true, |
| 80 | + }, |
| 81 | + "pipeline_events": { |
| 82 | + Type: schema.TypeBool, |
| 83 | + Optional: true, |
| 84 | + }, |
| 85 | + "wiki_page_events": { |
| 86 | + Type: schema.TypeBool, |
| 87 | + Optional: true, |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func resourceGitlabServiceMicrosoftTeamsCreate(d *schema.ResourceData, meta interface{}) error { |
| 94 | + client := meta.(*gitlab.Client) |
| 95 | + project := d.Get("project").(string) |
| 96 | + d.SetId(project) |
| 97 | + |
| 98 | + options := &gitlab.SetMicrosoftTeamsServiceOptions{ |
| 99 | + WebHook: gitlab.String(d.Get("webhook").(string)), |
| 100 | + NotifyOnlyBrokenPipelines: gitlab.Bool(d.Get("notify_only_broken_pipelines").(bool)), |
| 101 | + BranchesToBeNotified: gitlab.String(d.Get("branches_to_be_notified").(string)), |
| 102 | + PushEvents: gitlab.Bool(d.Get("push_events").(bool)), |
| 103 | + IssuesEvents: gitlab.Bool(d.Get("issues_events").(bool)), |
| 104 | + ConfidentialIssuesEvents: gitlab.Bool(d.Get("confidential_issues_events").(bool)), |
| 105 | + MergeRequestsEvents: gitlab.Bool(d.Get("merge_requests_events").(bool)), |
| 106 | + TagPushEvents: gitlab.Bool(d.Get("tag_push_events").(bool)), |
| 107 | + NoteEvents: gitlab.Bool(d.Get("note_events").(bool)), |
| 108 | + ConfidentialNoteEvents: gitlab.Bool(d.Get("confidential_note_events").(bool)), |
| 109 | + PipelineEvents: gitlab.Bool(d.Get("pipeline_events").(bool)), |
| 110 | + WikiPageEvents: gitlab.Bool(d.Get("wiki_page_events").(bool)), |
| 111 | + } |
| 112 | + |
| 113 | + log.Printf("[DEBUG] Create Gitlab Microsoft Teams service") |
| 114 | + |
| 115 | + if _, err := client.Services.SetMicrosoftTeamsService(project, options); err != nil { |
| 116 | + return fmt.Errorf("couldn't create Gitlab Microsoft Teams service: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + return resourceGitlabServiceMicrosoftTeamsRead(d, meta) |
| 120 | +} |
| 121 | + |
| 122 | +func resourceGitlabServiceMicrosoftTeamsRead(d *schema.ResourceData, meta interface{}) error { |
| 123 | + client := meta.(*gitlab.Client) |
| 124 | + project := d.Id() |
| 125 | + |
| 126 | + p, resp, err := client.Projects.GetProject(project, nil) |
| 127 | + if err != nil { |
| 128 | + if resp != nil && resp.StatusCode == http.StatusNotFound { |
| 129 | + log.Printf("[DEBUG] Removing Gitlab Microsoft Teams service %s because project %s not found", d.Id(), p.Name) |
| 130 | + d.SetId("") |
| 131 | + return nil |
| 132 | + } |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + log.Printf("[DEBUG] Read Gitlab Microsoft Teams service for project %s", d.Id()) |
| 137 | + |
| 138 | + teamsService, _, err := client.Services.GetMicrosoftTeamsService(project) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + d.Set("project", project) |
| 144 | + d.Set("created_at", teamsService.CreatedAt.String()) |
| 145 | + d.Set("updated_at", teamsService.UpdatedAt.String()) |
| 146 | + d.Set("active", teamsService.Active) |
| 147 | + d.Set("webhook", teamsService.Properties.WebHook) |
| 148 | + d.Set("notify_only_broken_pipelines", teamsService.Properties.NotifyOnlyBrokenPipelines) |
| 149 | + d.Set("branches_to_be_notified", teamsService.Properties.BranchesToBeNotified) |
| 150 | + d.Set("push_events", teamsService.PushEvents) |
| 151 | + d.Set("issues_events", teamsService.IssuesEvents) |
| 152 | + d.Set("confidential_issues_events", teamsService.ConfidentialIssuesEvents) |
| 153 | + d.Set("merge_requests_events", teamsService.MergeRequestsEvents) |
| 154 | + d.Set("tag_push_events", teamsService.TagPushEvents) |
| 155 | + d.Set("note_events", teamsService.NoteEvents) |
| 156 | + d.Set("confidential_note_events", teamsService.ConfidentialNoteEvents) |
| 157 | + d.Set("pipeline_events", teamsService.PipelineEvents) |
| 158 | + d.Set("wiki_page_events", teamsService.WikiPageEvents) |
| 159 | + |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +func resourceGitlabServiceMicrosoftTeamsUpdate(d *schema.ResourceData, meta interface{}) error { |
| 164 | + return resourceGitlabServiceMicrosoftTeamsCreate(d, meta) |
| 165 | +} |
| 166 | + |
| 167 | +func resourceGitlabServiceMicrosoftTeamsDelete(d *schema.ResourceData, meta interface{}) error { |
| 168 | + client := meta.(*gitlab.Client) |
| 169 | + project := d.Id() |
| 170 | + |
| 171 | + log.Printf("[DEBUG] Delete Gitlab Microsoft Teams service for project %s", d.Id()) |
| 172 | + |
| 173 | + _, err := client.Services.DeleteMicrosoftTeamsService(project) |
| 174 | + return err |
| 175 | +} |
0 commit comments