Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Merge pull request #9 from tsouza/thiago/fleet_resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiago Souza authored Apr 30, 2021
2 parents 45ea6a9 + ed6a41b commit 4bdf0fe
Show file tree
Hide file tree
Showing 31 changed files with 15,525 additions and 124 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/aws/aws-sdk-go v1.38.28 // indirect
github.com/elastic/go-elasticsearch/v7 v7.12.0
github.com/fatih/color v1.10.0 // indirect
github.com/go-resty/resty/v2 v2.6.0
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-hclog v0.16.0 // indirect
Expand Down
110 changes: 6 additions & 104 deletions go.sum

Large diffs are not rendered by default.

45 changes: 39 additions & 6 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

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

"github.com/go-resty/resty/v2"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -35,6 +37,14 @@ func newSchema() map[string]*schema.Schema {
"ELASTICSEARCH_URL", "",
),
},
"kibana_url": {
Description: "Kibana URL to use for API Authentication.",
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc(
"KIBANA_URL", "",
),
},
"username": {
Description: "Username to use for API authentication.",
Type: schema.TypeString,
Expand All @@ -60,9 +70,10 @@ func New(version string) func() *schema.Provider {
p := &schema.Provider{
Schema: newSchema(),
ResourcesMap: map[string]*schema.Resource{
"elasticstack_auth_user": resourceElasticstackAuthUser(),
"elasticstack_auth_role": resourceElasticstackAuthRole(),
"elasticstack_auth_role_mapping": resourceElasticstackAuthRoleMapping(),
"elasticstack_auth_user": resourceElasticstackAuthUser(),
"elasticstack_auth_role": resourceElasticstackAuthRole(),
"elasticstack_auth_role_mapping": resourceElasticstackAuthRoleMapping(),
"elasticstack_fleet_agent_policy": resourceElasticstackFleetAgentPolicy(),
},
}

Expand All @@ -72,14 +83,22 @@ func New(version string) func() *schema.Provider {
}
}

type apiClient struct {
es *elasticsearch.Client
k *resty.Client
}

func configure(version string, p *schema.Provider) func(context.Context, *schema.ResourceData) (interface{}, diag.Diagnostics) {
return func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
var diags diag.Diagnostics

username := d.Get("username").(string)
password := d.Get("password").(string)

es, err := elasticsearch.NewClient(elasticsearch.Config{
Addresses: []string{d.Get("elasticsearch_url").(string)},
Username: d.Get("username").(string),
Password: d.Get("password").(string),
Username: username,
Password: password,
})
if err != nil {
diags = append(diags, diag.Diagnostic{
Expand All @@ -89,6 +108,20 @@ func configure(version string, p *schema.Provider) func(context.Context, *schema
})
}

return es, diags
k := resty.New().
SetHeader("kbn-xsrf", "true").
SetBasicAuth(username, password).
SetHostURL(d.Get("kibana_url").(string))

_, err = k.R().Get("/")
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to create Kibana client",
Detail: err.Error(),
})
}

return apiClient{es, k}, diags
}
}
3 changes: 3 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func TestProvider(t *testing.T) {
}

func testAccPreCheck(t *testing.T) {
if err := os.Getenv("KIBANA_URL"); err == "" {
t.Fatal("KIBANA_URL must be set for acceptance tests")
}
if err := os.Getenv("ELASTICSEARCH_URL"); err == "" {
t.Fatal("ELASTICSEARCH_URL must be set for acceptance tests")
}
Expand Down
7 changes: 3 additions & 4 deletions internal/provider/resource_elasticstack_auth_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"regexp"

"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -183,7 +182,7 @@ func parseRoleData(d *schema.ResourceData) (esapiRoleData, error) {
}

func resourceElasticstackAuthRoleCreate(d *schema.ResourceData, meta interface{}) error {
es := meta.(*elasticsearch.Client)
es := meta.(apiClient).es

roleData, err := parseRoleData(d)
if err != nil {
Expand Down Expand Up @@ -214,7 +213,7 @@ func resourceElasticstackAuthRoleCreate(d *schema.ResourceData, meta interface{}
}

func resourceElasticstackAuthRoleRead(d *schema.ResourceData, meta interface{}) error {
es := meta.(*elasticsearch.Client)
es := meta.(apiClient).es

name := d.Get("name").(string)

Expand Down Expand Up @@ -291,7 +290,7 @@ func resourceElasticstackAuthRoleUpdate(d *schema.ResourceData, meta interface{}
}

func resourceElasticstackAuthRoleDelete(d *schema.ResourceData, meta interface{}) error {
es := meta.(*elasticsearch.Client)
es := meta.(apiClient).es

name := d.Get("name").(string)
req := esapi.SecurityDeleteRoleRequest{
Expand Down
7 changes: 3 additions & 4 deletions internal/provider/resource_elasticstack_auth_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"

"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -89,7 +88,7 @@ func parseUserResourceData(d *schema.ResourceData) (esapiUserData, error) {
}

func resourceElasticstackAuthUserCreate(d *schema.ResourceData, meta interface{}) error {
es := meta.(*elasticsearch.Client)
es := meta.(apiClient).es

userData, err := parseUserResourceData(d)
if err != nil {
Expand Down Expand Up @@ -120,7 +119,7 @@ func resourceElasticstackAuthUserCreate(d *schema.ResourceData, meta interface{}
}

func resourceElasticstackAuthUserRead(d *schema.ResourceData, meta interface{}) error {
es := meta.(*elasticsearch.Client)
es := meta.(apiClient).es

username := d.Get("username").(string)

Expand Down Expand Up @@ -164,7 +163,7 @@ func resourceElasticstackAuthUserUpdate(d *schema.ResourceData, meta interface{}
}

func resourceElasticstackAuthUserDelete(d *schema.ResourceData, meta interface{}) error {
es := meta.(*elasticsearch.Client)
es := meta.(*apiClient).es

username := d.Get("username").(string)
req := esapi.SecurityDeleteUserRequest{
Expand Down
3 changes: 1 addition & 2 deletions internal/provider/resource_elasticstack_auth_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"testing"

"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
Expand All @@ -28,7 +27,7 @@ func TestAccElasticstackAuthUserBasic(t *testing.T) {
}

func testAccCheckElasticstackAuthUserDestroy(s *terraform.State) error {
es := testAccProvider.Meta().(*elasticsearch.Client)
es := testAccProvider.Meta().(*apiClient).es

for _, rs := range s.RootModule().Resources {
if rs.Type != "elasticstack_auth_user" {
Expand Down
Loading

0 comments on commit 4bdf0fe

Please sign in to comment.