Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #62 from danmx/tag-fix
Browse files Browse the repository at this point in the history
Fixing list filters
  • Loading branch information
danmx authored Apr 16, 2020
2 parents 56a8082 + 2b3d392 commit 96dbf56
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# Change this and commit to create new release
override VERSION ?= 0.4.0
override VERSION ?= 0.4.1
NAME = sigil
REPO = danmx/$(NAME)
MODULE = github.com/$(REPO)
Expand Down
11 changes: 5 additions & 6 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Filter format examples:
var filters aws.Filters
if err := cfg.UnmarshalKey("filters", &filters); err != nil {
log.Error("failed unmarshaling filters")
return err
return fmt.Errorf("failed unmarshaling filters: %s", err)
}
outputFormat := cfg.GetString("output-format")
profile := cfg.GetString("profile")
Expand All @@ -92,23 +92,22 @@ Filter format examples:
instanceIDs := cfg.GetStringSlice("filters.instance.ids")
// hack to get map[string]string from args
// https://github.com/spf13/viper/issues/608
if cfg.IsSet("filters.session") {
if cmd.Flags().Changed("session-filters") {
filters.Session = aws.SessionFilters{
After: sessionFilters["after"],
Before: sessionFilters["before"],
Target: sessionFilters["target"],
Owner: sessionFilters["owner"],
}
}
if cfg.IsSet("filters.instance.ids") {
if cmd.Flags().Changed("instance-ids") {
filters.Instance.IDs = instanceIDs
}
var tags []aws.TagValues
if cfg.IsSet("filters.instance.tags") {

if cmd.Flags().Changed("instance-tags") {
if err := json.Unmarshal([]byte(cfg.GetString("filters.instance.tags")), &tags); err != nil {
log.WithField("tags", cfg.GetString("filters.instance.tags")).Error("failed unmarshaling tags")
return err
return fmt.Errorf("failed unmarshaling tags: %s", err)
}
filters.Instance.Tags = tags
}
Expand Down
28 changes: 16 additions & 12 deletions pkg/aws/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ func (p *Provider) ListInstances() ([]*Instance, error) {
},
},
}
for _, tag := range p.filters.Instance.Tags {
log.WithFields(log.Fields{
"key": tag.Key,
"values": tag.Values,
}).Debug("Tags Filter")
ssmDescribeInstancesInput.Filters = append(ssmDescribeInstancesInput.Filters, &ssm.InstanceInformationStringFilter{
Key: aws.String("tag:" + tag.Key),
Values: aws.StringSlice(tag.Values),
})
}
if len(p.filters.Instance.IDs) > 0 {
log.WithFields(log.Fields{
"IDs": p.filters.Instance.IDs,
Expand Down Expand Up @@ -86,11 +76,22 @@ func (p *Provider) ListInstances() ([]*Instance, error) {
},
},
}

for _, tag := range p.filters.Instance.Tags {
log.WithFields(log.Fields{
"key": tag.Key,
"values": tag.Values,
}).Debug("Tags Filter")
describeInstancesInput.Filters = append(describeInstancesInput.Filters, &ec2.Filter{
Name: aws.String("tag:" + tag.Key),
Values: aws.StringSlice(tag.Values),
})
}

outputInstances := make([]*Instance, 0, len(instances))
// Discovering instances private DNS names
for _, instance := range instances {
describeInstancesInput.InstanceIds = append(describeInstancesInput.InstanceIds, &instance.ID)
outputInstances = append(outputInstances, instance)
}
ec2Client := ec2.New(p.session)
err = ec2Client.DescribeInstancesPages(describeInstancesInput,
Expand All @@ -106,6 +107,7 @@ func (p *Provider) ListInstances() ([]*Instance, error) {
}
instances[*instance.InstanceId].PrivateDNSName = *instance.PrivateDnsName
instances[*instance.InstanceId].Name = nameTag
outputInstances = append(outputInstances, instances[*instance.InstanceId])
}
}
return !lastPage
Expand Down Expand Up @@ -152,7 +154,9 @@ func (p *Provider) ListSessions() ([]*Session, error) {
Value: &p.filters.Session.Owner,
})
}
ssmDescribeSessionsInput.Filters = filters
if len(filters) > 0 {
ssmDescribeSessionsInput.Filters = filters
}
ssmClient := ssm.New(p.session)
sessions := []*Session{}
for out, err := ssmClient.DescribeSessions(ssmDescribeSessionsInput); ; {
Expand Down

0 comments on commit 96dbf56

Please sign in to comment.