|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package tfe |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "net/url" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +var _ OrganizationAuditConfigurations = (*organizationAuditConfigurations)(nil) |
| 14 | + |
| 15 | +// OrganizationAuditConfigurations describes the configuration for auditing events for the organization. |
| 16 | +type OrganizationAuditConfigurations interface { |
| 17 | + // Read the audit configuration of an organization by its name. |
| 18 | + Read(ctx context.Context, organization string) (*OrganizationAuditConfiguration, error) |
| 19 | + |
| 20 | + // Send a test audit event for an organization by its name. |
| 21 | + Test(ctx context.Context, organization string) (*OrganizationAuditConfigurationTest, error) |
| 22 | + |
| 23 | + // Update the audit configuration of an organization by its name. |
| 24 | + Update(ctx context.Context, organization string, options OrganizationAuditConfigurationOptions) (*OrganizationAuditConfiguration, error) |
| 25 | +} |
| 26 | + |
| 27 | +// OrganizationAuditConfiguration represents the auditing configuration for a HCP Terraform Organization. |
| 28 | +type OrganizationAuditConfiguration struct { |
| 29 | + AuditTrails *OrganizationAuditConfigAuditTrails `jsonapi:"attr,audit-trails,omitempty"` |
| 30 | + HCPAuditLogStreaming *OrganizationAuditConfigAuditStreaming `jsonapi:"attr,hcp-audit-log-streaming,omitempty"` |
| 31 | + ID string `jsonapi:"primary,audit-configurations"` |
| 32 | + Permissions *OrganizationAuditConfigPermissions `jsonapi:"attr,permissions,omitempty"` |
| 33 | + Timestamps *OrganizationAuditConfigTimestamps `jsonapi:"attr,timestamps,omitempty"` |
| 34 | + UpdatedAt time.Time `jsonapi:"attr,updated-at,iso8601"` |
| 35 | + |
| 36 | + Organization *Organization `jsonapi:"relation,organization"` |
| 37 | +} |
| 38 | + |
| 39 | +type OrganizationAuditConfigAuditTrails struct { |
| 40 | + Enabled bool `jsonapi:"attr,enabled"` |
| 41 | +} |
| 42 | + |
| 43 | +type OrganizationAuditConfigAuditStreaming struct { |
| 44 | + Enabled bool `jsonapi:"attr,enabled"` |
| 45 | + OrganizationID string `jsonapi:"attr,organization-id"` |
| 46 | + UseDefaultOrganization bool `jsonapi:"attr,use-default-organization"` |
| 47 | +} |
| 48 | + |
| 49 | +type OrganizationAuditConfigPermissions struct { |
| 50 | + CanEnableHCPAuditLogStreaming bool `jsonapi:"attr,can-enable-hcp-audit-log-streaming"` |
| 51 | + CanSetHCPAuditLogStreamingOrganization bool `jsonapi:"attr,can-set-hcp-audit-log-streaming-organization-id"` |
| 52 | + CanUseDefaultAuditLogStreamingOrganization bool `jsonapi:"attr,can-use-default-audit-log-streaming-organization"` |
| 53 | +} |
| 54 | + |
| 55 | +type OrganizationAuditConfigTimestamps struct { |
| 56 | + AuditTrailsDisabledAt *time.Time `jsonapi:"attr,audit-trails-disabled-at,iso8601,omitempty"` |
| 57 | + AuditTrailsEnabledAt *time.Time `jsonapi:"attr,audit-trails-enabled-at,iso8601,omitempty"` |
| 58 | + AuditTrailsLastFailure *time.Time `jsonapi:"attr,audit-trails-last-failure,iso8601,omitempty"` |
| 59 | + AuditTrailsLastSuccess *time.Time `jsonapi:"attr,audit-trails-last-success,iso8601,omitempty"` |
| 60 | + HCPAuditLogStreamingDisabledAt *time.Time `jsonapi:"attr,hcp-audit-log-streaming-disabled-at,iso8601,omitempty"` |
| 61 | + HCPAuditLogStreamingEnabledAt *time.Time `jsonapi:"attr,hcp-audit-log-streaming-enabled-at,iso8601,omitempty"` |
| 62 | + HCPAuditLogStreamingLastFailure *time.Time `jsonapi:"attr,hcp-audit-log-streaming-last-failure,iso8601,omitempty"` |
| 63 | + HCPAuditLogStreamingLastSuccess *time.Time `jsonapi:"attr,hcp-audit-log-streaming-last-success,iso8601,omitempty"` |
| 64 | +} |
| 65 | + |
| 66 | +type OrganizationAuditConfigurationTest struct { |
| 67 | + RequestID *string `json:"request-id,omitempty"` |
| 68 | +} |
| 69 | + |
| 70 | +type OrganizationAuditConfigurationOptions struct { |
| 71 | + AuditTrails *OrganizationAuditConfigAuditTrails `jsonapi:"attr,audit-trails,omitempty"` |
| 72 | + HCPAuditLogStreaming *OrganizationAuditConfigAuditStreaming `jsonapi:"attr,hcp-audit-log-streaming,omitempty"` |
| 73 | +} |
| 74 | + |
| 75 | +type organizationAuditConfigurations struct { |
| 76 | + client *Client |
| 77 | +} |
| 78 | + |
| 79 | +// Read the audit configuration of an organization by its name. |
| 80 | +func (s *organizationAuditConfigurations) Read(ctx context.Context, organization string) (*OrganizationAuditConfiguration, error) { |
| 81 | + if !validStringID(&organization) { |
| 82 | + return nil, ErrInvalidOrg |
| 83 | + } |
| 84 | + |
| 85 | + u := fmt.Sprintf("organizations/%s/audit-configuration", url.PathEscape(organization)) |
| 86 | + req, err := s.client.NewRequest("GET", u, nil) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + ac := &OrganizationAuditConfiguration{} |
| 92 | + err = req.Do(ctx, ac) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + |
| 97 | + return ac, err |
| 98 | +} |
| 99 | + |
| 100 | +// Send a test audit event for an organization by its name. |
| 101 | +func (s *organizationAuditConfigurations) Test(ctx context.Context, organization string) (*OrganizationAuditConfigurationTest, error) { |
| 102 | + if !validStringID(&organization) { |
| 103 | + return nil, ErrInvalidOrg |
| 104 | + } |
| 105 | + |
| 106 | + u := fmt.Sprintf("organizations/%s/audit-configuration/test", url.PathEscape(organization)) |
| 107 | + req, err := s.client.NewRequest("POST", u, nil) |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + |
| 112 | + result := &OrganizationAuditConfigurationTest{} |
| 113 | + err = req.DoJSON(ctx, result) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + return result, err |
| 119 | +} |
| 120 | + |
| 121 | +// Update the audit configuration of an organization by its name. |
| 122 | +func (s *organizationAuditConfigurations) Update(ctx context.Context, organization string, options OrganizationAuditConfigurationOptions) (*OrganizationAuditConfiguration, error) { |
| 123 | + if !validStringID(&organization) { |
| 124 | + return nil, ErrInvalidOrg |
| 125 | + } |
| 126 | + |
| 127 | + u := fmt.Sprintf("organizations/%s/audit-configuration", url.PathEscape(organization)) |
| 128 | + req, err := s.client.NewRequest("PATCH", u, &options) |
| 129 | + if err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + |
| 133 | + ac := &OrganizationAuditConfiguration{} |
| 134 | + err = req.Do(ctx, ac) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + |
| 139 | + return ac, err |
| 140 | +} |
0 commit comments