Skip to content

Commit

Permalink
feat: create address_value terraform provider function (#154)
Browse files Browse the repository at this point in the history
* Create address_value terraform provider function

Given either address_object resource, or one of address_objects addresses,
this function will return the value, depending on the type of the address
this resource describes.

* add function example

* add function example

---------

Co-authored-by: Migara Ekanayake <[email protected]>
  • Loading branch information
kklimonda-cl and migara authored Sep 9, 2024
1 parent 84350fc commit ef492d8
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
28 changes: 28 additions & 0 deletions assets/terraform/examples/functions/address_value/function.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
resource "panos_address_objects" "name" {
location = {
device_group = {
name = "<device_group_name>"
}
}

addresses = {
"foo" = {
description = "foo"
ip_netmask = "1.1.1.1"
}
"bar" = {
description = "foo"
ip_netmask = "2.2.2.2"
}
}
}

# Example 1: Get the value of a single address object.
output "foo_value" {
value = provider::panos::address_value(panos_address_objects.name.addresses.foo)
}

# Example 2: Transform all the address objects into a map of values.
output "address_values" {
value = { for k, v in panos_address_objects.name.addresses : k => provider::panos::address_value(panos_address_objects.name.addresses[k]) }
}
74 changes: 74 additions & 0 deletions assets/terraform/internal/provider/func_address_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/function"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var _ function.Function = &AddressValueFunction{}

type AddressValueFunction struct{}

func NewAddressValueFunction() function.Function {
return &AddressValueFunction{}
}

func (f *AddressValueFunction) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
resp.Name = "address_value"
}

func (f *AddressValueFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
Summary: "Return value of a given address resource",
Description: "Given an address object resource, return its value.",

Parameters: []function.Parameter{
function.ObjectParameter{
Name: "address",
Description: "address resource to get value from",
AttributeTypes: map[string]attr.Type{
"ip_netmask": types.StringType,
"ip_range": types.StringType,
"ip_wildcard": types.StringType,
"fqdn": types.StringType,
},
},
},
Return: function.StringReturn{},
}
}

func (f *AddressValueFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
var address struct {
IpNetmask *string `tfsdk:"ip_netmask"`
IpRange *string `tfsdk:"ip_range"`
IpWildcard *string `tfsdk:"ip_wildcard"`
Fqdn *string `tfsdk:"fqdn"`
}

// Read Terraform argument data into the variable
resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &address))
if resp.Error != nil {
return
}

var value string
if address.IpNetmask != nil {
value = *address.IpNetmask
} else if address.IpRange != nil {
value = *address.IpRange
} else if address.IpWildcard != nil {
value = *address.IpWildcard
} else if address.Fqdn != nil {
value = *address.Fqdn
} else {
resp.Error = function.ConcatFuncErrors(resp.Error, function.NewFuncError("given address has no value set"))
return
}

// Set the result to the same data
resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, value))
}
6 changes: 6 additions & 0 deletions pkg/translate/terraform_provider/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,12 @@ func (p *PanosProvider) Resources(_ context.Context) []func() resource.Resource
}
}
func (p *PanosProvider) Functions(_ context.Context) []func() function.Function {
return []func() function.Function{
NewAddressValueFunction,
}
}
// New is a helper function to get the provider implementation.
func New(version string) func() provider.Provider {
return func() provider.Provider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ func (g *GenerateTerraformProvider) GenerateTerraformProvider(terraformProvider
terraformProvider.ImportManager.AddStandardImport("context", "")
terraformProvider.ImportManager.AddSdkImport("github.com/PaloAltoNetworks/pango", "sdk")
terraformProvider.ImportManager.AddHashicorpImport("github.com/hashicorp/terraform-plugin-framework/datasource", "")
terraformProvider.ImportManager.AddHashicorpImport("github.com/hashicorp/terraform-plugin-framework/function", "")
terraformProvider.ImportManager.AddHashicorpImport("github.com/hashicorp/terraform-plugin-framework/provider", "")
terraformProvider.ImportManager.AddHashicorpImport("github.com/hashicorp/terraform-plugin-framework/provider/schema", "")
terraformProvider.ImportManager.AddHashicorpImport("github.com/hashicorp/terraform-plugin-framework/resource", "")
Expand Down

0 comments on commit ef492d8

Please sign in to comment.