diff --git a/docs/data-sources/role.md b/docs/data-sources/role.md index 54a2b780..eb810997 100644 --- a/docs/data-sources/role.md +++ b/docs/data-sources/role.md @@ -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 } ``` @@ -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. diff --git a/examples/data-sources/spectrocloud_role/data-source.tf b/examples/data-sources/spectrocloud_role/data-source.tf index cfe3bd33..dcf48362 100644 --- a/examples/data-sources/spectrocloud_role/data-source.tf +++ b/examples/data-sources/spectrocloud_role/data-source.tf @@ -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 +} \ No newline at end of file diff --git a/go.mod b/go.mod index 24e2cff1..14d068e3 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index f234f5f9..e7538b97 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/spectrocloud/data_source_role.go b/spectrocloud/data_source_role.go index 848ae584..8e5baa40 100644 --- a/spectrocloud/data_source_role.go +++ b/spectrocloud/data_source_role.go @@ -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" @@ -23,6 +24,15 @@ 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. ", + }, }, } } @@ -30,15 +40,28 @@ func dataSourceRole() *schema.Resource { 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 }