Skip to content

Commit

Permalink
added resources for saved filter handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mplushnikov committed Jan 15, 2024
1 parent 4d3a072 commit e6ab655
Show file tree
Hide file tree
Showing 15 changed files with 795 additions and 0 deletions.
39 changes: 39 additions & 0 deletions docs/data-sources/saved_filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "spacelift_saved_filter Data Source - terraform-provider-spacelift"
subcategory: ""
description: |-
spacelift_saved_filter represents a Spacelift saved filter.
---

# spacelift_saved_filter (Data Source)

`spacelift_saved_filter` represents a Spacelift saved filter.

## Example Usage

```terraform
data "spacelift_saved_filter" "filter" {
filter_id = spacelift_saved_filter.filter.id
}
output "filter_data" {
value = data.spacelift_saved_filter.filter.data
}
```

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

### Required

- `filter_id` (String) immutable ID (slug) of the filter

### Read-Only

- `created_by` (String) Login of the user who created the saved filter
- `data` (String) Data is the JSON representation of the filter data
- `id` (String) Globally unique ID of the saved filter
- `is_public` (Boolean) Toggle whether the filter is public or not
- `name` (String) Name of the filter
- `type` (String) Type describes the type of the filter. It is used to determine which view the filter is for
57 changes: 57 additions & 0 deletions docs/data-sources/saved_filters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "spacelift_saved_filters Data Source - terraform-provider-spacelift"
subcategory: ""
description: |-
spacelift_saved_filters can find all saved filters that have certain type or name
---

# spacelift_saved_filters (Data Source)

`spacelift_saved_filters` can find all saved filters that have certain type or name

## Example Usage

```terraform
# For all saved filter
data "spacelift_saved_filters" "all" {}
# Filters with a matching type
data "spacelift_saved_filters" "stack_filters" {
type = "stacks"
}
# Filters with a matching name
data "spacelift_saved_filters" "my_filters" {
name = "My best filter"
}
output "filter_ids" {
value = data.spacelift_saved_filters.stack_filters.filters[*].id
}
```

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

### Optional

- `filter_name` (String) filter name to look for
- `filter_type` (String) filter type to look for

### Read-Only

- `filters` (List of Object) (see [below for nested schema](#nestedatt--filters))
- `id` (String) The ID of this resource.

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

Read-Only:

- `created_by` (String)
- `data` (String)
- `id` (String)
- `is_public` (Boolean)
- `name` (String)
- `type` (String)
93 changes: 93 additions & 0 deletions docs/resources/saved_filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "spacelift_saved_filter Resource - terraform-provider-spacelift"
subcategory: ""
description: |-
spacelift_saved_filter represents a Spacelift filter - a collection of customer-defined criteria that are applied by Spacelift at one of the decision points within the application.
---

# spacelift_saved_filter (Resource)

`spacelift_saved_filter` represents a Spacelift **filter** - a collection of customer-defined criteria that are applied by Spacelift at one of the decision points within the application.

## Example Usage

```terraform
resource "spacelift_saved_filter" "my_filter" {
type = "webhooks"
name = "filter for all xyz teams"
is_public = true
data = jsonencode({
"key" : "activeFilters",
"value" : jsonencode({
"filters" : [
[
"name",
{
"key" : "name",
"filterName" : "name",
"type" : "STRING",
"values" : [
"team_xyz_*"
]
}
]
],
"sort" : {
"direction" : "ASC",
"option" : "space"
},
"text" : null,
"order" : [
{
"name" : "enabled",
"visible" : true
},
{
"name" : "endpoint",
"visible" : true
},
{
"name" : "slug",
"visible" : true
},
{
"name" : "label",
"visible" : true
},
{
"name" : "name",
"visible" : true
},
{
"name" : "space",
"visible" : true
}
]
})
})
}
```

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

### Required

- `data` (String) Data is the JSON representation of the filter data
- `is_public` (Boolean) Toggle whether the filter is public or not
- `name` (String) Name of the saved filter
- `type` (String) Type describes the type of the filter. It is used to determine which view the filter is for. Possible values are `stacks`, `blueprints`, `contexts`, `webhooks`.

### Read-Only

- `created_by` (String) Login of the user who created the saved filter
- `id` (String) Globally unique ID of the saved filter

## Import

Import is supported using the following syntax:

```shell
terraform import spacelift_saved_filter.my_filter $FILTER_ID
```
7 changes: 7 additions & 0 deletions examples/data-sources/spacelift_saved_filter/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
data "spacelift_saved_filter" "filter" {
filter_id = spacelift_saved_filter.filter.id
}

output "filter_data" {
value = data.spacelift_saved_filter.filter.data
}
16 changes: 16 additions & 0 deletions examples/data-sources/spacelift_saved_filters/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# For all saved filter
data "spacelift_saved_filters" "all" {}

# Filters with a matching type
data "spacelift_saved_filters" "stack_filters" {
type = "stacks"
}

# Filters with a matching name
data "spacelift_saved_filters" "my_filters" {
name = "My best filter"
}

output "filter_ids" {
value = data.spacelift_saved_filters.stack_filters.filters[*].id
}
1 change: 1 addition & 0 deletions examples/resources/spacelift_saved_filter/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import spacelift_saved_filter.my_filter $FILTER_ID
54 changes: 54 additions & 0 deletions examples/resources/spacelift_saved_filter/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
resource "spacelift_saved_filter" "my_filter" {
type = "webhooks"
name = "filter for all xyz teams"
is_public = true
data = jsonencode({
"key" : "activeFilters",
"value" : jsonencode({
"filters" : [
[
"name",
{
"key" : "name",
"filterName" : "name",
"type" : "STRING",
"values" : [
"team_xyz_*"
]
}
]
],
"sort" : {
"direction" : "ASC",
"option" : "space"
},
"text" : null,
"order" : [
{
"name" : "enabled",
"visible" : true
},
{
"name" : "endpoint",
"visible" : true
},
{
"name" : "slug",
"visible" : true
},
{
"name" : "label",
"visible" : true
},
{
"name" : "name",
"visible" : true
},
{
"name" : "space",
"visible" : true
}
]
})
})
}
84 changes: 84 additions & 0 deletions spacelift/data_saved_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package spacelift

import (
"context"

Check failure on line 4 in spacelift/data_saved_filter.go

View workflow job for this annotation

GitHub Actions / Run linter

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/spacelift-io/terraform-provider-spacelift) (gci)
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal"
)

func dataSavedFilter() *schema.Resource {
return &schema.Resource{
Description: "" +
"`spacelift_saved_filter` represents a Spacelift saved filter.",

ReadContext: dataFilterRead,

Schema: map[string]*schema.Schema{
"filter_id": {
Type: schema.TypeString,
Description: " immutable ID (slug) of the filter",
Required: true,
},
"id": {
Type: schema.TypeString,
Description: "Globally unique ID of the saved filter",
Computed: true,
},
"is_public": {
Type: schema.TypeBool,
Description: "Toggle whether the filter is public or not",
Computed: true,
},
"created_by": {
Type: schema.TypeString,
Description: "Login of the user who created the saved filter",
Computed: true,
},
"name": {
Type: schema.TypeString,
Description: "Name of the filter",
Computed: true,
},
"type": {
Type: schema.TypeString,
Description: "Type describes the type of the filter. It is used to determine which view the filter is for",
Computed: true,
},
"data": {
Type: schema.TypeString,
Description: "Data is the JSON representation of the filter data",
Computed: true,
},
},
}
}

func dataFilterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var query struct {
Filter struct {
ID string `graphql:"id"`
IsPublic bool `graphql:"isPublic"`
CreatedBy string `graphql:"createdBy"`
Name string `graphql:"name"`
Type string `graphql:"type"`
Data string `graphql:"data"`
} `graphql:"savedFilter(id: $id)"`
}

variables := map[string]interface{}{"id": d.Get("filter_id")}

if err := meta.(*internal.Client).Query(ctx, "savedFilter", &query, variables); err != nil {
return diag.Errorf("could not query for filter: %v", err)
}

d.SetId(query.Filter.ID)
d.Set("is_public", query.Filter.IsPublic)
d.Set("name", query.Filter.Name)
d.Set("type", query.Filter.Type)
d.Set("created_by", query.Filter.CreatedBy)
d.Set("data", query.Filter.Data)

return nil
}
Loading

0 comments on commit e6ab655

Please sign in to comment.