Skip to content

Commit

Permalink
feat: add cidr data source and functions (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmeligio authored Sep 25, 2024
1 parent e07376f commit c5b89c4
Show file tree
Hide file tree
Showing 42 changed files with 1,421 additions and 509 deletions.
13 changes: 6 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ jobs:
go-version-file: 'go.mod'
# Temporarily download Terraform 1.8 prerelease for function documentation support.
# When Terraform 1.8.0 final is released, this can be removed.
- uses: hashicorp/setup-terraform@a1502cd9e758c50496cc9ac5308c4843bcd56d36 # v3.0.0
- uses: hashicorp/setup-terraform@c23d9fb1b7ead2e283cd20aab7c4974b032f61f6 # v3
with:
terraform_version: '1.8.0-alpha20240216'
terraform_version: '1.9.6'
terraform_wrapper: false
- run: go generate ./...
- name: git diff
Expand All @@ -66,16 +66,15 @@ jobs:
# list whatever Terraform versions here you would like to support
terraform:
- '1.0.*'
- '1.1.*'
- '1.2.*'
- '1.3.*'
- '1.4.*'
- '1.7.*'
- '1.8.*'
- '1.9.*'
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
with:
go-version-file: 'go.mod'
- uses: hashicorp/setup-terraform@a1502cd9e758c50496cc9ac5308c4843bcd56d36 # v3.0.0
- uses: hashicorp/setup-terraform@c23d9fb1b7ead2e283cd20aab7c4974b032f61f6 # v3
with:
terraform_version: ${{ matrix.terraform }}
terraform_wrapper: false
Expand Down
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
default: testacc

# Run acceptance tests
.PHONY: testacc
testacc:
TF_ACC=1 go test ./... -v $(TESTARGS) -timeout 120m

generate:
go generate ./...

lint:
golangci-lint run --fix

# Run acceptance tests
.PHONY: generate lint testacc
55 changes: 55 additions & 0 deletions docs/data-sources/cidr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "netparse_cidr Data Source - netparse"
subcategory: ""
description: |-
Parses an IP address and prefix length in CIDR notation. It uses the net/netip https://pkg.go.dev/net/netip#Prefix.Masked go package to parse the CDIR. For more details in CIDR notation, see RFC 4632 https://rfc-editor.org/rfc/rfc4632.html and RFC 4291 https://rfc-editor.org/rfc/rfc4291.html.
---

# netparse_cidr (Data Source)

Parses an IP address and prefix length in CIDR notation. It uses the [net/netip](https://pkg.go.dev/net/netip#Prefix.Masked) go package to parse the CDIR. For more details in CIDR notation, see [RFC 4632](https://rfc-editor.org/rfc/rfc4632.html) and [RFC 4291](https://rfc-editor.org/rfc/rfc4291.html).

## Example Usage

```terraform
data "netparse_cidr" "example1" {
cidr = "192.0.2.1/24"
}
output "cidr1" {
value = data.netparse_cidr.example
# {
# cidr = "192.0.2.1/24"
# ip = "192.0.2.1"
# network = "192.0.2.0/24"
# }
}
data "netparse_cidr" "example2" {
cidr = "192.0.2.1/24"
}
output "cidr2" {
value = data.netparse_cidr.example
# {
# cidr = "2001:db8:a0b:12f0::1/32"
# ip = "2001:db8:a0b:12f0::1"
# network = "2001:db8::/32"
# }
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `cidr` (String) The IP address and prefix length in CIDR notation.

### Read-Only

- `ip` (String) The IP address.
- `network` (String) The IP network.
35 changes: 27 additions & 8 deletions docs/data-sources/domain.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,45 @@
page_title: "netparse_domain Data Source - netparse"
subcategory: ""
description: |-
Parses Public Suffix List properties from a domain. For more details on the domain parts, see What is a Domain Name? https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Web_mechanics/What_is_a_domain_name.
Parses Public Suffix List properties from a domain. It uses the publicsuffix https://pkg.go.dev/golang.org/x/net/publicsuffix go package to parse the domain. For more details on the domain parts, see What is a Domain Name? https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Web_mechanics/What_is_a_domain_name.
---

# netparse_domain (Data Source)

Parses Public Suffix List properties from a domain. For more details on the domain parts, see [What is a Domain Name?](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Web_mechanics/What_is_a_domain_name).
Parses Public Suffix List properties from a domain. It uses the [publicsuffix](https://pkg.go.dev/golang.org/x/net/publicsuffix) go package to parse the domain. For more details on the domain parts, see [What is a Domain Name?](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Web_mechanics/What_is_a_domain_name).

## Example Usage

```terraform
data "netparse_domain" "example" {
host = "foo.bar.example.com"
}
output "domain" {
value = data.netparse_domain.example
# {
# host = "foo.bar.example.com"
# domain = "example.com"
# manager = "ICANN"
# sld = "example"
# subdomain = "foo.bar"
# tld = "com"
# }
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `host` (String) The host that identifies the domain name
- `host` (String) The host that identifies the domain name.

### Read-Only

- `domain` (String) The domain name. It's the tld plus one more label. For example: example.com for host foo.example.com
- `manager` (String) The manager is the entity that manages the domain. It can be one of the following: ICANN, Private, or None.
- `sld` (String) The second-level domain (SLD) is the label to the left of the effective TLD. For example: example for example.com, or foo for foo.co.uk
- `subdomain` (String) The subdomain is the left part of the host that is not the domain. For example: www for www.example.com, mail for mail.foo.org, blog for blog.bar.org
- `tld` (String) The effective top-level domain (eTLD) of the domain. This is the public suffix of the domain. For example: com for example.com, or co.uk for foo.co.uk
- `domain` (String) The domain name. It's the tld plus one more label.
- `manager` (String) The manager is the entity that manages the domain. It can be one of: ICANN, Private, or None.
- `sld` (String) The second-level domain (SLD) is the label to the left of the effective TLD.
- `subdomain` (String) The subdomain is the left part of the host that is not the domain.
- `tld` (String) The effective top-level domain (eTLD) of the domain. This is the public suffix of the domain.
76 changes: 61 additions & 15 deletions docs/data-sources/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,60 @@
page_title: "netparse_url Data Source - netparse"
subcategory: ""
description: |-
Parses URL components from a URL string. For more details on the URL components, see What is a URL? https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL.
Parses URL components from a URL string. It uses the net/url https://pkg.go.dev/net/url go package to parse the URL. For more details on the URL components, see What is a URL? https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL and WHATWG URL Standard https://url.spec.whatwg.org/#api.
---

# netparse_url (Data Source)

Parses URL components from a URL string. For more details on the URL components, see [What is a URL?](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL).
Parses URL components from a URL string. It uses the [net/url](https://pkg.go.dev/net/url) go package to parse the URL. For more details on the URL components, see [What is a URL?](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL) and [WHATWG URL Standard](https://url.spec.whatwg.org/#api).

## Example Usage

```terraform
# Get the host from the URL
data "netparse_url" "example" {
url = "https://abc:[email protected]:45/path/to/somewhere?foo=bar&baz=qux#231"
}
output "url" {
value = data.netparse_url.example
# {
# url = "https://abc:[email protected]:45/path/to/somewhere?foo=bar&baz=qux#231"
# authority = "abc:[email protected]:45"
# credentials = "abc:def"
# fragment = "231"
# hash = "#231"
# host = "example.com"
# password = "def"
# path = "/path/to/somewhere"
# port = 45
# protocol = "https:"
# query = "foo=bar&baz=qux"
# scheme = "https"
# search = "?foo=bar&baz=qux"
# username = "abc"
# }
}
# Then get the domain from the host
data "netparse_domain" "example" {
host = data.netparse_url.example.host
}
output "domain" {
value = data.netparse_domain.example
# {
# host = "example.com"
# domain = "example.com"
# manager = "ICANN"
# sld = "example"
# subdomain = ""
# tld = "com"
# }
}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand All @@ -21,16 +67,16 @@ Parses URL components from a URL string. For more details on the URL components,

### Read-Only

- `authority` (String) The concatenation of the username, password, host, and port. It's separated from the scheme by :// . For example: user1:[email protected]:3000 for http://user1:[email protected]:3000 .
- `credentials` (String) The concatenation of the username and password. For example: user1:123 for https://user1:[email protected] .
- `fragment` (String) The URL component after the search. For example: section for https://example.com/path/to/resource?key=value#section .
- `hash` (String) The concatenation of a # with the fragment. For example: #section for https://example.com/path/to/resource?key=value#section .
- `host` (String) The domain part of the authority. For example: example.com for https://example.com .
- `password` (String, Sensitive) The second component of the URL credentials. For example: 123 for https://user1:[email protected] .
- `path` (String) The URL component after the authority. For example: /path/to/resource for https://example.com/path/to/resource .
- `port` (String) The last component of the URL authority. For example: 443 for https://example.com:443 .
- `protocol` (String) The concatenation of the scheme and the port. For example: http:, https:, ftp:, sftp:, file:, etc.
- `query` (String) The URL component of the search starting at the ? and before the fragment. For example: key=value for https://example.com/path/to/resource?key=value#section .
- `scheme` (String) The protocol used to access the domain. For example: http, https, ftp, sftp, file, etc.
- `search` (String) The URL component after the path. For example: ?key=value for https://example.com/path/to/resource?key=value .
- `username` (String, Sensitive) The first component of the URL credentials. For example: user1 for https://user1:[email protected] .
- `authority` (String) The concatenation of the username, password, host, and port. It's separated from the scheme by `://`.
- `credentials` (String) The concatenation of the username and password.
- `fragment` (String) The component after the search.
- `hash` (String) The concatenation of a `#` with the fragment.
- `host` (String) The domain part of the authority.
- `password` (String, Sensitive) The second component of the credentials.
- `path` (String) The component after the authority.
- `port` (String) The last component of the authority.
- `protocol` (String) The concatenation of the protocol scheme and `:`.
- `query` (String) A substring of the search component, after the `?` and before the fragment.
- `scheme` (String) The protocol scheme used to access the domain.
- `search` (String) The component after the path.
- `username` (String, Sensitive) The first component of the credentials.
35 changes: 35 additions & 0 deletions docs/functions/contains_ip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "contains_ip function - netparse"
subcategory: ""
description: |-
Checks if an IP address is within a network.
---

# function: contains_ip

Checks if an IP address is within a network.

## Example Usage

```terraform
locals {
example1 = provider::netparse::contains_ip("192.0.2.0/24", "192.0.2.1") # true
example2 = provider::netparse::contains_ip("192.0.2.0/24", "192.1.0.0") # false
}
```

## Signature

<!-- signature generated by tfplugindocs -->
```text
contains_ip(ip string, network string) bool
```

## Arguments

<!-- arguments generated by tfplugindocs -->
1. `ip` (String) The IP address.
1. `network` (String) The IP network.

37 changes: 37 additions & 0 deletions docs/functions/parse_cidr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "parse_cidr function - netparse"
subcategory: ""
description: |-
Parses an IP address and prefix length in CIDR notation. It uses the net/netip https://pkg.go.dev/net/netip#Prefix.Masked go package to parse the CDIR. For more details in CIDR notation, see RFC 4632 https://rfc-editor.org/rfc/rfc4632.html and RFC 4291 https://rfc-editor.org/rfc/rfc4291.html. The functionality is equivalent to the netparse_cidr data source.
---

# function: parse_cidr

Parses an IP address and prefix length in CIDR notation. It uses the [net/netip](https://pkg.go.dev/net/netip#Prefix.Masked) go package to parse the CDIR. For more details in CIDR notation, see [RFC 4632](https://rfc-editor.org/rfc/rfc4632.html) and [RFC 4291](https://rfc-editor.org/rfc/rfc4291.html). The functionality is equivalent to the `netparse_cidr` data source.

## Example Usage

```terraform
output "domain" {
value = provider::netparse::parse_cidr("192.0.2.1/24")
# {
# ip = "192.0.2.1"
# network = "192.0.2.0/24"
# }
}
```

## Signature

<!-- signature generated by tfplugindocs -->
```text
parse_cidr(cidr string) object
```

## Arguments

<!-- arguments generated by tfplugindocs -->
1. `cidr` (String) The IP address and prefix length in CIDR notation.

20 changes: 17 additions & 3 deletions docs/functions/parse_domain.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,28 @@
page_title: "parse_domain function - netparse"
subcategory: ""
description: |-
Parses Public Suffix List properties from a domain. For more details on the domain parts, see What is a Domain Name? https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Web_mechanics/What_is_a_domain_name.
Parses Public Suffix List properties from a domain. It uses the publicsuffix https://pkg.go.dev/golang.org/x/net/publicsuffix go package to parse the domain. For more details on the domain parts, see What is a Domain Name? https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Web_mechanics/What_is_a_domain_name. The functionality is equivalent to the netparse_domain data source.
---

# function: parse_domain

Parses Public Suffix List properties from a domain. For more details on the domain parts, see [What is a Domain Name?](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Web_mechanics/What_is_a_domain_name).
Parses Public Suffix List properties from a domain. It uses the [publicsuffix](https://pkg.go.dev/golang.org/x/net/publicsuffix) go package to parse the domain. For more details on the domain parts, see [What is a Domain Name?](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Web_mechanics/What_is_a_domain_name). The functionality is equivalent to the `netparse_domain` data source.

## Example Usage

```terraform
output "domain" {
value = provider::netparse::parse_domain("foo.bar.example.com")
# {
# domain = "example.com"
# manager = "ICANN"
# sld = "example"
# subdomain = "foo.bar"
# tld = "com"
# }
}
```

## Signature

Expand All @@ -22,5 +36,5 @@ parse_domain(host string) object
## Arguments

<!-- arguments generated by tfplugindocs -->
1. `host` (String) The host that identifies the domain name
1. `host` (String) The host that identifies the domain name.

Loading

0 comments on commit c5b89c4

Please sign in to comment.