129 lines
3.7 KiB
Go
129 lines
3.7 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
|
"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"
|
|
|
|
"terraform-provider-hpe1810/internal/provider/client"
|
|
)
|
|
|
|
var _ provider.Provider = &hpe1810Provider{}
|
|
|
|
type hpe1810Provider struct {
|
|
version string
|
|
}
|
|
|
|
// hpe1810ProviderModel mirrors config.py's three settings (SWITCH_HOST,
|
|
// SWITCH_HTTPS, SWITCH_PASSWORD) from the Python reference implementation.
|
|
type hpe1810ProviderModel struct {
|
|
Host types.String `tfsdk:"host"`
|
|
HTTPS types.Bool `tfsdk:"https"`
|
|
Password types.String `tfsdk:"password"`
|
|
}
|
|
|
|
func New(version string) func() provider.Provider {
|
|
return func() provider.Provider {
|
|
return &hpe1810Provider{version: version}
|
|
}
|
|
}
|
|
|
|
func (p *hpe1810Provider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
|
|
resp.TypeName = "hpe1810"
|
|
resp.Version = p.version
|
|
}
|
|
|
|
func (p *hpe1810Provider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
Description: "Manages an HP ProCurve/OfficeConnect 1810 switch over its built-in web UI.",
|
|
Attributes: map[string]schema.Attribute{
|
|
"host": schema.StringAttribute{
|
|
Optional: true,
|
|
Description: "Switch IP or hostname. Falls back to the HPE1810_HOST env var, then \"192.168.2.10\".",
|
|
},
|
|
"https": schema.BoolAttribute{
|
|
Optional: true,
|
|
Description: "Use HTTPS instead of HTTP. Falls back to the HPE1810_HTTPS env var, then false.",
|
|
},
|
|
"password": schema.StringAttribute{
|
|
Optional: true,
|
|
Sensitive: true,
|
|
Description: "Switch login password. Falls back to the HPE1810_PASSWORD env var, then empty.",
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (p *hpe1810Provider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
|
|
var config hpe1810ProviderModel
|
|
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
host := os.Getenv("HPE1810_HOST")
|
|
if host == "" {
|
|
host = "192.168.2.10"
|
|
}
|
|
if !config.Host.IsNull() {
|
|
host = config.Host.ValueString()
|
|
}
|
|
|
|
https := false
|
|
if envHTTPS := os.Getenv("HPE1810_HTTPS"); envHTTPS != "" {
|
|
https, _ = strconv.ParseBool(envHTTPS)
|
|
}
|
|
if !config.HTTPS.IsNull() {
|
|
https = config.HTTPS.ValueBool()
|
|
}
|
|
|
|
password := os.Getenv("HPE1810_PASSWORD")
|
|
if !config.Password.IsNull() {
|
|
password = config.Password.ValueString()
|
|
}
|
|
|
|
c, err := client.NewClient(host, https, password)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Unable to create switch client", err.Error())
|
|
return
|
|
}
|
|
if err := c.Login(); err != nil {
|
|
resp.Diagnostics.AddError("Unable to log in to switch", err.Error())
|
|
return
|
|
}
|
|
|
|
resp.DataSourceData = c
|
|
resp.ResourceData = c
|
|
}
|
|
|
|
// DataSources: one entry per extractors/*.py equivalent.
|
|
func (p *hpe1810Provider) DataSources(_ context.Context) []func() datasource.DataSource {
|
|
return []func() datasource.DataSource{
|
|
NewSystemDescriptionDataSource,
|
|
NewNetworkSetupDataSource,
|
|
NewPortSummaryDataSource,
|
|
NewLLDPStatisticsDataSource,
|
|
NewBufferedLogDataSource,
|
|
NewMACTableDataSource,
|
|
NewTrunkStatusDataSource,
|
|
NewLoopProtectionStatusDataSource,
|
|
NewDualImageStatusDataSource,
|
|
NewClockDataSource,
|
|
NewSNTPDataSource,
|
|
NewLogConfigurationDataSource,
|
|
NewBackupManagerDataSource,
|
|
}
|
|
}
|
|
|
|
// Resources will grow one entry per actions/*.py equivalent (locator first)
|
|
// as they're ported.
|
|
func (p *hpe1810Provider) Resources(_ context.Context) []func() resource.Resource {
|
|
return []func() resource.Resource{}
|
|
}
|