Skip to content

Commit

Permalink
feat: change provider attribute api_key to api_token, to keep compati…
Browse files Browse the repository at this point in the history
…bility
  • Loading branch information
caliban0 committed Aug 20, 2024
1 parent 40250e2 commit a0e5ad5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
10 changes: 5 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ terraform {
# Set the variable value in variables.tf file.
# Or set the CHERRY_AUTH_KEY or CHERRY_AUTH_TOKEN environment variables.
variable "cherry_api_key" {
description = "Cherry servers API key"
variable "cherry_api_token" {
description = "Cherry servers API token"
type = string
default = "my_api_key_goes_here"
default = "my_api_token_goes_here"
}
# Configure the Cherry Servers Provider.
provider "cherryservers" {
api_key = var.cherry_api_key // API key can be found in Cherry Servers client portal - https://portal.cherryservers.com/settings/api-keys
api_token = var.cherry_api_token // API token can be found in Cherry Servers client portal - https://portal.cherryservers.com/settings/api-keys
}
```

Expand All @@ -40,4 +40,4 @@ provider "cherryservers" {

### Optional

- `api_key` (String, Sensitive) Cherry Servers [API Key](https://portal.cherryservers.com/settings/api-keys) that allows interactions with the API.
- `api_token` (String, Sensitive) Cherry Servers [API Key](https://portal.cherryservers.com/settings/api-keys) that allows interactions with the API.
8 changes: 4 additions & 4 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ terraform {

# Set the variable value in variables.tf file.
# Or set the CHERRY_AUTH_KEY or CHERRY_AUTH_TOKEN environment variables.
variable "cherry_api_key" {
description = "Cherry servers API key"
variable "cherry_api_token" {
description = "Cherry servers API token"
type = string
default = "my_api_key_goes_here"
default = "my_api_token_goes_here"
}

# Configure the Cherry Servers Provider.
provider "cherryservers" {
api_key = var.cherry_api_key // API key can be found in Cherry Servers client portal - https://portal.cherryservers.com/settings/api-keys
api_token = var.cherry_api_token // API token can be found in Cherry Servers client portal - https://portal.cherryservers.com/settings/api-keys
}
38 changes: 19 additions & 19 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type CherryServersProvider struct {

// CherryServersProviderModel describes the provider data model.
type CherryServersProviderModel struct {
APIKey types.String `tfsdk:"api_key"`
APIToken types.String `tfsdk:"api_token"`
}

func (p *CherryServersProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
Expand All @@ -41,7 +41,7 @@ func (p *CherryServersProvider) Metadata(ctx context.Context, req provider.Metad
func (p *CherryServersProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"api_key": schema.StringAttribute{
"api_token": schema.StringAttribute{
Description: "Cherry Servers [API Key](https://portal.cherryservers.com/settings/api-keys) that allows interactions with the API.",
Optional: true,
Sensitive: true,
Expand All @@ -63,11 +63,11 @@ func (p *CherryServersProvider) Configure(ctx context.Context, req provider.Conf

// Configuration values are now available.
// if data.Endpoint.IsNull() { /* ... */ }
if data.APIKey.IsUnknown() {
if data.APIToken.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("api_key"),
"Unknown CherryServers API Key",
"The provider cannot create the CherryServers API client as there is an unknown configuration value for the CherryServers API Key. "+
path.Root("api_token"),
"Unknown CherryServers API Token",
"The provider cannot create the CherryServers API client as there is an unknown configuration value for the CherryServers API Token. "+
"Either target apply the source of the value first, set the value statically in the configuration,"+
" or use the CHERRY_AUTH_TOKEN or CHERRY_AUTH_KEY environment variables.",
)
Expand All @@ -77,21 +77,21 @@ func (p *CherryServersProvider) Configure(ctx context.Context, req provider.Conf
return
}

apiKey := os.Getenv("CHERRY_AUTH_KEY")
if apiKey == "" {
apiKey = os.Getenv("CHERRY_AUTH_TOKEN")
apiToken := os.Getenv("CHERRY_AUTH_KEY")
if apiToken == "" {
apiToken = os.Getenv("CHERRY_AUTH_TOKEN")
}

if !data.APIKey.IsNull() {
apiKey = data.APIKey.ValueString()
if !data.APIToken.IsNull() {
apiToken = data.APIToken.ValueString()
}

if apiKey == "" {
if apiToken == "" {
resp.Diagnostics.AddAttributeError(
path.Root("api_key"),
"Missing CherryServers API Key",
"The provider cannot create the CherryServers API client as there is a missing or empty value for the CherryServers API key. "+
"Set the API key value in the configuration or use the CHERRY_AUTH_TOKEN or CHERRY_AUTH_KEY environment variables. "+
path.Root("api_token"),
"Missing CherryServers API Token",
"The provider cannot create the CherryServers API client as there is a missing or empty value for the CherryServers API token. "+
"Set the API token value in the configuration or use the CHERRY_AUTH_TOKEN or CHERRY_AUTH_KEY environment variables. "+
"If either is already set, ensure the value is not empty.",
)
}
Expand All @@ -100,14 +100,14 @@ func (p *CherryServersProvider) Configure(ctx context.Context, req provider.Conf
return
}

ctx = tflog.SetField(ctx, "cherryservers_api_key", apiKey)
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "cherryservers_api_key")
ctx = tflog.SetField(ctx, "cherryservers_api_token", apiToken)
ctx = tflog.MaskFieldValuesWithFieldKeys(ctx, "cherryservers_api_token")

tflog.Debug(ctx, "Creating CherryServers client")

// Example client configuration for data sources and resources
userAgent := fmt.Sprintf("terraform-provider/cherryservers/%s terraform/%s", p.version, req.TerraformVersion)
args := []cherrygo.ClientOpt{cherrygo.WithAuthToken(apiKey), cherrygo.WithUserAgent(userAgent)}
args := []cherrygo.ClientOpt{cherrygo.WithAuthToken(apiToken), cherrygo.WithUserAgent(userAgent)}
client, err := cherrygo.NewClient(args...)
if err != nil {
resp.Diagnostics.AddError(
Expand Down

0 comments on commit a0e5ad5

Please sign in to comment.