Skip to content

Commit c794a0e

Browse files
authored
feat: Support providing API key from an environment variable (#20)
Signed-off-by: Natsuki Ikeguchi <me@s6n.jp>
1 parent 22da09b commit c794a0e

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

nextdns/provider.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package nextdns
22

33
import (
44
"context"
5+
"os"
56

67
"github.com/amalucelli/nextdns-go/nextdns"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -13,7 +14,7 @@ func Provider() *schema.Provider {
1314
Schema: map[string]*schema.Schema{
1415
"api_key": {
1516
Type: schema.TypeString,
16-
Required: true,
17+
Optional: true,
1718
Description: "NextDNS API Key",
1819
},
1920
},
@@ -37,8 +38,19 @@ func Provider() *schema.Provider {
3738

3839
// nolint:revive
3940
func configure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
40-
client, err := nextdns.New(
41-
nextdns.WithAPIKey(d.Get("api_key").(string)))
41+
apiKey := os.Getenv("NEXTDNS_API_KEY")
42+
43+
if key, ok := d.Get("api_key").(string); ok && len(key) > 0 {
44+
apiKey = key
45+
}
46+
47+
if len(apiKey) == 0 {
48+
return nil, diag.Errorf(
49+
"NextDNS API key must be provided in the provider block or NEXTDNS_API_KEY environment variable.",
50+
)
51+
}
52+
53+
client, err := nextdns.New(nextdns.WithAPIKey(apiKey))
4254
if err != nil {
4355
return nil, diag.FromErr(err)
4456
}

0 commit comments

Comments
 (0)