From d39155ed9c32387627d2b60f20670292d6e83d5c Mon Sep 17 00:00:00 2001 From: sam Date: Thu, 12 Dec 2024 16:03:47 +0000 Subject: [PATCH] Add secret data source boilerplate Add initial boilerplate code for the "secret" data source. Currently this code does nothing, the relevant functionality will be added in later changes. --- internal/datasources/secret/data_source.go | 78 ++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 internal/datasources/secret/data_source.go diff --git a/internal/datasources/secret/data_source.go b/internal/datasources/secret/data_source.go new file mode 100644 index 0000000..c66610f --- /dev/null +++ b/internal/datasources/secret/data_source.go @@ -0,0 +1,78 @@ +// (C) Copyright 2024 Hewlett Packard Enterprise Development LP + +//go:build experimental + +package secret + +import ( + "context" + "errors" + "fmt" + + "github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/client" + "github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/constants" + "github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/sdk/systems/privatecloudbusiness" + "github.com/hashicorp/terraform-plugin-framework/attr" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" +) + +// Ensure provider defined types fully satisfy framework interfaces. +var _ datasource.DataSource = &DataSource{} + +func NewDataSource() datasource.DataSource { + return &DataSource{} +} + +// DataSource defines the data source implementation. +type DataSource struct { + client *client.PCBeClient +} + +func (s *DataSource) Metadata( + ctx context.Context, + req datasource.MetadataRequest, + resp *datasource.MetadataResponse, +) { + resp.TypeName = req.ProviderTypeName + "_secret" +} + +func (s *DataSource) Schema( + ctx context.Context, + req datasource.SchemaRequest, + resp *datasource.SchemaResponse, +) { + resp.Schema = SecretDataSourceSchema(ctx) +} + +func (s *DataSource) Configure( + ctx context.Context, + req datasource.ConfigureRequest, + resp *datasource.ConfigureResponse, +) { + // Prevent panic if the provider has not been configured. + if req.ProviderData == nil { + tflog.Warn(ctx, "provider has not been configured.") + + return + } + + s.client = req.ProviderData.(*client.PCBeClient) +} + +func (s *DataSource) Read( + ctx context.Context, + req datasource.ReadRequest, + resp *datasource.ReadResponse, +) { + var data SecretModel + + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +}