0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-30 18:42:58 +00:00
terraform-provider-proxmox/proxmoxtf/data_source_virtual_environment_groups.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

79 lines
1.9 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 (
mkDataSourceVirtualEnvironmentGroupsComments = "comments"
mkDataSourceVirtualEnvironmentGroupsGroupIDs = "group_ids"
)
func dataSourceVirtualEnvironmentGroups() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
mkDataSourceVirtualEnvironmentGroupsComments: {
Type: schema.TypeList,
Description: "The group comments",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
mkDataSourceVirtualEnvironmentGroupsGroupIDs: {
Type: schema.TypeList,
Description: "The group ids",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
ReadContext: dataSourceVirtualEnvironmentGroupsRead,
}
}
func dataSourceVirtualEnvironmentGroupsRead(
ctx context.Context,
d *schema.ResourceData,
m interface{},
) diag.Diagnostics {
var diags diag.Diagnostics
config := m.(providerConfiguration)
veClient, err := config.GetVEClient()
if err != nil {
return diag.FromErr(err)
}
list, err := veClient.ListGroups(ctx)
if err != nil {
return diag.FromErr(err)
}
comments := make([]interface{}, len(list))
groupIDs := make([]interface{}, len(list))
for i, v := range list {
if v.Comment != nil {
comments[i] = v.Comment
} else {
comments[i] = ""
}
groupIDs[i] = v.ID
}
d.SetId("groups")
err = d.Set(mkDataSourceVirtualEnvironmentGroupsComments, comments)
diags = append(diags, diag.FromErr(err)...)
err = d.Set(mkDataSourceVirtualEnvironmentGroupsGroupIDs, groupIDs)
diags = append(diags, diag.FromErr(err)...)
return diags
}