|
| 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 | + "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 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + _ ephemeral.EphemeralResource = &TeamTokenEphemeralResource{} |
| 19 | + _ ephemeral.EphemeralResourceWithConfigure = &TeamTokenEphemeralResource{} |
| 20 | +) |
| 21 | + |
| 22 | +func NewTeamTokenEphemeralResource() ephemeral.EphemeralResource { |
| 23 | + return &TeamTokenEphemeralResource{} |
| 24 | +} |
| 25 | + |
| 26 | +type TeamTokenEphemeralResource struct { |
| 27 | + config ConfiguredClient |
| 28 | +} |
| 29 | + |
| 30 | +type TeamTokenEphemeralResourceModel struct { |
| 31 | + TeamID types.String `tfsdk:"team_id"` |
| 32 | + Token types.String `tfsdk:"token"` |
| 33 | + ExpiredAt timetypes.RFC3339 `tfsdk:"expired_at"` |
| 34 | +} |
| 35 | + |
| 36 | +func (e *TeamTokenEphemeralResource) Schema(ctx context.Context, req ephemeral.SchemaRequest, resp *ephemeral.SchemaResponse) { |
| 37 | + resp.Schema = schema.Schema{ |
| 38 | + Description: "This ephemeral resource can be used to retrieve a team token without saving its value in state.", |
| 39 | + Attributes: map[string]schema.Attribute{ |
| 40 | + "team_id": schema.StringAttribute{ |
| 41 | + Description: `ID of the team.`, |
| 42 | + Required: true, |
| 43 | + }, |
| 44 | + "token": schema.StringAttribute{ |
| 45 | + Description: `The generated token.`, |
| 46 | + Computed: true, |
| 47 | + Sensitive: true, |
| 48 | + }, |
| 49 | + "expired_at": schema.StringAttribute{ |
| 50 | + Description: `The token's expiration date.`, |
| 51 | + Optional: true, |
| 52 | + Computed: true, |
| 53 | + CustomType: timetypes.RFC3339Type{}, |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Configure adds the provider configured client to the data source. |
| 60 | +func (e *TeamTokenEphemeralResource) Configure(_ context.Context, req ephemeral.ConfigureRequest, resp *ephemeral.ConfigureResponse) { |
| 61 | + if req.ProviderData == nil { |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + client, ok := req.ProviderData.(ConfiguredClient) |
| 66 | + if !ok { |
| 67 | + resp.Diagnostics.AddError( |
| 68 | + "Unexpected Ephemeral Resource Configure Type", |
| 69 | + fmt.Sprintf("Expected tfe.ConfiguredClient, got %T. This is a bug in the tfe provider, so please report it on GitHub.", req.ProviderData), |
| 70 | + ) |
| 71 | + |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + e.config = client |
| 76 | +} |
| 77 | + |
| 78 | +func (e *TeamTokenEphemeralResource) Metadata(ctx context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { |
| 79 | + resp.TypeName = req.ProviderTypeName + "_team_token" |
| 80 | +} |
| 81 | + |
| 82 | +func (e *TeamTokenEphemeralResource) Open(ctx context.Context, req ephemeral.OpenRequest, resp *ephemeral.OpenResponse) { |
| 83 | + var config TeamTokenEphemeralResourceModel |
| 84 | + |
| 85 | + // Read Terraform config data into the model |
| 86 | + resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) |
| 87 | + if resp.Diagnostics.HasError() { |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + // Create a new options struct |
| 92 | + options := tfe.TeamTokenCreateOptions{} |
| 93 | + |
| 94 | + if !config.ExpiredAt.IsNull() { |
| 95 | + expiredAt, diags := config.ExpiredAt.ValueRFC3339Time() |
| 96 | + if diags.HasError() { |
| 97 | + resp.Diagnostics.Append(diags...) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + options.ExpiredAt = &expiredAt |
| 102 | + } |
| 103 | + |
| 104 | + var teamID = config.TeamID.ValueString() |
| 105 | + result, err := e.config.Client.TeamTokens.CreateWithOptions(ctx, config.TeamID.ValueString(), options) |
| 106 | + if err != nil { |
| 107 | + resp.Diagnostics.AddError("Unable to read resource", err.Error()) |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + config = ephemeralResourceModelFromTFETeamToken(teamID, result) |
| 112 | + |
| 113 | + // Save to ephemeral result data |
| 114 | + resp.Diagnostics.Append(resp.Result.Set(ctx, &config)...) |
| 115 | +} |
| 116 | + |
| 117 | +// ephemeralResourceModelFromTFETeamToken builds a TeamTokenEphemeralResourceModel struct from a |
| 118 | +// tfe.TeamToken value. |
| 119 | +func ephemeralResourceModelFromTFETeamToken(teamID string, v *tfe.TeamToken) TeamTokenEphemeralResourceModel { |
| 120 | + return TeamTokenEphemeralResourceModel{ |
| 121 | + TeamID: types.StringValue(teamID), |
| 122 | + Token: types.StringValue(v.Token), |
| 123 | + ExpiredAt: timetypes.NewRFC3339TimeValue(v.ExpiredAt), |
| 124 | + } |
| 125 | +} |
0 commit comments