forked from vmware/terraform-provider-nsxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_nsxt_ns_group.go
105 lines (94 loc) · 2.79 KB
/
data_source_nsxt_ns_group.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* Copyright © 2017 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */
package nsxt
import (
"fmt"
"net/http"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/go-vmware-nsxt/manager"
)
func dataSourceNsxtNsGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtNsGroupRead,
DeprecationMessage: mpObjectDataSourceDeprecationMessage,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "Unique ID of this resource",
Optional: true,
Computed: true,
},
"display_name": {
Type: schema.TypeString,
Description: "The display name of this resource",
Optional: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Description: "Description of this resource",
Optional: true,
Computed: true,
},
},
}
}
func dataSourceNsxtNsGroupRead(d *schema.ResourceData, m interface{}) error {
// Read NS Group by name or id
nsxClient := m.(nsxtClients).NsxtClient
if nsxClient == nil {
return dataSourceNotSupportedError()
}
objID := d.Get("id").(string)
objName := d.Get("display_name").(string)
var obj manager.NsGroup
if objID != "" {
// Get by id
localVarOptionals := make(map[string]interface{})
localVarOptionals["populateReferences"] = true
objGet, resp, err := nsxClient.GroupingObjectsApi.ReadNSGroup(nsxClient.Context, objID, localVarOptionals)
if resp != nil && resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("NS group %s was not found", objID)
}
if err != nil {
return fmt.Errorf("Error while reading NS group %s: %v", objID, err)
}
obj = objGet
} else if objName != "" {
// Get by full name
found := false
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 {
if objInList.DisplayName == objName {
if found {
return fmt.Errorf("Found multiple NS groups with name '%s'", objName)
}
obj = objInList
found = true
}
}
return nil
}
total, err := handlePagination(lister)
if err != nil {
return err
}
if !found {
return fmt.Errorf("NS group with name '%s' was not found among %d groups", objName, total)
}
} else {
return fmt.Errorf("Error obtaining NS group ID or name during read")
}
d.SetId(obj.Id)
d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)
return nil
}