Skip to content

Commit

Permalink
PLT-1480:Added permission attribute support (#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
SivaanandM authored Nov 13, 2024
1 parent f5d27fc commit 1cee37c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
15 changes: 12 additions & 3 deletions docs/data-sources/role.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ description: |-
## Example Usage

```terraform
data "spectrocloud_role" "role1" {
name = "Project Editor"
data "spectrocloud_role" "role" {
name = "Resource Cluster Admin"
# (alternatively)
# id = "5fd0ca727c411c71b55a359c"
# id = "66fbea622947f81fb62294ac"
}
output "role_id" {
value = data.spectrocloud_role.role.id
}
output "role_permissions" {
value = data.spectrocloud_role.role.permissions
}
```

Expand All @@ -31,3 +39,4 @@ data "spectrocloud_role" "role1" {
### Read-Only

- `id` (String) The ID of this resource.
- `permissions` (Set of String) List of permissions associated with the role.
14 changes: 11 additions & 3 deletions examples/data-sources/spectrocloud_role/data-source.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
data "spectrocloud_role" "role1" {
name = "Project Editor"
data "spectrocloud_role" "role" {
name = "Resource Cluster Admin"

# (alternatively)
# id = "5fd0ca727c411c71b55a359c"
# id = "66fbea622947f81fb62294ac"
}

output "role_id" {
value = data.spectrocloud_role.role.id
}

output "role_permissions" {
value = data.spectrocloud_role.role.permissions
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/robfig/cron v1.2.0
github.com/spectrocloud/gomi v1.14.1-0.20240214074114-c19394812368
github.com/spectrocloud/hapi v1.14.1-0.20240214071352-81f589b1d86d
github.com/spectrocloud/palette-sdk-go v0.0.0-20241022161234-3782615736bb
github.com/spectrocloud/palette-sdk-go v0.0.0-20241113133445-a5e87250e68d
github.com/stretchr/testify v1.9.0
gotest.tools v2.2.0+incompatible
k8s.io/api v0.23.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,8 @@ github.com/spectrocloud/gomi v1.14.1-0.20240214074114-c19394812368 h1:eY0BOyEbGu
github.com/spectrocloud/gomi v1.14.1-0.20240214074114-c19394812368/go.mod h1:LlZ9We4kDaELYi7Is0SVmnySuDhwphJLS6ZT4wXxFIk=
github.com/spectrocloud/hapi v1.14.1-0.20240214071352-81f589b1d86d h1:OMRbHxMJ1a+G1BYzvUYuMM0wLkYJPdnEOFx16faQ/UY=
github.com/spectrocloud/hapi v1.14.1-0.20240214071352-81f589b1d86d/go.mod h1:MktpRPnSXDTHsQrFSD+daJFQ1zMLSR+1gWOL31jVvWE=
github.com/spectrocloud/palette-sdk-go v0.0.0-20241022161234-3782615736bb h1:LVeVFAMVdZRhtn1VY3DnDi32ts90r8/RXP5+1RZBZEA=
github.com/spectrocloud/palette-sdk-go v0.0.0-20241022161234-3782615736bb/go.mod h1:dSlNvDS0qwUWTbrYI6P8x981mcbbRHFrBg67v5zl81U=
github.com/spectrocloud/palette-sdk-go v0.0.0-20241113133445-a5e87250e68d h1:RkU8p4K15zpH1FB2roV3yrpLiKn+/FcRxuxyJrXmtsk=
github.com/spectrocloud/palette-sdk-go v0.0.0-20241113133445-a5e87250e68d/go.mod h1:dSlNvDS0qwUWTbrYI6P8x981mcbbRHFrBg67v5zl81U=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
Expand Down
25 changes: 24 additions & 1 deletion spectrocloud/data_source_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package spectrocloud

import (
"context"
"github.com/spectrocloud/palette-sdk-go/api/models"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -23,22 +24,44 @@ func dataSourceRole() *schema.Resource {
Computed: true,
Optional: true,
},
"permissions": {
Type: schema.TypeSet,
Computed: true,
Set: schema.HashString,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "List of permissions associated with the role. ",
},
},
}
}

func dataSourceRoleRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := getV1ClientWithResourceContext(m, "")
var diags diag.Diagnostics
var role *models.V1Role
var err error
if i, ok := d.GetOk("id"); ok {
role, err = c.GetRoleByID(i.(string))
if err != nil {
return diag.FromErr(err)
}
}
if v, ok := d.GetOk("name"); ok {
role, err := c.GetRole(v.(string))
role, err = c.GetRole(v.(string))
if err != nil {
return diag.FromErr(err)
}
}
if role != nil {
d.SetId(role.Metadata.UID)
if err := d.Set("name", role.Metadata.Name); err != nil {
return diag.FromErr(err)
}
if err := d.Set("permissions", role.Spec.Permissions); err != nil {
return diag.FromErr(err)
}
}
return diags
}

0 comments on commit 1cee37c

Please sign in to comment.