Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Kleinsasser <mario.kleinsasser@gmail.com>

Change fmt

Signed-off-by: Mario Kleinsasser <mario.kleinsasser@gmail.com>

Correct lint

Signed-off-by: Mario Kleinsasser <mario.kleinsasser@gmail.com>

Change the subnet check logic

Signed-off-by: Mario Kleinsasser <mario.kleinsasser@gmail.com>
  • Loading branch information
m4r10k authored and shin- committed Oct 13, 2017
1 parent 295730c commit 83812d4
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions drivers/azure/azureutil/azureutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,23 +232,40 @@ func (a AzureClient) GetSubnet(resourceGroup, virtualNetwork, name string) (netw
return a.subnetsClient().Get(resourceGroup, virtualNetwork, name, "")
}

// CreateSubnet creates or updates a subnet if it does not already exist.
func (a AzureClient) CreateSubnet(ctx *DeploymentContext, resourceGroup, virtualNetwork, name, subnetPrefix string) error {
log.Info("Configuring subnet.", logutil.Fields{
"name": name,
"vnet": virtualNetwork,
"cidr": subnetPrefix})
_, err := a.subnetsClient().CreateOrUpdate(resourceGroup, virtualNetwork, name,
network.Subnet{
Properties: &network.SubnetPropertiesFormat{
AddressPrefix: to.StringPtr(subnetPrefix),
},
}, nil)
if err != nil {
subnet, err := a.GetSubnet(resourceGroup, virtualNetwork, name)
if err == nil {
log.Info("Subnet already exists.")
ctx.SubnetID = to.String(subnet.ID)
return err
}
subnet, err := a.subnetsClient().Get(resourceGroup, virtualNetwork, name, "")
ctx.SubnetID = to.String(subnet.ID)

// If the subnet is not found, create it
if err.(autorest.DetailedError).StatusCode == 404 {
log.Info("Configuring subnet.", logutil.Fields{
"name": name,
"vnet": virtualNetwork,
"cidr": subnetPrefix})
_, err = a.subnetsClient().CreateOrUpdate(resourceGroup, virtualNetwork, name,
network.Subnet{
Properties: &network.SubnetPropertiesFormat{
AddressPrefix: to.StringPtr(subnetPrefix),
},
}, nil)

if err != nil {
return err
}

subnet, err = a.subnetsClient().Get(resourceGroup, virtualNetwork, name, "")
ctx.SubnetID = to.String(subnet.ID)
return err
}

log.Warn("Create subnet operation error %v: ", err)
return err

}

// CleanupSubnetIfExists removes a subnet if there are no IP configurations
Expand Down

0 comments on commit 83812d4

Please sign in to comment.