diff --git a/example/receitas.tf b/example/receitas.tf new file mode 100644 index 0000000..e69de29 diff --git a/internal/provider/provider.go b/internal/provider/provider.go new file mode 100644 index 0000000..82517a8 --- /dev/null +++ b/internal/provider/provider.go @@ -0,0 +1,79 @@ +package provider + +import ( + "context" + "net/http" + + "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" +) + +// Struct for the provider implementation +type ReceitaProvider struct { + //Version identfier for our provider + version string +} + +// Datamodel for our provider +type ReceitaProviderModel struct { + Folder types.String `tfsdk:"folder"` +} + +// Now we need to define the contract defined by provider.Provider from terraform + +// Set the metadata callback for the provider +// This callback is responsible to build metadata associated to the provider +func (p *ReceitaProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) { + //set the metadata on the response object + //get the version from the provider object + resp.Version = p.version + //set the name of the provider + resp.TypeName = "receita" +} + +// Set the schema of the provider +// This callback will be responsible to define the provider schema +func (p *ReceitaProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) { + resp.Schema = schema.Schema{ + Attributes: map[string]schema.Attribute{ + "folder": schema.StringAttribute{ + MarkdownDescription: "Folder under which the receitas will be stored", + Optional: false, + }, + }, + } +} + +// Set the initializer for the provider +// Callback to build the provider + +func (p *ReceitaProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) { + var data ReceitaProviderModel + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + + if resp.Diagnostics.HasError() { + return + } + + client := http.DefaultClient + resp.DataSourceData = client + resp.ResourceData = client +} + +// Set the resources enabled by this provider + +func (p *ReceitaProvider) Resources(ctx context.Context) []func() resource.Resource { + return nil +} + +func (p *ReceitaProvider) DataSources(ctx context.Context) []func() datasource.DataSource { + return nil +} + +// This is the builder method for our ReceitaProvider instances +func New() func() provider.Provider { + return nil +} diff --git a/internal/provider/receita_datasource.go b/internal/provider/receita_datasource.go new file mode 100644 index 0000000..4f504f6 --- /dev/null +++ b/internal/provider/receita_datasource.go @@ -0,0 +1 @@ +package provider diff --git a/internal/provider/receita_resource.go b/internal/provider/receita_resource.go new file mode 100644 index 0000000..4f504f6 --- /dev/null +++ b/internal/provider/receita_resource.go @@ -0,0 +1 @@ +package provider diff --git a/main.go b/main.go index 827c926..4de0ad4 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,32 @@ package main -import( - "github.com/hashicorp/terraform/plugin" - "github.com/hashicorp/terraform/provider" +import ( + "context" + "flag" + + "github.com/hashicorp/terraform-plugin-framework/providerserver" ) -func main(){ - plugin.Serve(&plugin.ServeOpts{ - ProviderFunc: func() terraform.ResourceProvider { - return Provider() - } - }) +func main() { + + var debug bool + + flag.BoolVar(&debug, "debug", "set to true if debug mode") + flag.Parse() + + // Initialize the structure to setup the provider service + // create the folder in .terraform.d/plugins/terraform.local/local + // otherwise you'll need to publish this provider to be able to use it + opts := providerserver.ServeOpts{ + Address: "terraform.local/balhau/receita", + Debug: debug, + } + + err := providerserver.Serve(context.Background(), provider.New(version), opts) + + // If the creation of service give us an error just print it. + if err != nil { + log.Fatal(err.Error()) + } + }