Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DNM] add user resource #1

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions elasticstack/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package elasticstack

import (
"context"

"github.com/elastic/go-elasticsearch/v7"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// Provider returns a schema.Provider.
func Provider() *schema.Provider {
return &schema.Provider{
Schema: newSchema(),
ResourcesMap: map[string]*schema.Resource{
"elasticstack_auth_user": resourceElasticstackAuthUser(),
"elasticstack_auth_role": resourceElasticstackAuthRole(),
"elasticstack_auth_role_mapping": resourceElasticstackAuthRoleMapping(),
},
ConfigureContextFunc: providerConfigure,
}
}

func newSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"elasticsearch_url": {
Description: "Elasticsearch URL to use for API Authentication.",
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc(
"ELASTICSEARCH_URL", "",
),
},
"username": {
Description: "Username to use for API authentication.",
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc(
"ELASTICSEARCH_USER", "",
),
},
"password": {
Description: "Password to use for API authentication.",
Type: schema.TypeString,
Required: true,
Sensitive: true,
DefaultFunc: schema.MultiEnvDefaultFunc(
[]string{"ELASTICSEARCH_PASS", "ELASTICSEARCH_PASSWORD"}, "",
),
},
}
}

func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
var diags diag.Diagnostics

es, err := elasticsearch.NewClient(elasticsearch.Config{
Addresses: []string{d.Get("elasticsearch_url").(string)},
Username: d.Get("username").(string),
Password: d.Get("password").(string),
})
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to create Elasticsearch client",
Detail: err.Error(),
})
}

return es, diags
}
40 changes: 40 additions & 0 deletions elasticstack/provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package elasticstack

import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var testAccProviders map[string]*schema.Provider
var testAccProvider *schema.Provider

func init() {
testAccProvider = Provider()
testAccProviders = map[string]*schema.Provider{
"elasticstack": testAccProvider,
}
}

func TestProvider(t *testing.T) {
if err := Provider().InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestProvider_impl(t *testing.T) {
var _ *schema.Provider = Provider()
}

func testAccPreCheck(t *testing.T) {
if err := os.Getenv("ELASTICSEARCH_URL"); err == "" {
t.Fatal("ELASTICSEARCH_URL must be set for acceptance tests")
}
if err := os.Getenv("ELASTICSEARCH_USER"); err == "" {
t.Fatal("ELASTICSEARCH_USER must be set for acceptance tests")
}
if err := os.Getenv("ELASTICSEARCH_PASSWORD"); err == "" {
t.Fatal("ELASTICSEARCH_PASSWORD must be set for acceptance tests")
}
}
Loading