0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-01 19:12:59 +00:00
terraform-provider-proxmox/proxmoxtf/data_source_virtual_environment_role.go
Pavel Boldyrev bf9e31ecfc
chore: lint and reformat the code (#204)
* chore: reformat code

* chore: add commitlint config

* reformat README.md

* add linter config

* lint & reformat docs

* go linter: only new issues

* fix some linting errors

* more reformatting

* disable linter warning for some duplicated code
2023-01-16 18:07:30 -05:00

69 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 proxmoxtf
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
mkDataSourceVirtualEnvironmentRoleID = "role_id"
mkDataSourceVirtualEnvironmentRolePrivileges = "privileges"
)
func dataSourceVirtualEnvironmentRole() *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: dataSourceVirtualEnvironmentRoleRead,
}
}
func dataSourceVirtualEnvironmentRoleRead(
ctx context.Context,
d *schema.ResourceData,
m interface{},
) diag.Diagnostics {
config := m.(providerConfiguration)
veClient, err := config.GetVEClient()
if err != nil {
return diag.FromErr(err)
}
roleID := d.Get(mkDataSourceVirtualEnvironmentRoleID).(string)
accessRole, err := veClient.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)
}