|
| 1 | +// Copyright (c) Venafi, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package provider |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "terraform-provider-tlspc/internal/tlspc" |
| 11 | + |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 15 | +) |
| 16 | + |
| 17 | +// Ensure the implementation satisfies the expected interfaces. |
| 18 | +var ( |
| 19 | + _ datasource.DataSource = &caProductDataSource{} |
| 20 | + _ datasource.DataSourceWithConfigure = &caProductDataSource{} |
| 21 | +) |
| 22 | + |
| 23 | +// NewCoffeesDataSource is a helper function to simplify the provider implementation. |
| 24 | +func NewCAProductDataSource() datasource.DataSource { |
| 25 | + return &caProductDataSource{} |
| 26 | +} |
| 27 | + |
| 28 | +// caProductDataSource is the data source implementation. |
| 29 | +type caProductDataSource struct { |
| 30 | + client *tlspc.Client |
| 31 | +} |
| 32 | + |
| 33 | +// Configure adds the provider configured client to the data source. |
| 34 | +func (d *caProductDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 35 | + // Add a nil check when handling ProviderData because Terraform |
| 36 | + // sets that data after it calls the ConfigureProvider RPC. |
| 37 | + if req.ProviderData == nil { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + client, ok := req.ProviderData.(*tlspc.Client) |
| 42 | + if !ok { |
| 43 | + resp.Diagnostics.AddError( |
| 44 | + "Unexpected Data Source Configure Type", |
| 45 | + fmt.Sprintf("Expected *tlspc.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 46 | + ) |
| 47 | + |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + d.client = client |
| 52 | +} |
| 53 | + |
| 54 | +// Metadata returns the data source type name. |
| 55 | +func (d *caProductDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 56 | + resp.TypeName = req.ProviderTypeName + "_ca_product" |
| 57 | +} |
| 58 | + |
| 59 | +// Schema defines the schema for the data source. |
| 60 | +func (d *caProductDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 61 | + resp.Schema = schema.Schema{ |
| 62 | + Attributes: map[string]schema.Attribute{ |
| 63 | + "id": schema.StringAttribute{ |
| 64 | + Computed: true, |
| 65 | + }, |
| 66 | + "type": schema.StringAttribute{ |
| 67 | + Required: true, |
| 68 | + }, |
| 69 | + "ca_name": schema.StringAttribute{ |
| 70 | + Required: true, |
| 71 | + }, |
| 72 | + "product_option": schema.StringAttribute{ |
| 73 | + Required: true, |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +type caProductDataSourceModel struct { |
| 80 | + ID types.String `tfsdk:"id"` |
| 81 | + Type types.String `tfsdk:"type"` |
| 82 | + CAName types.String `tfsdk:"ca_name"` |
| 83 | + ProductOption types.String `tfsdk:"product_option"` |
| 84 | +} |
| 85 | + |
| 86 | +// Read refreshes the Terraform state with the latest data. |
| 87 | +func (d *caProductDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 88 | + var model caProductDataSourceModel |
| 89 | + diags := req.Config.Get(ctx, &model) |
| 90 | + resp.Diagnostics.Append(diags...) |
| 91 | + if resp.Diagnostics.HasError() { |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + caProduct, err := d.client.GetCAProductOption(model.Type.ValueString(), model.CAName.ValueString(), model.ProductOption.ValueString()) |
| 96 | + if err != nil { |
| 97 | + resp.Diagnostics.AddError( |
| 98 | + "Error retrieving CA Product", |
| 99 | + fmt.Sprintf("Error retrieving CA Product: %s", err.Error()), |
| 100 | + ) |
| 101 | + return |
| 102 | + } |
| 103 | + model.ID = types.StringValue(caProduct.ID) |
| 104 | + diags = resp.State.Set(ctx, &model) |
| 105 | + resp.Diagnostics.Append(diags...) |
| 106 | +} |
0 commit comments