-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_nsxt_logical_tier0_router.go
118 lines (110 loc) · 3.54 KB
/
data_source_nsxt_logical_tier0_router.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
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Copyright © 2017 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */
package nsxt
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
api "github.com/vmware/go-vmware-nsxt"
"github.com/vmware/go-vmware-nsxt/manager"
"net/http"
"strings"
)
func dataSourceNsxtLogicalTier0Router() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtLogicalTier0RouterRead,
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,
},
"edge_cluster_id": {
Type: schema.TypeString,
Description: "The ID of the edge cluster connected to this router",
Optional: true,
Computed: true,
},
"high_availability_mode": {
Type: schema.TypeString,
Description: "The High availability mode of this router",
Optional: true,
Computed: true,
},
},
}
}
func dataSourceNsxtLogicalTier0RouterRead(d *schema.ResourceData, m interface{}) error {
// Read a logical tier0 router by name or id
nsxClient := m.(*api.APIClient)
objID := d.Get("id").(string)
objName := d.Get("display_name").(string)
var obj manager.LogicalRouter
if objID != "" {
// Get by id
objGet, resp, err := nsxClient.LogicalRoutingAndServicesApi.ReadLogicalRouter(nsxClient.Context, objID)
if err != nil {
return fmt.Errorf("Error while reading logical tier0 router %s: %v", objID, err)
}
if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("Logical tier0 router %s was not found", objID)
}
if objGet.RouterType != "TIER0" {
return fmt.Errorf("Logical router %s is not a tier0 router", objID)
}
obj = objGet
} else if objName == "" {
return fmt.Errorf("Error obtaining logical tier0 router ID or name during read")
} else {
// Get by full name/prefix
// TODO use 2nd parameter localVarOptionals for paging
objList, _, err := nsxClient.LogicalRoutingAndServicesApi.ListLogicalRouters(nsxClient.Context, nil)
if err != nil {
return fmt.Errorf("Error while reading logical tier0 routers: %v", err)
}
// go over the list to find the correct one (prefer a perfect match. If not - prefix match)
var perfectMatch []manager.LogicalRouter
var prefixMatch []manager.LogicalRouter
for _, objInList := range objList.Results {
if objInList.RouterType == "TIER0" {
if strings.HasPrefix(objInList.DisplayName, objName) {
prefixMatch = append(prefixMatch, objInList)
}
if objInList.DisplayName == objName {
perfectMatch = append(perfectMatch, objInList)
}
}
}
if len(perfectMatch) > 0 {
if len(perfectMatch) > 1 {
return fmt.Errorf("Found multiple logical tier0 routers with name '%s'", objName)
}
obj = perfectMatch[0]
} else if len(prefixMatch) > 0 {
if len(prefixMatch) > 1 {
return fmt.Errorf("Found multiple logical tier0 routers with name starting with '%s'", objName)
}
obj = prefixMatch[0]
} else {
return fmt.Errorf("Logical tier0 router '%s' was not found", objName)
}
}
d.SetId(obj.Id)
d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)
d.Set("edge_cluster_id", obj.EdgeClusterId)
d.Set("high_availability_mode", obj.HighAvailabilityMode)
return nil
}