|
| 1 | +// Copyright IBM Corp. 2018, 2025 |
| 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/datasource" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 15 | +) |
| 16 | + |
| 17 | +var _ datasource.DataSource = &dataSourceTFEOrgMaxTokenTTLPolicy{} |
| 18 | +var _ datasource.DataSourceWithConfigure = &dataSourceTFEOrgMaxTokenTTLPolicy{} |
| 19 | + |
| 20 | +func NewOrgMaxTokenTTLPolicyDataSource() datasource.DataSource { |
| 21 | + return &dataSourceTFEOrgMaxTokenTTLPolicy{} |
| 22 | +} |
| 23 | + |
| 24 | +type dataSourceTFEOrgMaxTokenTTLPolicy struct { |
| 25 | + config ConfiguredClient |
| 26 | +} |
| 27 | + |
| 28 | +type modelTFEOrgMaxTokenTTLPolicyData struct { |
| 29 | + Organization types.String `tfsdk:"organization"` |
| 30 | + Enabled types.Bool `tfsdk:"enabled"` |
| 31 | + OrgTokenMaxTTLMs types.Int64 `tfsdk:"org_token_max_ttl_ms"` |
| 32 | + TeamTokenMaxTTLMs types.Int64 `tfsdk:"team_token_max_ttl_ms"` |
| 33 | + AuditTrailTokenMaxTTLMs types.Int64 `tfsdk:"audit_trail_token_max_ttl_ms"` |
| 34 | + UserTokenMaxTTLMs types.Int64 `tfsdk:"user_token_max_ttl_ms"` |
| 35 | +} |
| 36 | + |
| 37 | +func (d *dataSourceTFEOrgMaxTokenTTLPolicy) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 38 | + resp.TypeName = req.ProviderTypeName + "_org_max_token_ttl_policy" |
| 39 | +} |
| 40 | + |
| 41 | +func (d *dataSourceTFEOrgMaxTokenTTLPolicy) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 42 | + resp.Schema = schema.Schema{ |
| 43 | + Description: "Retrieves the maximum time-to-live (TTL) policy for API tokens in an organization. " + |
| 44 | + "This data source fetches the current TTL limits for organization, team, audit trail, " + |
| 45 | + "and user tokens from the database.", |
| 46 | + |
| 47 | + Attributes: map[string]schema.Attribute{ |
| 48 | + "organization": schema.StringAttribute{ |
| 49 | + Description: "Name of the organization.", |
| 50 | + Required: true, |
| 51 | + }, |
| 52 | + "enabled": schema.BoolAttribute{ |
| 53 | + Description: "Indicates whether the maximum TTL token policy is enabled (true) or disabled (false) for the organization.", |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + "org_token_max_ttl_ms": schema.Int64Attribute{ |
| 57 | + Description: "Maximum lifespan allowed for organization tokens in milliseconds.", |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + "team_token_max_ttl_ms": schema.Int64Attribute{ |
| 61 | + Description: "Maximum lifespan allowed for team tokens in milliseconds.", |
| 62 | + Computed: true, |
| 63 | + }, |
| 64 | + "audit_trail_token_max_ttl_ms": schema.Int64Attribute{ |
| 65 | + Description: "Maximum lifespan allowed for audit trail tokens in milliseconds.", |
| 66 | + Computed: true, |
| 67 | + }, |
| 68 | + "user_token_max_ttl_ms": schema.Int64Attribute{ |
| 69 | + Description: "Maximum lifespan allowed for user tokens in milliseconds.", |
| 70 | + Computed: true, |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func (d *dataSourceTFEOrgMaxTokenTTLPolicy) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 77 | + if req.ProviderData == nil { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + client, ok := req.ProviderData.(ConfiguredClient) |
| 82 | + if !ok { |
| 83 | + resp.Diagnostics.AddError( |
| 84 | + "Unexpected data source Configure type", |
| 85 | + fmt.Sprintf("Expected tfe.ConfiguredClient, got %T", req.ProviderData), |
| 86 | + ) |
| 87 | + return |
| 88 | + } |
| 89 | + d.config = client |
| 90 | +} |
| 91 | + |
| 92 | +func (d *dataSourceTFEOrgMaxTokenTTLPolicy) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 93 | + var config modelTFEOrgMaxTokenTTLPolicyData |
| 94 | + |
| 95 | + resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) |
| 96 | + if resp.Diagnostics.HasError() { |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + organization := config.Organization.ValueString() |
| 101 | + |
| 102 | + tflog.Debug(ctx, fmt.Sprintf("Reading token TTL policies for organization: %s", organization)) |
| 103 | + policyList, err := d.config.Client.OrganizationTokenTTLPolicies.List(ctx, organization, nil) |
| 104 | + if err != nil { |
| 105 | + resp.Diagnostics.AddError("Unable to read organization token TTL policies", err.Error()) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + // If no policies exist, treat as disabled with default values |
| 110 | + enabled := len(policyList.Items) > 0 |
| 111 | + |
| 112 | + // Convert the API response to the data source model |
| 113 | + result := modelFromTokenTTLPoliciesData(organization, enabled, policyList.Items) |
| 114 | + |
| 115 | + resp.Diagnostics.Append(resp.State.Set(ctx, &result)...) |
| 116 | +} |
| 117 | + |
| 118 | +func modelFromTokenTTLPoliciesData(organization string, enabled bool, policies []*tfe.OrganizationTokenTTLPolicy) modelTFEOrgMaxTokenTTLPolicyData { |
| 119 | + // Default TTL: 2 years in milliseconds |
| 120 | + defaultTTLMs := int64(63072000000000) // 2 years = 365 * 2 * 24 * 60 * 60 * 1000 |
| 121 | + |
| 122 | + result := modelTFEOrgMaxTokenTTLPolicyData{ |
| 123 | + Organization: types.StringValue(organization), |
| 124 | + Enabled: types.BoolValue(enabled), |
| 125 | + OrgTokenMaxTTLMs: types.Int64Value(defaultTTLMs), |
| 126 | + TeamTokenMaxTTLMs: types.Int64Value(defaultTTLMs), |
| 127 | + AuditTrailTokenMaxTTLMs: types.Int64Value(defaultTTLMs), |
| 128 | + UserTokenMaxTTLMs: types.Int64Value(defaultTTLMs), |
| 129 | + } |
| 130 | + |
| 131 | + // Set actual values from policies |
| 132 | + for _, policy := range policies { |
| 133 | + switch policy.TokenType { |
| 134 | + case tfe.TokenTypeOrganization: |
| 135 | + result.OrgTokenMaxTTLMs = types.Int64Value(policy.MaxTTLMs) |
| 136 | + case tfe.TokenTypeTeam: |
| 137 | + result.TeamTokenMaxTTLMs = types.Int64Value(policy.MaxTTLMs) |
| 138 | + case tfe.TokenTypeAuditTrails: |
| 139 | + result.AuditTrailTokenMaxTTLMs = types.Int64Value(policy.MaxTTLMs) |
| 140 | + case tfe.TokenTypeUser: |
| 141 | + result.UserTokenMaxTTLMs = types.Int64Value(policy.MaxTTLMs) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return result |
| 146 | +} |
0 commit comments