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

Move config into separate package #165

Merged
merged 2 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without the "*Config" it feels like it's missing context, because the package it is in, is not something i'd look at first when guessing what a struct does. just my 2 cents...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.