-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(audit-trail): add support for audit trail to terraform provider
Signed-off-by: Michal Wasilewski <[email protected]>
- Loading branch information
Showing
7 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "spacelift_audit_trail_webhook Resource - terraform-provider-spacelift" | ||
subcategory: "" | ||
description: |- | ||
spacelift_audit_trail_webhook represents a webhook endpoint to which Spacelift sends the POST request about events the user wants to track. | ||
--- | ||
|
||
# spacelift_audit_trail_webhook (Resource) | ||
|
||
`spacelift_audit_trail_webhook` represents a webhook endpoint to which Spacelift sends the POST request about events the user wants to track. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "spacelift_audit_trail_webhook" "example" { | ||
endpoint = "https://example.com" | ||
enabled = true | ||
secret = "mysecretkey" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `enabled` (Boolean) | ||
- `endpoint` (String) | ||
- `secret` (String, Sensitive) | ||
|
||
### Optional | ||
|
||
- `include_runs` (Boolean) | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
resource "spacelift_audit_trail_webhook" "example" { | ||
endpoint = "https://example.com" | ||
enabled = true | ||
secret = "mysecretkey" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package structs | ||
|
||
type AuditTrailWebhook struct { | ||
Enabled bool `graphql:"enabled"` | ||
Endpoint string `graphql:"endpoint"` | ||
IncludeRuns bool `graphql:"includeRuns"` | ||
Secret string `graphql:"secret"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package structs | ||
|
||
import "github.com/shurcooL/graphql" | ||
|
||
type AuditTrailWebhookInput struct { | ||
Enabled graphql.Boolean `json:"enabled"` | ||
Endpoint graphql.String `json:"endpoint"` | ||
IncludeRuns graphql.Boolean `json:"includeRuns"` | ||
Secret graphql.String `json:"secret"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package spacelift | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal" | ||
"github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/structs" | ||
) | ||
|
||
func resourceAuditTrailWebhook() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "" + | ||
"`spacelift_audit_trail_webhook` represents a webhook endpoint to which Spacelift " + | ||
"sends the POST request about events the user wants to track.", | ||
CreateContext: resourceAuditTrailWebhookCreate, | ||
ReadContext: resourceAuditTrailWebhookRead, | ||
UpdateContext: resourceAuditTrailWebhookUpdate, | ||
DeleteContext: resourceAuditTrailWebhookDelete, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"enabled": { | ||
Type: schema.TypeBool, | ||
Required: true, | ||
}, | ||
"endpoint": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"include_runs": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
}, | ||
"secret": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Sensitive: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAuditTrailWebhookCreate(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
var mutation struct { | ||
AuditTrailWebhook *structs.AuditTrailWebhook `graphql:"auditTrailSetWebhook(input: $input)"` | ||
} | ||
variables := map[string]interface{}{ | ||
"input": structs.AuditTrailWebhookInput{ | ||
Enabled: toBool(data.Get("enabled")), | ||
Endpoint: toString(data.Get("endpoint")), | ||
IncludeRuns: toBool(data.Get("include_runs")), | ||
Secret: toString(data.Get("secret")), | ||
}, | ||
} | ||
if err := i.(*internal.Client).Mutate(ctx, "AuditTrailWebhookCreate", &mutation, variables); err != nil { | ||
return diag.Errorf("could not create audit trail webhook: %v", internal.FromSpaceliftError(err)) | ||
} | ||
|
||
data.SetId(time.Now().String()) | ||
|
||
return resourceAuditTrailWebhookRead(ctx, data, i) | ||
} | ||
|
||
func resourceAuditTrailWebhookRead(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
var query struct { | ||
AuditTrailWebhook *structs.AuditTrailWebhook `graphql:"auditTrailWebhook"` | ||
} | ||
if err := i.(*internal.Client).Query(ctx, "AuditTrailWebhookRead", &query, nil); err != nil { | ||
return diag.Errorf("could not query for audit trail webhook: %v", internal.FromSpaceliftError(err)) | ||
} | ||
|
||
if query.AuditTrailWebhook == nil { | ||
data.SetId("") | ||
return nil | ||
} | ||
|
||
data.Set("enabled", query.AuditTrailWebhook.Enabled) | ||
data.Set("endpoint", query.AuditTrailWebhook.Endpoint) | ||
data.Set("include_runs", query.AuditTrailWebhook.IncludeRuns) | ||
data.Set("secret", query.AuditTrailWebhook.Secret) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAuditTrailWebhookUpdate(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
var mutation struct { | ||
AuditTrailWebhook *structs.AuditTrailWebhook `graphql:"auditTrailSetWebhook(input: $input)"` | ||
} | ||
variables := map[string]interface{}{ | ||
"input": structs.AuditTrailWebhookInput{ | ||
Enabled: toBool(data.Get("enabled")), | ||
Endpoint: toString(data.Get("endpoint")), | ||
IncludeRuns: toBool(data.Get("include_runs")), | ||
Secret: toString(data.Get("secret")), | ||
}, | ||
} | ||
if err := i.(*internal.Client).Mutate(ctx, "AuditTrailWebhookUpdate", &mutation, variables); err != nil { | ||
return diag.Errorf("could not update audit trail webhook: %v", internal.FromSpaceliftError(err)) | ||
} | ||
|
||
return resourceAuditTrailWebhookRead(ctx, data, i) | ||
} | ||
|
||
func resourceAuditTrailWebhookDelete(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
var mutation struct { | ||
AuditTrailWebhook *structs.AuditTrailWebhook `graphql:"auditTrailDeleteWebhook"` | ||
} | ||
if err := i.(*internal.Client).Mutate(ctx, "AuditTrailWebhookDelete", &mutation, nil); err != nil { | ||
return diag.Errorf("could not delete audit trail webhook: %v", internal.FromSpaceliftError(err)) | ||
} | ||
|
||
data.SetId("") | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package spacelift | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
. "github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/testhelpers" | ||
) | ||
|
||
var auditTrailWebhookSimple = ` | ||
resource "spacelift_audit_trail_webhook" "test" { | ||
enabled = true | ||
endpoint = "%s" | ||
include_runs = true | ||
secret = "secret" | ||
} | ||
` | ||
|
||
func Test_resourceAuditTrailWebhook(t *testing.T) { | ||
const resourceName = "spacelift_audit_trail_webhook.test" | ||
|
||
t.Run("creates an audit trail webhook without an error", func(t *testing.T) { | ||
testSteps(t, []resource.TestStep{ | ||
{ | ||
Config: fmt.Sprintf(auditTrailWebhookSimple, "https://example.com"), | ||
Check: Resource( | ||
resourceName, | ||
Attribute("enabled", Equals("true")), | ||
Attribute("endpoint", Equals("https://example.com")), | ||
Attribute("include_runs", Equals("true")), | ||
Attribute("secret", Equals("secret")), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}) | ||
}) | ||
|
||
t.Run("cannot change endpoint", func(t *testing.T) { | ||
testSteps(t, []resource.TestStep{ | ||
{ | ||
Config: fmt.Sprintf(auditTrailWebhookSimple, "https://example2.com/"), | ||
ExpectError: regexp.MustCompile(`could not send webhook to given endpoint`), | ||
}, | ||
}) | ||
}) | ||
} |