Skip to content

Commit

Permalink
switched to curl2 provider which includes timeout configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
atrull committed Nov 19, 2023
1 parent ce3a3e4 commit 9abc5d0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@

This module polls a series of fairly well known but occasionally unreliable 'what is my ip' services. It then produces the most common response that is a valid ipv4 or ipv6 address (both outputs are separately provided).

We support two providers - `curl` and `http`. `curl` is the default provider because it has better failure handling whereas `http` provider will fail a plan/apply if the endpoint doesn't respond.
We support two providers - `curl2` and `http`. `curl2` is the default provider because it has better failure handling whereas `http` provider will fail a plan/apply if the endpoint doesn't respond.

## Limitations

Neither the `curl` nor `http` providers are perfect. The `curl` provider is slightly more reliable than the `http` provider, but `curl` is missing a timeout option (for now).
Neither the `curl2` nor `http` providers are perfect. The `curl2` provider is slightly more reliable than the `http` provider.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | ~> 1.0 |
| <a name="requirement_curl"></a> [curl](#requirement\_curl) | ~> 1.0.2 |
| <a name="requirement_curl2"></a> [curl2](#requirement\_curl2) | ~> 1.6 |
| <a name="requirement_http"></a> [http](#requirement\_http) | ~> 3 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_curl"></a> [curl](#provider\_curl) | 1.0.2 |
| <a name="provider_curl2"></a> [curl2](#provider\_curl2) | 1.6.1 |
| <a name="provider_http"></a> [http](#provider\_http) | 3.4.0 |

## Modules
Expand All @@ -34,15 +34,17 @@ No modules.

| Name | Type |
|------|------|
| [curl_curl.myip](https://registry.terraform.io/providers/anschoewe/curl/latest/docs/data-sources/curl) | data source |
| [curl2_curl2.myip](https://registry.terraform.io/providers/mehulgohil/curl2/latest/docs/data-sources/curl2) | data source |
| [http_http.myip](https://registry.terraform.io/providers/hashicorp/http/latest/docs/data-sources/http) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_data_provider"></a> [data\_provider](#input\_data\_provider) | `curl` or `http` providers are both supported - we recommend `curl` | `string` | `"curl"` | no |
| <a name="input_data_provider"></a> [data\_provider](#input\_data\_provider) | `curl2` or `http` providers are both supported - we recommend `curl2` | `string` | `"curl2"` | no |
| <a name="input_extra_service_urls"></a> [extra\_service\_urls](#input\_extra\_service\_urls) | Put your own in here if you want extra ones, this gets merged with the `service_urls` list | `list(string)` | `[]` | no |
| <a name="input_request_timeout"></a> [request\_timeout](#input\_request\_timeout) | Request timeout in milliseconds | `number` | `500` | no |
| <a name="input_retry_attempts"></a> [retry\_attempts](#input\_retry\_attempts) | Request retries | `number` | `0` | no |
| <a name="input_service_urls"></a> [service\_urls](#input\_service\_urls) | List of urls to use for getting our IP | `list(string)` | <pre>[<br> "https://api.seeip.org",<br> "https://ipinfo.io/ip",<br> "https://ifconfig.co",<br> "https://icanhazip.com",<br> "https://api.ipify.org",<br> "https://ifconfig.me",<br> "https://ipecho.net/plain",<br> "https://ifconfig.io",<br> "https://ident.me",<br> "https://ipv4.ident.me"<br>]</pre> | no |

## Outputs
Expand Down
29 changes: 20 additions & 9 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
terraform {
required_version = "~> 1.0"
required_providers {
curl = {
version = "~> 1.0.2"
source = "anschoewe/curl"
curl2 = {
version = "~> 1.6"
source = "mehulgohil/curl2"
}
http = {
version = "~> 3"
Expand All @@ -12,27 +12,38 @@ terraform {
}
}

# curl is the default method
data "curl" "myip" {
for_each = var.data_provider == "curl" ? toset(local.service_urls) : []
http_method = "GET"
provider "curl2" {
timeout_ms = var.request_timeout
retry {
retry_attempts = var.retry_attempts
}
}

# curl2 is the default method
data "curl2" "myip" {
for_each = var.data_provider == "curl2" ? toset(local.service_urls) : []
uri = each.key
http_method = "GET"

}

# but we can use http if you prefer
data "http" "myip" {
for_each = var.data_provider == "http" ? toset(local.service_urls) : []
url = each.key
method = "GET"
request_timeout_ms = 500
request_timeout_ms = var.request_timeout
retry {
attempts = var.retry_attempts
}
}

locals {
# merge extra with primary list and make sure entries are unique
service_urls = distinct(concat(var.service_urls, var.extra_service_urls))

# build a list of responses
service_response_bodies = var.data_provider == "curl" ? values(data.curl.myip)[*].response : values(data.http.myip)[*].response_body
service_response_bodies = var.data_provider == "curl2" ? values(data.curl2.myip)[*].response.body : values(data.http.myip)[*].response_body

# remunge it without whitespace as a list of strings
split_output = split(",", replace(trimspace(join(",", local.service_response_bodies)), "/\\s/", ""))
Expand Down
16 changes: 14 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,26 @@ variable "service_urls" {
description = "List of urls to use for getting our IP"
}

variable "request_timeout" {
default = 500
type = number
description = "Request timeout in milliseconds"
}

variable "retry_attempts" {
default = 0
type = number
description = "Request retries"
}

variable "extra_service_urls" {
default = []
type = list(string)
description = "Put your own in here if you want extra ones, this gets merged with the `service_urls` list"
}

variable "data_provider" {
default = "curl"
default = "curl2"
type = string
description = "`curl` or `http` providers are both supported - we recommend `curl`"
description = "`curl2` or `http` providers are both supported - we recommend `curl2`"
}

0 comments on commit 9abc5d0

Please sign in to comment.