Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutex change #255

Merged
merged 33 commits into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
cb6e118
Add initial Mutex change
vbauzys May 31, 2019
5dd310c
Improvements
vbauzys Jun 3, 2019
72816cd
Improvements
vbauzys Jun 3, 2019
5d13a51
Added logging and improved key construction
vbauzys Jun 3, 2019
e754201
Added lock for vm update
vbauzys Jun 3, 2019
fb8ec5f
Improve imports
vbauzys Jun 3, 2019
f015028
Improvements
vbauzys Jun 3, 2019
0ebe81d
Add missing dependency
vbauzys Jun 3, 2019
4cc5f27
fix mistype
vbauzys Jun 3, 2019
836c4a3
Removed additional retry call
vbauzys Jun 3, 2019
5c18736
Improvement
vbauzys Jun 4, 2019
280890a
Improvement
vbauzys Jun 4, 2019
1891e7b
Fix test
vbauzys Jun 4, 2019
eec8035
Test improvements
vbauzys Jun 4, 2019
7dd5da4
Add improvements
vbauzys Jun 5, 2019
a1bed33
Bump govcd version and add networks detach from VAPP before delete
vbauzys Jun 5, 2019
30041c7
Remove not need code
vbauzys Jun 5, 2019
94e9d7d
Improve test
vbauzys Jun 5, 2019
480536d
fix mistypes
vbauzys Jun 5, 2019
b66f6bc
Improve comment
vbauzys Jun 5, 2019
c84aae9
Remove local env specifics
vbauzys Jun 5, 2019
30b9f24
Add needed depends on
vbauzys Jun 5, 2019
9f68eb2
Add needed depends on
vbauzys Jun 5, 2019
4fa08c3
Fix for race condition with deleting vm and depending independent disk
vbauzys Jun 6, 2019
998776c
Improve locking - fix issue with not exisiting inheritance
vbauzys Jun 6, 2019
9f77106
Moved detach disk after power off
vbauzys Jun 6, 2019
253516f
Rename function names
vbauzys Jun 6, 2019
82fdc30
Add checks for errors
vbauzys Jun 6, 2019
fcca995
Improve comment
vbauzys Jun 7, 2019
f39bb62
Added custom locks for vapp network
vbauzys Jun 7, 2019
2f738a4
Added custom locks for insert media, cause impacts vApp
vbauzys Jun 7, 2019
63430f8
Improved error messages
vbauzys Jun 7, 2019
94bb963
Improved error messages
vbauzys Jun 7, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ FEATURES:
* `vcd_vapp_vm` - Ability to add metadata to a VM. For previous behaviour please see `BACKWARDS INCOMPATIBILITIES` ([#158](https://github.com/terraform-providers/terraform-provider-vcd/issues/158))
* `vcd_vapp_vm` - Ability to enable hardware assisted CPU virtualization for VM. It allows hypervisor nesting. ([#219](https://github.com/terraform-providers/terraform-provider-vcd/issues/219))
* **New Resource:** external network - `vcd_external_network` - ([#230](https://github.com/terraform-providers/terraform-provider-vcd/issues/230))
* **New Resource:** VDC resource `vcd_org_vdc` - ([#234](https://github.com/terraform-providers/terraform-provider-vcd/issues/234))
* **New Resource:** VDC resource `vcd_org_vdc` - ([#236](https://github.com/terraform-providers/terraform-provider-vcd/issues/236))
* resource/vcd_vapp_vm: Add `network` argument for multiple NIC support and more flexible configuration ([#233](https://github.com/terraform-providers/terraform-provider-vcd/issues/233))
* resource/vcd_vapp_vm: Add `mac` argument to store MAC address in state file ([#233](https://github.com/terraform-providers/terraform-provider-vcd/issues/233))

Expand Down
28 changes: 28 additions & 0 deletions vcd/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vcd

import (
"github.com/hashicorp/terraform/helper/mutexkv"
dataclouder marked this conversation as resolved.
Show resolved Hide resolved
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/vmware/go-vcloud-director/v2/util"
Expand Down Expand Up @@ -108,6 +109,33 @@ func Provider() terraform.ResourceProvider {
}
}

// This is a global MutexKV for all resources
var vcdMutexKV = mutexkv.NewMutexKV()
Didainius marked this conversation as resolved.
Show resolved Hide resolved

func lockVapp(d *schema.ResourceData) {
lvirbalas marked this conversation as resolved.
Show resolved Hide resolved
vcdMutexKV.Lock(d.Get("vdc").(string) + d.Get("name").(string))
Didainius marked this conversation as resolved.
Show resolved Hide resolved
dataclouder marked this conversation as resolved.
Show resolved Hide resolved
}

func unLockVapp(d *schema.ResourceData) {
vcdMutexKV.Unlock(d.Get("vdc").(string) + d.Get("name").(string))
}

func lockParentVapp(d *schema.ResourceData) {
vcdMutexKV.Lock(d.Get("vdc").(string) + d.Get("vapp_name").(string))
}

func unLockParentVapp(d *schema.ResourceData) {
vcdMutexKV.Unlock(d.Get("vdc").(string) + d.Get("vapp_name").(string))
}

func lockParentEdgeGtw(d *schema.ResourceData) {
vcdMutexKV.Lock(d.Get("vdc").(string) + d.Get("edge_gateway").(string))
}

func unLockParentEdgeGtw(d *schema.ResourceData) {
vcdMutexKV.Unlock(d.Get("vdc").(string) + d.Get("edge_gateway").(string))
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
maxRetryTimeout := d.Get("max_retry_timeout").(int)

Expand Down
78 changes: 35 additions & 43 deletions vcd/resource_vcd_dnat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"strings"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/vmware/go-vcloud-director/v2/types/v56"
Expand Down Expand Up @@ -90,8 +89,9 @@ func resourceVcdDNATCreate(d *schema.ResourceData, meta interface{}) error {
// Multiple VCD components need to run operations on the Edge Gateway, as
// the edge gateway will throw back an error if it is already performing an
// operation we must wait until we can acquire a lock on the client
vcdClient.Mutex.Lock()
defer vcdClient.Mutex.Unlock()
lockParentEdgeGtw(d)
defer unLockParentEdgeGtw(d)

portString := getPortString(d.Get("port").(int))
translatedPortString := portString // default
if d.Get("translated_port").(int) > 0 {
Expand Down Expand Up @@ -128,36 +128,30 @@ func resourceVcdDNATCreate(d *schema.ResourceData, meta interface{}) error {
// 3 seconds and then try again. Continue until a non-busy error or success

if nil != providedNetworkName && providedNetworkName != "" {
err = retryCall(vcdClient.MaxRetryTimeout, func() *resource.RetryError {
task, err := edgeGateway.AddNATPortMappingWithUplink(orgVdcnetwork, "DNAT",
d.Get("external_ip").(string),
portString,
d.Get("internal_ip").(string),
translatedPortString, protocol,
icmpSubType)
if err != nil {
return resource.RetryableError(
fmt.Errorf("error setting DNAT rules: %#v", err))
}
task, err := edgeGateway.AddNATPortMappingWithUplink(orgVdcnetwork, "DNAT",
d.Get("external_ip").(string),
portString,
d.Get("internal_ip").(string),
translatedPortString, protocol,
icmpSubType)
if err != nil {
return fmt.Errorf("error setting DNAT rules: %#v", err)
}

return resource.RetryableError(task.WaitTaskCompletion())
})
err = task.WaitTaskCompletion()
dataclouder marked this conversation as resolved.
Show resolved Hide resolved
} else {
// TODO remove when major release is done
err = retryCall(vcdClient.MaxRetryTimeout, func() *resource.RetryError {
task, err := edgeGateway.AddNATPortMapping("DNAT",
d.Get("external_ip").(string),
portString,
d.Get("internal_ip").(string),
translatedPortString, protocol,
icmpSubType)
if err != nil {
return resource.RetryableError(
fmt.Errorf("error setting DNAT rules: %#v", err))
}
task, err := edgeGateway.AddNATPortMapping("DNAT",
d.Get("external_ip").(string),
portString,
d.Get("internal_ip").(string),
translatedPortString, protocol,
icmpSubType)
if err != nil {
return fmt.Errorf("error setting DNAT rules: %#v", err)
}

return resource.RetryableError(task.WaitTaskCompletion())
})
err = task.WaitTaskCompletion()
}

if err != nil {
Expand Down Expand Up @@ -218,8 +212,9 @@ func resourceVcdDNATDelete(d *schema.ResourceData, meta interface{}) error {
// 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
vcdClient.Mutex.Lock()
defer vcdClient.Mutex.Unlock()
lockParentEdgeGtw(d)
defer unLockParentEdgeGtw(d)

portString := getPortString(d.Get("port").(int))
translatedPortString := portString // default
if d.Get("translated_port").(int) > 0 {
Expand All @@ -231,19 +226,16 @@ func resourceVcdDNATDelete(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return fmt.Errorf(errorUnableToFindEdgeGateway, err)
}
err = retryCall(vcdClient.MaxRetryTimeout, func() *resource.RetryError {
task, err := edgeGateway.RemoveNATPortMapping("DNAT",
d.Get("external_ip").(string),
portString,
d.Get("internal_ip").(string),
translatedPortString)
if err != nil {
return resource.RetryableError(
fmt.Errorf("error setting DNAT rules: %#v", err))
}
task, err := edgeGateway.RemoveNATPortMapping("DNAT",
d.Get("external_ip").(string),
portString,
d.Get("internal_ip").(string),
translatedPortString)
if err != nil {
return fmt.Errorf("error setting DNAT rules: %#v", err)
}

return resource.RetryableError(task.WaitTaskCompletion())
})
err = task.WaitTaskCompletion()
if err != nil {
return fmt.Errorf("error completing tasks: %#v", err)
}
Expand Down
61 changes: 26 additions & 35 deletions vcd/resource_vcd_edgegateway_vpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"strconv"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)
Expand Down Expand Up @@ -142,8 +141,8 @@ func resourceVcdEdgeGatewayVpnCreate(d *schema.ResourceData, meta interface{}) e
vcdClient := meta.(*VCDClient)
log.Printf("[TRACE] CLIENT: %#v", vcdClient)

vcdClient.Mutex.Lock()
defer vcdClient.Mutex.Unlock()
lockParentEdgeGtw(d)
defer unLockParentEdgeGtw(d)

edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d)
if err != nil {
Expand Down Expand Up @@ -206,22 +205,18 @@ func resourceVcdEdgeGatewayVpnCreate(d *schema.ResourceData, meta interface{}) e

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

err = retryCall(vcdClient.MaxRetryTimeout, func() *resource.RetryError {
err := edgeGateway.Refresh()
if err != nil {
log.Printf("[INFO] Error refreshing edge gateway: %#v", err)
return resource.RetryableError(
fmt.Errorf("error error refreshing edge gateway: %#v", err))
}
task, err := edgeGateway.AddIpsecVPN(ipsecVPNConfig)
if err != nil {
log.Printf("[INFO] Error setting ipsecVPNConfig rules: %s", err)
return resource.RetryableError(
fmt.Errorf("error setting ipsecVPNConfig rules: %#v", err))
}
err = edgeGateway.Refresh()
if err != nil {
log.Printf("[INFO] Error refreshing edge gateway: %#v", err)
return fmt.Errorf("error error refreshing edge gateway: %#v", err)
lvirbalas marked this conversation as resolved.
Show resolved Hide resolved
}
task, err := edgeGateway.AddIpsecVPN(ipsecVPNConfig)
if err != nil {
log.Printf("[INFO] Error setting ipsecVPNConfig rules: %s", err)
return fmt.Errorf("error setting ipsecVPNConfig rules: %#v", err)
}

return resource.RetryableError(task.WaitTaskCompletion())
})
err = task.WaitTaskCompletion()
if err != nil {
return fmt.Errorf(errorCompletingTask, err)
}
Expand All @@ -236,8 +231,8 @@ func resourceVcdEdgeGatewayVpnDelete(d *schema.ResourceData, meta interface{}) e

log.Printf("[TRACE] CLIENT: %#v", vcdClient)

vcdClient.Mutex.Lock()
defer vcdClient.Mutex.Unlock()
lockParentEdgeGtw(d)
defer unLockParentEdgeGtw(d)

edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d)
if err != nil {
Expand All @@ -253,22 +248,18 @@ func resourceVcdEdgeGatewayVpnDelete(d *schema.ResourceData, meta interface{}) e

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

err = retryCall(vcdClient.MaxRetryTimeout, func() *resource.RetryError {
err := edgeGateway.Refresh()
if err != nil {
log.Printf("[INFO] Error refreshing edge gateway: %#v", err)
return resource.RetryableError(
fmt.Errorf("error error refreshing edge gateway: %#v", err))
}
task, err := edgeGateway.AddIpsecVPN(ipsecVPNConfig)
if err != nil {
log.Printf("[INFO] Error setting ipsecVPNConfig rules: %s", err)
return resource.RetryableError(
fmt.Errorf("error setting ipsecVPNConfig rules: %#v", err))
}
err = edgeGateway.Refresh()
if err != nil {
log.Printf("[INFO] Error refreshing edge gateway: %#v", err)
return fmt.Errorf("error error refreshing edge gateway: %#v", err)
lvirbalas marked this conversation as resolved.
Show resolved Hide resolved
}
task, err := edgeGateway.AddIpsecVPN(ipsecVPNConfig)
if err != nil {
log.Printf("[INFO] Error setting ipsecVPNConfig rules: %s", err)
return fmt.Errorf("error setting ipsecVPNConfig rules: %#v", err)
}

return resource.RetryableError(task.WaitTaskCompletion())
})
err = task.WaitTaskCompletion()
if err != nil {
return fmt.Errorf(errorCompletingTask, err)
}
Expand Down
37 changes: 16 additions & 21 deletions vcd/resource_vcd_firewall_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"strings"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)
Expand Down Expand Up @@ -96,8 +95,8 @@ func resourceVcdFirewallRules() *schema.Resource {
func resourceVcdFirewallRulesCreate(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*VCDClient)

vcdClient.Mutex.Lock()
defer vcdClient.Mutex.Unlock()
lockParentEdgeGtw(d)
defer unLockParentEdgeGtw(d)

edgeGatewayName := d.Get("edge_gateway").(string)

Expand All @@ -106,23 +105,19 @@ func resourceVcdFirewallRulesCreate(d *schema.ResourceData, meta interface{}) er
return fmt.Errorf(errorUnableToFindEdgeGateway, err)
}

err = retryCall(vcdClient.MaxRetryTimeout, func() *resource.RetryError {
err := edgeGateway.Refresh()
if err != nil {
log.Printf("[INFO] Error refreshing edge gateway: %#v", err)
return resource.RetryableError(
fmt.Errorf("error error refreshing edge gateway: %#v", err))
}
firewallRules, _ := expandFirewallRules(d, edgeGateway.EdgeGateway)
task, err := edgeGateway.CreateFirewallRules(d.Get("default_action").(string), firewallRules)
if err != nil {
log.Printf("[INFO] Error setting firewall rules: %s", err)
return resource.RetryableError(
fmt.Errorf("error setting firewall rules: %#v", err))
}
err = edgeGateway.Refresh()
if err != nil {
log.Printf("[INFO] Error refreshing edge gateway: %#v", err)
return fmt.Errorf("error error refreshing edge gateway: %#v", err)
lvirbalas marked this conversation as resolved.
Show resolved Hide resolved
}
firewallRules, _ := expandFirewallRules(d, edgeGateway.EdgeGateway)
task, err := edgeGateway.CreateFirewallRules(d.Get("default_action").(string), firewallRules)
if err != nil {
log.Printf("[INFO] Error setting firewall rules: %s", err)
return fmt.Errorf("error setting firewall rules: %#v", err)
}

return resource.RetryableError(task.WaitTaskCompletion())
})
err = task.WaitTaskCompletion()
if err != nil {
return fmt.Errorf(errorCompletingTask, err)
}
Expand All @@ -135,8 +130,8 @@ func resourceVcdFirewallRulesCreate(d *schema.ResourceData, meta interface{}) er
func resourceFirewallRulesDelete(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*VCDClient)

vcdClient.Mutex.Lock()
defer vcdClient.Mutex.Unlock()
lockParentEdgeGtw(d)
defer unLockParentEdgeGtw(d)

edgeGateway, err := vcdClient.GetEdgeGatewayFromResource(d)
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions vcd/resource_vcd_network_direct.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/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/vmware/go-vcloud-director/v2/govcd"
"github.com/vmware/go-vcloud-director/v2/types/v56"
Expand Down Expand Up @@ -57,8 +56,6 @@ func resourceVcdNetworkDirect() *schema.Resource {

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

_, vdc, err := vcdClient.GetOrgAndVdcFromResource(d)
if err != nil {
Expand Down Expand Up @@ -93,9 +90,7 @@ func resourceVcdNetworkDirectCreate(d *schema.ResourceData, meta interface{}) er
IsShared: d.Get("shared").(bool),
}

err = retryCall(vcdClient.MaxRetryTimeout, func() *resource.RetryError {
return resource.RetryableError(vdc.CreateOrgVDCNetworkWait(orgVDCNetwork))
})
err = vdc.CreateOrgVDCNetworkWait(orgVDCNetwork)
Didainius marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("error: %#v", err)
}
Expand Down
7 changes: 1 addition & 6 deletions vcd/resource_vcd_network_isolated.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/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)
Expand Down Expand Up @@ -135,8 +134,6 @@ func resourceVcdNetworkIsolated() *schema.Resource {

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

_, vdc, err := vcdClient.GetOrgAndVdcFromResource(d)
if err != nil {
Expand Down Expand Up @@ -172,9 +169,7 @@ func resourceVcdNetworkIsolatedCreate(d *schema.ResourceData, meta interface{})
IsShared: d.Get("shared").(bool),
}

err = retryCall(vcdClient.MaxRetryTimeout, func() *resource.RetryError {
return resource.RetryableError(vdc.CreateOrgVDCNetworkWait(orgVDCNetwork))
})
err = vdc.CreateOrgVDCNetworkWait(orgVDCNetwork)
if err != nil {
return fmt.Errorf("error: %#v", err)
}
Expand Down
Loading