Exemplo n.º 1
0
func (e *EC2Events) Events() error {
	// Request Input
	var input *ec2.DescribeInstanceStatusInput = &ec2.DescribeInstanceStatusInput{
		DryRun: aws.Bool(false),
		Filters: []*ec2.Filter{
			{
				Name: aws.String("event.code"),
				Values: []*string{
					aws.String("instance-reboot"),
					aws.String("system-reboot"),
					aws.String("system-maintenance"),
					aws.String("instance-retirement"),
					aws.String("instance-stop"),
				},
			},
		},
		MaxResults: aws.Int64(1000),
	}

	// Request response and error
	var (
		resp *ec2.DescribeInstanceStatusOutput
		err  error
	)

	// Instance Status Request
	resp, err = e.client.DescribeInstanceStatus(input)
	if err != nil {
		return err
	}

	// Assign Instance Status Result List into Object
	e.InstanceStatuses = resp.InstanceStatuses
	// In the case Instance Status Result has NextToken
	for resp.NextToken != nil {
		input.NextToken = resp.NextToken
		resp, err = e.client.DescribeInstanceStatus(input)
		if err != nil {
			return err
		}
		e.InstanceStatuses = append(e.InstanceStatuses, resp.InstanceStatuses...)
	}

	// InstanceNames
	e.instanceNames()

	return nil
}
Exemplo n.º 2
0
func (c *Client) instanceStatuses(params *ec2.DescribeInstanceStatusInput) (statuses []*ec2.InstanceStatus, err error) {
	if params == nil {
		params = &ec2.DescribeInstanceStatusInput{}
	}
	// Update MaxResults param if no filtering options were set.
	if params.Filters == nil && params.InstanceIds == nil && c.MaxResults != 0 {
		params.MaxResults = aws.Int64(c.MaxResults)
	}
	var page int
	return statuses, c.EC2.DescribeInstanceStatusPages(params, func(resp *ec2.DescribeInstanceStatusOutput, _ bool) bool {
		page++
		c.Log.Debug("received %d instance statuses (page=%d)", len(resp.InstanceStatuses), page)
		statuses = append(statuses, resp.InstanceStatuses...)
		return true
	})
}