0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-08 14:55:02 +00:00
terraform-provider-proxmox/proxmoxtf/datasource/role.go
renovate[bot] 6a8f367c46
chore(deps): update golangci/golangci-lint (v2.1.6 → v2.2.1) (#2013)
* chore(deps): update golangci/golangci-lint (v2.1.6 → v2.2.1)

| datasource      | package                | from   | to     |
| --------------- | ---------------------- | ------ | ------ |
| github-releases | golangci/golangci-lint | v2.1.6 | v2.2.1 |

* chore: update rules & run linter

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>

---------

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
2025-07-04 17:56:38 -04:00

72 lines
1.7 KiB
Go

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package datasource
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/bpg/terraform-provider-proxmox/proxmoxtf"
)
const (
mkDataSourceVirtualEnvironmentRoleID = "role_id"
mkDataSourceVirtualEnvironmentRolePrivileges = "privileges"
)
// Role returns a resource for a single user role.
func Role() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
mkDataSourceVirtualEnvironmentRoleID: {
Type: schema.TypeString,
Description: "The role id",
Required: true,
},
mkDataSourceVirtualEnvironmentRolePrivileges: {
Type: schema.TypeSet,
Description: "The role privileges",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
ReadContext: roleRead,
}
}
func roleRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
config := m.(proxmoxtf.ProviderConfiguration)
api, err := config.GetClient()
if err != nil {
return diag.FromErr(err)
}
roleID := d.Get(mkDataSourceVirtualEnvironmentRoleID).(string)
accessRole, err := api.Access().GetRole(ctx, roleID)
if err != nil {
return diag.FromErr(err)
}
privileges := schema.NewSet(schema.HashString, []interface{}{})
if *accessRole != nil {
for _, v := range *accessRole {
privileges.Add(v)
}
}
d.SetId(roleID)
err = d.Set(mkDataSourceVirtualEnvironmentRolePrivileges, privileges)
return diag.FromErr(err)
}