More on the receitas provider

This commit is contained in:
Bitor Tonixa Balenca 2023-12-19 23:31:43 +00:00
parent 76c96c8d37
commit 735c45f663
No known key found for this signature in database
GPG key ID: AF4454DAA92FA1F4
5 changed files with 108 additions and 9 deletions

0
example/receitas.tf Normal file
View file

View file

@ -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
}

View file

@ -0,0 +1 @@
package provider

View file

@ -0,0 +1 @@
package provider

36
main.go
View file

@ -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())
}
}