|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package provider |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + tfe "github.com/hashicorp/go-tfe" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/ephemeral/schema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 15 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + _ ephemeral.EphemeralResource = &AuditTrailTokenEphemeralResource{} |
| 20 | + _ ephemeral.EphemeralResourceWithConfigure = &AuditTrailTokenEphemeralResource{} |
| 21 | +) |
| 22 | + |
| 23 | +func NewAuditTrailTokenEphemeralResource() ephemeral.EphemeralResource { |
| 24 | + return &AuditTrailTokenEphemeralResource{} |
| 25 | +} |
| 26 | + |
| 27 | +type AuditTrailTokenEphemeralResource struct { |
| 28 | + config ConfiguredClient |
| 29 | +} |
| 30 | + |
| 31 | +type AuditTrailTokenEphemeralResourceModel struct { |
| 32 | + Organization types.String `tfsdk:"organization"` |
| 33 | + Token types.String `tfsdk:"token"` |
| 34 | + ExpiredAt timetypes.RFC3339 `tfsdk:"expired_at"` |
| 35 | +} |
| 36 | + |
| 37 | +func (e *AuditTrailTokenEphemeralResource) Schema(ctx context.Context, req ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { |
| 38 | + resp.Schema = schema.Schema{ |
| 39 | + Description: "This ephemeral resource can be used to retrieve an audit trail token without saving its value in state. Using this ephemeral resource will generate a new token each time it is used, invalidating any existing audit trail token.", |
| 40 | + Attributes: map[string]schema.Attribute{ |
| 41 | + "organization": schema.StringAttribute{ |
| 42 | + Description: `Name of the organization. If omitted, organization must be defined in the provider config.`, |
| 43 | + Optional: true, |
| 44 | + Computed: true, |
| 45 | + }, |
| 46 | + "token": schema.StringAttribute{ |
| 47 | + Description: `The generated token.`, |
| 48 | + Computed: true, |
| 49 | + Sensitive: true, |
| 50 | + }, |
| 51 | + "expired_at": schema.StringAttribute{ |
| 52 | + Description: `The token's expiration date. The expiration date must be a date/time string in RFC3339 format (e.g., "2024-12-31T23:59:59Z"). If no expiration date is supplied, the expiration date will default to null and never expire.`, |
| 53 | + Optional: true, |
| 54 | + CustomType: timetypes.RFC3339Type{}, |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Configure adds the provider configured client to the data source. |
| 61 | +func (e *AuditTrailTokenEphemeralResource) Configure(_ context.Context, req ephemeral.ConfigureRequest, resp *ephemeral.ConfigureResponse) { |
| 62 | + if req.ProviderData == nil { |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + client, ok := req.ProviderData.(ConfiguredClient) |
| 67 | + if !ok { |
| 68 | + resp.Diagnostics.AddError( |
| 69 | + "Unexpected Ephemeral Resource Configure Type", |
| 70 | + fmt.Sprintf("Expected tfe.ConfiguredClient, got %T. This is a bug in the tfe provider, so please report it on GitHub.", req.ProviderData), |
| 71 | + ) |
| 72 | + |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + e.config = client |
| 77 | +} |
| 78 | + |
| 79 | +func (e *AuditTrailTokenEphemeralResource) Metadata(ctx context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { |
| 80 | + resp.TypeName = req.ProviderTypeName + "_audit_trail_token" |
| 81 | +} |
| 82 | + |
| 83 | +func (e *AuditTrailTokenEphemeralResource) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) { |
| 84 | + // Read Terraform config |
| 85 | + var config AuditTrailTokenEphemeralResourceModel |
| 86 | + resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) |
| 87 | + if resp.Diagnostics.HasError() { |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + // Get org name or default |
| 92 | + var orgName string |
| 93 | + resp.Diagnostics.Append(e.config.dataOrDefaultOrganization(ctx, req.Config, &orgName)...) |
| 94 | + if resp.Diagnostics.HasError() { |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + // Create options struct |
| 99 | + tokenType := tfe.AuditTrailToken // "audit_trail" |
| 100 | + opts := tfe.OrganizationTokenCreateOptions{ |
| 101 | + TokenType: &tokenType, |
| 102 | + } |
| 103 | + |
| 104 | + if !config.ExpiredAt.IsNull() { |
| 105 | + expiredAt, diags := config.ExpiredAt.ValueRFC3339Time() |
| 106 | + if diags.HasError() { |
| 107 | + resp.Diagnostics.Append(diags...) |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + opts.ExpiredAt = &expiredAt |
| 112 | + } |
| 113 | + |
| 114 | + tflog.Debug(ctx, fmt.Sprintf("Creating audit trail token for organization %s", orgName)) |
| 115 | + result, err := e.config.Client.OrganizationTokens.CreateWithOptions(ctx, orgName, opts) |
| 116 | + if err != nil { |
| 117 | + resp.Diagnostics.AddError("Unable to create organization audit trail token", err.Error()) |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + // Set the token in the model |
| 122 | + config.Token = types.StringValue(result.Token) |
| 123 | + |
| 124 | + // Write the data back to the ephemeral resource |
| 125 | + resp.Diagnostics.Append(resp.Result.Set(ctx, &config)...) |
| 126 | +} |
0 commit comments