Skip to content

Commit

Permalink
feat: add additional properties to the spacelift_current_space data s…
Browse files Browse the repository at this point in the history
…ource (#575)

* feat: make current_space return more space properties

* feat: add additional properties to the spacelift_current_space data source

* remove bad err return

* remove query

* fix assignment
  • Loading branch information
TheMacies authored Oct 24, 2024
1 parent bdff95d commit 154331b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
5 changes: 5 additions & 0 deletions docs/data-sources/current_space.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ resource "spacelift_context" "prod-k8s-ie" {

### Read-Only

- `description` (String) free-form space description for users
- `id` (String) The ID of this resource.
- `inherit_entities` (Boolean) indication whether access to this space inherits read access to entities from the parent space
- `labels` (Set of String) list of labels describing a space
- `name` (String) name of the space
- `parent_space_id` (String) immutable ID (slug) of parent space
59 changes: 52 additions & 7 deletions spacelift/data_current_space.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,35 @@ func dataCurrentSpace() *schema.Resource {
"Spacelift by a stack or module. This makes it easier to create resources " +
"within the same space.",
ReadContext: dataCurrentSpaceRead,

Schema: map[string]*schema.Schema{
"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,
},
},
}
}

Expand Down Expand Up @@ -57,15 +86,31 @@ func dataCurrentSpaceRead(ctx context.Context, d *schema.ResourceData, meta inte
return diag.Errorf("could not query for stack: %v", err)
}

if stack := query.Stack; stack != nil {
d.SetId(stack.Space)
return nil
var space structs.Space

switch {
case query.Stack != nil:
space = query.Stack.SpaceDetails
case query.Module != nil:
space = query.Module.SpaceDetails
default:
return diag.Errorf("could not find stack or module with ID %s", stackID)
}

d.SetId(space.ID)
d.Set("name", space.Name)
d.Set("description", space.Description)
d.Set("inherit_entities", space.InheritEntities)

labels := schema.NewSet(schema.HashString, []interface{}{})
for _, label := range space.Labels {
labels.Add(label)
}
d.Set("labels", labels)

if module := query.Module; module != nil {
d.SetId(module.Space)
return nil
if space.ParentSpace != nil {
d.Set("parent_space_id", *space.ParentSpace)
}

return diag.Errorf("could not find stack or module with ID %s", stackID)
return nil
}
1 change: 1 addition & 0 deletions spacelift/internal/structs/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Module struct {
RepositoryURL *string `graphql:"repositoryURL"`
SharedAccounts []string `graphql:"sharedAccounts"`
Space string `graphql:"space"`
SpaceDetails Space `graphql:"spaceDetails"`
TerraformProvider string `graphql:"terraformProvider"`
VCSIntegration *struct {
ID string `graphql:"id"`
Expand Down
1 change: 1 addition & 0 deletions spacelift/internal/structs/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Stack struct {
RepositoryURL *string `graphql:"repositoryURL"`
RunnerImage *string `graphql:"runnerImage"`
Space string `graphql:"space"`
SpaceDetails Space `graphql:"spaceDetails"`
TerraformVersion *string `graphql:"terraformVersion"`
VCSIntegration *struct {
ID string `graphql:"id"`
Expand Down

0 comments on commit 154331b

Please sign in to comment.