From 9099292d03568eb1af51334d82f59dacf00a4fdc Mon Sep 17 00:00:00 2001 From: Chris Marchesi Date: Thu, 7 Oct 2021 14:15:32 -0700 Subject: [PATCH] Don't ignore non-running instances, rely on filter instead We now add the instance-state = ["running"] filter to all queries if instance-state is not explicitly defined beforehand. This should ensure that all queries return running instances beforehand (saving some post-processing later too). As such, I'm removing an artifact of the old code where we were not doing this, which was basically to skip all running instances. Additionally, this will allow an operator to include non-running instances in a filter should this be desired for whatever reason. --- plugin/plugin.go | 22 ------------------ plugin/plugin_test.go | 54 ++----------------------------------------- 2 files changed, 2 insertions(+), 74 deletions(-) diff --git a/plugin/plugin.go b/plugin/plugin.go index 486cddc..2f276e6 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -414,12 +414,6 @@ func (p *AwsPlugin) ListHosts(ctx context.Context, req *pb.ListHostsRequest) (*p return nil, fmt.Errorf("error processing host results for host set id %q: %w", query.Id, err) } - if host == nil { - // Nil result means we don't include the host (instance not - // running, or some other issue). - continue - } - queries[i].OutputHosts = append(queries[i].OutputHosts, host) maxLen++ // Increment maximum counter for allocation later } @@ -612,9 +606,6 @@ func buildDescribeInstancesInput(attrs *structpb.Struct, dryRun bool) (*ec2.Desc // awsInstanceToHost processes data from an ec2.Instance and returns // a ListHostsResponseHost with the ID and network addresses // populated. -// -// Returns nil if the instance is not ready (ie: powered down, -// hibernated, or terminated). func awsInstanceToHost(instance *ec2.Instance) (*pb.ListHostsResponseHost, error) { // Integrity check: some fields should always be non-nil. Check // those. @@ -622,23 +613,10 @@ func awsInstanceToHost(instance *ec2.Instance) (*pb.ListHostsResponseHost, error return nil, errors.New("response integrity error: missing instance entry") } - if instance.State == nil { - return nil, errors.New("response integrity error: missing instance state") - } - - if aws.StringValue(instance.State.Name) == "" { - return nil, errors.New("response integrity error: missing instance state name") - } - if aws.StringValue(instance.InstanceId) == "" { return nil, errors.New("response integrity error: missing instance id") } - // Abort this if the instance is not ready - if aws.StringValue(instance.State.Name) != ec2.InstanceStateNameRunning { - return nil, nil - } - result := new(pb.ListHostsResponseHost) // External ID is the instance ID. diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go index 8b3d3fa..3f9fd9d 100644 --- a/plugin/plugin_test.go +++ b/plugin/plugin_test.go @@ -1183,7 +1183,7 @@ func TestPluginListHostsErr(t *testing.T) { ), )), }, - expectedErr: "error processing host results for host set id \"foobar\": response integrity error: missing instance state", + expectedErr: "error processing host results for host set id \"foobar\": response integrity error: missing instance id", }, } @@ -1674,42 +1674,13 @@ func TestAwsInstanceToHost(t *testing.T) { expectedErr: "response integrity error: missing instance entry", }, { - name: "nil instance state", + name: "missing instance id", instance: &ec2.Instance{}, - expectedErr: "response integrity error: missing instance state", - }, - { - name: "missing instance state name", - instance: &ec2.Instance{ - State: &ec2.InstanceState{}, - }, - expectedErr: "response integrity error: missing instance state name", - }, - { - name: "missing instance id", - instance: &ec2.Instance{ - State: &ec2.InstanceState{ - Name: aws.String(ec2.InstanceStateNameRunning), - }, - }, expectedErr: "response integrity error: missing instance id", }, - { - name: "instance not running", - instance: &ec2.Instance{ - State: &ec2.InstanceState{ - Name: aws.String(ec2.InstanceStateNameTerminated), - }, - InstanceId: aws.String("foobar"), - }, - expected: nil, - }, { name: "nil interface entry", instance: &ec2.Instance{ - State: &ec2.InstanceState{ - Name: aws.String(ec2.InstanceStateNameRunning), - }, InstanceId: aws.String("foobar"), NetworkInterfaces: []*ec2.InstanceNetworkInterface{nil}, }, @@ -1718,9 +1689,6 @@ func TestAwsInstanceToHost(t *testing.T) { { name: "nil interface address entry", instance: &ec2.Instance{ - State: &ec2.InstanceState{ - Name: aws.String(ec2.InstanceStateNameRunning), - }, InstanceId: aws.String("foobar"), NetworkInterfaces: []*ec2.InstanceNetworkInterface{ &ec2.InstanceNetworkInterface{ @@ -1733,9 +1701,6 @@ func TestAwsInstanceToHost(t *testing.T) { { name: "good, single IP w/public addr", instance: &ec2.Instance{ - State: &ec2.InstanceState{ - Name: aws.String(ec2.InstanceStateNameRunning), - }, InstanceId: aws.String("foobar"), PrivateIpAddress: aws.String("10.0.0.1"), PublicIpAddress: aws.String("1.1.1.1"), @@ -1760,9 +1725,6 @@ func TestAwsInstanceToHost(t *testing.T) { { name: "good, private", instance: &ec2.Instance{ - State: &ec2.InstanceState{ - Name: aws.String(ec2.InstanceStateNameRunning), - }, InstanceId: aws.String("foobar"), PrivateIpAddress: aws.String("10.0.0.1"), NetworkInterfaces: []*ec2.InstanceNetworkInterface{ @@ -1783,9 +1745,6 @@ func TestAwsInstanceToHost(t *testing.T) { { name: "good, multiple interfaces", instance: &ec2.Instance{ - State: &ec2.InstanceState{ - Name: aws.String(ec2.InstanceStateNameRunning), - }, InstanceId: aws.String("foobar"), PrivateIpAddress: aws.String("10.0.0.1"), PublicIpAddress: aws.String("1.1.1.1"), @@ -1817,9 +1776,6 @@ func TestAwsInstanceToHost(t *testing.T) { { name: "good, multiple public interfaces", instance: &ec2.Instance{ - State: &ec2.InstanceState{ - Name: aws.String(ec2.InstanceStateNameRunning), - }, InstanceId: aws.String("foobar"), PrivateIpAddress: aws.String("10.0.0.1"), PublicIpAddress: aws.String("1.1.1.1"), @@ -1854,9 +1810,6 @@ func TestAwsInstanceToHost(t *testing.T) { { name: "good, multiple addresses on single interface", instance: &ec2.Instance{ - State: &ec2.InstanceState{ - Name: aws.String(ec2.InstanceStateNameRunning), - }, InstanceId: aws.String("foobar"), PrivateIpAddress: aws.String("10.0.0.1"), PublicIpAddress: aws.String("1.1.1.1"), @@ -1884,9 +1837,6 @@ func TestAwsInstanceToHost(t *testing.T) { { name: "good, single IP w/public addr and IPv6", instance: &ec2.Instance{ - State: &ec2.InstanceState{ - Name: aws.String(ec2.InstanceStateNameRunning), - }, InstanceId: aws.String("foobar"), PrivateIpAddress: aws.String("10.0.0.1"), PublicIpAddress: aws.String("1.1.1.1"),