Ver Fonte

Tutorial 9: Functions.

Frederic G. MARAND há 5 meses atrás
pai
commit
be2b09e5b8

+ 18 - 0
examples/compute_tax/main.tf

@@ -0,0 +1,18 @@
+terraform {
+  required_providers {
+    hashicups = {
+      source = "hashicorp.com/edu/hashicups"
+    }
+  }
+  required_version = ">= 1.8.0"
+}
+
+provider "hashicups" {
+  username = "education"
+  password = "test123"
+  host     = "http://localhost:19090"
+}
+
+output "total_price" {
+  value = provider::hashicups::compute_tax(5.00, 0.085)
+}

+ 61 - 0
internal/provider/compute_tax_function.go

@@ -0,0 +1,61 @@
+package provider
+
+import (
+	"context"
+	"math"
+
+	"github.com/hashicorp/terraform-plugin-framework/function"
+)
+
+var (
+	_ function.Function = &ComputeTaxFunction{}
+)
+
+type ComputeTaxFunction struct{}
+
+func NewComputeTaxFunction() function.Function {
+	return &ComputeTaxFunction{}
+}
+
+func (f *ComputeTaxFunction) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
+	resp.Name = "compute_tax"
+}
+
+func (f *ComputeTaxFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
+	resp.Definition = function.Definition{
+		Parameters: []function.Parameter{
+			function.Float64Parameter{
+				Description: "Price of coffee item.",
+				Name:        "price",
+			},
+			function.Float64Parameter{
+				Description: "Tax rate. 0.085 == 8.5%",
+				Name:        "rate",
+			},
+		},
+		Return:      function.Float64Return{},
+		Summary:     "Compute tax for coffee",
+		Description: "Given a price and tax rate, return the total cost including tax.",
+	}
+}
+
+func (f *ComputeTaxFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
+	var (
+		price, rate, total float64
+	)
+
+	// 1. Read Terraform argument data into the variables.
+	resp.Error = function.ConcatFuncErrors(
+		resp.Error,
+		req.Arguments.Get(ctx, &price, &rate),
+	)
+
+	// 2. Perform the calculation
+	total = math.Round((price+price*rate)*100) / 100
+
+	// 3. Set the result on the state
+	resp.Error = function.ConcatFuncErrors(
+		resp.Error,
+		resp.Result.Set(ctx, total),
+	)
+}

+ 9 - 1
internal/provider/provider.go

@@ -6,6 +6,7 @@ import (
 
 	"github.com/hashicorp-demoapp/hashicups-client-go"
 	"github.com/hashicorp/terraform-plugin-framework/datasource"
+	"github.com/hashicorp/terraform-plugin-framework/function"
 	"github.com/hashicorp/terraform-plugin-framework/path"
 	"github.com/hashicorp/terraform-plugin-framework/provider"
 	"github.com/hashicorp/terraform-plugin-framework/provider/schema"
@@ -16,7 +17,8 @@ import (
 
 // Ensure the implementation satisfies the expected interfaces.
 var (
-	_ provider.Provider = &hashicupsProvider{}
+	_ provider.Provider              = &hashicupsProvider{}
+	_ provider.ProviderWithFunctions = &hashicupsProvider{}
 )
 
 // New is a helper function to simplify provider server and testing implementation.
@@ -230,6 +232,12 @@ func (p *hashicupsProvider) Resources(_ context.Context) []func() resource.Resou
 	}
 }
 
+func (p *hashicupsProvider) Functions(ctx context.Context) []func() function.Function {
+	return []func() function.Function{
+		NewComputeTaxFunction,
+	}
+}
+
 //// Ensure hashicupsProvider satisfies various provider interfaces.
 //var _ provider.Provider = &hashicupsProvider{}
 //var _ provider.ProviderWithFunctions = &hashicupsProvider{}