|
| 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 = &certTemplateDataSource{} |
| 20 | + _ datasource.DataSourceWithConfigure = &certTemplateDataSource{} |
| 21 | +) |
| 22 | + |
| 23 | +// NewCertificateTemplateDataSource is a helper function to simplify the provider implementation. |
| 24 | +func NewCertificateTemplateDataSource() datasource.DataSource { |
| 25 | + return &certTemplateDataSource{} |
| 26 | +} |
| 27 | + |
| 28 | +// certTemplateDataSource is the data source implementation. |
| 29 | +type certTemplateDataSource struct { |
| 30 | + client *tlspc.Client |
| 31 | +} |
| 32 | + |
| 33 | +// Configure adds the provider configured client to the data source. |
| 34 | +func (d *certTemplateDataSource) 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 *certTemplateDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 56 | + resp.TypeName = req.ProviderTypeName + "_certificate_template" |
| 57 | +} |
| 58 | + |
| 59 | +// Schema defines the schema for the data source. |
| 60 | +func (d *certTemplateDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 61 | + resp.Schema = schema.Schema{ |
| 62 | + MarkdownDescription: "Look up properties of a Certificate Template", |
| 63 | + Attributes: map[string]schema.Attribute{ |
| 64 | + "id": schema.StringAttribute{ |
| 65 | + Computed: true, |
| 66 | + }, |
| 67 | + "name": schema.StringAttribute{ |
| 68 | + Required: true, |
| 69 | + MarkdownDescription: "Name of the Certificate Issuing Template", |
| 70 | + }, |
| 71 | + "ca_type": schema.StringAttribute{ |
| 72 | + Required: true, |
| 73 | + MarkdownDescription: "Type of Certificate Authority (see Certificate Authority Product Option data source)", |
| 74 | + }, |
| 75 | + "ca_product_id": schema.StringAttribute{ |
| 76 | + Computed: true, |
| 77 | + MarkdownDescription: "The ID of a Certificate Authority Product Option", |
| 78 | + }, |
| 79 | + "key_reuse": schema.BoolAttribute{ |
| 80 | + Computed: true, |
| 81 | + MarkdownDescription: "Allow Private Key Reuse", |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +type certTemplateDataSourceModel struct { |
| 88 | + ID types.String `tfsdk:"id"` |
| 89 | + Name types.String `tfsdk:"name"` |
| 90 | + CAType types.String `tfsdk:"ca_type"` |
| 91 | + CAProductID types.String `tfsdk:"ca_product_id"` |
| 92 | + KeyReuse types.Bool `tfsdk:"key_reuse"` |
| 93 | +} |
| 94 | + |
| 95 | +// Read refreshes the Terraform state with the latest data. |
| 96 | +func (d *certTemplateDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 97 | + var model certTemplateDataSourceModel |
| 98 | + diags := req.Config.Get(ctx, &model) |
| 99 | + resp.Diagnostics.Append(diags...) |
| 100 | + if resp.Diagnostics.HasError() { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + certTemplates, err := d.client.GetCertTemplates() |
| 105 | + if err != nil { |
| 106 | + resp.Diagnostics.AddError( |
| 107 | + "Error retrieving Certificate Templates", |
| 108 | + fmt.Sprintf("Error retrieving Certificate Templates: %s", err.Error()), |
| 109 | + ) |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + found := false |
| 114 | + for _, v := range certTemplates { |
| 115 | + if model.CAType.ValueString() == v.CertificateAuthorityType && model.Name.ValueString() == v.Name { |
| 116 | + model.ID = types.StringValue(v.ID) |
| 117 | + model.CAProductID = types.StringValue(v.CertificateAuthorityProductOptionID) |
| 118 | + model.KeyReuse = types.BoolValue(v.KeyReuse) |
| 119 | + found = true |
| 120 | + continue |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if !found { |
| 125 | + resp.Diagnostics.AddError( |
| 126 | + "Certificate Template not found", |
| 127 | + "", |
| 128 | + ) |
| 129 | + return |
| 130 | + } |
| 131 | + diags = resp.State.Set(ctx, &model) |
| 132 | + resp.Diagnostics.Append(diags...) |
| 133 | +} |
0 commit comments