12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- // Copyright (c) HashiCorp, Inc.
- // SPDX-License-Identifier: MPL-2.0
- 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),
- )
- }
|