Skip to content

Commit

Permalink
Fixed uncommented exported items
Browse files Browse the repository at this point in the history
  • Loading branch information
tdi committed Jul 23, 2019
1 parent 3fc3b0f commit e148362
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 33 deletions.
4 changes: 2 additions & 2 deletions cmd/cli/commands/retagrg.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ var resourceGroupTagCommand = &cobra.Command{
}

rules := rules.TagRules{Rules: []rules.Rule{
rules.Rule{Name: "name", Conditions: []rules.ConditionItem{
rules.ConditionItem{"type": "rgEqual", "resourceGroup": resourceGroup},
{Name: "name", Conditions: []rules.ConditionItem{
{"type": "rgEqual", "resourceGroup": resourceGroup},
},
Actions: actions,
},
Expand Down
3 changes: 0 additions & 3 deletions cmd/cli/commands/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import (
"github.com/spf13/cobra"
)

type ActionExecution struct {
}

const (
usageMappingFile = "Location of the tag rules definition (json)"
usageDryRun = "The tagger will not execute any actions"
Expand Down
14 changes: 8 additions & 6 deletions cmd/cli/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ import (
"github.com/spf13/cobra"
)

var verbose bool

func init() {
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
}

var rootCmd = &cobra.Command{
Use: "tagmanager",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if Verbose {
if verbose {
log.SetLevel(log.InfoLevel)
}
},
}
var Verbose bool

func init() {
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
}

// Execute handles command
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
8 changes: 8 additions & 0 deletions internal/azure/rules/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/pkg/errors"
)

// NewFromFile reads filename and returns TagRules
func NewFromFile(filename string) (TagRules, error) {
dat, err := ioutil.ReadFile(filename)
if err != nil {
Expand All @@ -20,32 +21,39 @@ func NewFromFile(filename string) (TagRules, error) {
return NewFromString(string(dat))
}

// NewFromString parses rulesDef and returns TagRules
func NewFromString(rulesDef string) (TagRules, error) {
return parseRulesDefinitions(rulesDef)
}

// TagRules represents rules parsed from a rules definition
type TagRules struct {
DryRun *bool `json:"dryrun,omitempty"`
Rules []Rule `json:"rules"`
}

// Rule represnts single rule
type Rule struct {
Name string `json:"name,omitempty"`
Conditions []ConditionItem `json:"conditions"`
Actions []ActionItem `json:"actions"`
}

// ConditionItem represnts one condition
type ConditionItem map[string]string

// GetType retrurn the type of the condition
func (p ConditionItem) GetType() string {
if val, ok := p["type"]; ok {
return val
}
return ""
}

// ActionItem represnts a single action
type ActionItem map[string]string

// GetType retrurn the type of the action
func (p ActionItem) GetType() string {
if val, ok := p["type"]; ok {
return val
Expand Down
16 changes: 8 additions & 8 deletions internal/azure/rules/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,25 @@ rules:
}`
empty = `{}`
onlyDryRun = `{"dryrun": true}`
wrongJson = `{ew2`
wrongJSON = `{ew2`
wrongYaml = `223322`
)

var (
twoRulesWant = TagRules{Rules: []Rule{
Rule{Name: "name", Conditions: []ConditionItem{
ConditionItem{"type": "tagEqual", "tag": "test", "value": "test"},
ConditionItem{"type": "tagExists", "tag": "test"},
{Name: "name", Conditions: []ConditionItem{
{"type": "tagEqual", "tag": "test", "value": "test"},
{"type": "tagExists", "tag": "test"},
},
Actions: []ActionItem{
ActionItem{"type": "addTag", "tag": "test", "value": "test"},
{"type": "addTag", "tag": "test", "value": "test"},
},
},
}}
)

var dryRunFalse bool = false
var dryRunTrue bool = true
var dryRunFalse = false
var dryRunTrue = true

func TestNewFromString(t *testing.T) {
type args struct {
Expand All @@ -95,7 +95,7 @@ func TestNewFromString(t *testing.T) {
{name: "only dryrun defined", args: args{rulesDef: onlyDryRun}, want: TagRules{DryRun: &dryRunTrue}, wantErr: false},
{name: "one rule", args: args{rulesDef: two}, want: twoRulesWant, wantErr: false},
{name: "one rule yaml", args: args{rulesDef: yamlTwo}, want: twoRulesWant, wantErr: false},
{name: "wrong json", args: args{rulesDef: wrongJson}, want: TagRules{}, wantErr: true},
{name: "wrong json", args: args{rulesDef: wrongJSON}, want: TagRules{}, wantErr: true},
{name: "wrong yaml", args: args{rulesDef: wrongYaml}, want: TagRules{}, wantErr: true},
}

Expand Down
28 changes: 14 additions & 14 deletions internal/azure/tagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,49 @@ import (
"context"
"testing"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-02-01/resources"
"github.com/nordcloud/azure-tag-manager/internal/azure/rules"
"github.com/nordcloud/azure-tag-manager/mocks"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-02-01/resources"
"github.com/stretchr/testify/assert"
)

var (
twoRulesWant = rules.TagRules{Rules: []rules.Rule{
rules.Rule{Name: "name", Conditions: []rules.ConditionItem{
rules.ConditionItem{"type": "tagEqual", "tag": "test", "value": "test"},
rules.ConditionItem{"type": "tagExists", "tag": "test"},
{Name: "name", Conditions: []rules.ConditionItem{
{"type": "tagEqual", "tag": "test", "value": "test"},
{"type": "tagExists", "tag": "test"},
},
Actions: []rules.ActionItem{
rules.ActionItem{"type": "addTag", "tag": "test2", "value": "test2"},
{"type": "addTag", "tag": "test2", "value": "test2"},
},
},
}}

deleteTag = rules.TagRules{Rules: []rules.Rule{
rules.Rule{Name: "name", Conditions: []rules.ConditionItem{
rules.ConditionItem{"type": "tagEqual", "tag": "test2", "value": "test2"},
{Name: "name", Conditions: []rules.ConditionItem{
{"type": "tagEqual", "tag": "test2", "value": "test2"},
},
Actions: []rules.ActionItem{
rules.ActionItem{"type": "delTag", "tag": "test3"},
{"type": "delTag", "tag": "test3"},
},
},
}}

deleteAllTags = rules.TagRules{Rules: []rules.Rule{
rules.Rule{Name: "name", Conditions: []rules.ConditionItem{
rules.ConditionItem{"type": "tagEqual", "tag": "test2", "value": "test2"},
{Name: "name", Conditions: []rules.ConditionItem{
{"type": "tagEqual", "tag": "test2", "value": "test2"},
},
Actions: []rules.ActionItem{
rules.ActionItem{"type": "cleanTags"},
{"type": "cleanTags"},
},
},
}}
)

var testResources = []Resource{
Resource{ID: "1", Region: "westeurope", Tags: map[string]*string{"test": String("test")}, ResourceGroup: String("test"), Name: String("name")},
Resource{ID: "2", Region: "westeurope", Tags: map[string]*string{"test2": String("test2"), "test3": String("test3")}, ResourceGroup: String("te3st"), Name: String("name2")},
Resource{ID: "3", Region: "easteurope", Tags: map[string]*string{"test-region": String("other"), "othertest": String("test56")}, ResourceGroup: String("rg2"), Name: String("name3")},
{ID: "1", Region: "westeurope", Tags: map[string]*string{"test": String("test")}, ResourceGroup: String("test"), Name: String("name")},
{ID: "2", Region: "westeurope", Tags: map[string]*string{"test2": String("test2"), "test3": String("test3")}, ResourceGroup: String("te3st"), Name: String("name2")},
{ID: "3", Region: "easteurope", Tags: map[string]*string{"test-region": String("other"), "othertest": String("test56")}, ResourceGroup: String("rg2"), Name: String("name3")},
}

func TestTagger_ExecuteActions(t *testing.T) {
Expand Down

0 comments on commit e148362

Please sign in to comment.