cidrtypes

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2023 License: MPL-2.0 Imports: 9 Imported by: 2

Documentation

Overview

Package cidrtypes contains Terraform Plugin Framework Custom Type implementations for IPv4 and IPv6 CIDR strings.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IPv4Prefix

type IPv4Prefix struct {
	basetypes.StringValue
}

IPv4Prefix represents a valid IPv4 CIDR string (RFC 4632). No semantic equality logic is defined for IPv4Prefix, so it will follow Terraform's data-consistency rules for strings, which must match byte-for-byte.

func NewIPv4PrefixNull

func NewIPv4PrefixNull() IPv4Prefix

NewIPv4PrefixNull creates an IPv4Prefix with a null value. Determine whether the value is null via IsNull method.

func NewIPv4PrefixPointerValue

func NewIPv4PrefixPointerValue(value *string) IPv4Prefix

NewIPv4PrefixPointerValue creates an IPv4Prefix with a null value if nil or a known value. Access the value via ValueStringPointer method.

func NewIPv4PrefixUnknown

func NewIPv4PrefixUnknown() IPv4Prefix

NewIPv4PrefixUnknown creates an IPv4Prefix with an unknown value. Determine whether the value is unknown via IsUnknown method.

func NewIPv4PrefixValue

func NewIPv4PrefixValue(value string) IPv4Prefix

NewIPv4PrefixValue creates an IPv4Prefix with a known value. Access the value via ValueString method.

func (IPv4Prefix) Equal

func (v IPv4Prefix) Equal(o attr.Value) bool

Equal returns true if the given value is equivalent.

func (IPv4Prefix) Type

func (v IPv4Prefix) Type(_ context.Context) attr.Type

Type returns an IPv4PrefixType.

func (IPv4Prefix) ValueIPv4Prefix

func (v IPv4Prefix) ValueIPv4Prefix() (netip.Prefix, diag.Diagnostics)

ValueIPv4Prefix calls netip.ParsePrefix with the IPv4Prefix StringValue. A null or unknown value will produce an error diagnostic.

Example
package main

import (
	"fmt"
	"net/netip"

	"github.com/hashicorp/terraform-plugin-framework-nettypes/cidrtypes"
)

type IPv4PrefixResourceModel struct {
	IPv4CIDR cidrtypes.IPv4Prefix `tfsdk:"ipv4_cidr"`
}

func main() {
	// For example purposes, typically the data model would be populated automatically by Plugin Framework via Config, Plan or State.
	// https://developer.hashicorp.com/terraform/plugin/framework/handling-data/accessing-values
	data := IPv4PrefixResourceModel{
		IPv4CIDR: cidrtypes.NewIPv4PrefixValue("127.0.0.0/8"),
	}

	// Check that the IPv4CIDR data is known and able to be converted to netip.Prefix
	if !data.IPv4CIDR.IsNull() && !data.IPv4CIDR.IsUnknown() {
		ipPrefix, diags := data.IPv4CIDR.ValueIPv4Prefix()
		if diags.HasError() {
			return
		}

		loopback := netip.MustParseAddr("127.0.0.1")

		fmt.Printf("%t, %s\n", ipPrefix.Contains(loopback), ipPrefix)
	}
}
Output:

true, 127.0.0.0/8

type IPv4PrefixType

type IPv4PrefixType struct {
	basetypes.StringType
}

IPv4PrefixType is an attribute type that represents a valid IPv4 CIDR string (RFC 4632). No semantic equality logic is defined for IPv4PrefixType, so it will follow Terraform's data-consistency rules for strings, which must match byte-for-byte.

func (IPv4PrefixType) Equal

func (t IPv4PrefixType) Equal(o attr.Type) bool

Equal returns true if the given type is equivalent.

func (IPv4PrefixType) String

func (t IPv4PrefixType) String() string

String returns a human readable string of the type name.

func (IPv4PrefixType) Validate

func (t IPv4PrefixType) Validate(ctx context.Context, in tftypes.Value, path path.Path) diag.Diagnostics

Validate implements type validation. This type requires the value provided to be a String value that is a valid IPv4 CIDR (RFC 4632).

func (IPv4PrefixType) ValueFromString

ValueFromString returns a StringValuable type given a StringValue.

func (IPv4PrefixType) ValueFromTerraform

func (t IPv4PrefixType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error)

ValueFromTerraform returns a Value given a tftypes.Value. This is meant to convert the tftypes.Value into a more convenient Go type for the provider to consume the data with.

func (IPv4PrefixType) ValueType

func (t IPv4PrefixType) ValueType(ctx context.Context) attr.Value

ValueType returns the Value type.

type IPv6Prefix

type IPv6Prefix struct {
	basetypes.StringValue
}

IPv6Prefix represents a valid IPv6 CIDR string (RFC 4291). Semantic equality logic is defined for IPv6Prefix such that a CIDR string with the address zero bits `compressed` will be considered equivalent to the `non-compressed` string.

Examples:

  • `0:0:0:0:0:0:0:0/128` is semantically equal to `::/128`
  • `2001:0DB8:0:0:0:0:0:0CD3/60` is semantically equal to `2001:0DB8::CD30/60`
  • `FF00:0:0:0:0:0:0:0/8` is semantically equal to `FF00::/8`

func NewIPv6PrefixNull

func NewIPv6PrefixNull() IPv6Prefix

NewIPv6PrefixNull creates an IPv6Prefix with a null value. Determine whether the value is null via IsNull method.

func NewIPv6PrefixPointerValue

func NewIPv6PrefixPointerValue(value *string) IPv6Prefix

NewIPv6PrefixPointerValue creates an IPv6Prefix with a null value if nil or a known value. Access the value via ValueStringPointer method.

func NewIPv6PrefixUnknown

func NewIPv6PrefixUnknown() IPv6Prefix

NewIPv6PrefixUnknown creates an IPv6Prefix with an unknown value. Determine whether the value is unknown via IsUnknown method.

func NewIPv6PrefixValue

func NewIPv6PrefixValue(value string) IPv6Prefix

NewIPv6PrefixValue creates an IPv6Prefix with a known value. Access the value via ValueString method.

func (IPv6Prefix) Equal

func (v IPv6Prefix) Equal(o attr.Value) bool

Equal returns true if the given value is equivalent.

func (IPv6Prefix) StringSemanticEquals

func (v IPv6Prefix) StringSemanticEquals(_ context.Context, newValuable basetypes.StringValuable) (bool, diag.Diagnostics)

StringSemanticEquals returns true if the given IPv6 CIDR string value is semantically equal to the current IPv6 CIDR string value. This comparison utilizes netip.ParsePrefix and then compares the resulting netip.Prefix representations (comparing (Prefix).Addr() and (Prefix).Bits() respectively). This means `compressed` IPv6 CIDR values are considered semantically equal to `non-compressed` IPv6 CIDR values.

Examples:

  • `0:0:0:0:0:0:0:0/128` is semantically equal to `::/128`
  • `2001:0DB8:0:0:0:0:0:0CD3/60` is semantically equal to `2001:0DB8::CD30/60`
  • `FF00:0:0:0:0:0:0:0/8` is semantically equal to `FF00::/8`

See RFC 4291 for more details on IPv6 CIDR string format: https://www.rfc-editor.org/rfc/rfc4291.html#section-2.3

func (IPv6Prefix) Type

func (v IPv6Prefix) Type(_ context.Context) attr.Type

Type returns an IPv6PrefixType.

func (IPv6Prefix) ValueIPv6Prefix

func (v IPv6Prefix) ValueIPv6Prefix() (netip.Prefix, diag.Diagnostics)

ValueIPv6Prefix calls netip.ParsePrefix with the IPv6Prefix StringValue. A null or unknown value will produce an error diagnostic.

Example
package main

import (
	"fmt"
	"net/netip"

	"github.com/hashicorp/terraform-plugin-framework-nettypes/cidrtypes"
)

type IPv6PrefixResourceModel struct {
	IPv6CIDR cidrtypes.IPv6Prefix `tfsdk:"ipv6_cidr"`
}

func main() {
	// For example purposes, typically the data model would be populated automatically by Plugin Framework via Config, Plan or State.
	// https://developer.hashicorp.com/terraform/plugin/framework/handling-data/accessing-values
	data := IPv6PrefixResourceModel{
		IPv6CIDR: cidrtypes.NewIPv6PrefixValue("::1/128"),
	}

	// Check that the IPv6CIDR data is known and able to be converted to netip.Prefix
	if !data.IPv6CIDR.IsNull() && !data.IPv6CIDR.IsUnknown() {
		ipPrefix, diags := data.IPv6CIDR.ValueIPv6Prefix()
		if diags.HasError() {
			return
		}

		loopback := netip.MustParseAddr("::1")

		fmt.Printf("%t, %s\n", ipPrefix.Contains(loopback), ipPrefix)
	}
}
Output:

true, ::1/128

type IPv6PrefixType

type IPv6PrefixType struct {
	basetypes.StringType
}

IPv6PrefixType is an attribute type that represents a valid IPv6 CIDR string (RFC 4291). Semantic equality logic is defined for IPv6PrefixType such that a CIDR string with the address zero bits `compressed` will be considered equivalent to the `non-compressed` string.

Examples:

  • `0:0:0:0:0:0:0:0/128` is semantically equal to `::/128`
  • `2001:0DB8:0:0:0:0:0:0CD3/60` is semantically equal to `2001:0DB8::CD30/60`
  • `FF00:0:0:0:0:0:0:0/8` is semantically equal to `FF00::/8`

func (IPv6PrefixType) Equal

func (t IPv6PrefixType) Equal(o attr.Type) bool

Equal returns true if the given type is equivalent.

func (IPv6PrefixType) String

func (t IPv6PrefixType) String() string

String returns a human readable string of the type name.

func (IPv6PrefixType) Validate

func (t IPv6PrefixType) Validate(ctx context.Context, in tftypes.Value, path path.Path) diag.Diagnostics

Validate implements type validation. This type requires the value provided to be a String value that is a valid IPv6 CIDR.

func (IPv6PrefixType) ValueFromString

ValueFromString returns a StringValuable type given a StringValue.

func (IPv6PrefixType) ValueFromTerraform

func (t IPv6PrefixType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error)

ValueFromTerraform returns a Value given a tftypes.Value. This is meant to convert the tftypes.Value into a more convenient Go type for the provider to consume the data with.

func (IPv6PrefixType) ValueType

func (t IPv6PrefixType) ValueType(ctx context.Context) attr.Value

ValueType returns the Value type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL