Archived
Require provider api_key and remove env var fallback
This commit is contained in:
@@ -10,7 +10,7 @@ A standalone Terraform provider for Dynu DNS and domain management.
|
|||||||
- `dynu_domains`
|
- `dynu_domains`
|
||||||
- `dynu_domain`
|
- `dynu_domain`
|
||||||
- `dynu_dns_records`
|
- `dynu_dns_records`
|
||||||
- Provider authentication via `api_key` or `DYNU_API_KEY`.
|
- Provider authentication via `api_key`.
|
||||||
|
|
||||||
## Minimal usage example
|
## Minimal usage example
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -29,5 +29,5 @@ provider "dynu" {
|
|||||||
|
|
||||||
### Optional
|
### Optional
|
||||||
|
|
||||||
- `api_key` (String, Sensitive) Dynu API key. If omitted, `DYNU_API_KEY` is used.
|
- `api_key` (String, Sensitive) Dynu API key. Configure via provider argument (for example with `var.dynu_api_key` from `terraform.tfvars`).
|
||||||
- `base_url` (String) Override API base URL. Primarily intended for automated tests.
|
- `base_url` (String) Override API base URL. Primarily intended for automated tests.
|
||||||
|
|||||||
@@ -20,9 +20,7 @@ This example performs full live validation of the Dynu provider lifecycle by cre
|
|||||||
## Prerequisites
|
## Prerequisites
|
||||||
|
|
||||||
- A local build of this provider with Terraform `dev_overrides` for `dynu/dynu`.
|
- A local build of this provider with Terraform `dev_overrides` for `dynu/dynu`.
|
||||||
- Dynu API key provided either by:
|
- Dynu API key provided as Terraform variable `dynu_api_key` (for example via `terraform.tfvars`).
|
||||||
- environment variable `DYNU_API_KEY`, or
|
|
||||||
- Terraform variable `dynu_api_key`.
|
|
||||||
- A disposable domain value for `test_domain`.
|
- A disposable domain value for `test_domain`.
|
||||||
|
|
||||||
## Required variables
|
## Required variables
|
||||||
@@ -31,7 +29,7 @@ Create `terraform.tfvars` in this folder:
|
|||||||
|
|
||||||
```hcl
|
```hcl
|
||||||
test_domain = "my-disposable-test-domain.example"
|
test_domain = "my-disposable-test-domain.example"
|
||||||
# dynu_api_key = "..." # optional if DYNU_API_KEY is exported
|
dynu_api_key = "..."
|
||||||
```
|
```
|
||||||
|
|
||||||
## Commands
|
## Commands
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
variable "dynu_api_key" {
|
variable "dynu_api_key" {
|
||||||
description = "Dynu API key. Leave null to use DYNU_API_KEY from environment."
|
description = "Dynu API key used by the provider. Set in terraform.tfvars."
|
||||||
type = string
|
type = string
|
||||||
default = null
|
default = null
|
||||||
sensitive = true
|
sensitive = true
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ Using a single suffix (`test_suffix`), this example creates five DNS record scen
|
|||||||
## Prerequisites
|
## Prerequisites
|
||||||
|
|
||||||
- Local provider binary + Terraform `dev_overrides` for `dynu/dynu`
|
- Local provider binary + Terraform `dev_overrides` for `dynu/dynu`
|
||||||
- `DYNU_API_KEY` exported, or `dynu_api_key` set in `terraform.tfvars`
|
- `dynu_api_key` set in `terraform.tfvars`
|
||||||
- A Dynu-managed root domain you control
|
- A Dynu-managed root domain you control
|
||||||
|
|
||||||
## Configure
|
## Configure
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
# Optional if DYNU_API_KEY is exported in your shell.
|
dynu_api_key = "REPLACE_ME"
|
||||||
# dynu_api_key = "REPLACE_ME"
|
|
||||||
|
|
||||||
# Required: a Dynu-managed root domain/zone that you control.
|
# Required: a Dynu-managed root domain/zone that you control.
|
||||||
# Do NOT put an existing production hostname here.
|
# Do NOT put an existing production hostname here.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
variable "dynu_api_key" {
|
variable "dynu_api_key" {
|
||||||
description = "Dynu API key. Leave null to use DYNU_API_KEY from environment."
|
description = "Dynu API key used by the provider. Set in terraform.tfvars."
|
||||||
type = string
|
type = string
|
||||||
default = null
|
default = null
|
||||||
sensitive = true
|
sensitive = true
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ terraform {
|
|||||||
}
|
}
|
||||||
|
|
||||||
provider "dynu" {
|
provider "dynu" {
|
||||||
# api_key can be omitted when DYNU_API_KEY is set.
|
# Set var.dynu_api_key in terraform.tfvars.
|
||||||
api_key = var.dynu_api_key
|
api_key = var.dynu_api_key
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ terraform {
|
|||||||
# This source address stays "dynu/dynu" while using ~/.terraformrc dev_overrides.
|
# This source address stays "dynu/dynu" while using ~/.terraformrc dev_overrides.
|
||||||
# Terraform will load your local terraform-provider-dynu binary instead of the public registry.
|
# Terraform will load your local terraform-provider-dynu binary instead of the public registry.
|
||||||
provider "dynu" {
|
provider "dynu" {
|
||||||
# Use DYNU_API_KEY environment variable by default.
|
# Set var.dynu_api_key in terraform.tfvars.
|
||||||
# Set var.dynu_api_key in terraform.tfvars to override.
|
|
||||||
api_key = var.dynu_api_key
|
api_key = var.dynu_api_key
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
# Copy to terraform.tfvars and adjust values for your environment.
|
# Copy to terraform.tfvars and adjust values for your environment.
|
||||||
# dynu_api_key can be omitted when DYNU_API_KEY is exported in your shell.
|
dynu_api_key = "replace-with-dynu-api-key"
|
||||||
# dynu_api_key = "replace-with-dynu-api-key"
|
|
||||||
|
|
||||||
# Use a hostname that exists in your Dynu account.
|
# Use a hostname that exists in your Dynu account.
|
||||||
hostname = "www.example.com"
|
hostname = "www.example.com"
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
variable "dynu_api_key" {
|
variable "dynu_api_key" {
|
||||||
description = "Dynu API key. Leave null to use DYNU_API_KEY from environment."
|
description = "Dynu API key used by the provider. Set in terraform.tfvars."
|
||||||
type = string
|
type = string
|
||||||
default = null
|
default = null
|
||||||
sensitive = true
|
sensitive = true
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package provider
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||||
@@ -48,7 +47,7 @@ func (p *dynuProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp
|
|||||||
"api_key": schema.StringAttribute{
|
"api_key": schema.StringAttribute{
|
||||||
Optional: true,
|
Optional: true,
|
||||||
Sensitive: true,
|
Sensitive: true,
|
||||||
Description: "Dynu API key. If omitted, the provider uses the DYNU_API_KEY environment variable.",
|
Description: "Dynu API key. Set this explicitly, such as via a Terraform variable in terraform.tfvars.",
|
||||||
},
|
},
|
||||||
"base_url": schema.StringAttribute{
|
"base_url": schema.StringAttribute{
|
||||||
Optional: true,
|
Optional: true,
|
||||||
@@ -66,12 +65,12 @@ func (p *dynuProvider) Configure(ctx context.Context, req provider.ConfigureRequ
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
apiKey := resolveAPIKey(data.APIKey, os.Getenv("DYNU_API_KEY"))
|
apiKey := resolveAPIKey(data.APIKey)
|
||||||
if apiKey == "" {
|
if apiKey == "" {
|
||||||
resp.Diagnostics.AddAttributeError(
|
resp.Diagnostics.AddAttributeError(
|
||||||
path.Root("api_key"),
|
path.Root("api_key"),
|
||||||
"Missing Dynu API key",
|
"Missing Dynu API key",
|
||||||
"Configure api_key in the provider block or set the DYNU_API_KEY environment variable.",
|
"Configure api_key in the provider block (for example, from var.dynu_api_key set in terraform.tfvars).",
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -81,11 +80,11 @@ func (p *dynuProvider) Configure(ctx context.Context, req provider.ConfigureRequ
|
|||||||
resp.ResourceData = providerData
|
resp.ResourceData = providerData
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveAPIKey(configValue types.String, envValue string) string {
|
func resolveAPIKey(configValue types.String) string {
|
||||||
if !configValue.IsNull() && !configValue.IsUnknown() {
|
if !configValue.IsNull() && !configValue.IsUnknown() {
|
||||||
return strings.TrimSpace(configValue.ValueString())
|
return strings.TrimSpace(configValue.ValueString())
|
||||||
}
|
}
|
||||||
return strings.TrimSpace(envValue)
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *dynuProvider) DataSources(_ context.Context) []func() datasource.DataSource {
|
func (p *dynuProvider) DataSources(_ context.Context) []func() datasource.DataSource {
|
||||||
|
|||||||
@@ -16,19 +16,17 @@ func TestResolveAPIKey(t *testing.T) {
|
|||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
config types.String
|
config types.String
|
||||||
env string
|
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{name: "config wins", config: types.StringValue("config-key"), env: "env-key", want: "config-key"},
|
{name: "configured", config: types.StringValue("config-key"), want: "config-key"},
|
||||||
{name: "env fallback", config: types.StringNull(), env: "env-key", want: "env-key"},
|
{name: "null when missing", config: types.StringNull(), want: ""},
|
||||||
{name: "trim spaces", config: types.StringValue(" config-key "), env: " env-key ", want: "config-key"},
|
{name: "trim spaces", config: types.StringValue(" config-key "), want: "config-key"},
|
||||||
{name: "unknown uses env", config: types.StringUnknown(), env: "env-key", want: "env-key"},
|
{name: "unknown when pending", config: types.StringUnknown(), want: ""},
|
||||||
{name: "empty when missing", config: types.StringNull(), env: "", want: ""},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
if got := resolveAPIKey(tc.config, tc.env); got != tc.want {
|
if got := resolveAPIKey(tc.config); got != tc.want {
|
||||||
t.Fatalf("resolveAPIKey() = %q, want %q", got, tc.want)
|
t.Fatalf("resolveAPIKey() = %q, want %q", got, tc.want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user