forked from vmware/terraform-provider-nsxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_nsxt_edge_upgrade_group.go
105 lines (90 loc) · 3.13 KB
/
data_source_nsxt_edge_upgrade_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 © 2024 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */
package nsxt
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/upgrade"
)
var (
edgeUpgradeGroup = "EDGE"
hostUpgradeGroup = "HOST"
mpUpgradeGroup = "MP"
)
func dataSourceNsxtEdgeUpgradeGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtEdgeUpgradeGroupRead,
Schema: map[string]*schema.Schema{
"upgrade_prepare_id": {
Type: schema.TypeString,
Description: "ID of corresponding nsxt_upgrade_prepare resource",
Required: true,
},
"id": getDataSourceIDSchema(),
"display_name": getDataSourceDisplayNameSchema(),
"description": getDataSourceDescriptionSchema(),
},
}
}
func dataSourceNsxtEdgeUpgradeGroupRead(d *schema.ResourceData, m interface{}) error {
return upgradeGroupRead(d, m, edgeUpgradeGroup)
}
func upgradeGroupRead(d *schema.ResourceData, m interface{}, groupType string) error {
connector := getPolicyConnector(m)
client := upgrade.NewUpgradeUnitGroupsClient(connector)
objID := d.Get("id").(string)
objName := d.Get("display_name").(string)
var obj model.UpgradeUnitGroup
if objID != "" {
// Get by id
objGet, err := client.Get(objID, nil)
if isNotFoundError(err) {
return fmt.Errorf("%s UpgradeUnitGroup with ID %s was not found", groupType, objID)
}
if err != nil {
return fmt.Errorf("error while reading %s UpgradeUnitGroup %s: %v", groupType, objID, err)
}
if *objGet.Type_ != groupType {
return fmt.Errorf("%s UpgradeUnitGroup with ID %s was not found", groupType, objID)
}
obj = objGet
} else if objName == "" {
return fmt.Errorf("error obtaining %s UpgradeUnitGroup ID or name during read", groupType)
} else {
// Get by full name/prefix
objList, err := client.List(&groupType, nil, nil, nil, nil, nil, nil, nil)
if err != nil {
return fmt.Errorf("error while reading %s UpgradeUnitGroup: %v", groupType, err)
}
// go over the list to find the correct one (prefer a perfect match. If not - prefix match)
var perfectMatch []model.UpgradeUnitGroup
var prefixMatch []model.UpgradeUnitGroup
for _, objInList := range objList.Results {
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 %s UpgradeUnitGroup with name '%s'", groupType, objName)
}
obj = perfectMatch[0]
} else if len(prefixMatch) > 0 {
if len(prefixMatch) > 1 {
return fmt.Errorf("found multiple %s UpgradeUnitGroup with name starting with '%s'", groupType, objName)
}
obj = prefixMatch[0]
} else {
return fmt.Errorf("%s UpgradeUnitGroup with name '%s' was not found", groupType, objName)
}
}
d.SetId(*obj.Id)
d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)
return nil
}