terraform-provider-system/internal/provider/provider.go

91 lines
2.5 KiB
Go

package provider
import (
"context"
"fmt"
"git.davepedu.com/dave/terraform-provider-system/system"
"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"
)
// Ensure SystemProvider satisfies various provider interfaces.
var _ provider.Provider = &SystemProvider{}
// SystemProvider defines the provider implementation.
type SystemProvider struct {
// version is set to the provider version on release, "dev" when the
// provider is built and ran locally, and "test" when running acceptance
// testing.
version string
}
// SystemProviderModel describes the provider data model.
type SystemProviderModel struct {
ConnectionString types.String `tfsdk:"connection_string"`
}
func (p *SystemProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "system"
resp.Version = p.version
}
func (p *SystemProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"connection_string": schema.StringAttribute{
MarkdownDescription: "SSH connection string",
Required: true,
},
},
}
}
func (p *SystemProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var data SystemProviderModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
client, err := system.NewSystemManagerFromUri(data.ConnectionString.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Unable to create System Manager API client",
fmt.Sprintf("%s", err),
)
}
if err := client.Test(); err != nil {
resp.Diagnostics.AddError(
"System Manager API Client connection test failed",
fmt.Sprintf("%s", err),
)
}
resp.DataSourceData = client
resp.ResourceData = client
}
func (p *SystemProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewDirResource,
NewFileResource,
}
}
func (p *SystemProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
//NewExampleDataSource,
}
}
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &SystemProvider{
version: version,
}
}
}