Skip to content

Commit 057e382

Browse files
committed
Kibana spaces data source
1 parent a91668e commit 057e382

File tree

11 files changed

+270
-151
lines changed

11 files changed

+270
-151
lines changed

docs/data-sources/kibana_spaces.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
subcategory: "Kibana"
3+
layout: ""
4+
page_title: "Elasticstack: elasticstack_kibana_spaces Data Source"
5+
description: |-
6+
Retrieve all Kibana spaces. See https://www.elastic.co/guide/en/kibana/master/spaces-api-get-all.html
7+
---
8+
9+
# Data Source: elasticstack_kibana_spaces
10+
11+
Use this data source to retrieve and get information about all existing Kibana spaces.
12+
13+
## Example Usage
14+
15+
```terraform
16+
provider "elasticstack" {
17+
elasticsearch {}
18+
kibana {}
19+
}
20+
21+
data "elasticstack_kibana_spaces" "all_spaces" {
22+
23+
}
24+
```
25+
26+
<!-- schema generated by tfplugindocs -->
27+
## Schema
28+
29+
### Read-Only
30+
31+
- `id` (String) Generated ID for the spaces.
32+
- `spaces` (Attributes List) The list of spaces. (see [below for nested schema](#nestedatt--spaces))
33+
34+
<a id="nestedatt--spaces"></a>
35+
### Nested Schema for `spaces`
36+
37+
Required:
38+
39+
- `name` (String) The display name for the space.
40+
41+
Optional:
42+
43+
- `description` (String) The description for the space.
44+
- `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).
45+
- `image_url` (String) The data-URL encoded image to display in the space avatar.
46+
47+
Read-Only:
48+
49+
- `color` (String) The hexadecimal color code used in the space avatar. By default, the color is automatically generated from the space name.
50+
- `id` (String) Internal identifier of the resource.
51+
- `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.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
provider "elasticstack" {
3+
elasticsearch {}
4+
kibana {}
5+
}
6+
7+
data "elasticstack_kibana_spaces" "all_spaces" {
8+
9+
}

internal/kibana/spaces/data_source.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package spaces
2+
3+
import (
4+
"context"
5+
6+
"github.com/disaster37/go-kibana-rest/v8/kbapi"
7+
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
8+
"github.com/hashicorp/terraform-plugin-framework/datasource"
9+
)
10+
11+
// Ensure the implementation satisfies the expected interfaces.
12+
var (
13+
_ datasource.DataSource = &dataSource{}
14+
_ datasource.DataSource = &dataSource{}
15+
_ datasource.DataSourceWithConfigure = &dataSource{}
16+
)
17+
18+
// NewDataSource is a helper function to simplify the provider implementation.
19+
func NewDataSource() datasource.DataSource {
20+
return &dataSource{}
21+
}
22+
23+
// dataSource is the data source implementation.
24+
type dataSource struct {
25+
client *kbapi.KibanaSpacesAPI
26+
}
27+
28+
// Metadata returns the data source type name.
29+
func (d *dataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
30+
resp.TypeName = req.ProviderTypeName + "_kibana_spaces"
31+
}
32+
33+
// Configure adds the provider configured client to the data source.
34+
func (d *dataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
35+
// Add a nil check when handling ProviderData because Terraform
36+
// sets that data after it calls the ConfigureProvider RPC.
37+
if req.ProviderData == nil {
38+
return
39+
}
40+
41+
client, diags := clients.ConvertProviderData(req.ProviderData)
42+
resp.Diagnostics.Append(diags...)
43+
44+
kibanaClient, err := client.GetKibanaClient()
45+
if err != nil {
46+
resp.Diagnostics.AddError("unable to get spaces client", err.Error())
47+
return
48+
}
49+
50+
d.client = kibanaClient.KibanaSpaces
51+
}
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package kibana_test
1+
package spaces_test
22

33
import (
44
"testing"
@@ -7,37 +7,30 @@ import (
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
88
)
99

10-
func TestAccDataSourceKibanaSpaces(t *testing.T) {
10+
func TestAccSpacesDataSource(t *testing.T) {
1111
resource.Test(t, resource.TestCase{
1212
PreCheck: func() { acctest.PreCheck(t) },
1313
ProtoV6ProviderFactories: acctest.Providers,
1414
Steps: []resource.TestStep{
1515
{
16-
Config: testAccDataSourceSpaces,
17-
Check: resource.ComposeTestCheckFunc(
18-
resource.TestCheckResourceAttr("data.elasticstack_kibana_spaces.myspaces", "spaces[0].name", "my_space"),
19-
resource.TestCheckResourceAttr("data.elasticstack_kibana_spaces.myspaces", "spaces[0].space_id", "my_space_id"),
20-
resource.TestCheckResourceAttr("data.elasticstack_kibana_spaces.myspaces", "spaces[0].description", "My Space"),
16+
Config: testAccSpacesDataSourceConfig,
17+
Check: resource.ComposeAggregateTestCheckFunc(
18+
resource.TestCheckResourceAttr("data.elasticstack_kibana_spaces.all_spaces", "spaces.0.id", "default"),
19+
resource.TestCheckResourceAttr("data.elasticstack_kibana_spaces.all_spaces", "spaces.0.name", "Default"),
20+
resource.TestCheckResourceAttr("data.elasticstack_kibana_spaces.all_spaces", "spaces.0.description", "This is your default space!"),
2121
),
2222
},
2323
},
2424
})
2525
}
2626

27-
const testAccDataSourceSpaces = `
27+
const testAccSpacesDataSourceConfig = `
2828
provider "elasticstack" {
2929
elasticsearch {}
3030
kibana {}
3131
}
3232
33-
resource "elasticstack_kibana_space" "myspace" {
34-
name = "my_space"
35-
space_id = "my_space_id"
36-
description = "My Space"
37-
disabled_features = ["dev_tools"]
38-
}
33+
data "elasticstack_kibana_spaces" "all_spaces" {
3934
40-
data "elasticstack_kibana_spaces" "myspaces" {
41-
search = elasticstack_kibana_space.myspace.name
4235
}
4336
`

internal/kibana/spaces/models.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package spaces
2+
3+
import "github.com/hashicorp/terraform-plugin-framework/types"
4+
5+
// spacesDataSourceModel maps the data source schema data.
6+
type dataSourceModel struct {
7+
ID types.String `tfsdk:"id"`
8+
Spaces []model `tfsdk:"spaces"`
9+
}
10+
11+
// spacesModel maps coffees schema data.
12+
type model struct {
13+
ID types.String `tfsdk:"id"`
14+
Name types.String `tfsdk:"name"`
15+
Description types.String `tfsdk:"description"`
16+
DisabledFeatures types.List `tfsdk:"disabled_features"`
17+
Initials types.String `tfsdk:"initials"`
18+
Color types.String `tfsdk:"color"`
19+
ImageUrl types.String `tfsdk:"image_url"`
20+
}

internal/kibana/spaces/read.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package spaces
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/datasource"
7+
"github.com/hashicorp/terraform-plugin-framework/types"
8+
)
9+
10+
// Read refreshes the Terraform state with the latest data.
11+
func (d *dataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
12+
var state dataSourceModel
13+
14+
// Call client API
15+
spaces, err := d.client.List()
16+
if err != nil {
17+
resp.Diagnostics.AddError("unable to list spaces", err.Error())
18+
return
19+
}
20+
21+
// Map response body to model
22+
for _, space := range spaces {
23+
spaceState := model{
24+
ID: types.StringValue(space.ID),
25+
Name: types.StringValue(space.Name),
26+
Description: types.StringValue(space.Description),
27+
Initials: types.StringValue(space.Initials),
28+
Color: types.StringValue(space.Color),
29+
ImageUrl: types.StringValue(space.ImageURL),
30+
}
31+
32+
if disabledFeatures, diags := types.ListValueFrom(ctx, types.StringType, space.DisabledFeatures); diags.HasError() {
33+
resp.Diagnostics.Append(diags...)
34+
return
35+
} else {
36+
spaceState.DisabledFeatures = disabledFeatures
37+
}
38+
39+
state.Spaces = append(state.Spaces, spaceState)
40+
}
41+
42+
state.ID = types.StringValue("spaces")
43+
44+
// Set state
45+
diags := resp.State.Set(ctx, &state)
46+
resp.Diagnostics.Append(diags...)
47+
if resp.Diagnostics.HasError() {
48+
return
49+
}
50+
}

internal/kibana/spaces/schema.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package spaces
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/datasource"
7+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
8+
"github.com/hashicorp/terraform-plugin-framework/types"
9+
)
10+
11+
// Schema defines the schema for the data source.
12+
func (d *dataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
13+
resp.Schema = schema.Schema{
14+
Description: "Manages Kibana spaces",
15+
Attributes: map[string]schema.Attribute{
16+
"id": schema.StringAttribute{
17+
Description: "Generated ID for the spaces.",
18+
Computed: true,
19+
},
20+
"spaces": schema.ListNestedAttribute{
21+
Description: "The list of spaces.",
22+
Computed: true,
23+
NestedObject: schema.NestedAttributeObject{
24+
Attributes: map[string]schema.Attribute{
25+
"id": schema.StringAttribute{
26+
Description: "Internal identifier of the resource.",
27+
Computed: true,
28+
},
29+
"name": schema.StringAttribute{
30+
Description: "The display name for the space.",
31+
Required: true,
32+
},
33+
"description": schema.StringAttribute{
34+
Description: "The description for the space.",
35+
Optional: true,
36+
},
37+
"disabled_features": schema.ListAttribute{
38+
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).",
39+
ElementType: types.StringType,
40+
Optional: true,
41+
},
42+
"initials": schema.StringAttribute{
43+
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.",
44+
Computed: true,
45+
},
46+
"color": schema.StringAttribute{
47+
Description: "The hexadecimal color code used in the space avatar. By default, the color is automatically generated from the space name.",
48+
Computed: true,
49+
},
50+
"image_url": schema.StringAttribute{
51+
Description: "The data-URL encoded image to display in the space avatar.",
52+
Optional: true,
53+
},
54+
},
55+
},
56+
},
57+
},
58+
}
59+
}

0 commit comments

Comments
 (0)