-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
6 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())), | ||
), | ||
}}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters