Skip to content

Commit

Permalink
Begin DRYing up ES connection resource
Browse files Browse the repository at this point in the history
  • Loading branch information
webfella committed Nov 23, 2022
1 parent 53df57e commit d01159e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
38 changes: 27 additions & 11 deletions internal/schema/connection.go
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
}
5 changes: 3 additions & 2 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down
6 changes: 4 additions & 2 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit d01159e

Please sign in to comment.