|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package provider |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + _ datasource.DataSource = &dataSourceHYOKCustomerKeyVersion{} |
| 17 | + _ datasource.DataSourceWithConfigure = &dataSourceHYOKCustomerKeyVersion{} |
| 18 | +) |
| 19 | + |
| 20 | +func NewHYOKCustomerKeyVersionDataSource() datasource.DataSource { |
| 21 | + return &dataSourceHYOKCustomerKeyVersion{} |
| 22 | +} |
| 23 | + |
| 24 | +type dataSourceHYOKCustomerKeyVersion struct { |
| 25 | + config ConfiguredClient |
| 26 | +} |
| 27 | + |
| 28 | +type HYOKCustomerKeyVersionDataSourceModel struct { |
| 29 | + ID types.String `tfsdk:"id"` |
| 30 | + Status types.String `tfsdk:"status"` |
| 31 | + Error types.String `tfsdk:"error"` |
| 32 | + KeyVersion types.String `tfsdk:"key_version"` |
| 33 | + CreatedAt types.String `tfsdk:"created_at"` |
| 34 | + WorkspacesSecured types.Int64 `tfsdk:"workspaces_secured"` |
| 35 | +} |
| 36 | + |
| 37 | +func (d *dataSourceHYOKCustomerKeyVersion) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 38 | + if req.ProviderData == nil { |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + client, ok := req.ProviderData.(ConfiguredClient) |
| 43 | + if !ok { |
| 44 | + resp.Diagnostics.AddError( |
| 45 | + "Unexpected Data Source Configure Type", |
| 46 | + fmt.Sprintf("Expected tfe.ConfiguredClient, got %T. This is a bug in the tfe provider, so please report it on GitHub.", req.ProviderData), |
| 47 | + ) |
| 48 | + |
| 49 | + return |
| 50 | + } |
| 51 | + d.config = client |
| 52 | +} |
| 53 | + |
| 54 | +func (d *dataSourceHYOKCustomerKeyVersion) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 55 | + resp.TypeName = req.ProviderTypeName + "_hyok_customer_key_version" |
| 56 | +} |
| 57 | + |
| 58 | +func (d *dataSourceHYOKCustomerKeyVersion) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 59 | + resp.Schema = schema.Schema{ |
| 60 | + Description: "This data source can be used to retrieve a HYOK customer key version.", |
| 61 | + Attributes: map[string]schema.Attribute{ |
| 62 | + "id": schema.StringAttribute{ |
| 63 | + Description: "The ID of the HYOK customer key version.", |
| 64 | + Required: true, |
| 65 | + }, |
| 66 | + "status": schema.StringAttribute{ |
| 67 | + Description: "The status of the HYOK customer key version.", |
| 68 | + Computed: true, |
| 69 | + }, |
| 70 | + "error": schema.StringAttribute{ |
| 71 | + Description: "Any error message associated with the HYOK customer key version.", |
| 72 | + Computed: true, |
| 73 | + }, |
| 74 | + "key_version": schema.StringAttribute{ |
| 75 | + Description: "The version number of the customer key version.", |
| 76 | + Computed: true, |
| 77 | + }, |
| 78 | + "workspaces_secured": schema.Int64Attribute{ |
| 79 | + Description: "The number workspaces secured by this customer key version.", |
| 80 | + Computed: true, |
| 81 | + }, |
| 82 | + "created_at": schema.StringAttribute{ |
| 83 | + Description: "The timestamp when the key version was created.", |
| 84 | + Computed: true, |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func (d *dataSourceHYOKCustomerKeyVersion) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 91 | + var data HYOKCustomerKeyVersionDataSourceModel |
| 92 | + |
| 93 | + // Read Terraform configuration data into the model |
| 94 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 95 | + if resp.Diagnostics.HasError() { |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + // Make API call to fetch the HYOK customer key version |
| 100 | + keyVersion, err := d.config.Client.HYOKCustomerKeyVersions.Read(ctx, data.ID.ValueString()) |
| 101 | + if err != nil { |
| 102 | + resp.Diagnostics.AddError("Unable to read HYOK customer key version", err.Error()) |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + // Set the computed attributes from the API response |
| 107 | + data.Status = types.StringValue(string(keyVersion.Status)) |
| 108 | + data.KeyVersion = types.StringValue(keyVersion.KeyVersion) |
| 109 | + data.CreatedAt = types.StringValue(keyVersion.CreatedAt.Format(time.RFC3339)) |
| 110 | + data.WorkspacesSecured = types.Int64Value(int64(keyVersion.WorkspacesSecured)) |
| 111 | + data.Error = types.StringValue(keyVersion.Error) |
| 112 | + |
| 113 | + // Save data into Terraform state |
| 114 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 115 | +} |
0 commit comments