Skip to content

Commit

Permalink
new scheme to set api_key via environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
dgulinobw committed Nov 22, 2022
1 parent 80bde1c commit eb1dca5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
41 changes: 34 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,63 @@ go install

## Using the provider

An example provider configuration:
An example provider configuration (set api key value in ENVIRONMENT variable "TF_VAR_dog_api_key_qa"):
main.tf
```
terraform {
required_providers {
dog = {
source = "relaypro-open/dog"
version = "1.0.9"
version = "1.0.10"
}
}
module "dog" {
source = "./dog"
source = "./dog"
api_key = var.dog_api_key_qa
api_endpoint = var.dog_api_endpoint
}
```

dog/dog.tf
variables.tf
```
variable "dog_api_key_qa" {
type = string
sensitive = true
}
variable "dog_api_endpoint" {
default = "https://qa-dog.DOMAIN.SOMETHING:8443/api/V2"
}
```

dog/variables.tf
```
variable "api_key" {
type = string
sensitive = true
}
variable "api_endpoint" {
type = string
sensitive = true
}
```

dog/main.tf
```
terraform {
required_providers {
dog = {
source = "relaypro-open/dog"
version = ">=1.0.9"
version = ">=1.0.10"
}
}
}
provider "dog" {
api_endpoint = "https://qa-dog.DOMAIN.SOMETHING:8443/api/V2"
api_key_variable_name = "DOG_QA_API_KEY"
api_endpoint = var.api_endpoint
api_key = var.api_key
alias = "qa"
}
```
Expand Down
25 changes: 5 additions & 20 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type (
dogProviderModel struct {
API_Key types.String `tfsdk:"api_key"`
API_Endpoint types.String `tfsdk:"api_endpoint"`
API_Key_Variable_Name types.String `tfsdk:"api_key_variable_name"`
}
)

Expand Down Expand Up @@ -62,12 +61,6 @@ func (*dogProvider) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnosti
Type: types.StringType,
Sensitive: true,
},
"api_key_variable_name": {
MarkdownDescription: "Name of ENVIRONMENT variable that contains api_key",
Optional: true,
Type: types.StringType,
Sensitive: true,
},
},
}, nil
}
Expand All @@ -86,18 +79,17 @@ func (p *dogProvider) Configure(ctx context.Context, req provider.ConfigureReque

if config.API_Endpoint.Unknown {
resp.Diagnostics.AddError(
"Unknown dog API Endpoint",
"Unknown Dog API Endpoint",
"The provider cannot create the Dog API client as there is an unknown configuration value for the Dog API endpoint. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the DOG_API_Endpoint environment variable.",
)
}

if config.API_Key.Unknown && config.API_Key_Variable_Name.Unknown {
if config.API_Key.Unknown {
resp.Diagnostics.AddError(
"Unknown Dog API Key and API Key Variable Name",
"Unknown Dog API Key",
"The provider cannot create the Dog API client as there is an unknown configuration value for the Dog API key. "+
"Either target apply the source of the value first, set the value statically in the configuration, or use the DOG_API_KEY environment variable. "+
"Or set 'api_key_variable_name terraform variable and set the API Key as the value of that ENV variable",
"Either target apply the source of the value first, set the value statically in the configuration, or use the DOG_API_KEY environment variable. ",
)
}

Expand All @@ -110,10 +102,6 @@ func (p *dogProvider) Configure(ctx context.Context, req provider.ConfigureReque

api_endpoint := os.Getenv("DOG_API_ENDPOINT")
api_key := os.Getenv("DOG_API_KEY")
dog_qa_api_key := os.Getenv("DOG_QA_API_KEY")
if os.Getenv(config.API_Key_Variable_Name.Value) != "" {
api_key = os.Getenv(config.API_Key_Variable_Name.Value)
}

if !config.API_Endpoint.IsNull() {
api_endpoint = config.API_Endpoint.ValueString()
Expand All @@ -128,10 +116,8 @@ func (p *dogProvider) Configure(ctx context.Context, req provider.ConfigureReque
"config values",
fmt.Sprintf("config.API_Key: %+v\n", config.API_Key.Value)+
fmt.Sprintf("config.API_Endpoint: %+v\n", config.API_Endpoint.Value)+
fmt.Sprintf("config.API_Key_Variable_Name: %+v\n", config.API_Key_Variable_Name)+
fmt.Sprintf("api_endpoint: %+v\n", api_endpoint)+
fmt.Sprintf("api_key: %+v\n", api_key)+
fmt.Sprintf("dog_qa_api_key: %+v\n", dog_qa_api_key),
fmt.Sprintf("api_key: %+v\n", api_key),
)
}

Expand All @@ -153,7 +139,6 @@ func (p *dogProvider) Configure(ctx context.Context, req provider.ConfigureReque
"Missing Dog API Key",
"The provider cannot create the Dog API client as there is a missing or empty value for the Dog API key. "+
"Set the API Key value in the configuration or use the DOG_API_KEY environment variable. "+
"Or set 'api_key_variable_name terraform variable and set the API Key as the value of that ENV variable"+
"If either is already set, ensure the value is not empty.",
)
}
Expand Down

0 comments on commit eb1dca5

Please sign in to comment.