diff --git a/pkg/resourcemanager/vnet/reconcile.go b/pkg/resourcemanager/vnet/reconcile.go index 81355dd3079..931147dbfd7 100644 --- a/pkg/resourcemanager/vnet/reconcile.go +++ b/pkg/resourcemanager/vnet/reconcile.go @@ -29,18 +29,21 @@ func (g *AzureVNetManager) Ensure(ctx context.Context, obj runtime.Object, opts addressSpace := instance.Spec.AddressSpace subnets := instance.Spec.Subnets - instance.Status.Provisioning = true - instance.Status.Provisioned = false - // check first to see if the VNet exists, if it does, dont create it and // consider the reconcilliation successful - if exists, _ := g.VNetExists(ctx, resourceGroup, resourceName); exists { + vNet, err := g.GetVNet(ctx, resourceGroup, resourceName) + if err == nil { + // succeeded! end reconcilliation successfully instance.Status.Provisioning = false instance.Status.Provisioned = true instance.Status.Message = resourcemanager.SuccessMsg + instance.Status.ResourceId = *vNet.ID return true, nil } + instance.Status.Provisioning = true + instance.Status.Provisioned = false + _, err = g.CreateVNet( ctx, location, @@ -84,13 +87,7 @@ func (g *AzureVNetManager) Ensure(ctx context.Context, obj runtime.Object, opts return false, fmt.Errorf("Error creating VNet: %s, %s - %v", resourceGroup, resourceName, err) } - // success - instance.Status.Message = resourcemanager.SuccessMsg - instance.Status.Provisioning = false - instance.Status.Provisioned = true - instance.Status.Message = resourcemanager.SuccessMsg - - return true, nil + return false, nil } // Delete makes sure that the VNet has been deleted diff --git a/pkg/resourcemanager/vnet/vnet.go b/pkg/resourcemanager/vnet/vnet.go index 7dd91ac934c..00e43f907d4 100644 --- a/pkg/resourcemanager/vnet/vnet.go +++ b/pkg/resourcemanager/vnet/vnet.go @@ -92,23 +92,12 @@ func (_ *AzureVNetManager) DeleteVNet(ctx context.Context, resourceGroupName str return future.Result(client) } -// VNetExists checks to see if a VNet exists -func (_ *AzureVNetManager) VNetExists(ctx context.Context, resourceGroupName string, resourceName string) (bool, error) { +// GetVNet gets a VNet +func (v *AzureVNetManager) GetVNet(ctx context.Context, resourceGroupName string, resourceName string) (vNet vnetwork.VirtualNetwork, err error) { client, err := getVNetClient() if err != nil { - return false, err - } - - result, err := client.Get( - ctx, - resourceGroupName, - resourceName, - "") - if err != nil { - return false, err - } else if result.Name == nil { - return false, nil + return vnetwork.VirtualNetwork{}, err } - return true, nil + return client.Get(ctx, resourceGroupName, resourceName, "") } diff --git a/pkg/resourcemanager/vnet/vnet_manager.go b/pkg/resourcemanager/vnet/vnet_manager.go index dd9bd6b8444..246d4986bfe 100644 --- a/pkg/resourcemanager/vnet/vnet_manager.go +++ b/pkg/resourcemanager/vnet/vnet_manager.go @@ -30,9 +30,9 @@ type VNetManager interface { resourceGroupName string, resourceName string) (autorest.Response, error) - VNetExists(ctx context.Context, + GetVNet(ctx context.Context, resourceGroupName string, - resourceName string) (bool, error) + resourceName string) (vnetwork.VirtualNetwork, error) // also embed async client methods resourcemanager.ARMClient diff --git a/pkg/resourcemanager/vnet/vnet_test.go b/pkg/resourcemanager/vnet/vnet_test.go index df442c02917..320cff05984 100644 --- a/pkg/resourcemanager/vnet/vnet_test.go +++ b/pkg/resourcemanager/vnet/vnet_test.go @@ -42,11 +42,11 @@ var _ = Describe("VNet", func() { // Create vnet instance Eventually(func() bool { time.Sleep(3 * time.Second) - exists, _ := vnetManager.VNetExists(ctx, rgName, vnetName) - if exists { + _, err := vnetManager.GetVNet(ctx, rgName, vnetName) + if err == nil { return true } - _, err := vnetManager.CreateVNet(ctx, location, rgName, vnetName, addressSpace, []azurev1alpha1.VNetSubnets{ + _, err = vnetManager.CreateVNet(ctx, location, rgName, vnetName, addressSpace, []azurev1alpha1.VNetSubnets{ azurev1alpha1.VNetSubnets{ SubnetName: subnetName, SubnetAddressPrefix: subnetPrefix, @@ -68,11 +68,11 @@ var _ = Describe("VNet", func() { // Delete vnet instance Eventually(func() bool { time.Sleep(3 * time.Second) - exists, _ := vnetManager.VNetExists(ctx, rgName, vnetName) - if !exists { + _, err := vnetManager.GetVNet(ctx, rgName, vnetName) + if err != nil { return true } - _, err := vnetManager.DeleteVNet(ctx, rgName, vnetName) + _, err = vnetManager.DeleteVNet(ctx, rgName, vnetName) if err != nil { fmt.Println(err.Error()) if !errhelp.IsAsynchronousOperationNotComplete(err) {