|
| 1 | +package sysdig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + "time" |
| 7 | + |
| 8 | + v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func resourceSysdigMonitorNotificationChannelMSTeams() *schema.Resource { |
| 15 | + timeout := 5 * time.Minute |
| 16 | + |
| 17 | + return &schema.Resource{ |
| 18 | + CreateContext: resourceSysdigMonitorNotificationChannelMSTeamsCreate, |
| 19 | + UpdateContext: resourceSysdigMonitorNotificationChannelMSTeamsUpdate, |
| 20 | + ReadContext: resourceSysdigMonitorNotificationChannelMSTeamsRead, |
| 21 | + DeleteContext: resourceSysdigMonitorNotificationChannelMSTeamsDelete, |
| 22 | + Importer: &schema.ResourceImporter{ |
| 23 | + StateContext: schema.ImportStatePassthroughContext, |
| 24 | + }, |
| 25 | + |
| 26 | + Timeouts: &schema.ResourceTimeout{ |
| 27 | + Create: schema.DefaultTimeout(timeout), |
| 28 | + Update: schema.DefaultTimeout(timeout), |
| 29 | + Read: schema.DefaultTimeout(timeout), |
| 30 | + Delete: schema.DefaultTimeout(timeout), |
| 31 | + }, |
| 32 | + |
| 33 | + Schema: createMonitorNotificationChannelSchema(map[string]*schema.Schema{ |
| 34 | + "url": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + }, |
| 38 | + }), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func resourceSysdigMonitorNotificationChannelMSTeamsCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 43 | + clients := meta.(SysdigClients) |
| 44 | + client, err := getMonitorNotificationChannelClient(clients) |
| 45 | + if err != nil { |
| 46 | + return diag.FromErr(err) |
| 47 | + } |
| 48 | + |
| 49 | + teamID, err := client.CurrentTeamID(ctx) |
| 50 | + if err != nil { |
| 51 | + return diag.FromErr(err) |
| 52 | + } |
| 53 | + |
| 54 | + notificationChannel, err := monitorNotificationChannelMSTeamsFromResourceData(d, teamID) |
| 55 | + if err != nil { |
| 56 | + return diag.FromErr(err) |
| 57 | + } |
| 58 | + |
| 59 | + notificationChannel, err = client.CreateNotificationChannel(ctx, notificationChannel) |
| 60 | + if err != nil { |
| 61 | + return diag.FromErr(err) |
| 62 | + } |
| 63 | + |
| 64 | + d.SetId(strconv.Itoa(notificationChannel.ID)) |
| 65 | + |
| 66 | + return resourceSysdigMonitorNotificationChannelMSTeamsRead(ctx, d, meta) |
| 67 | +} |
| 68 | + |
| 69 | +func resourceSysdigMonitorNotificationChannelMSTeamsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 70 | + client, err := getMonitorNotificationChannelClient(meta.(SysdigClients)) |
| 71 | + if err != nil { |
| 72 | + return diag.FromErr(err) |
| 73 | + } |
| 74 | + |
| 75 | + id, err := strconv.Atoi(d.Id()) |
| 76 | + if err != nil { |
| 77 | + return diag.FromErr(err) |
| 78 | + } |
| 79 | + |
| 80 | + nc, err := client.GetNotificationChannelById(ctx, id) |
| 81 | + |
| 82 | + if err != nil { |
| 83 | + d.SetId("") |
| 84 | + return diag.FromErr(err) |
| 85 | + } |
| 86 | + |
| 87 | + err = monitorNotificationChannelMSTeamsToResourceData(&nc, d) |
| 88 | + if err != nil { |
| 89 | + return diag.FromErr(err) |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func resourceSysdigMonitorNotificationChannelMSTeamsUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 96 | + clients := meta.(SysdigClients) |
| 97 | + client, err := getMonitorNotificationChannelClient(clients) |
| 98 | + if err != nil { |
| 99 | + return diag.FromErr(err) |
| 100 | + } |
| 101 | + |
| 102 | + teamID, err := client.CurrentTeamID(ctx) |
| 103 | + if err != nil { |
| 104 | + return diag.FromErr(err) |
| 105 | + } |
| 106 | + |
| 107 | + nc, err := monitorNotificationChannelMSTeamsFromResourceData(d, teamID) |
| 108 | + if err != nil { |
| 109 | + return diag.FromErr(err) |
| 110 | + } |
| 111 | + |
| 112 | + nc.Version = d.Get("version").(int) |
| 113 | + nc.ID, err = strconv.Atoi(d.Id()) |
| 114 | + if err != nil { |
| 115 | + return diag.FromErr(err) |
| 116 | + } |
| 117 | + |
| 118 | + _, err = client.UpdateNotificationChannel(ctx, nc) |
| 119 | + if err != nil { |
| 120 | + return diag.FromErr(err) |
| 121 | + } |
| 122 | + |
| 123 | + return resourceSysdigMonitorNotificationChannelMSTeamsRead(ctx, d, meta) |
| 124 | +} |
| 125 | + |
| 126 | +func resourceSysdigMonitorNotificationChannelMSTeamsDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 127 | + client, err := getMonitorNotificationChannelClient(meta.(SysdigClients)) |
| 128 | + if err != nil { |
| 129 | + return diag.FromErr(err) |
| 130 | + } |
| 131 | + |
| 132 | + id, err := strconv.Atoi(d.Id()) |
| 133 | + if err != nil { |
| 134 | + return diag.FromErr(err) |
| 135 | + } |
| 136 | + |
| 137 | + err = client.DeleteNotificationChannel(ctx, id) |
| 138 | + if err != nil { |
| 139 | + return diag.FromErr(err) |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +func monitorNotificationChannelMSTeamsFromResourceData(d *schema.ResourceData, teamID int) (nc v2.NotificationChannel, err error) { |
| 146 | + nc, err = monitorNotificationChannelFromResourceData(d, teamID) |
| 147 | + if err != nil { |
| 148 | + return |
| 149 | + } |
| 150 | + |
| 151 | + nc.Type = NOTIFICATION_CHANNEL_TYPE_MS_TEAMS |
| 152 | + nc.Options.Url = d.Get("url").(string) |
| 153 | + return |
| 154 | +} |
| 155 | + |
| 156 | +func monitorNotificationChannelMSTeamsToResourceData(nc *v2.NotificationChannel, d *schema.ResourceData) (err error) { |
| 157 | + err = monitorNotificationChannelToResourceData(nc, d) |
| 158 | + if err != nil { |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + _ = d.Set("url", nc.Options.Url) |
| 163 | + |
| 164 | + return |
| 165 | +} |
0 commit comments