Skip to content

Commit

Permalink
Add ResourceID to VirtualNetwork (#952)
Browse files Browse the repository at this point in the history
* fixing get return values for resourceid

* removing test vars

* false nil at end of ensure

* get name change

* method header comment fix

Co-authored-by: Erin Corson <frodopwns@gmail.com>
  • Loading branch information
melonrush13 and frodopwns authored Apr 17, 2020
1 parent 6179c65 commit bf4decd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 34 deletions.
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

0 comments on commit bf4decd

Please sign in to comment.