-
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.
- Loading branch information
1 parent
eaf078a
commit 70c11bf
Showing
10 changed files
with
461 additions
and
5 deletions.
There are no files selected for viewing
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,27 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "cidr_contains function - pf" | ||
subcategory: "" | ||
description: |- | ||
Returns true if the provided IP address is in the indicated CIDR block. | ||
--- | ||
|
||
# function: cidr_contains | ||
|
||
|
||
|
||
|
||
|
||
## Signature | ||
|
||
<!-- signature generated by tfplugindocs --> | ||
```text | ||
cidr_contains(ipv4_cidr_block string, ipv4_address string) bool | ||
``` | ||
|
||
## Arguments | ||
|
||
<!-- arguments generated by tfplugindocs --> | ||
1. `ipv4_cidr_block` (String) The CIDR block to check against | ||
1. `ipv4_address` (String) The IP to use for the check | ||
|
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,26 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "cidr_count_hosts function - pf" | ||
subcategory: "" | ||
description: |- | ||
Returns the number of hosts / ip addresses in a given CIDR range. | ||
--- | ||
|
||
# function: cidr_count_hosts | ||
|
||
|
||
|
||
|
||
|
||
## Signature | ||
|
||
<!-- signature generated by tfplugindocs --> | ||
```text | ||
cidr_count_hosts(cidr_block string) number | ||
``` | ||
|
||
## Arguments | ||
|
||
<!-- arguments generated by tfplugindocs --> | ||
1. `cidr_block` (String) The CIDR block to count | ||
|
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,26 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "cidrs_overlap function - pf" | ||
subcategory: "" | ||
description: |- | ||
Returns true if at least one of the listed CIDR ranges overlaps with another. | ||
--- | ||
|
||
# function: cidrs_overlap | ||
|
||
|
||
|
||
|
||
|
||
## Signature | ||
|
||
<!-- signature generated by tfplugindocs --> | ||
```text | ||
cidrs_overlap(cidr_blocks list of string) bool | ||
``` | ||
|
||
## Arguments | ||
|
||
<!-- arguments generated by tfplugindocs --> | ||
1. `cidr_blocks` (List of String) The CIDR blocks to check against | ||
|
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
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,70 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
"net" | ||
) | ||
|
||
var ( | ||
_ function.Function = CIDRContainsFunction{} | ||
) | ||
|
||
func NewCIDRContainsFunction() function.Function { | ||
return CIDRContainsFunction{} | ||
} | ||
|
||
type CIDRContainsFunction struct{} | ||
|
||
func (f CIDRContainsFunction) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { | ||
resp.Name = "cidr_contains" | ||
} | ||
|
||
func (f CIDRContainsFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { | ||
resp.Definition = function.Definition{ | ||
Summary: "Returns true if the provided IP address is in the indicated CIDR block.", | ||
Parameters: []function.Parameter{ | ||
function.StringParameter{ | ||
AllowNullValue: false, | ||
AllowUnknownValues: false, | ||
Description: "The CIDR block to check against", | ||
Name: "ipv4_cidr_block", | ||
}, | ||
function.StringParameter{ | ||
AllowNullValue: false, | ||
AllowUnknownValues: false, | ||
Description: "The IP to use for the check", | ||
Name: "ipv4_address", | ||
}, | ||
}, | ||
Return: function.BoolReturn{}, | ||
} | ||
} | ||
|
||
func (f CIDRContainsFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { | ||
var cidrStr, ipStr string | ||
|
||
resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &cidrStr, &ipStr)) | ||
if resp.Error != nil { | ||
return | ||
|
||
} | ||
|
||
ip := net.ParseIP(ipStr) | ||
if ip == nil { | ||
resp.Error = function.ConcatFuncErrors(resp.Error, function.NewFuncError(fmt.Sprintf("Invalid IP address: %s\n", ipStr))) | ||
return | ||
} | ||
|
||
_, cidrNet, err := net.ParseCIDR(cidrStr) | ||
if err != nil { | ||
resp.Error = function.ConcatFuncErrors(resp.Error, function.NewFuncError(fmt.Sprintf("Invalid CIDR block: %s\n", cidrStr))) | ||
return | ||
} | ||
|
||
resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, cidrNet.Contains(ip))) | ||
} |
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,81 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
"net" | ||
) | ||
|
||
var ( | ||
_ function.Function = CIDRCountHosts{} | ||
) | ||
|
||
func NewCIDRCountHosts() function.Function { | ||
return CIDRCountHosts{} | ||
} | ||
|
||
type CIDRCountHosts struct{} | ||
|
||
func (f CIDRCountHosts) Metadata(_ context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { | ||
resp.Name = "cidr_count_hosts" | ||
} | ||
|
||
func (f CIDRCountHosts) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { | ||
resp.Definition = function.Definition{ | ||
Summary: "Returns the number of hosts / ip addresses in a given CIDR range.", | ||
Parameters: []function.Parameter{ | ||
function.StringParameter{ | ||
AllowNullValue: false, | ||
AllowUnknownValues: false, | ||
Description: "The CIDR block to count", | ||
Name: "cidr_block", | ||
}, | ||
}, | ||
Return: function.Int64Return{}, | ||
} | ||
} | ||
|
||
func (f CIDRCountHosts) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { | ||
var cidrStr string | ||
|
||
resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &cidrStr)) | ||
if resp.Error != nil { | ||
return | ||
|
||
} | ||
|
||
_, cidrNet, err := net.ParseCIDR(cidrStr) | ||
if err != nil { | ||
resp.Error = function.ConcatFuncErrors(resp.Error, function.NewFuncError(fmt.Sprintf("Invalid CIDR block: %s\n", cidrStr))) | ||
return | ||
} | ||
|
||
resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, countHosts(cidrNet))) | ||
} | ||
|
||
func countHosts(cidrNet *net.IPNet) int64 { | ||
maskOnes, _ := cidrNet.Mask.Size() | ||
hostBits := 32 - maskOnes | ||
total := int64(1) << hostBits // total addresses in the subnet | ||
|
||
// Edge cases: | ||
// /31: 2 usable addresses (RFC 3021) | ||
// /32: 1 address, considered usable | ||
// Otherwise: total - 2 for the network and broadcast addresses | ||
switch maskOnes { | ||
case 31: | ||
return 2 | ||
case 32: | ||
return 1 | ||
default: | ||
if total > 2 { | ||
return total - 2 | ||
} | ||
// If the subnet is somehow smaller or invalid, just return 0 | ||
return 0 | ||
} | ||
} |
Oops, something went wrong.