Skip to content

Commit

Permalink
Merge pull request #165 from rebuy-de/move-config
Browse files Browse the repository at this point in the history
Move config into separate package
  • Loading branch information
svenwltr committed Mar 22, 2018
2 parents 3ebe024 + ee6a7d3 commit 6b831e4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
3 changes: 2 additions & 1 deletion cmd/nuke.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"time"

"github.com/rebuy-de/aws-nuke/pkg/awsutil"
"github.com/rebuy-de/aws-nuke/pkg/config"
"github.com/rebuy-de/aws-nuke/pkg/types"
"github.com/rebuy-de/aws-nuke/resources"
)

type Nuke struct {
Parameters NukeParameters
Account awsutil.Account
Config *NukeConfig
Config *config.Nuke

ResourceTypes types.Collection

Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sort"

"github.com/rebuy-de/aws-nuke/pkg/awsutil"
"github.com/rebuy-de/aws-nuke/pkg/config"
"github.com/rebuy-de/aws-nuke/resources"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -52,7 +53,7 @@ func NewRootCommand() *cobra.Command {

n := NewNuke(params, *account)

n.Config, err = LoadConfig(n.Parameters.ConfigPath)
n.Config, err = config.Load(n.Parameters.ConfigPath)
if err != nil {
return err
}
Expand Down
30 changes: 15 additions & 15 deletions cmd/config.go → pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package config

import (
"fmt"
Expand All @@ -11,32 +11,32 @@ import (
"gopkg.in/yaml.v2"
)

type ResourceConfig struct {
type ResourceTypes struct {
Targets types.Collection `yaml:"targets"`
Excludes types.Collection `yaml:"excludes"`
}

type NukeConfig struct {
AccountBlacklist []string `yaml:"account-blacklist"`
Regions []string `yaml:"regions"`
Accounts map[string]NukeConfigAccount `yaml:"accounts"`
ResourceTypes ResourceConfig `yaml:"resource-types"`
type Nuke struct {
AccountBlacklist []string `yaml:"account-blacklist"`
Regions []string `yaml:"regions"`
Accounts map[string]Account `yaml:"accounts"`
ResourceTypes ResourceTypes `yaml:"resource-types"`
}

type NukeConfigAccount struct {
type Account struct {
Filters map[string][]string `yaml:"filters"`
ResourceTypes ResourceConfig `yaml:"resource-types"`
ResourceTypes ResourceTypes `yaml:"resource-types"`
}

func LoadConfig(path string) (*NukeConfig, error) {
func Load(path string) (*Nuke, error) {
var err error

raw, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}

config := new(NukeConfig)
config := new(Nuke)
err = yaml.Unmarshal(raw, config)
if err != nil {
return nil, err
Expand All @@ -49,11 +49,11 @@ func LoadConfig(path string) (*NukeConfig, error) {
return config, nil
}

func (c *NukeConfig) HasBlacklist() bool {
func (c *Nuke) HasBlacklist() bool {
return c.AccountBlacklist != nil && len(c.AccountBlacklist) > 0
}

func (c *NukeConfig) InBlacklist(searchID string) bool {
func (c *Nuke) InBlacklist(searchID string) bool {
for _, blacklistID := range c.AccountBlacklist {
if blacklistID == searchID {
return true
Expand All @@ -63,7 +63,7 @@ func (c *NukeConfig) InBlacklist(searchID string) bool {
return false
}

func (c *NukeConfig) ValidateAccount(accountID string, aliases []string) error {
func (c *Nuke) ValidateAccount(accountID string, aliases []string) error {
if !c.HasBlacklist() {
return fmt.Errorf("The config file contains an empty blacklist. " +
"For safety reasons you need to specify at least one account ID. " +
Expand Down Expand Up @@ -96,7 +96,7 @@ func (c *NukeConfig) ValidateAccount(accountID string, aliases []string) error {
return nil
}

func (c *NukeConfig) resolveDeprecations() error {
func (c *Nuke) resolveDeprecations() error {
deprecations := map[string]string{
"EC2DhcpOptions": "EC2DHCPOptions",
"EC2InternetGatewayAttachement": "EC2InternetGatewayAttachment",
Expand Down
24 changes: 12 additions & 12 deletions cmd/config_test.go → pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package config

import (
"fmt"
Expand All @@ -8,7 +8,7 @@ import (
)

func TestConfigBlacklist(t *testing.T) {
config := new(NukeConfig)
config := new(Nuke)

if config.HasBlacklist() {
t.Errorf("HasBlacklist() returned true on a nil backlist.")
Expand Down Expand Up @@ -44,16 +44,16 @@ func TestConfigBlacklist(t *testing.T) {
}

func TestLoadExampleConfig(t *testing.T) {
config, err := LoadConfig("test-fixtures/example.yaml")
config, err := Load("test-fixtures/example.yaml")
if err != nil {
t.Fatal(err)
}

expect := NukeConfig{
expect := Nuke{
AccountBlacklist: []string{"1234567890"},
Regions: []string{"eu-west-1"},
Accounts: map[string]NukeConfigAccount{
"555133742": NukeConfigAccount{
Accounts: map[string]Account{
"555133742": Account{
Filters: map[string][]string{
"IAMRole": []string{
"uber.admin",
Expand All @@ -74,10 +74,10 @@ func TestLoadExampleConfig(t *testing.T) {
}

func TestResolveDeprecations(t *testing.T) {
config := NukeConfig{
config := Nuke{
AccountBlacklist: []string{"1234567890"},
Regions: []string{"eu-west-1"},
Accounts: map[string]NukeConfigAccount{
Accounts: map[string]Account{
"555133742": {
Filters: map[string][]string{
"IamRole": {"uber.admin", "foo.bar"},
Expand All @@ -93,7 +93,7 @@ func TestResolveDeprecations(t *testing.T) {
},
}

expect := map[string]NukeConfigAccount{
expect := map[string]Account{
"555133742": {
Filters: map[string][]string{
"IAMRole": {"uber.admin", "foo.bar"},
Expand All @@ -118,10 +118,10 @@ func TestResolveDeprecations(t *testing.T) {
t.Errorf(" Expected: %#v", expect)
}

invalidConfig := NukeConfig{
invalidConfig := Nuke{
AccountBlacklist: []string{"1234567890"},
Regions: []string{"eu-west-1"},
Accounts: map[string]NukeConfigAccount{
Accounts: map[string]Account{
"555133742": {
Filters: map[string][]string{
"IamUserAccessKeys": {"X"},
Expand All @@ -138,7 +138,7 @@ func TestResolveDeprecations(t *testing.T) {
}

func TestConfigValidation(t *testing.T) {
config, err := LoadConfig("test-fixtures/example.yaml")
config, err := Load("test-fixtures/example.yaml")
if err != nil {
t.Fatal(err)
}
Expand Down
File renamed without changes.

0 comments on commit 6b831e4

Please sign in to comment.