Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add provider schema #17

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 129 additions & 8 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{}
Expand Down
Loading