From d01159ef03339801de87ae11b98553390d3bc2fc Mon Sep 17 00:00:00 2001 From: Adrian Bruinhout Date: Wed, 23 Nov 2022 15:22:30 +1100 Subject: [PATCH] Begin DRYing up ES connection resource --- internal/schema/connection.go | 38 +++++++++++++++++++++++++---------- internal/utils/utils.go | 5 +++-- provider/provider.go | 6 ++++-- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/internal/schema/connection.go b/internal/schema/connection.go index b94f46c2f..9d49650b5 100644 --- a/internal/schema/connection.go +++ b/internal/schema/connection.go @@ -1,32 +1,44 @@ package schema -import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func GetConnectionResource(keyName string) *schema.Resource { + usernamePath := makePathRef(keyName, "username") + passwordPath := makePathRef(keyName, "password") + caDataPath := makePathRef(keyName, "ca_data") + caFilePath := makePathRef(keyName, "ca_file") -func GetConnectionResource() *schema.Resource { return &schema.Resource{ Schema: map[string]*schema.Schema{ "username": { - Description: "A username to use for API authentication to Elasticsearch.", + Description: "Username to use for API authentication to Elasticsearch.", Type: schema.TypeString, Optional: true, - RequiredWith: []string{"elasticsearch_connection.0.password"}, + DefaultFunc: schema.EnvDefaultFunc("ELASTICSEARCH_USERNAME", nil), + RequiredWith: []string{passwordPath}, }, "password": { - Description: "A password to use for API authentication to Elasticsearch.", + Description: "Password to use for API authentication to Elasticsearch.", Type: schema.TypeString, Optional: true, Sensitive: true, - RequiredWith: []string{"elasticsearch_connection.0.username"}, + DefaultFunc: schema.EnvDefaultFunc("ELASTICSEARCH_PASSWORD", nil), + RequiredWith: []string{usernamePath}, }, "api_key": { Description: "API Key to use for authentication to Elasticsearch", Type: schema.TypeString, Optional: true, Sensitive: true, - ConflictsWith: []string{"elasticsearch_connection.0.username", "elasticsearch_connection.0.password"}, + DefaultFunc: schema.EnvDefaultFunc("ELASTICSEARCH_API_KEY", nil), + ConflictsWith: []string{usernamePath, passwordPath}, }, "endpoints": { - Description: "A list of endpoints the Terraform provider will point to. They must include the http(s) schema and port number.", + Description: "A comma-separated list of endpoints where the terraform provider will point to, this must include the http(s) schema and port number.", Type: schema.TypeList, Optional: true, Sensitive: true, @@ -38,20 +50,24 @@ func GetConnectionResource() *schema.Resource { Description: "Disable TLS certificate validation", Type: schema.TypeBool, Optional: true, - Default: false, + DefaultFunc: schema.EnvDefaultFunc("ELASTICSEARCH_INSECURE", false), }, "ca_file": { Description: "Path to a custom Certificate Authority certificate", Type: schema.TypeString, Optional: true, - ConflictsWith: []string{"elasticsearch_connection.ca_data"}, + ConflictsWith: []string{caDataPath}, }, "ca_data": { Description: "PEM-encoded custom Certificate Authority certificate", Type: schema.TypeString, Optional: true, - ConflictsWith: []string{"elasticsearch_connection.ca_file"}, + ConflictsWith: []string{caFilePath}, }, }, } } + +func makePathRef(keyName string, keyValue string) string { + return fmt.Sprintf("%s.0.%s", keyName, keyValue) +} diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 9879f6236..cebc71506 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -124,12 +124,13 @@ func IsEmpty(v interface{}) bool { // Returns the common connection schema for all the Elasticsearch resources, // which defines the fields which can be used to configure the API access func AddConnectionSchema(providedSchema map[string]*schema.Schema) { - providedSchema["elasticsearch_connection"] = &schema.Schema{ + keyName := "elasticsearch_connection" + providedSchema[keyName] = &schema.Schema{ Description: "Used to establish connection to Elasticsearch server. Overrides environment variables if present.", Type: schema.TypeList, Optional: true, MaxItems: 1, - Elem: providerSchema.GetConnectionResource(), + Elem: providerSchema.GetConnectionResource(keyName), } } diff --git a/provider/provider.go b/provider/provider.go index 2c810eb0f..a671fb8d9 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -18,16 +18,18 @@ func init() { } func New(version string) func() *schema.Provider { + esKeyName := "elasticsearch" + return func() *schema.Provider { p := &schema.Provider{ Schema: map[string]*schema.Schema{ - "elasticsearch": { + esKeyName: { Description: "Default Elasticsearch connection configuration block.", Type: schema.TypeList, MaxItems: 1, Optional: true, - Elem: providerSchema.GetConnectionResource(), + Elem: providerSchema.GetConnectionResource(esKeyName), }, }, DataSourcesMap: map[string]*schema.Resource{