Skip to content

Kibana spaces data source #682

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

Merged
merged 12 commits into from
Aug 19, 2024
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [Unreleased]

- Add `elasticstack_kibana_spaces` data source ([#682](https://github.com/elastic/terraform-provider-elasticstack/pull/682))

## [0.11.5] - 2024-08-12

- Fix setting `id` for Fleet outputs and servers ([#666](https://github.com/elastic/terraform-provider-elasticstack/pull/666))
Expand Down
51 changes: 51 additions & 0 deletions docs/data-sources/kibana_spaces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
subcategory: "Kibana"
layout: ""
page_title: "Elasticstack: elasticstack_kibana_spaces Data Source"
description: |-
Retrieve all Kibana spaces. See https://www.elastic.co/guide/en/kibana/master/spaces-api-get-all.html
---

# Data Source: elasticstack_kibana_spaces

Use this data source to retrieve and get information about all existing Kibana spaces.

## Example Usage

```terraform
provider "elasticstack" {
elasticsearch {}
kibana {}
}

data "elasticstack_kibana_spaces" "all_spaces" {

}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Read-Only

- `id` (String) Generated ID for the spaces.
- `spaces` (Attributes List) The list of spaces. (see [below for nested schema](#nestedatt--spaces))

<a id="nestedatt--spaces"></a>
### Nested Schema for `spaces`

Required:

- `name` (String) The display name for the space.

Optional:

- `description` (String) The description for the space.
- `disabled_features` (List of String) The list of disabled features for the space. To get a list of available feature IDs, use the Features API (https://www.elastic.co/guide/en/kibana/master/features-api-get.html).
- `image_url` (String) The data-URL encoded image to display in the space avatar.

Read-Only:

- `color` (String) The hexadecimal color code used in the space avatar. By default, the color is automatically generated from the space name.
- `id` (String) Internal identifier of the resource.
- `initials` (String) The initials shown in the space avatar. By default, the initials are automatically generated from the space name. Initials must be 1 or 2 characters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

provider "elasticstack" {
elasticsearch {}
kibana {}
}

data "elasticstack_kibana_spaces" "all_spaces" {

}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-mux v0.16.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
github.com/hashicorp/terraform-plugin-testing v1.9.0
github.com/mitchellh/mapstructure v1.5.0
github.com/oapi-codegen/runtime v1.1.1
github.com/stretchr/testify v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ github.com/hashicorp/terraform-plugin-mux v0.16.0 h1:RCzXHGDYwUwwqfYYWJKBFaS3fQs
github.com/hashicorp/terraform-plugin-mux v0.16.0/go.mod h1:PF79mAsPc8CpusXPfEVa4X8PtkB+ngWoiUClMrNZlYo=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 h1:kJiWGx2kiQVo97Y5IOGR4EMcZ8DtMswHhUuFibsCQQE=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0/go.mod h1:sl/UoabMc37HA6ICVMmGO+/0wofkVIRxf+BMb/dnoIg=
github.com/hashicorp/terraform-plugin-testing v1.9.0 h1:xOsQRqqlHKXpFq6etTxih3ubdK3HVDtfE1IY7Rpd37o=
github.com/hashicorp/terraform-plugin-testing v1.9.0/go.mod h1:fhhVx/8+XNJZTD5o3b4stfZ6+q7z9+lIWigIYdT6/44=
github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI=
github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM=
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=
Expand Down
53 changes: 53 additions & 0 deletions internal/kibana/spaces/data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package spaces

import (
"context"

"github.com/disaster37/go-kibana-rest/v8/kbapi"
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/hashicorp/terraform-plugin-framework/datasource"
)

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &dataSource{}
_ datasource.DataSourceWithConfigure = &dataSource{}
)

// NewDataSource is a helper function to simplify the provider implementation.
func NewDataSource() datasource.DataSource {
return &dataSource{}
}

// dataSource is the data source implementation.
type dataSource struct {
client *kbapi.KibanaSpacesAPI
}

// Metadata returns the data source type name.
func (d *dataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_kibana_spaces"
}

// Configure adds the provider configured client to the data source.
func (d *dataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Add a nil check when handling ProviderData because Terraform
// sets that data after it calls the ConfigureProvider RPC.
if req.ProviderData == nil {
return
}

client, diags := clients.ConvertProviderData(req.ProviderData)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

kibanaClient, err := client.GetKibanaClient()
if err != nil {
resp.Diagnostics.AddError("unable to get Kibana client", err.Error())
return
}

d.client = kibanaClient.KibanaSpaces
}
36 changes: 36 additions & 0 deletions internal/kibana/spaces/data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package spaces_test

import (
"testing"

"github.com/elastic/terraform-provider-elasticstack/internal/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccSpacesDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: acctest.Providers,
Steps: []resource.TestStep{
{
Config: testAccSpacesDataSourceConfig,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.elasticstack_kibana_spaces.all_spaces", "spaces.0.id", "default"),
resource.TestCheckResourceAttr("data.elasticstack_kibana_spaces.all_spaces", "spaces.0.name", "Default"),
resource.TestCheckResourceAttr("data.elasticstack_kibana_spaces.all_spaces", "spaces.0.description", "This is your default space!"),
),
},
},
})
}

const testAccSpacesDataSourceConfig = `
provider "elasticstack" {
elasticsearch {}
kibana {}
}

data "elasticstack_kibana_spaces" "all_spaces" {

}
`
20 changes: 20 additions & 0 deletions internal/kibana/spaces/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package spaces

import "github.com/hashicorp/terraform-plugin-framework/types"

// dataSourceModel maps the data source schema data.
type dataSourceModel struct {
ID types.String `tfsdk:"id"`
Spaces []model `tfsdk:"spaces"`
}

// model maps spaces schema data.
type model struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
DisabledFeatures types.List `tfsdk:"disabled_features"`
Initials types.String `tfsdk:"initials"`
Color types.String `tfsdk:"color"`
ImageUrl types.String `tfsdk:"image_url"`
}
51 changes: 51 additions & 0 deletions internal/kibana/spaces/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package spaces

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Read refreshes the Terraform state with the latest data.
func (d *dataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state dataSourceModel

// Call client API
spaces, err := d.client.List()
if err != nil {
resp.Diagnostics.AddError("unable to list spaces", err.Error())
return
}

// Map response body to model
for _, space := range spaces {
spaceState := model{
ID: types.StringValue(space.ID),
Name: types.StringValue(space.Name),
Description: types.StringValue(space.Description),
Initials: types.StringValue(space.Initials),
Color: types.StringValue(space.Color),
ImageUrl: types.StringValue(space.ImageURL),
}

disabledFeatures, diags := types.ListValueFrom(ctx, types.StringType, space.DisabledFeatures)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
}

spaceState.DisabledFeatures = disabledFeatures

state.Spaces = append(state.Spaces, spaceState)
}

state.ID = types.StringValue("spaces")

// Set state
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
59 changes: 59 additions & 0 deletions internal/kibana/spaces/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package spaces

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

// Schema defines the schema for the data source.
func (d *dataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages Kibana spaces",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "Generated ID for the spaces.",
Computed: true,
},
"spaces": schema.ListNestedAttribute{
Description: "The list of spaces.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "Internal identifier of the resource.",
Computed: true,
},
"name": schema.StringAttribute{
Description: "The display name for the space.",
Required: true,
},
"description": schema.StringAttribute{
Description: "The description for the space.",
Optional: true,
},
"disabled_features": schema.ListAttribute{
Description: "The list of disabled features for the space. To get a list of available feature IDs, use the Features API (https://www.elastic.co/guide/en/kibana/master/features-api-get.html).",
ElementType: types.StringType,
Optional: true,
},
"initials": schema.StringAttribute{
Description: "The initials shown in the space avatar. By default, the initials are automatically generated from the space name. Initials must be 1 or 2 characters.",
Computed: true,
},
"color": schema.StringAttribute{
Description: "The hexadecimal color code used in the space avatar. By default, the color is automatically generated from the space name.",
Computed: true,
},
"image_url": schema.StringAttribute{
Description: "The data-URL encoded image to display in the space avatar.",
Optional: true,
},
},
},
},
},
}
}
5 changes: 4 additions & 1 deletion provider/plugin_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/elastic/terraform-provider-elasticstack/internal/clients/config"
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/data_view"
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/import_saved_objects"
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/spaces"
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics/private_location"
"github.com/elastic/terraform-provider-elasticstack/internal/schema"
"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand Down Expand Up @@ -65,7 +66,9 @@ func (p *Provider) Configure(ctx context.Context, req fwprovider.ConfigureReques
}

func (p *Provider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{}
return []func() datasource.DataSource{
spaces.NewDataSource,
}
}

func (p *Provider) Resources(ctx context.Context) []func() resource.Resource {
Expand Down
17 changes: 17 additions & 0 deletions templates/data-sources/kibana_spaces.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
subcategory: "Kibana"
layout: ""
page_title: "Elasticstack: elasticstack_kibana_spaces Data Source"
description: |-
Retrieve all Kibana spaces. See https://www.elastic.co/guide/en/kibana/master/spaces-api-get-all.html
---

# Data Source: elasticstack_kibana_spaces

Use this data source to retrieve and get information about all existing Kibana spaces.

## Example Usage

{{ tffile "examples/data-sources/elasticstack_kibana_spaces/data-source.tf" }}

{{ .SchemaMarkdown | trimspace }}