-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add azure app security groups resource type.
- TODO: Unpend acceptance test, create app sg, figure out how to say No to the resource group prompt but Yes to the app sg prompt. Maybe only test it when deleting by type? - TODO: Wire up the app security groups collector to each respective resource group since it has a dependency on the rg name. For #84
- Loading branch information
Genevieve Lesperance
committed
Sep 15, 2019
1 parent
990caff
commit 563acce
Showing
8 changed files
with
342 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package azure | ||
|
||
import "fmt" | ||
|
||
type AppSecurityGroup struct { | ||
client appSecurityGroupsClient | ||
name string | ||
rgName string | ||
} | ||
|
||
// AppSecurityGroup represents an Azure application security group. | ||
func NewAppSecurityGroup(client appSecurityGroupsClient, rgName, name string) AppSecurityGroup { | ||
return AppSecurityGroup{ | ||
client: client, | ||
rgName: rgName, | ||
name: name, | ||
} | ||
} | ||
|
||
// Delete deletes an Azure application security group. | ||
func (g AppSecurityGroup) Delete() error { | ||
err := g.client.DeleteAppSecurityGroup(g.rgName, g.name) | ||
if err != nil { | ||
return fmt.Errorf("Delete: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (g AppSecurityGroup) Name() string { | ||
return g.name | ||
} | ||
|
||
func (g AppSecurityGroup) Type() string { | ||
return "Application Security Group" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package azure_test | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/genevieve/leftovers/azure" | ||
"github.com/genevieve/leftovers/azure/fakes" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("AppSecurityGroup", func() { | ||
var ( | ||
client *fakes.AppSecurityGroupsClient | ||
name string | ||
rgName string | ||
|
||
group azure.AppSecurityGroup | ||
) | ||
|
||
BeforeEach(func() { | ||
client = &fakes.AppSecurityGroupsClient{} | ||
name = "banana-group" | ||
rgName = "major-banana-group" | ||
|
||
group = azure.NewAppSecurityGroup(client, rgName, name) | ||
}) | ||
|
||
Describe("Delete", func() { | ||
It("deletes resource groups", func() { | ||
err := group.Delete() | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(client.DeleteAppSecurityGroupCall.CallCount).To(Equal(1)) | ||
Expect(client.DeleteAppSecurityGroupCall.Receives.Name).To(Equal(name)) | ||
Expect(client.DeleteAppSecurityGroupCall.Receives.ResourceGroupName).To(Equal(rgName)) | ||
}) | ||
|
||
Context("when client fails to delete the app security group", func() { | ||
BeforeEach(func() { | ||
client.DeleteAppSecurityGroupCall.Returns.Error = errors.New("some error") | ||
}) | ||
|
||
It("logs the error", func() { | ||
err := group.Delete() | ||
Expect(err).To(MatchError("Delete: some error")) | ||
}) | ||
}) | ||
}) | ||
|
||
Describe("Type", func() { | ||
It("returns the type", func() { | ||
Expect(group.Type()).To(Equal("Application Security Group")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package azure | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/genevieve/leftovers/common" | ||
) | ||
|
||
type appSecurityGroupsClient interface { | ||
ListAppSecurityGroups(rgName string) ([]string, error) | ||
DeleteAppSecurityGroup(rgName string, name string) error | ||
} | ||
|
||
type AppSecurityGroups struct { | ||
client appSecurityGroupsClient | ||
rgName string | ||
logger logger | ||
} | ||
|
||
func NewAppSecurityGroups(client appSecurityGroupsClient, rgName string, logger logger) AppSecurityGroups { | ||
return AppSecurityGroups{ | ||
client: client, | ||
rgName: rgName, | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (g AppSecurityGroups) List(filter string) ([]common.Deletable, error) { | ||
groups, err := g.client.ListAppSecurityGroups(g.rgName) | ||
if err != nil { | ||
return nil, fmt.Errorf("Listing Application Security Groups: %s", err) | ||
} | ||
|
||
var resources []common.Deletable | ||
for _, group := range groups { | ||
r := NewAppSecurityGroup(g.client, g.rgName, group) | ||
|
||
if !strings.Contains(r.Name(), filter) { | ||
continue | ||
} | ||
|
||
proceed := g.logger.PromptWithDetails(r.Type(), r.Name()) | ||
if !proceed { | ||
continue | ||
} | ||
|
||
resources = append(resources, r) | ||
} | ||
|
||
return resources, nil | ||
} | ||
|
||
func (g AppSecurityGroups) Type() string { | ||
return "app-security-group" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package azure_test | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/genevieve/leftovers/azure" | ||
"github.com/genevieve/leftovers/azure/fakes" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("AppSecurityGroups", func() { | ||
var ( | ||
client *fakes.AppSecurityGroupsClient | ||
logger *fakes.Logger | ||
filter string | ||
rgName string | ||
|
||
groups azure.AppSecurityGroups | ||
) | ||
|
||
BeforeEach(func() { | ||
client = &fakes.AppSecurityGroupsClient{} | ||
logger = &fakes.Logger{} | ||
filter = "banana" | ||
rgName = "resource-group" | ||
|
||
groups = azure.NewAppSecurityGroups(client, rgName, logger) | ||
}) | ||
|
||
Describe("List", func() { | ||
BeforeEach(func() { | ||
logger.PromptWithDetailsCall.Returns.Proceed = true | ||
client.ListAppSecurityGroupsCall.Returns.List = []string{"banana-group", "kiwi-group"} | ||
}) | ||
|
||
It("returns a list of resources to delete", func() { | ||
items, err := groups.List(filter) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(client.ListAppSecurityGroupsCall.CallCount).To(Equal(1)) | ||
Expect(client.ListAppSecurityGroupsCall.Receives.ResourceGroupName).To(Equal(rgName)) | ||
|
||
Expect(logger.PromptWithDetailsCall.CallCount).To(Equal(1)) | ||
|
||
Expect(items).To(HaveLen(1)) | ||
}) | ||
|
||
Context("when client fails to list the resource", func() { | ||
BeforeEach(func() { | ||
client.ListAppSecurityGroupsCall.Returns.Error = errors.New("some error") | ||
}) | ||
|
||
It("returns the error", func() { | ||
_, err := groups.List(filter) | ||
Expect(err).To(MatchError("Listing Application Security Groups: some error")) | ||
}) | ||
}) | ||
|
||
Context("when the user responds no to the prompt", func() { | ||
BeforeEach(func() { | ||
logger.PromptWithDetailsCall.Returns.Proceed = false | ||
}) | ||
|
||
It("does not return it in the list", func() { | ||
items, err := groups.List(filter) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(logger.PromptWithDetailsCall.Receives.Type).To(Equal("Application Security Group")) | ||
Expect(logger.PromptWithDetailsCall.Receives.Name).To(Equal("banana-group")) | ||
|
||
Expect(items).To(HaveLen(0)) | ||
}) | ||
}) | ||
|
||
Context("when the resource group name does not contain the filter", func() { | ||
It("does not return it in the list", func() { | ||
items, err := groups.List("grape") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(logger.PromptWithDetailsCall.CallCount).To(Equal(0)) | ||
Expect(items).To(HaveLen(0)) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package fakes | ||
|
||
type AppSecurityGroupsClient struct { | ||
ListAppSecurityGroupsCall struct { | ||
CallCount int | ||
Receives struct { | ||
ResourceGroupName string | ||
} | ||
Returns struct { | ||
List []string | ||
Error error | ||
} | ||
} | ||
|
||
DeleteAppSecurityGroupCall struct { | ||
CallCount int | ||
Receives struct { | ||
ResourceGroupName string | ||
Name string | ||
} | ||
Returns struct { | ||
Error error | ||
} | ||
} | ||
} | ||
|
||
func (c *AppSecurityGroupsClient) ListAppSecurityGroups(rgName string) ([]string, error) { | ||
c.ListAppSecurityGroupsCall.CallCount++ | ||
c.ListAppSecurityGroupsCall.Receives.ResourceGroupName = rgName | ||
return c.ListAppSecurityGroupsCall.Returns.List, c.ListAppSecurityGroupsCall.Returns.Error | ||
} | ||
|
||
func (c *AppSecurityGroupsClient) DeleteAppSecurityGroup(rgName, name string) error { | ||
c.DeleteAppSecurityGroupCall.CallCount++ | ||
c.DeleteAppSecurityGroupCall.Receives.ResourceGroupName = rgName | ||
c.DeleteAppSecurityGroupCall.Receives.Name = name | ||
|
||
return c.DeleteAppSecurityGroupCall.Returns.Error | ||
} |
Oops, something went wrong.