Skip to content

Commit

Permalink
Merge pull request #1 from hmrc/timeout
Browse files Browse the repository at this point in the history
 Make maxRetryTimeout (in seconds) configurable
  • Loading branch information
devopsbrett committed Nov 23, 2015
2 parents 815ff7a + 3809315 commit 5dde514
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 65 deletions.
22 changes: 15 additions & 7 deletions builtin/providers/vcd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@ import (
)

type Config struct {
User string
Password string
Org string
Href string
VDC string
User string
Password string
Org string
Href string
VDC string
MaxRetryTimeout int
}

func (c *Config) Client() (*govcd.VCDClient, error) {
type VCDClient struct {
*govcd.VCDClient
MaxRetryTimeout int
}

func (c *Config) Client() (*VCDClient, error) {
u, err := url.ParseRequestURI(c.Href)
if err != nil {
return nil, fmt.Errorf("Something went wrong: %s", err)
}

vcdclient := govcd.NewVCDClient(*u)
vcdclient := &VCDClient{
govcd.NewVCDClient(*u),
c.MaxRetryTimeout}
org, vcd, err := vcdclient.Authenticate(c.User, c.Password, c.Org, c.VDC)
if err != nil {
return nil, fmt.Errorf("Something went wrong: %s", err)
Expand Down
19 changes: 14 additions & 5 deletions builtin/providers/vcd/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,20 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("VCD_URL", nil),
Description: "The vcd url for vcd API operations.",
},

"vdc": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCD_VDC", ""),
Description: "The name of the VDC to run operations on",
},

"maxRetryTimeout": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("VCD_MAX_RETRY_TIMEOUT", 60),
Description: "Max num seconds to wait for successful response when operating on resources within vCloud (defaults to 60)",
},
},

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

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := Config{
User: d.Get("user").(string),
Password: d.Get("password").(string),
Org: d.Get("org").(string),
Href: d.Get("url").(string),
VDC: d.Get("vdc").(string),
User: d.Get("user").(string),
Password: d.Get("password").(string),
Org: d.Get("org").(string),
Href: d.Get("url").(string),
VDC: d.Get("vdc").(string),
MaxRetryTimeout: d.Get("maxRetryTimeout").(int),
}

return config.Client()
Expand Down
11 changes: 5 additions & 6 deletions builtin/providers/vcd/resource_vcd_dnat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vcd
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hmrc/vmware-govcd"
)

func resourceVcdDNAT() *schema.Resource {
Expand Down Expand Up @@ -41,7 +40,7 @@ func resourceVcdDNAT() *schema.Resource {
}

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

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

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

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

func resourceVcdDNATDelete(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*govcd.VCDClient)
vcdClient := meta.(*VCDClient)
// Multiple VCD components need to run operations on the Edge Gateway, as
// the edge gatway will throw back an error if it is already performing an
// operation we must wait until we can aquire a lock on the client
Expand All @@ -119,7 +118,7 @@ func resourceVcdDNATDelete(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return fmt.Errorf("Unable to find edge gateway: %#v", err)
}
err = retryCall(4, func() error {
err = retryCall(vcdClient.MaxRetryTimeout, func() error {
task, err := edgeGateway.RemoveNATMapping("DNAT", d.Get("external_ip").(string),
d.Get("internal_ip").(string),
portString)
Expand Down
4 changes: 2 additions & 2 deletions builtin/providers/vcd/resource_vcd_dnat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func testAccCheckVcdDNATExists(n string, gateway *govcd.EdgeGateway) resource.Te
return fmt.Errorf("No DNAT ID is set")
}

conn := testAccProvider.Meta().(*govcd.VCDClient)
conn := testAccProvider.Meta().(*VCDClient)

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

func testAccCheckVcdDNATDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*govcd.VCDClient)
conn := testAccProvider.Meta().(*VCDClient)
for _, rs := range s.RootModule().Resources {
if rs.Type != "vcd_dnat" {
continue
Expand Down
9 changes: 4 additions & 5 deletions builtin/providers/vcd/resource_vcd_firewall_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vcd
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hmrc/vmware-govcd"
types "github.com/hmrc/vmware-govcd/types/v56"
"log"
"strings"
Expand Down Expand Up @@ -82,7 +81,7 @@ func resourceVcdFirewallRules() *schema.Resource {
}

func resourceVcdFirewallRulesCreate(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*govcd.VCDClient)
vcdClient := meta.(*VCDClient)
vcdClient.Mutex.Lock()
defer vcdClient.Mutex.Unlock()

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

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

func resourceFirewallRulesDelete(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*govcd.VCDClient)
vcdClient := meta.(*VCDClient)
vcdClient.Mutex.Lock()
defer vcdClient.Mutex.Unlock()

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

func resourceFirewallRulesRead(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*govcd.VCDClient)
vcdClient := meta.(*VCDClient)

edgeGateway, err := vcdClient.OrgVdc.FindEdgeGateway(d.Get("edge_gateway").(string))
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion builtin/providers/vcd/resource_vcd_firewall_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func testAccCheckVcdFirewallRulesExists(n string, gateway *govcd.EdgeGateway) re
return fmt.Errorf("No Record ID is set")
}

conn := testAccProvider.Meta().(*govcd.VCDClient)
conn := testAccProvider.Meta().(*VCDClient)

resp, err := conn.OrgVdc.FindEdgeGateway(rs.Primary.ID)
if err != nil {
Expand Down Expand Up @@ -77,6 +77,7 @@ func createFirewallRulesConfigs(existingRules *govcd.EdgeGateway) string {
Org: os.Getenv("VCD_ORG"),
Href: os.Getenv("VCD_URL"),
VDC: os.Getenv("VCD_VDC"),
MaxRetryTimeout: 240,
}
conn, err := config.Client()
if err != nil {
Expand Down
13 changes: 6 additions & 7 deletions builtin/providers/vcd/resource_vcd_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hmrc/vmware-govcd"
types "github.com/hmrc/vmware-govcd/types/v56"
"strings"
)
Expand Down Expand Up @@ -121,7 +120,7 @@ func resourceVcdNetwork() *schema.Resource {
}

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

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

err = retryCall(4, func() error {
err = retryCall(vcdClient.MaxRetryTimeout, func() error {
return vcdClient.OrgVdc.CreateOrgVDCNetwork(newnetwork)
})
if err != nil {
Expand All @@ -174,7 +173,7 @@ func resourceVcdNetworkCreate(d *schema.ResourceData, meta interface{}) error {
}

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

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

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

func resourceVcdNetworkDelete(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*govcd.VCDClient)
vcdClient := meta.(*VCDClient)
vcdClient.Mutex.Lock()
defer vcdClient.Mutex.Unlock()
err := vcdClient.OrgVdc.Refresh()
Expand All @@ -239,7 +238,7 @@ func resourceVcdNetworkDelete(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error finding network: %#v", err)
}

err = retryCall(4, func() error {
err = retryCall(vcdClient.MaxRetryTimeout, func() error {
task, err := network.Delete()
if err != nil {
return fmt.Errorf("Error Deleting Network: %#v", err)
Expand Down
4 changes: 2 additions & 2 deletions builtin/providers/vcd/resource_vcd_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func testAccCheckVcdNetworkExists(n string, network *govcd.OrgVDCNetwork) resour
return fmt.Errorf("No VAPP ID is set")
}

conn := testAccProvider.Meta().(*govcd.VCDClient)
conn := testAccProvider.Meta().(*VCDClient)

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

func testAccCheckVcdNetworkDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*govcd.VCDClient)
conn := testAccProvider.Meta().(*VCDClient)

for _, rs := range s.RootModule().Resources {
if rs.Type != "vcd_network" {
Expand Down
11 changes: 5 additions & 6 deletions builtin/providers/vcd/resource_vcd_snat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vcd
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hmrc/vmware-govcd"
)

func resourceVcdSNAT() *schema.Resource {
Expand Down Expand Up @@ -35,7 +34,7 @@ func resourceVcdSNAT() *schema.Resource {
}

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

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

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

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

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

err = retryCall(4, func() error {
err = retryCall(vcdClient.MaxRetryTimeout, func() error {
task, err := edgeGateway.RemoveNATMapping("SNAT", d.Get("internal_ip").(string),
d.Get("external_ip").(string),
"")
Expand Down
4 changes: 2 additions & 2 deletions builtin/providers/vcd/resource_vcd_snat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func testAccCheckVcdSNATExists(n string, gateway *govcd.EdgeGateway) resource.Te
return fmt.Errorf("No SNAT ID is set")
}

conn := testAccProvider.Meta().(*govcd.VCDClient)
conn := testAccProvider.Meta().(*VCDClient)

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

func testAccCheckVcdSNATDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*govcd.VCDClient)
conn := testAccProvider.Meta().(*VCDClient)
for _, rs := range s.RootModule().Resources {
if rs.Type != "vcd_snat" {
continue
Expand Down

0 comments on commit 5dde514

Please sign in to comment.