Skip to content

Commit

Permalink
Add Spaces datasource (#456)
Browse files Browse the repository at this point in the history
* add spaces data source

* fix bug with param name

* update deprecated syntax

* add examples

* remove filtering per pr comments

* Use time-based id. Try to fix test.

---------

Co-authored-by: Matt Calhoun <[email protected]>
  • Loading branch information
cube2222 and mcalhoun authored Jul 13, 2023
1 parent 499de08 commit 331fc76
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ To develop the provider locally you need the following tools:
### Generating the Documentation

To generate the documentation, run the following command:

```shell
cd tools
go generate ./...
Expand All @@ -51,7 +52,7 @@ This involves the following steps:
To build the provider, run the following command:

```shell
goreleaser build --rm-dist --snapshot
goreleaser build --clean --snapshot
```

This will produce a number of binaries in subfolders of the `dist` folder for each supported
Expand Down
42 changes: 42 additions & 0 deletions docs/data-sources/spaces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "spacelift_spaces Data Source - terraform-provider-spacelift"
subcategory: ""
description: |-
spacelift_spaces can find all spaces in the spacelift organization.
---

# spacelift_spaces (Data Source)

`spacelift_spaces` can find all spaces in the spacelift organization.

## Example Usage

```terraform
data "spacelift_spaces" "this" {
}
output "spaces" {
value = data.spacelift_spaces.this.spaces
}
```

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

### Read-Only

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

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

Read-Only:

- `description` (String)
- `inherit_entities` (Boolean)
- `labels` (Set of String)
- `name` (String)
- `parent_space_id` (String)
- `space_id` (String)
6 changes: 6 additions & 0 deletions examples/data-sources/spacelift_spaces/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data "spacelift_spaces" "this" {
}

output "spaces" {
value = data.spacelift_spaces.this.spaces
}
102 changes: 102 additions & 0 deletions spacelift/data_spaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package spacelift

import (
"context"
"fmt"
"time"

"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"
"github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/validations"
)

func dataSpaces() *schema.Resource {
return &schema.Resource{
Description: "" +
"`spacelift_spaces` can find all spaces in the spacelift organization.",

ReadContext: dataSpacesRead,

Schema: map[string]*schema.Schema{
"spaces": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"space_id": {
Type: schema.TypeString,
Description: "immutable ID (slug) of the space",
Required: true,
ValidateDiagFunc: validations.DisallowEmptyString,
},
"parent_space_id": {
Type: schema.TypeString,
Description: "immutable ID (slug) of parent space",
Computed: true,
},
"description": {
Type: schema.TypeString,
Description: "free-form space description for users",
Computed: true,
},
"name": {
Type: schema.TypeString,
Description: "name of the space",
Computed: true,
},
"inherit_entities": {
Type: schema.TypeBool,
Description: "indication whether access to this space inherits read access to entities from the parent space",
Computed: true,
},
"labels": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "list of labels describing a space",
Computed: true,
},
},
},
},
},
}
}

func dataSpacesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var query struct {
Spaces []struct {
ID string `graphql:"id"`
Labels []string `graphql:"labels"`
Name string `graphql:"name"`
Description string `graphql:"description"`
ParentSpace string `graphql:"parentSpace"`
InheritEntities bool `graphql:"inheritEntities"`
} `graphql:"spaces()"`
}

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

var spaces []interface{}
for _, space := range query.Spaces {
spaces = append(spaces, map[string]interface{}{
"space_id": space.ID,
"name": space.Name,
"description": space.Description,
"parent_space_id": space.ParentSpace,
"inherit_entities": space.InheritEntities,
"labels": space.Labels,
})
}

d.SetId(fmt.Sprintf("spaces-%d", time.Now().UnixNano()))

if err := d.Set("spaces", spaces); err != nil {
return diag.Errorf("could not set stacks: %v", err)
}

return nil
}
27 changes: 27 additions & 0 deletions spacelift/data_spaces_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package spacelift

import (
"fmt"
"testing"

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

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

func TestSpacesData(t *testing.T) {
t.Run("load all spaces", func(t *testing.T) {
datasourceName := "data.spacelift_spaces.test"

testSteps(t, []resource.TestStep{{
// Should find at least the legacy and root spaces.
Config: fmt.Sprintf(`
data "spacelift_spaces" "test" {
}
`),
Check: resource.ComposeTestCheckFunc(
Resource(datasourceName, Attribute("id", IsNotEmpty())),
),
}})
})
}
1 change: 1 addition & 0 deletions spacelift/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func Provider(commit, version string) plugin.ProviderFunc {
"spacelift_policies": dataPolicies(),
"spacelift_policy": dataPolicy(),
"spacelift_space": dataSpace(),
"spacelift_spaces": dataSpaces(),
"spacelift_space_by_path": dataSpaceByPath(),
"spacelift_scheduled_task": dataScheduledTask(),
"spacelift_scheduled_delete_stack": dataScheduledDeleteStack(),
Expand Down

0 comments on commit 331fc76

Please sign in to comment.