forked from vmware/terraform-provider-nsxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_nsxt_ns_groups.go
62 lines (52 loc) · 1.48 KB
/
data_source_nsxt_ns_groups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* Copyright © 2017 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */
package nsxt
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceNsxtNsGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtNsGroupsRead,
DeprecationMessage: mpObjectDataSourceDeprecationMessage,
Schema: map[string]*schema.Schema{
"items": {
Type: schema.TypeMap,
Description: "Mapping of group UUID by display name",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func dataSourceNsxtNsGroupsRead(d *schema.ResourceData, m interface{}) error {
nsxClient := m.(nsxtClients).NsxtClient
if nsxClient == nil {
return dataSourceNotSupportedError()
}
// Get by full name
groupMap := make(map[string]string)
lister := func(info *paginationInfo) error {
objList, _, err := nsxClient.GroupingObjectsApi.ListNSGroups(nsxClient.Context, info.LocalVarOptionals)
if err != nil {
return fmt.Errorf("Error while reading NS groups: %v", err)
}
info.PageCount = int64(len(objList.Results))
info.TotalCount = objList.ResultCount
info.Cursor = objList.Cursor
// go over the list to find the correct one
for _, objInList := range objList.Results {
groupMap[objInList.DisplayName] = objInList.Id
}
return nil
}
_, err := handlePagination(lister)
if err != nil {
return err
}
d.SetId(newUUID())
d.Set("items", groupMap)
return nil
}