Locking on the Subnet's and Virtual Network's given only one NIC in a subnet can be modified at once

This commit is contained in:
tombuildsstuff 2017-05-10 13:09:53 +01:00 committed by Paul Stack
parent 58ce4a7223
commit 3e23e94c39
2 changed files with 44 additions and 5 deletions

View File

@ -200,16 +200,19 @@ func resourceArmNetworkInterfaceCreate(d *schema.ResourceData, meta interface{})
if hasNameLabel {
name_label := nameLabel.(string)
ifaceDnsSettings.InternalDNSNameLabel = &name_label
}
properties.DNSSettings = &ifaceDnsSettings
}
ipConfigs, sgErr := expandAzureRmNetworkInterfaceIpConfigurations(d)
ipConfigs, namesToLock, sgErr := expandAzureRmNetworkInterfaceIpConfigurations(d)
if sgErr != nil {
return fmt.Errorf("Error Building list of Network Interface IP Configurations: %s", sgErr)
}
azureRMLockMultiple(namesToLock)
defer azureRMUnlockMultiple(namesToLock)
if len(ipConfigs) > 0 {
properties.IPConfigurations = &ipConfigs
}
@ -327,6 +330,26 @@ func resourceArmNetworkInterfaceDelete(d *schema.ResourceData, meta interface{})
defer armMutexKV.Unlock(networkSecurityGroupName)
}
configs := d.Get("ip_configuration").(*schema.Set).List()
namesToLock := make([]string, 0)
for _, configRaw := range configs {
data := configRaw.(map[string]interface{})
subnet_id := data["subnet_id"].(string)
subnetId, err := parseAzureResourceID(subnet_id)
if err != nil {
return err
}
subnetName := subnetId.Path["subnets"]
virtualNetworkName := subnetId.Path["virtualNetworks"]
namesToLock = append(namesToLock, subnetName)
namesToLock = append(namesToLock, virtualNetworkName)
}
azureRMLockMultiple(&namesToLock)
defer azureRMUnlockMultiple(&namesToLock)
_, err = ifaceClient.Delete(resGroup, name, make(chan struct{}))
return err
@ -373,9 +396,10 @@ func validateNetworkInterfacePrivateIpAddressAllocation(v interface{}, k string)
return
}
func expandAzureRmNetworkInterfaceIpConfigurations(d *schema.ResourceData) ([]network.InterfaceIPConfiguration, error) {
func expandAzureRmNetworkInterfaceIpConfigurations(d *schema.ResourceData) ([]network.InterfaceIPConfiguration, *[]string, error) {
configs := d.Get("ip_configuration").(*schema.Set).List()
ipConfigs := make([]network.InterfaceIPConfiguration, 0, len(configs))
namesToLock := make([]string, 0)
for _, configRaw := range configs {
data := configRaw.(map[string]interface{})
@ -390,7 +414,7 @@ func expandAzureRmNetworkInterfaceIpConfigurations(d *schema.ResourceData) ([]ne
case "static":
allocationMethod = network.Static
default:
return []network.InterfaceIPConfiguration{}, fmt.Errorf(
return []network.InterfaceIPConfiguration{}, nil, fmt.Errorf(
"valid values for private_ip_allocation_method are 'dynamic' and 'static' - got '%s'",
private_ip_allocation_method)
}
@ -402,6 +426,15 @@ func expandAzureRmNetworkInterfaceIpConfigurations(d *schema.ResourceData) ([]ne
PrivateIPAllocationMethod: allocationMethod,
}
subnetId, err := parseAzureResourceID(subnet_id)
if err != nil {
return nil, nil, err
}
subnetName := subnetId.Path["subnets"]
virtualNetworkName := subnetId.Path["virtualNetworks"]
namesToLock = append(namesToLock, subnetName)
namesToLock = append(namesToLock, virtualNetworkName)
if v := data["private_ip_address"].(string); v != "" {
properties.PrivateIPAddress = &v
}
@ -451,5 +484,5 @@ func expandAzureRmNetworkInterfaceIpConfigurations(d *schema.ResourceData) ([]ne
ipConfigs = append(ipConfigs, ipConfig)
}
return ipConfigs, nil
return ipConfigs, &namesToLock, nil
}

View File

@ -77,6 +77,9 @@ func resourceArmSubnetCreate(d *schema.ResourceData, meta interface{}) error {
resGroup := d.Get("resource_group_name").(string)
addressPrefix := d.Get("address_prefix").(string)
armMutexKV.Lock(name)
defer armMutexKV.Unlock(name)
armMutexKV.Lock(vnetName)
defer armMutexKV.Unlock(vnetName)
@ -223,6 +226,9 @@ func resourceArmSubnetDelete(d *schema.ResourceData, meta interface{}) error {
armMutexKV.Lock(vnetName)
defer armMutexKV.Unlock(vnetName)
armMutexKV.Lock(name)
defer armMutexKV.Unlock(name)
_, err = subnetClient.Delete(resGroup, vnetName, name, make(chan struct{}))
return err