|
| 1 | +package spacelift |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + |
| 10 | + "github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal" |
| 11 | + "github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/structs" |
| 12 | +) |
| 13 | + |
| 14 | +func resourceAuditTrailWebhook() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Description: "" + |
| 17 | + "`spacelift_audit_trail_webhook` represents a webhook endpoint to which Spacelift " + |
| 18 | + "sends POST requests about audit events.", |
| 19 | + CreateContext: resourceAuditTrailWebhookCreate, |
| 20 | + ReadContext: resourceAuditTrailWebhookRead, |
| 21 | + UpdateContext: resourceAuditTrailWebhookUpdate, |
| 22 | + DeleteContext: resourceAuditTrailWebhookDelete, |
| 23 | + Importer: &schema.ResourceImporter{ |
| 24 | + StateContext: schema.ImportStatePassthroughContext, |
| 25 | + }, |
| 26 | + |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "enabled": { |
| 29 | + Type: schema.TypeBool, |
| 30 | + Required: true, |
| 31 | + Description: "`enabled` determines whether the webhook is enabled. If it is not, " + |
| 32 | + "Spacelift will not send any requests to the endpoint.", |
| 33 | + }, |
| 34 | + "endpoint": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + Description: "`endpoint` is the URL to which Spacelift will send POST requests " + |
| 38 | + "about audit events.", |
| 39 | + }, |
| 40 | + "include_runs": { |
| 41 | + Type: schema.TypeBool, |
| 42 | + Optional: true, |
| 43 | + Description: "`include_runs` determines whether the webhook should include " + |
| 44 | + "information about the run that triggered the event.", |
| 45 | + }, |
| 46 | + "secret": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Required: true, |
| 49 | + Sensitive: true, |
| 50 | + Description: "`secret` is a secret that Spacelift will send with the request", |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func resourceAuditTrailWebhookCreate(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics { |
| 57 | + var mutation struct { |
| 58 | + AuditTrailWebhook *structs.AuditTrailWebhook `graphql:"auditTrailSetWebhook(input: $input)"` |
| 59 | + } |
| 60 | + variables := map[string]interface{}{ |
| 61 | + "input": structs.AuditTrailWebhookInput{ |
| 62 | + Enabled: toBool(data.Get("enabled")), |
| 63 | + Endpoint: toString(data.Get("endpoint")), |
| 64 | + IncludeRuns: toBool(data.Get("include_runs")), |
| 65 | + Secret: toString(data.Get("secret")), |
| 66 | + }, |
| 67 | + } |
| 68 | + if err := i.(*internal.Client).Mutate(ctx, "AuditTrailWebhookCreate", &mutation, variables); err != nil { |
| 69 | + return diag.Errorf("could not create audit trail webhook: %v", internal.FromSpaceliftError(err)) |
| 70 | + } |
| 71 | + |
| 72 | + data.SetId(time.Now().String()) |
| 73 | + |
| 74 | + return resourceAuditTrailWebhookRead(ctx, data, i) |
| 75 | +} |
| 76 | + |
| 77 | +func resourceAuditTrailWebhookRead(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics { |
| 78 | + var query struct { |
| 79 | + AuditTrailWebhook *structs.AuditTrailWebhook `graphql:"auditTrailWebhook"` |
| 80 | + } |
| 81 | + if err := i.(*internal.Client).Query(ctx, "AuditTrailWebhookRead", &query, nil); err != nil { |
| 82 | + return diag.Errorf("could not query for audit trail webhook: %v", internal.FromSpaceliftError(err)) |
| 83 | + } |
| 84 | + |
| 85 | + if query.AuditTrailWebhook == nil { |
| 86 | + data.SetId("") |
| 87 | + return nil |
| 88 | + } |
| 89 | + |
| 90 | + data.Set("enabled", query.AuditTrailWebhook.Enabled) |
| 91 | + data.Set("endpoint", query.AuditTrailWebhook.Endpoint) |
| 92 | + data.Set("include_runs", query.AuditTrailWebhook.IncludeRuns) |
| 93 | + data.Set("secret", query.AuditTrailWebhook.Secret) |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func resourceAuditTrailWebhookUpdate(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics { |
| 99 | + var mutation struct { |
| 100 | + AuditTrailWebhook *structs.AuditTrailWebhook `graphql:"auditTrailSetWebhook(input: $input)"` |
| 101 | + } |
| 102 | + variables := map[string]interface{}{ |
| 103 | + "input": structs.AuditTrailWebhookInput{ |
| 104 | + Enabled: toBool(data.Get("enabled")), |
| 105 | + Endpoint: toString(data.Get("endpoint")), |
| 106 | + IncludeRuns: toBool(data.Get("include_runs")), |
| 107 | + Secret: toString(data.Get("secret")), |
| 108 | + }, |
| 109 | + } |
| 110 | + if err := i.(*internal.Client).Mutate(ctx, "AuditTrailWebhookUpdate", &mutation, variables); err != nil { |
| 111 | + return diag.Errorf("could not update audit trail webhook: %v", internal.FromSpaceliftError(err)) |
| 112 | + } |
| 113 | + |
| 114 | + return resourceAuditTrailWebhookRead(ctx, data, i) |
| 115 | +} |
| 116 | + |
| 117 | +func resourceAuditTrailWebhookDelete(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics { |
| 118 | + var mutation struct { |
| 119 | + AuditTrailWebhook *structs.AuditTrailWebhook `graphql:"auditTrailDeleteWebhook"` |
| 120 | + } |
| 121 | + if err := i.(*internal.Client).Mutate(ctx, "AuditTrailWebhookDelete", &mutation, nil); err != nil { |
| 122 | + return diag.Errorf("could not delete audit trail webhook: %v", internal.FromSpaceliftError(err)) |
| 123 | + } |
| 124 | + |
| 125 | + data.SetId("") |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
0 commit comments