Skip to content

feat: create list command #18

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
60 changes: 60 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cmd

import (
"fmt"
"log"

"github.com/mcastellin/aws-fail-az/awsapis"
"github.com/mcastellin/aws-fail-az/service"
"github.com/mcastellin/aws-fail-az/state"
)

type ListCommand struct {
Provider awsapis.AWSProvider
Namespace string
}

type AutoScalingGroupState struct {
AutoScalingGroupName string `json:"asgName"`
Subnets []string `json:"subnets"`
}

func (cmd *ListCommand) Run() error {
stateManager, err := state.NewStateManager(cmd.Provider, cmd.Namespace)
if err != nil {
log.Print("Failed to create AWS state manager")
return err
}

if err := stateManager.Initialize(); err != nil {
return err
}

states, err := stateManager.QueryStates(&state.QueryStatesInput{})
if err != nil {
return err
}

ns := cmd.Namespace
if len(ns) == 0 {
ns = "default"
}

if len(states) == 0 {
fmt.Printf("No attacked resources found for namespace '%s':\n", ns)
return nil
}

fmt.Printf("Attacked resources for namespace '%s':\n", ns)
faultTypes := service.InitServiceFaults()
for _, s := range states {
description, err := faultTypes.DescribeState(s)
if err != nil {
log.Println(err)
} else {
fmt.Println(description)
}
}

return nil
}
19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ var stateDeleteCmd = &cobra.Command{
},
}

var listCmd = &cobra.Command{
Use: "list",
Short: "List all the attacked resources",
RunE: func(_ *cobra.Command, args []string) error {
provider, err := createProvider()
if err != nil {
return err
}
op := &cmd.ListCommand{
Provider: provider,
Namespace: namespace,
}
return op.Run()
},
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the command version",
Expand Down Expand Up @@ -175,6 +191,8 @@ func main() {
stateDeleteCmd.MarkFlagRequired("type")
stateDeleteCmd.MarkFlagRequired("key")

listCmd.Flags().StringVar(&namespace, "ns", "", "The namespace assigned to this operation. Used to uniquely identify resources state for recovery.")

rootCmd.PersistentFlags().StringVar(&awsRegion, "region", "", "The AWS region")
rootCmd.PersistentFlags().StringVar(&awsProfile, "profile", "", "The AWS profile")
rootCmd.AddCommand(failCmd)
Expand All @@ -183,6 +201,7 @@ func main() {
rootCmd.AddCommand(stateSaveCmd)
rootCmd.AddCommand(stateReadCmd)
rootCmd.AddCommand(stateDeleteCmd)
rootCmd.AddCommand(listCmd)
rootCmd.SilenceUsage = true
rootCmd.SilenceErrors = true

Expand Down
10 changes: 10 additions & 0 deletions service/asg/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import (
"github.com/mcastellin/aws-fail-az/service/awsutils"
)

func DescribeAutoScalingGroupsState(stateData []byte) (string, error) {
var state AutoScalingGroupState
err := json.Unmarshal(stateData, &state)
if err != nil {
return "", err
}

return fmt.Sprintf("- Resource type: %s\n AutoScalingGroupName: %s", "AutoScaling Group", state.AutoScalingGroupName), nil
}

func RestoreAutoScalingGroupsFromState(stateData []byte, provider awsapis.AWSProvider) error {
var state AutoScalingGroupState
err := json.Unmarshal(stateData, &state)
Expand Down
10 changes: 10 additions & 0 deletions service/ecs/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import (
"github.com/mcastellin/aws-fail-az/service/awsutils"
)

func DescribeEcsServicesState(stateData []byte) (string, error) {
var state ECSServiceState
err := json.Unmarshal(stateData, &state)
if err != nil {
return "", err
}

return fmt.Sprintf("- Resource type: %s\n Cluster: %s\n ServiceName: %s", "ECS Service", state.ClusterArn, state.ServiceName), nil
}

func RestoreEcsServicesFromState(stateData []byte, provider awsapis.AWSProvider) error {
var state ECSServiceState
err := json.Unmarshal(stateData, &state)
Expand Down
10 changes: 10 additions & 0 deletions service/elbv2/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import (
"github.com/mcastellin/aws-fail-az/service/awsutils"
)

func DescribeElbv2LoadBalancersState(stateData []byte) (string, error) {
var state LoadBalancerState
err := json.Unmarshal(stateData, &state)
if err != nil {
return "", err
}

return fmt.Sprintf("- Resource type: %s\n LoadBalancerName: %s", "ELB Load Balancer", state.LoadBalancerName), nil
}

func RestoreElbv2LoadBalancersFromState(stateData []byte, provider awsapis.AWSProvider) error {
var state LoadBalancerState
err := json.Unmarshal(stateData, &state)
Expand Down
25 changes: 25 additions & 0 deletions service/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ func InitServiceFaults() *FaultsInitFns {
domain.ResourceTypeAutoScalingGroup: asg.RestoreAutoScalingGroupsFromState,
domain.ResourceTypeElbv2LoadBalancer: elbv2.RestoreElbv2LoadBalancersFromState,
},

describe: map[string]func([]byte) (string, error){
// Register describe state functions for new fault types in this structure

domain.ResourceTypeEcsService: ecs.DescribeEcsServicesState,
domain.ResourceTypeAutoScalingGroup: asg.DescribeAutoScalingGroupsState,
domain.ResourceTypeElbv2LoadBalancer: elbv2.DescribeElbv2LoadBalancersState,
},
}
return initFns
}
Expand All @@ -44,6 +52,9 @@ type FaultsInitFns struct {

// A map of all available fault types and their restore functions
restore map[string]func([]byte, awsapis.AWSProvider) error

// A map of all available fault types and their describe functions
describe map[string]func([]byte) (string, error)
}

// Initialize new resource faults from their selector
Expand Down Expand Up @@ -72,3 +83,17 @@ func (obj *FaultsInitFns) RestoreFromState(state state.ResourceState, provider a
)
return err
}

// Call the specific DescribeState function for the resource type specified in the state object
func (obj *FaultsInitFns) DescribeState(state state.ResourceState) (string, error) {
describeFn, ok := obj.describe[state.ResourceType]
if ok {
return describeFn(state.State)
}

err := fmt.Errorf("unknown resource of type %s found in state with key %s. Object will be ignored",
state.ResourceType,
state.Key,
)
return "", err
}