Skip to content

Commit 3741bf9

Browse files
authored
Merge branch 'main' into fix/2363
2 parents 99fdb4f + 9ad0f32 commit 3741bf9

File tree

8 files changed

+147
-35
lines changed

8 files changed

+147
-35
lines changed

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ module github.com/hashicorp/terraform-provider-vsphere
22

33
go 1.23.0
44

5-
toolchain go1.24.0
5+
toolchain go1.24.1
66

77
require (
88
github.com/davecgh/go-spew v1.1.1
99
github.com/hashicorp/terraform-plugin-sdk/v2 v2.36.1
1010
github.com/mitchellh/copystructure v1.2.0
11-
github.com/vmware/govmomi v0.48.1
11+
github.com/vmware/govmomi v0.49.0
1212
)
1313

1414
require (
@@ -18,7 +18,7 @@ require (
1818
github.com/cloudflare/circl v1.3.7 // indirect
1919
github.com/fatih/color v1.16.0 // indirect
2020
github.com/golang/protobuf v1.5.4 // indirect
21-
github.com/google/go-cmp v0.6.0 // indirect
21+
github.com/google/go-cmp v0.7.0 // indirect
2222
github.com/hashicorp/errwrap v1.0.0 // indirect
2323
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
2424
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
@@ -53,10 +53,10 @@ require (
5353
github.com/zclconf/go-cty v1.16.2 // indirect
5454
golang.org/x/crypto v0.35.0 // indirect
5555
golang.org/x/mod v0.22.0 // indirect
56-
golang.org/x/net v0.34.0 // indirect
57-
golang.org/x/sync v0.11.0 // indirect
56+
golang.org/x/net v0.36.0 // indirect
57+
golang.org/x/sync v0.12.0 // indirect
5858
golang.org/x/sys v0.30.0 // indirect
59-
golang.org/x/text v0.22.0 // indirect
59+
golang.org/x/text v0.23.0 // indirect
6060
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
6161
google.golang.org/appengine v1.6.8 // indirect
6262
google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53 // indirect

go.sum

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek
4646
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
4747
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
4848
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
49-
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
50-
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
49+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
50+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
5151
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
5252
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5353
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
@@ -150,8 +150,8 @@ github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IU
150150
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
151151
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
152152
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
153-
github.com/vmware/govmomi v0.48.1 h1:aAjmoFzSShYA9ED66JaOJzSBvukvrQLYZljZL+pgfKQ=
154-
github.com/vmware/govmomi v0.48.1/go.mod h1:UFM2aCkggPToQf8TqY3xfd9bOX58vbVa+UAK1JdDTNM=
153+
github.com/vmware/govmomi v0.49.0 h1:M80ExmFq3kOfeMvMJcHnXgA/4w5hUAFfYfc+Qm3lmPg=
154+
github.com/vmware/govmomi v0.49.0/go.mod h1:+oZ0tYJw/pXKoeWHLR9Egq5KENVr2hLePRzisFhEWpA=
155155
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
156156
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
157157
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
@@ -180,13 +180,13 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
180180
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
181181
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
182182
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
183-
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
184-
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
183+
golang.org/x/net v0.36.0 h1:vWF2fRbw4qslQsQzgFqZff+BItCvGFQqKzKIzx1rmoA=
184+
golang.org/x/net v0.36.0/go.mod h1:bFmbeoIPfrw4sMHNhb4J9f6+tPziuGjq7Jk/38fxi1I=
185185
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
186186
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
187187
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
188-
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
189-
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
188+
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
189+
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
190190
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
191191
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
192192
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -210,8 +210,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
210210
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
211211
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
212212
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
213-
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
214-
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
213+
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
214+
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
215215
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
216216
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
217217
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=

vsphere/data_source_vsphere_network.go

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@ package vsphere
55

66
import (
77
"fmt"
8+
"time"
89

10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
911
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1012
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13+
1114
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/network"
15+
1216
"github.com/vmware/govmomi/object"
1317
)
1418

19+
const (
20+
waitForNetworkPending = "waitForNetworkPending"
21+
waitForNetworkCompleted = "waitForNetworkCompleted"
22+
waitForNetworkError = "waitForNetworkError"
23+
)
24+
1525
func dataSourceVSphereNetwork() *schema.Resource {
1626
return &schema.Resource{
1727
Read: dataSourceVSphereNetworkRead,
@@ -52,6 +62,18 @@ func dataSourceVSphereNetwork() *schema.Resource {
5262
},
5363
},
5464
},
65+
"retry_timeout": {
66+
Type: schema.TypeInt,
67+
Description: "Timeout (in seconds) if network is not present yet",
68+
Optional: true,
69+
Default: 0,
70+
},
71+
"retry_interval": {
72+
Type: schema.TypeInt,
73+
Description: "Retry interval (in milliseconds) when probing the network",
74+
Optional: true,
75+
Default: 500,
76+
},
5577
},
5678
}
5779
}
@@ -69,8 +91,6 @@ func dataSourceVSphereNetworkRead(d *schema.ResourceData, meta interface{}) erro
6991
return fmt.Errorf("cannot locate datacenter: %s", err)
7092
}
7193
}
72-
var net object.NetworkReference
73-
var err error
7494

7595
vimClient := client.Client
7696

@@ -85,19 +105,65 @@ func dataSourceVSphereNetworkRead(d *schema.ResourceData, meta interface{}) erro
85105
}
86106
}
87107

88-
if dvSwitchUUID != "" {
89-
// Handle distributed virtual switch port group
90-
net, err = network.FromNameAndDVSUuid(client, name, dc, dvSwitchUUID)
91-
if err != nil {
92-
return fmt.Errorf("error fetching DVS network: %s", err)
108+
readRetryFunc := func() (interface{}, string, error) {
109+
var net interface{}
110+
var err error
111+
if dvSwitchUUID != "" {
112+
// Handle distributed virtual switch port group
113+
net, err = network.FromNameAndDVSUuid(client, name, dc, dvSwitchUUID)
114+
if err != nil {
115+
if _, ok := err.(network.NetworkNotFoundError); ok {
116+
return struct{}{}, waitForNetworkPending, nil
117+
}
118+
119+
return struct{}{}, waitForNetworkError, err
120+
}
121+
return net, waitForNetworkCompleted, nil
93122
}
94-
} else {
95123
// Handle standard switch port group
96124
net, err = network.FromName(vimClient, name, dc, filters) // Pass the *vim25.Client
97125
if err != nil {
98-
return fmt.Errorf("error fetching network: %s", err)
126+
if _, ok := err.(network.NetworkNotFoundError); ok {
127+
return struct{}{}, waitForNetworkPending, nil
128+
}
129+
return struct{}{}, waitForNetworkError, err
99130
}
131+
return net, waitForNetworkCompleted, nil
132+
133+
}
134+
135+
var net object.NetworkReference
136+
var netObj interface{}
137+
var err error
138+
var state string
139+
140+
retryTimeout := d.Get("retry_timeout").(int)
141+
retryInterval := d.Get("retry_interval").(int)
142+
143+
if retryTimeout == 0 {
144+
// no retry
145+
netObj, state, err = readRetryFunc()
146+
} else {
147+
148+
deleteRetry := &resource.StateChangeConf{
149+
Pending: []string{waitForNetworkPending},
150+
Target: []string{waitForNetworkCompleted},
151+
Refresh: readRetryFunc,
152+
Timeout: time.Duration(retryTimeout) * time.Second,
153+
MinTimeout: time.Duration(retryInterval) * time.Millisecond,
154+
}
155+
156+
netObj, err = deleteRetry.WaitForState()
157+
}
158+
159+
if state == waitForNetworkPending {
160+
err = fmt.Errorf("network %s not found", name)
161+
}
162+
163+
if err != nil {
164+
return err
100165
}
166+
net = netObj.(object.NetworkReference)
101167

102168
d.SetId(net.Reference().Value)
103169
_ = d.Set("type", net.Reference().Type)

vsphere/data_source_vsphere_network_test.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,31 @@ func TestAccDataSourceVSphereNetwork_dvsPortgroup(t *testing.T) {
2424
Providers: testAccProviders,
2525
Steps: []resource.TestStep{
2626
{
27-
Config: testAccDataSourceVSphereNetworkConfigDVSPortgroup(),
27+
Config: testAccDataSourceVSphereNetworkConfigDVSPortgroup(false),
28+
Check: resource.ComposeTestCheckFunc(
29+
resource.TestCheckResourceAttr("data.vsphere_network.net", "type", "DistributedVirtualPortgroup"),
30+
resource.TestCheckResourceAttrPair(
31+
"data.vsphere_network.net", "id",
32+
"vsphere_distributed_port_group.pg", "id",
33+
),
34+
),
35+
},
36+
},
37+
})
38+
}
39+
40+
func TestAccDataSourceVSphereNetwork_withTimeout(t *testing.T) {
41+
resource.Test(t, resource.TestCase{
42+
PreCheck: func() {
43+
RunSweepers()
44+
testAccPreCheck(t)
45+
testAccDataSourceVSphereNetworkPreCheck(t)
46+
testAccSkipIfEsxi(t)
47+
},
48+
Providers: testAccProviders,
49+
Steps: []resource.TestStep{
50+
{
51+
Config: testAccDataSourceVSphereNetworkConfigDVSPortgroup(true),
2852
Check: resource.ComposeTestCheckFunc(
2953
resource.TestCheckResourceAttr("data.vsphere_network.net", "type", "DistributedVirtualPortgroup"),
3054
resource.TestCheckResourceAttrPair(
@@ -86,7 +110,11 @@ func testAccDataSourceVSphereNetworkPreCheck(t *testing.T) {
86110
}
87111
}
88112

89-
func testAccDataSourceVSphereNetworkConfigDVSPortgroup() string {
113+
func testAccDataSourceVSphereNetworkConfigDVSPortgroup(withTimeout bool) string {
114+
additionalConfig := ""
115+
if withTimeout {
116+
additionalConfig = `retry_timeout = 180`
117+
}
90118
return fmt.Sprintf(`
91119
%s
92120
@@ -104,10 +132,10 @@ data "vsphere_network" "net" {
104132
name = "${vsphere_distributed_port_group.pg.name}"
105133
datacenter_id = "${data.vsphere_datacenter.rootdc1.id}"
106134
distributed_virtual_switch_uuid = "${vsphere_distributed_virtual_switch.dvs.id}"
135+
%s
107136
}
108137
`,
109-
testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootPortGroup1()),
110-
)
138+
testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootPortGroup1()), additionalConfig)
111139
}
112140

113141
func testAccDataSourceVSphereNetworkConfigDVSPortgroupAbsolute() string {

vsphere/internal/helper/customattribute/custom_attributes_helper.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ func (p *DiffProcessor) ProcessDiff(subject object.Reference) error {
126126
}
127127

128128
func GetDiffProcessorIfAttributesDefined(client *govmomi.Client, d *schema.ResourceData) (*DiffProcessor, error) {
129+
if !d.HasChange(ConfigKey) {
130+
return nil, nil
131+
}
129132
old, newValue := d.GetChange(ConfigKey)
130133
if len(old.(map[string]interface{})) > 0 || len(newValue.(map[string]interface{})) > 0 {
131134
if err := VerifySupport(client); err != nil {

vsphere/internal/helper/network/network_helper.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ var NetworkType = []string{
2424
"OpaqueNetwork",
2525
}
2626

27+
type NetworkNotFoundError struct {
28+
Name string
29+
ID string
30+
}
31+
32+
func (e NetworkNotFoundError) Error() string {
33+
if len(e.ID) > 0 {
34+
return fmt.Sprintf("Network with ID %s not found", e.ID)
35+
}
36+
37+
return fmt.Sprintf("Network %s not found", e.Name)
38+
}
39+
2740
// FromPath loads a network via its path.
2841
//
2942
// A network is a usually one of three kinds of networks: a DVS port group, a
@@ -57,7 +70,7 @@ func FromNameAndDVSUuid(client *govmomi.Client, name string, dc *object.Datacent
5770
return nil, err
5871
}
5972
if len(networks) == 0 {
60-
return nil, fmt.Errorf("%s %s not found", "Network", name)
73+
return nil, NetworkNotFoundError{Name: name}
6174
}
6275

6376
switch {
@@ -90,7 +103,7 @@ func FromNameAndDVSUuid(client *govmomi.Client, name string, dc *object.Datacent
90103
}
91104
return nil, fmt.Errorf("error while getting Network with name %s and Distributed virtual switch %s", name, dvsUUID)
92105
}
93-
return nil, fmt.Errorf("%s %s not found", "Network", name)
106+
return nil, NetworkNotFoundError{Name: name}
94107
}
95108

96109
func List(client *govmomi.Client) ([]*object.VmwareDistributedVirtualSwitch, error) {
@@ -169,7 +182,7 @@ func FromID(client *govmomi.Client, id string) (object.NetworkReference, error)
169182
return nref.(object.NetworkReference), nil
170183
}
171184
}
172-
return nil, fmt.Errorf("could not find network with ID %q", id)
185+
return nil, NetworkNotFoundError{ID: id}
173186
}
174187

175188
func dvsFromMOID(client *govmomi.Client, id string) (*object.VmwareDistributedVirtualSwitch, error) {
@@ -218,7 +231,7 @@ func FromName(client *vim25.Client, name string, dc *object.Datacenter, filters
218231
// Find the network by name
219232
networks, err := finder.NetworkList(ctx, name)
220233
if err != nil {
221-
return nil, fmt.Errorf("error finding network %s: %v", name, err)
234+
return nil, NetworkNotFoundError{Name: name}
222235
}
223236

224237
// If multiple networks are found and no filters are specified, return an error
@@ -255,5 +268,5 @@ func FromName(client *vim25.Client, name string, dc *object.Datacenter, filters
255268
}
256269
}
257270

258-
return nil, fmt.Errorf("no network found matching the specified criteria")
271+
return nil, NetworkNotFoundError{Name: name}
259272
}

vsphere/resource_vsphere_vmfs_datastore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ func resourceVSphereVmfsDatastoreDelete(d *schema.ResourceData, meta interface{}
378378
Pending: []string{retryDeletePending},
379379
Target: []string{retryDeleteCompleted},
380380
Refresh: deleteRetryFunc,
381-
Timeout: 30 * time.Second,
381+
Timeout: defaultAPITimeout,
382382
MinTimeout: 2 * time.Second,
383383
Delay: 2 * time.Second,
384384
}

website/docs/d/network.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ The following arguments are supported:
6262
the distributed virtual switch ID.
6363
* `filter` - (Optional) Apply a filter for the discovered network.
6464
* `network_type`: This is required if you have multiple port groups with the same name. This will be one of `DistributedVirtualPortgroup` for distributed port groups, `Network` for standard (host-based) port groups, or `OpaqueNetwork` for networks managed externally, such as those managed by NSX.
65+
* `retry_timeout` - (Optional) The timeout duration in seconds for the data source to retry read operations.
66+
* `retry_interval` - (Optional) The interval in milliseconds to retry the read operation if `retry_timeout` is set. Default: 500.
6567
[docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider
6668

6769
## Attribute Reference

0 commit comments

Comments
 (0)