-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create address_value terraform provider function (#154)
* 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
1 parent
84350fc
commit ef492d8
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
assets/terraform/examples/functions/address_value/function.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters