diff --git a/internal/provider/provider.go b/internal/provider/provider.go index b4cd1ca..1216c17 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -5,27 +5,44 @@ package provider import ( "context" + "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/defaults" "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/provider" + "github.com/hashicorp/terraform-plugin-framework/provider/schema" "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/types" ) -var _ provider.Provider = &pcbeProvider{} +var _ provider.Provider = &PCBeProvider{} func New(version string) func() provider.Provider { return func() provider.Provider { - return &pcbeProvider{ + return &PCBeProvider{ version: version, } } } -type pcbeProvider struct { +type PCBeCfg struct { + Host types.String `tfsdk:"host"` + Token types.String `tfsdk:"token"` + HTTPDump types.Bool `tfsdk:"http_dump"` + MaxPolls types.Int32 `tfsdk:"max_polls"` + PollInterval types.Float32 `tfsdk:"poll_interval"` +} + +type PCBeProviderModel struct { + PCBeCfg PCBeCfg `tfsdk:"pc"` +} + +type PCBeProvider struct { version string } -func (p *pcbeProvider) Metadata( +func (p *PCBeProvider) Metadata( _ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse, @@ -34,29 +51,133 @@ func (p *pcbeProvider) Metadata( resp.Version = p.version } -func (p *pcbeProvider) Schema( +func (p *PCBeProvider) Schema( _ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse, ) { + resp.Schema = schema.Schema{ + Blocks: map[string]schema.Block{ + "pc": schema.SingleNestedBlock{ + Attributes: map[string]schema.Attribute{ + "host": schema.StringAttribute{ + Required: true, + }, + "token": schema.StringAttribute{ + Required: true, + }, + "http_dump": schema.BoolAttribute{ + Optional: true, + }, + "max_polls": schema.Int32Attribute{ + Optional: true, + }, + "poll_interval": schema.Float32Attribute{ + Optional: true, + }, + }, + }, + }, + } } -func (p *pcbeProvider) Configure( +func (p *PCBeProvider) Configure( ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse, ) { + var config PCBeProviderModel + var httpDump bool + var host string + var token string + var maxPolls int32 + var pollInterval float32 + + diags := req.Config.Get(ctx, &config) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + if config.PCBeCfg.Host.IsUnknown() { + resp.Diagnostics.AddAttributeError( + path.Root("host"), + "unknown 'host' value in provider configuration block", + "the provider cannot create an API client "+ + "as there is an unknown configuration value for "+ + "the API host.", + ) + } + + if config.PCBeCfg.Token.IsUnknown() { + resp.Diagnostics.AddAttributeError( + path.Root("token"), + "unknown 'token' value in provider configuration block", + "the provider cannot create an API client "+ + "as there is an unknown configuration value for "+ + "the API token", + ) + } + + if resp.Diagnostics.HasError() { + return + } + + if !config.PCBeCfg.Host.IsNull() { + host = config.PCBeCfg.Host.ValueString() + } + + if !config.PCBeCfg.Token.IsNull() { + token = config.PCBeCfg.Token.ValueString() + } + + if !config.PCBeCfg.HTTPDump.IsNull() { + httpDump = config.PCBeCfg.HTTPDump.ValueBool() + } + + if !config.PCBeCfg.MaxPolls.IsNull() { + maxPolls = config.PCBeCfg.MaxPolls.ValueInt32() + } + + if !config.PCBeCfg.PollInterval.IsNull() { + pollInterval = config.PCBeCfg.PollInterval.ValueFloat32() + } + + if resp.Diagnostics.HasError() { + return + } + + cfg := client.Config{ + Token: token, + Host: host, + HTTPDump: httpDump, + MaxPolls: maxPolls, + PollInterval: pollInterval, + HTTPTimeout: defaults.HTTPTimeout, + } + client, err := client.NewPCBeClient(context.Background(), cfg) + if err != nil { + resp.Diagnostics.AddError( + "unable to create API client", + err.Error(), + ) + + return + } + + resp.DataSourceData = client + resp.ResourceData = client } // DataSources defines the data sources implemented in the provider. -func (p *pcbeProvider) DataSources( +func (p *PCBeProvider) DataSources( _ context.Context, ) []func() datasource.DataSource { return []func() datasource.DataSource{} } // Resources defines the resources implemented in the provider. -func (p *pcbeProvider) Resources( +func (p *PCBeProvider) Resources( _ context.Context, ) []func() resource.Resource { return []func() resource.Resource{}