forked from vmware/terraform-provider-nsxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_nsxt_ip_pool.go
104 lines (92 loc) · 2.65 KB
/
data_source_nsxt_ip_pool.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
/* Copyright © 2019 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 dataSourceNsxtIPPool() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtIPPoolRead,
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 dataSourceNsxtIPPoolRead(d *schema.ResourceData, m interface{}) error {
// Read IP Pool 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.IpPool
if objID != "" {
// Get by id
objGet, resp, err := nsxClient.PoolManagementApi.ReadIpPool(nsxClient.Context, objID)
if resp != nil && resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("IP pool %s was not found", objID)
}
if err != nil {
return fmt.Errorf("Error while reading ns service %s: %v", objID, err)
}
obj = objGet
} else if objName != "" {
// Get by full name
found := false
lister := func(info *paginationInfo) error {
objList, _, err := nsxClient.PoolManagementApi.ListIpPools(nsxClient.Context, info.LocalVarOptionals)
if err != nil {
return fmt.Errorf("Error while reading IP pool: %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 IP pool with name '%s'", objName)
}
obj = objInList
found = true
}
}
return nil
}
total, err := handlePagination(lister)
if err != nil {
return err
}
if !found {
return fmt.Errorf("IP pool '%s' was not found out of %d objects", objName, total)
}
} else {
return fmt.Errorf("Error obtaining IP pool ID or name during read")
}
d.SetId(obj.Id)
d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)
return nil
}