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

Add ResourceID to VirtualNetwork #952

Merged
merged 11 commits into from
Apr 17, 2020
19 changes: 8 additions & 11 deletions pkg/resourcemanager/vnet/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
19 changes: 4 additions & 15 deletions pkg/resourcemanager/vnet/vnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
}
4 changes: 2 additions & 2 deletions pkg/resourcemanager/vnet/vnet_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions pkg/resourcemanager/vnet/vnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down