Skip to content

Commit f1c2be9

Browse files
committed
Make maxRetryTimeout (in seconds) configurable
1 parent 815ff7a commit f1c2be9

13 files changed

Lines changed: 73 additions & 60 deletions

builtin/providers/vcd/config.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,28 @@ import (
88
)
99

1010
type Config struct {
11-
User string
12-
Password string
13-
Org string
14-
Href string
15-
VDC string
11+
User string
12+
Password string
13+
Org string
14+
Href string
15+
VDC string
16+
MaxRetryTimeout int
1617
}
1718

18-
func (c *Config) Client() (*govcd.VCDClient, error) {
19+
type VCDClient struct {
20+
*govcd.VCDClient
21+
MaxRetryTimeout int
22+
}
23+
24+
func (c *Config) Client() (*VCDClient, error) {
1925
u, err := url.ParseRequestURI(c.Href)
2026
if err != nil {
2127
return nil, fmt.Errorf("Something went wrong: %s", err)
2228
}
2329

24-
vcdclient := govcd.NewVCDClient(*u)
30+
vcdclient := &VCDClient{
31+
govcd.NewVCDClient(*u),
32+
c.MaxRetryTimeout}
2533
org, vcd, err := vcdclient.Authenticate(c.User, c.Password, c.Org, c.VDC)
2634
if err != nil {
2735
return nil, fmt.Errorf("Something went wrong: %s", err)

builtin/providers/vcd/provider.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,20 @@ func Provider() terraform.ResourceProvider {
3636
DefaultFunc: schema.EnvDefaultFunc("VCD_URL", nil),
3737
Description: "The vcd url for vcd API operations.",
3838
},
39+
3940
"vdc": &schema.Schema{
4041
Type: schema.TypeString,
4142
Optional: true,
4243
DefaultFunc: schema.EnvDefaultFunc("VCD_VDC", ""),
4344
Description: "The name of the VDC to run operations on",
4445
},
46+
47+
"maxRetryTimeout": &schema.Schema{
48+
Type: schema.TypeInt,
49+
Optional: true,
50+
DefaultFunc: schema.EnvDefaultFunc("VCD_MAX_RETRY_TIMEOUT", 30),
51+
Description: "Max num seconds to wait for successful response when operating on resources within vCloud (defaults to 30)",
52+
},
4553
},
4654

4755
ResourcesMap: map[string]*schema.Resource{
@@ -58,11 +66,12 @@ func Provider() terraform.ResourceProvider {
5866

5967
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
6068
config := Config{
61-
User: d.Get("user").(string),
62-
Password: d.Get("password").(string),
63-
Org: d.Get("org").(string),
64-
Href: d.Get("url").(string),
65-
VDC: d.Get("vdc").(string),
69+
User: d.Get("user").(string),
70+
Password: d.Get("password").(string),
71+
Org: d.Get("org").(string),
72+
Href: d.Get("url").(string),
73+
VDC: d.Get("vdc").(string),
74+
MaxRetryTimeout: d.Get("maxRetryTimeout").(int),
6675
}
6776

6877
return config.Client()

builtin/providers/vcd/resource_vcd_dnat.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package vcd
33
import (
44
"fmt"
55
"github.com/hashicorp/terraform/helper/schema"
6-
"github.com/hmrc/vmware-govcd"
76
)
87

98
func resourceVcdDNAT() *schema.Resource {
@@ -41,7 +40,7 @@ func resourceVcdDNAT() *schema.Resource {
4140
}
4241

4342
func resourceVcdDNATCreate(d *schema.ResourceData, meta interface{}) error {
44-
vcdClient := meta.(*govcd.VCDClient)
43+
vcdClient := meta.(*VCDClient)
4544
// Multiple VCD components need to run operations on the Edge Gateway, as
4645
// the edge gatway will throw back an error if it is already performing an
4746
// operation we must wait until we can aquire a lock on the client
@@ -60,7 +59,7 @@ func resourceVcdDNATCreate(d *schema.ResourceData, meta interface{}) error {
6059
// constrained by out lock. If the edge gateway reurns with a busy error, wait
6160
// 3 seconds and then try again. Continue until a non-busy error or success
6261

63-
err = retryCall(4, func() error {
62+
err = retryCall(vcdClient.MaxRetryTimeout, func() error {
6463
task, err := edgeGateway.AddNATMapping("DNAT", d.Get("external_ip").(string),
6564
d.Get("internal_ip").(string),
6665
portString)
@@ -80,7 +79,7 @@ func resourceVcdDNATCreate(d *schema.ResourceData, meta interface{}) error {
8079
}
8180

8281
func resourceVcdDNATRead(d *schema.ResourceData, meta interface{}) error {
83-
vcdClient := meta.(*govcd.VCDClient)
82+
vcdClient := meta.(*VCDClient)
8483
e, err := vcdClient.OrgVdc.FindEdgeGateway(d.Get("edge_gateway").(string))
8584

8685
if err != nil {
@@ -106,7 +105,7 @@ func resourceVcdDNATRead(d *schema.ResourceData, meta interface{}) error {
106105
}
107106

108107
func resourceVcdDNATDelete(d *schema.ResourceData, meta interface{}) error {
109-
vcdClient := meta.(*govcd.VCDClient)
108+
vcdClient := meta.(*VCDClient)
110109
// Multiple VCD components need to run operations on the Edge Gateway, as
111110
// the edge gatway will throw back an error if it is already performing an
112111
// operation we must wait until we can aquire a lock on the client
@@ -119,7 +118,7 @@ func resourceVcdDNATDelete(d *schema.ResourceData, meta interface{}) error {
119118
if err != nil {
120119
return fmt.Errorf("Unable to find edge gateway: %#v", err)
121120
}
122-
err = retryCall(4, func() error {
121+
err = retryCall(vcdClient.MaxRetryTimeout, func() error {
123122
task, err := edgeGateway.RemoveNATMapping("DNAT", d.Get("external_ip").(string),
124123
d.Get("internal_ip").(string),
125124
portString)

builtin/providers/vcd/resource_vcd_dnat_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func testAccCheckVcdDNATExists(n string, gateway *govcd.EdgeGateway) resource.Te
5050
return fmt.Errorf("No DNAT ID is set")
5151
}
5252

53-
conn := testAccProvider.Meta().(*govcd.VCDClient)
53+
conn := testAccProvider.Meta().(*VCDClient)
5454

5555
gatewayName := rs.Primary.Attributes["edge_gateway"]
5656
edgeGateway, err := conn.OrgVdc.FindEdgeGateway(gatewayName)
@@ -79,7 +79,7 @@ func testAccCheckVcdDNATExists(n string, gateway *govcd.EdgeGateway) resource.Te
7979
}
8080

8181
func testAccCheckVcdDNATDestroy(s *terraform.State) error {
82-
conn := testAccProvider.Meta().(*govcd.VCDClient)
82+
conn := testAccProvider.Meta().(*VCDClient)
8383
for _, rs := range s.RootModule().Resources {
8484
if rs.Type != "vcd_dnat" {
8585
continue

builtin/providers/vcd/resource_vcd_firewall_rules.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package vcd
33
import (
44
"fmt"
55
"github.com/hashicorp/terraform/helper/schema"
6-
"github.com/hmrc/vmware-govcd"
76
types "github.com/hmrc/vmware-govcd/types/v56"
87
"log"
98
"strings"
@@ -82,7 +81,7 @@ func resourceVcdFirewallRules() *schema.Resource {
8281
}
8382

8483
func resourceVcdFirewallRulesCreate(d *schema.ResourceData, meta interface{}) error {
85-
vcdClient := meta.(*govcd.VCDClient)
84+
vcdClient := meta.(*VCDClient)
8685
vcdClient.Mutex.Lock()
8786
defer vcdClient.Mutex.Unlock()
8887

@@ -91,7 +90,7 @@ func resourceVcdFirewallRulesCreate(d *schema.ResourceData, meta interface{}) er
9190
return fmt.Errorf("Unable to find edge gateway: %s", err)
9291
}
9392

94-
err = retryCall(5, func() error {
93+
err = retryCall(vcdClient.MaxRetryTimeout, func() error {
9594
edgeGateway.Refresh()
9695
firewallRules, _ := expandFirewallRules(d, edgeGateway.EdgeGateway)
9796
task, err := edgeGateway.CreateFirewallRules(d.Get("default_action").(string), firewallRules)
@@ -112,7 +111,7 @@ func resourceVcdFirewallRulesCreate(d *schema.ResourceData, meta interface{}) er
112111
}
113112

114113
func resourceFirewallRulesDelete(d *schema.ResourceData, meta interface{}) error {
115-
vcdClient := meta.(*govcd.VCDClient)
114+
vcdClient := meta.(*VCDClient)
116115
vcdClient.Mutex.Lock()
117116
defer vcdClient.Mutex.Unlock()
118117

@@ -134,7 +133,7 @@ func resourceFirewallRulesDelete(d *schema.ResourceData, meta interface{}) error
134133
}
135134

136135
func resourceFirewallRulesRead(d *schema.ResourceData, meta interface{}) error {
137-
vcdClient := meta.(*govcd.VCDClient)
136+
vcdClient := meta.(*VCDClient)
138137

139138
edgeGateway, err := vcdClient.OrgVdc.FindEdgeGateway(d.Get("edge_gateway").(string))
140139
if err != nil {

builtin/providers/vcd/resource_vcd_firewall_rules_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func testAccCheckVcdFirewallRulesExists(n string, gateway *govcd.EdgeGateway) re
4444
return fmt.Errorf("No Record ID is set")
4545
}
4646

47-
conn := testAccProvider.Meta().(*govcd.VCDClient)
47+
conn := testAccProvider.Meta().(*VCDClient)
4848

4949
resp, err := conn.OrgVdc.FindEdgeGateway(rs.Primary.ID)
5050
if err != nil {
@@ -77,6 +77,7 @@ func createFirewallRulesConfigs(existingRules *govcd.EdgeGateway) string {
7777
Org: os.Getenv("VCD_ORG"),
7878
Href: os.Getenv("VCD_URL"),
7979
VDC: os.Getenv("VCD_VDC"),
80+
MaxRetryTimeout: 240,
8081
}
8182
conn, err := config.Client()
8283
if err != nil {

builtin/providers/vcd/resource_vcd_network.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"github.com/hashicorp/terraform/helper/hashcode"
99
"github.com/hashicorp/terraform/helper/schema"
10-
"github.com/hmrc/vmware-govcd"
1110
types "github.com/hmrc/vmware-govcd/types/v56"
1211
"strings"
1312
)
@@ -121,7 +120,7 @@ func resourceVcdNetwork() *schema.Resource {
121120
}
122121

123122
func resourceVcdNetworkCreate(d *schema.ResourceData, meta interface{}) error {
124-
vcdClient := meta.(*govcd.VCDClient)
123+
vcdClient := meta.(*VCDClient)
125124
log.Printf("[TRACE] CLIENT: %#v", vcdClient)
126125
vcdClient.Mutex.Lock()
127126
defer vcdClient.Mutex.Unlock()
@@ -156,7 +155,7 @@ func resourceVcdNetworkCreate(d *schema.ResourceData, meta interface{}) error {
156155

157156
log.Printf("[INFO] NETWORK: %#v", newnetwork)
158157

159-
err = retryCall(4, func() error {
158+
err = retryCall(vcdClient.MaxRetryTimeout, func() error {
160159
return vcdClient.OrgVdc.CreateOrgVDCNetwork(newnetwork)
161160
})
162161
if err != nil {
@@ -174,7 +173,7 @@ func resourceVcdNetworkCreate(d *schema.ResourceData, meta interface{}) error {
174173
}
175174

176175
if dhcp, ok := d.GetOk("dhcp_pool"); ok {
177-
err = retryCall(4, func() error {
176+
err = retryCall(vcdClient.MaxRetryTimeout, func() error {
178177
task, err := edgeGateway.AddDhcpPool(network.OrgVDCNetwork, dhcp.(*schema.Set).List())
179178
if err != nil {
180179
return fmt.Errorf("Error adding DHCP pool: %#v", err)
@@ -194,7 +193,7 @@ func resourceVcdNetworkCreate(d *schema.ResourceData, meta interface{}) error {
194193
}
195194

196195
func resourceVcdNetworkRead(d *schema.ResourceData, meta interface{}) error {
197-
vcdClient := meta.(*govcd.VCDClient)
196+
vcdClient := meta.(*VCDClient)
198197
log.Printf("[DEBUG] VCD Client configuration: %#v", vcdClient)
199198
log.Printf("[DEBUG] VCD Client configuration: %#v", vcdClient.OrgVdc)
200199

@@ -226,7 +225,7 @@ func resourceVcdNetworkRead(d *schema.ResourceData, meta interface{}) error {
226225
}
227226

228227
func resourceVcdNetworkDelete(d *schema.ResourceData, meta interface{}) error {
229-
vcdClient := meta.(*govcd.VCDClient)
228+
vcdClient := meta.(*VCDClient)
230229
vcdClient.Mutex.Lock()
231230
defer vcdClient.Mutex.Unlock()
232231
err := vcdClient.OrgVdc.Refresh()
@@ -239,7 +238,7 @@ func resourceVcdNetworkDelete(d *schema.ResourceData, meta interface{}) error {
239238
return fmt.Errorf("Error finding network: %#v", err)
240239
}
241240

242-
err = retryCall(4, func() error {
241+
err = retryCall(vcdClient.MaxRetryTimeout, func() error {
243242
task, err := network.Delete()
244243
if err != nil {
245244
return fmt.Errorf("Error Deleting Network: %#v", err)

builtin/providers/vcd/resource_vcd_network_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func testAccCheckVcdNetworkExists(n string, network *govcd.OrgVDCNetwork) resour
5050
return fmt.Errorf("No VAPP ID is set")
5151
}
5252

53-
conn := testAccProvider.Meta().(*govcd.VCDClient)
53+
conn := testAccProvider.Meta().(*VCDClient)
5454

5555
resp, err := conn.OrgVdc.FindVDCNetwork(rs.Primary.ID)
5656
if err != nil {
@@ -64,7 +64,7 @@ func testAccCheckVcdNetworkExists(n string, network *govcd.OrgVDCNetwork) resour
6464
}
6565

6666
func testAccCheckVcdNetworkDestroy(s *terraform.State) error {
67-
conn := testAccProvider.Meta().(*govcd.VCDClient)
67+
conn := testAccProvider.Meta().(*VCDClient)
6868

6969
for _, rs := range s.RootModule().Resources {
7070
if rs.Type != "vcd_network" {

builtin/providers/vcd/resource_vcd_snat.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package vcd
33
import (
44
"fmt"
55
"github.com/hashicorp/terraform/helper/schema"
6-
"github.com/hmrc/vmware-govcd"
76
)
87

98
func resourceVcdSNAT() *schema.Resource {
@@ -35,7 +34,7 @@ func resourceVcdSNAT() *schema.Resource {
3534
}
3635

3736
func resourceVcdSNATCreate(d *schema.ResourceData, meta interface{}) error {
38-
vcdClient := meta.(*govcd.VCDClient)
37+
vcdClient := meta.(*VCDClient)
3938
// Multiple VCD components need to run operations on the Edge Gateway, as
4039
// the edge gatway will throw back an error if it is already performing an
4140
// operation we must wait until we can aquire a lock on the client
@@ -51,7 +50,7 @@ func resourceVcdSNATCreate(d *schema.ResourceData, meta interface{}) error {
5150
return fmt.Errorf("Unable to find edge gateway: %#v", err)
5251
}
5352

54-
err = retryCall(4, func() error {
53+
err = retryCall(vcdClient.MaxRetryTimeout, func() error {
5554
task, err := edgeGateway.AddNATMapping("SNAT", d.Get("internal_ip").(string),
5655
d.Get("external_ip").(string),
5756
"any")
@@ -69,7 +68,7 @@ func resourceVcdSNATCreate(d *schema.ResourceData, meta interface{}) error {
6968
}
7069

7170
func resourceVcdSNATRead(d *schema.ResourceData, meta interface{}) error {
72-
vcdClient := meta.(*govcd.VCDClient)
71+
vcdClient := meta.(*VCDClient)
7372
e, err := vcdClient.OrgVdc.FindEdgeGateway(d.Get("edge_gateway").(string))
7473

7574
if err != nil {
@@ -94,7 +93,7 @@ func resourceVcdSNATRead(d *schema.ResourceData, meta interface{}) error {
9493
}
9594

9695
func resourceVcdSNATDelete(d *schema.ResourceData, meta interface{}) error {
97-
vcdClient := meta.(*govcd.VCDClient)
96+
vcdClient := meta.(*VCDClient)
9897
// Multiple VCD components need to run operations on the Edge Gateway, as
9998
// the edge gatway will throw back an error if it is already performing an
10099
// operation we must wait until we can aquire a lock on the client
@@ -106,7 +105,7 @@ func resourceVcdSNATDelete(d *schema.ResourceData, meta interface{}) error {
106105
return fmt.Errorf("Unable to find edge gateway: %#v", err)
107106
}
108107

109-
err = retryCall(4, func() error {
108+
err = retryCall(vcdClient.MaxRetryTimeout, func() error {
110109
task, err := edgeGateway.RemoveNATMapping("SNAT", d.Get("internal_ip").(string),
111110
d.Get("external_ip").(string),
112111
"")

builtin/providers/vcd/resource_vcd_snat_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func testAccCheckVcdSNATExists(n string, gateway *govcd.EdgeGateway) resource.Te
5050
return fmt.Errorf("No SNAT ID is set")
5151
}
5252

53-
conn := testAccProvider.Meta().(*govcd.VCDClient)
53+
conn := testAccProvider.Meta().(*VCDClient)
5454

5555
gatewayName := rs.Primary.Attributes["edge_gateway"]
5656
edgeGateway, err := conn.OrgVdc.FindEdgeGateway(gatewayName)
@@ -79,7 +79,7 @@ func testAccCheckVcdSNATExists(n string, gateway *govcd.EdgeGateway) resource.Te
7979
}
8080

8181
func testAccCheckVcdSNATDestroy(s *terraform.State) error {
82-
conn := testAccProvider.Meta().(*govcd.VCDClient)
82+
conn := testAccProvider.Meta().(*VCDClient)
8383
for _, rs := range s.RootModule().Resources {
8484
if rs.Type != "vcd_snat" {
8585
continue

0 commit comments

Comments
 (0)