Skip to content

Commit

Permalink
feat: Support providing API key from an environment variable (#20)
Browse files Browse the repository at this point in the history
Signed-off-by: Natsuki Ikeguchi <me@s6n.jp>
  • Loading branch information
siketyan authored Feb 11, 2024
1 parent 22da09b commit c794a0e
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions nextdns/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nextdns

import (
"context"
"os"

"github.com/amalucelli/nextdns-go/nextdns"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -13,7 +14,7 @@ func Provider() *schema.Provider {
Schema: map[string]*schema.Schema{
"api_key": {
Type: schema.TypeString,
Required: true,
Optional: true,
Description: "NextDNS API Key",
},
},
Expand All @@ -37,8 +38,19 @@ func Provider() *schema.Provider {

// nolint:revive
func configure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
client, err := nextdns.New(
nextdns.WithAPIKey(d.Get("api_key").(string)))
apiKey := os.Getenv("NEXTDNS_API_KEY")

if key, ok := d.Get("api_key").(string); ok && len(key) > 0 {
apiKey = key
}

if len(apiKey) == 0 {
return nil, diag.Errorf(
"NextDNS API key must be provided in the provider block or NEXTDNS_API_KEY environment variable.",
)
}

client, err := nextdns.New(nextdns.WithAPIKey(apiKey))
if err != nil {
return nil, diag.FromErr(err)
}
Expand Down

0 comments on commit c794a0e

Please sign in to comment.