Skip to content

Commit 25f8582

Browse files
committed
Support supplying API key via env
1 parent a76c995 commit 25f8582

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

docs/index.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ provider "tlspc" {
3333
<!-- schema generated by tfplugindocs -->
3434
## Schema
3535

36-
### Required
37-
38-
- `apikey` (String) API Key
39-
4036
### Optional
4137

38+
- `apikey` (String) API Key. Required unless specified by setting the environment variable `TLSPC_APIKEY`
4239
- `endpoint` (String) TLSPC API Endpoint

internal/provider/provider.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/hashicorp/terraform-plugin-framework/datasource"
1313
"github.com/hashicorp/terraform-plugin-framework/function"
14+
"github.com/hashicorp/terraform-plugin-framework/path"
1415
"github.com/hashicorp/terraform-plugin-framework/provider"
1516
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
1617
"github.com/hashicorp/terraform-plugin-framework/resource"
@@ -55,9 +56,8 @@ We recommend that you create a custom user with the [permissions required](https
5556
Description: "Provider for the Venafi TLS Protect Cloud Platform",
5657
Attributes: map[string]schema.Attribute{
5758
"apikey": schema.StringAttribute{
58-
MarkdownDescription: "API Key",
59-
Optional: false,
60-
Required: true,
59+
MarkdownDescription: "API Key. Required unless specified by setting the environment variable `TLSPC_APIKEY`",
60+
Optional: true,
6161
},
6262
"endpoint": schema.StringAttribute{
6363
MarkdownDescription: "TLSPC API Endpoint",
@@ -81,9 +81,19 @@ func (p *tlspcProvider) Configure(ctx context.Context, req provider.ConfigureReq
8181
if !config.ApiKey.IsNull() {
8282
apikey = config.ApiKey.ValueString()
8383
}
84+
if apikey == "" {
85+
resp.Diagnostics.AddAttributeError(
86+
path.Root("apikey"),
87+
"API Key not provided",
88+
"The provider cannot create the TLSPC API client as the API Key has not been provided",
89+
)
90+
}
8491
if !config.Endpoint.IsNull() {
8592
endpoint = config.Endpoint.ValueString()
8693
}
94+
if resp.Diagnostics.HasError() {
95+
return
96+
}
8797

8898
client, _ := tlspc.NewClient(apikey, endpoint, p.version)
8999

0 commit comments

Comments
 (0)