Skip to content

Commit

Permalink
Merge branch 'master' into add-hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Fitzwater - IDI-C authored Feb 14, 2020
2 parents a3f951c + a0d7877 commit e429e82
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 198 deletions.
36 changes: 36 additions & 0 deletions handler/helpers/ec2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func TestInstances(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("Instances() failed. Expected: %#v (%T)\nGot: %#v (%T)", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func Images() ([]*ec2.Image, error)
Expand All @@ -92,6 +96,10 @@ func TestImages(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("Images() failed. Expected: %#v (%T)\nGot: %#v (%T)", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func Volumes() ([]*ec2.Volume, error)
Expand All @@ -105,6 +113,10 @@ func TestVolumes(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("Volumes() failed. Expected: %#v (%T)\nGot: %#v (%T)", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func Snapshots() ([]*ec2.Snapshot, error)
Expand All @@ -118,6 +130,10 @@ func TestSnapshots(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("Snapshots() failed. Expected: %#v (%T)\nGot: %#v (%T)", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func Vpcs() ([]*ec2.Vpc, error)
Expand All @@ -131,6 +147,10 @@ func TestVpcs(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("Vpcs() failed. Expected: %#v (%T)\nGot: %#v (%T)", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func Subnets() ([]*ec2.Subnet, error)
Expand All @@ -144,6 +164,10 @@ func TestSubnets(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("Subnets() failed. Expected: %#v (%T)\nGot: %#v (%T)", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func SecurityGroups() ([]*ec2.SecurityGroup, error)
Expand All @@ -157,6 +181,10 @@ func TestSecurityGroups(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("SecurityGroups() failed. Expected: %#v (%T)\nGot: %#v (%T)", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func Addresses() ([]*ec2.Address, error)
Expand All @@ -170,6 +198,10 @@ func TestAddresses(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("Addresses() failed. Expected: %#v (%T)\nGot: %#v (%T)", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func KeyPairs() ([]*ec2.KeyPairInfo, error)
Expand All @@ -183,6 +215,10 @@ func TestKeyPairs(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("KeyPairs() failed. Expected: %#v (%T)\nGot: %#v (%T)", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func KeyPairs(sess *session.Session, cred *credentials.Credentials) ([]*ec2.KeyPairInfo, error)
116 changes: 116 additions & 0 deletions handler/helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package helpers

import (
"errors"
"log"
"reflect"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -10,14 +13,20 @@ import (
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
"github.com/aws/aws-sdk-go/service/configservice"
"github.com/aws/aws-sdk-go/service/configservice/configserviceiface"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/aws/aws-sdk-go/service/elbv2/elbv2iface"
"github.com/aws/aws-sdk-go/service/glacier"
"github.com/aws/aws-sdk-go/service/glacier/glacieriface"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/kms/kmsiface"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
)
Expand Down Expand Up @@ -297,3 +306,110 @@ func Parameters(svc ssmiface.SSMAPI) ([]*ssm.ParameterMetadata, error) {
}
return results, nil
}

// Sheet name constants
const (
SheetRoles = "Roles"
SheetAccounts = "Accounts"
SheetGroups = "Groups"
SheetPolicies = "Policies"
SheetUsers = "Users"
SheetBuckets = "Buckets"
SheetInstances = "Instances"
SheetImages = "Images"
SheetVolumes = "Volumes"
SheetSnapshots = "Snapshots"
SheetVpcs = "VPCs"
SheetSubnets = "Subnets"
SheetSecurityGroups = "SecurityGroups"
SheetAddresses = "Addresses"
SheetKeyPairs = "KeyPairs"
SheetStacks = "Stacks"
SheetAlarms = "Alarms"
SheetConfigRules = "ConfigRules"
SheetLoadBalancers = "LoadBlancers"
SheetVaults = "Vaults"
SheetKeys = "Keys"
SheetDBInstances = "DBInstances"
SheetDBSnapshots = "DBSnapshots"
SheetSecrets = "Secrets"
SheetSubscriptions = "Subscriptions"
SheetTopics = "Topics"
SheetParameters = "Parameters"
)

// nolint: gocyclo
// TypeToSheet ... converts a slice type to a sheet name
func TypeToSheet(items interface{}) (string, error) {
var sheet string

s := reflect.ValueOf(items)
if s.Kind() != reflect.Slice {
return "", errors.New("items is not a sheet")
}

if s.Len() == 0 {
//Empty slice - this isn't an error, but we don't need to do anything
return "", nil
}
switch val := s.Index(0).Interface().(type) {
case *organizations.Account:
sheet = SheetAccounts
case *iam.Role:
sheet = SheetRoles
case *iam.Group:
sheet = SheetGroups
case *iam.Policy:
sheet = SheetPolicies
case *iam.User:
sheet = SheetUsers
case *s3.Bucket:
sheet = SheetBuckets
case *ec2.Instance:
sheet = SheetInstances
case *ec2.Image:
sheet = SheetImages
case *ec2.Volume:
sheet = SheetVolumes
case *ec2.Snapshot:
sheet = SheetSnapshots
case *ec2.Vpc:
sheet = SheetVpcs
case *ec2.Subnet:
sheet = SheetSubnets
case *ec2.SecurityGroup:
sheet = SheetSecurityGroups
case *ec2.Address:
sheet = SheetAddresses
case *ec2.KeyPairInfo:
sheet = SheetKeyPairs
case *cloudformation.Stack:
sheet = SheetStacks
case *cloudwatch.MetricAlarm:
sheet = SheetAlarms
case *configservice.ConfigRule:
sheet = SheetConfigRules
case *elbv2.LoadBalancer:
sheet = SheetLoadBalancers
case *glacier.DescribeVaultOutput:
sheet = SheetVaults
case *KmsKey:
sheet = SheetKeys
case *rds.DBInstance:
sheet = SheetDBInstances
case *rds.DBSnapshot:
sheet = SheetDBSnapshots
case *secretsmanager.SecretListEntry:
sheet = SheetSecrets
case *sns.Subscription:
sheet = SheetSubscriptions
case *SnsTopic:
sheet = SheetTopics
case *ssm.ParameterMetadata:
sheet = SheetParameters
default:
log.Printf("Unknown sheet type: %T", val)
return "", errors.New("unknown type")
}
return sheet, nil
}
51 changes: 47 additions & 4 deletions handler/helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func TestBuckets(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("Buckets() failed.\nExpected %#v (%T)\nGot: %#v (%T)\n", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func Stacks(svc *cloudformation.CloudFormationAPI) ([]*cloudformation.Stack, error)
Expand All @@ -120,6 +124,10 @@ func TestStacks(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("Stacks() failed.\nExpected %#v (%T)\nGot: %#v (%T)\n", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func Alarms(svc *cloudwatch.CloudWatchAPI) ([]*cloudwatch.MetricAlarm, error)
Expand All @@ -133,6 +141,10 @@ func TestAlarms(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("Alarms() failed.\nExpected %#v (%T)\nGot: %#v (%T)\n", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func ConfigRules(svc *configservice.ConfigServiceAPI) ([]*configservice.ConfigRule, error)
Expand All @@ -146,6 +158,10 @@ func TestConfigRules(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("ConfigRules() failed.\nExpected %#v (%T)\nGot: %#v (%T)\n", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func LoadBalancers(svc *elbv2.ELBV2API) ([]*elbv2.LoadBalancer, error)
Expand All @@ -159,6 +175,10 @@ func TestLoadBalancers(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("LoadBalancers() failed.\nExpected %#v (%T)\nGot: %#v (%T)\n", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

type mockGlacierClient struct {
Expand Down Expand Up @@ -233,7 +253,10 @@ func TestVaultsPagination(t *testing.T) {
if err != nil {
t.Fatalf("Vaults() failed: %v", err)
}

_, err = TypeToSheet(items)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
length := len(items)
if length != tc.ExpectedLength {
t.Errorf("items length invalid, expected: %d, got: %d", tc.ExpectedLength, length)
Expand Down Expand Up @@ -264,39 +287,55 @@ func TestKeys(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("Keys() failed.\nExpected %#v (%T)\nGot: %#v (%T)\n", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func DBInstances(cfg client.ConfigProvider, cred *credentials.Credentials) ([]*rds.DBInstance, error) {
func TestDBInstances(t *testing.T) {
svc := RDSSvc{
Client: mockedRDS{},
}
_, err := svc.DBInstances()
items, err := svc.DBInstances()
if err != nil {
t.Fatalf("DBInstances() failed: %v", err)
}
_, err = TypeToSheet(items)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func DBSnapshots(cfg client.ConfigProvider, cred *credentials.Credentials) ([]*rds.DBInstance, error) {
func TestDBSnapshots(t *testing.T) {
svc := RDSSvc{
Client: mockedRDS{},
}
_, err := svc.DBSnapshots()
items, err := svc.DBSnapshots()
if err != nil {
t.Fatalf("DBSnapshots() failed: %v", err)
}
_, err = TypeToSheet(items)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func Secrets(cfg client.ConfigProvider, cred *credentials.Credentials) ([]*secretsmanager.SecretListEntry, error) {
func TestSecrets(t *testing.T) {
svc := SecretsManagerSvc{
Client: mockedSecretsManager{},
}
_, err := svc.Secrets()
items, err := svc.Secrets()
if err != nil {
t.Fatalf("Secrets() failed: %v", err)
}
_, err = TypeToSheet(items)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}

// func Parameters(svc *ssm.SSMAPI) ([]*ssm.Parameter, error)
Expand All @@ -310,4 +349,8 @@ func TestParameters(t *testing.T) {
if !reflect.DeepEqual(expected, got) {
t.Errorf("Parameters() failed.\nExpected %#v (%T)\nGot: %#v (%T)\n", expected, expected, got, got)
}
_, err = TypeToSheet(expected)
if err != nil {
t.Fatalf("TypeToSheet failed: %v", err)
}
}
Loading

0 comments on commit e429e82

Please sign in to comment.