Skip to content

Commit

Permalink
Add EC2 DescribeInstanceStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
eherde committed Nov 28, 2014
1 parent 644f4b4 commit dd9237c
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
88 changes: 88 additions & 0 deletions ec2/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,94 @@ func (ec2 *EC2) DescribeInstances(instIds []string, filter *Filter) (resp *Descr
return
}

// DescribeInstanceStatusOptions encapsulates the query parameters for the corresponding action.
//
// See http:////goo.gl/2FBTdS for more details.
type DescribeInstanceStatusOptions struct {
InstanceIds []string // If non-empty, limit the query to this subset of instances. Maximum length of 100.
IncludeAllInstances bool // If true, describe all instances, instead of just running instances (the default).
MaxResults int // Maximum number of results to return. Minimum of 5. Maximum of 1000.
NextToken string // The token for the next set of items to return. (You received this token from a prior call.)
}

// Response to a DescribeInstanceStatus request.
//
// See http://goo.gl/2FBTdS for more details.
type DescribeInstanceStatusResp struct {
RequestId string `xml:"requestId"`
InstanceStatusSet []InstanceStatusItem `xml:"instanceStatusSet>item"`
NextToken string `xml:"nextToken"`
}

// InstanceStatusItem describes the instance status, cause, details, and potential actions to take in response.
//
// See http://goo.gl/oImFZZ for more details.
type InstanceStatusItem struct {
InstanceId string `xml:"instanceId"`
AvailabilityZone string `xml:"availabilityZone"`
Events []InstanceStatusEvent `xml:"eventsSet>item"` // Extra information regarding events associated with the instance.
InstanceState InstanceState `xml:"instanceState"` // The intended state of the instance. Calls to DescribeInstanceStatus require that an instance be in the running state.
SystemStatus InstanceStatus `xml:"systemStatus"`
InstanceStatus InstanceStatus `xml:"instanceStatus"`
}

// InstanceStatusEvent describes an instance event.
//
// See http://goo.gl/PXsDTn for more details.
type InstanceStatusEvent struct {
Code string `xml:"code"` // The associated code of the event.
Description string `xml:"description"` // A description of the event.
NotBefore string `xml:"notBefore"` // The earliest scheduled start time for the event.
NotAfter string `xml:"notAfter"` // The latest scheduled end time for the event.
}

// InstanceStatus describes the status of an instance with details.
//
// See http://goo.gl/eFch4S for more details.
type InstanceStatus struct {
Status string `xml:"status"` // The instance status.
Details InstanceStatusDetails `xml:"details"` // The system instance health or application instance health.
}

// InstanceStatusDetails describes the instance status with the cause and more detail.
//
// See http://goo.gl/3qoMC4 for more details.
type InstanceStatusDetails struct {
Name string `xml:"name"` // The type of instance status.
Status string `xml:"status"` // The status.
ImpairedSince string `xml:"impairedSince"` // The time when a status check failed. For an instance that was launched and impaired, this is the time when the instance was launched.
}

// DescribeInstanceStatus returns instance status information about instances in EC2.
// instIds and filter are optional, and if provided will limit the instances returned to those
// matching the given instance ids or filtering rules.
// all determines whether to report all matching instances or only those in the running state
//
// See http://goo.gl/2FBTdS for more details.
func (ec2 *EC2) DescribeInstanceStatus(options *DescribeInstanceStatusOptions, filter *Filter) (resp *DescribeInstanceStatusResp, err error) {
params := makeParams("DescribeInstanceStatus")
if len(options.InstanceIds) > 0 {
addParamsList(params, "InstanceId", options.InstanceIds)
}
if options.IncludeAllInstances {
params["IncludeAllInstances"] = "true"
}
if options.MaxResults != 0 {
params["MaxResults"] = strconv.Itoa(options.MaxResults)
}
if options.NextToken != "" {
params["NextToken"] = options.NextToken
}
filter.addParams(params)
resp = &DescribeInstanceStatusResp{}
err = ec2.query(params, resp)
if err != nil {
fmt.Println(err)
return nil, err
}
return
}

// ----------------------------------------------------------------------------
// KeyPair management functions and types.

Expand Down
36 changes: 36 additions & 0 deletions ec2/ec2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,42 @@ func (s *S) TestDescribeInstancesExample2(c *gocheck.C) {
c.Assert(r0t1.Value, gocheck.Equals, "Production")
}

func (s *S) TestDescribeInstanceStatusExample(c *gocheck.C) {
testServer.Response(200, nil, DescribeInstanceStatusExample)

resp, err := s.ec2.DescribeInstanceStatus(&ec2.DescribeInstanceStatusOptions{}, nil)

req := testServer.WaitRequest()
c.Assert(req.Form["Action"], gocheck.DeepEquals, []string{"DescribeInstanceStatus"})

c.Assert(err, gocheck.IsNil)
c.Assert(resp.RequestId, gocheck.Equals, "59dbff89-35bd-4eac-99ed-be587EXAMPLE")
c.Assert(resp.InstanceStatusSet, gocheck.HasLen, 1)
c.Assert(resp.NextToken, gocheck.Equals, "exampleToken")

i0 := resp.InstanceStatusSet[0]
c.Assert(i0.InstanceId, gocheck.Equals, "i-c7cd56ad")
c.Assert(i0.AvailabilityZone, gocheck.Equals, "us-east-1b")
c.Assert(i0.Events, gocheck.HasLen, 1)

e0 := i0.Events[0]
c.Assert(e0.Code, gocheck.Equals, "instance-reboot")
c.Assert(e0.Description, gocheck.Equals, "example description")
c.Assert(e0.NotBefore, gocheck.Equals, "2010-08-17T01:15:18.000Z")
c.Assert(e0.NotAfter, gocheck.Equals, "2010-08-17T01:15:18.000Z")

c.Assert(i0.InstanceState.Code, gocheck.Equals, 16)
c.Assert(i0.InstanceState.Name, gocheck.Equals, "running")
c.Assert(i0.SystemStatus.Status, gocheck.Equals, "ok")
c.Assert(i0.SystemStatus.Details.Name, gocheck.Equals, "reachability")
c.Assert(i0.SystemStatus.Details.Status, gocheck.Equals, "passed")
c.Assert(i0.SystemStatus.Details.ImpairedSince, gocheck.Equals, "2010-08-17T01:15:18.000Z")
c.Assert(i0.InstanceStatus.Status, gocheck.Equals, "ok")
c.Assert(i0.InstanceStatus.Details.Name, gocheck.Equals, "reachability")
c.Assert(i0.InstanceStatus.Details.Status, gocheck.Equals, "passed")
c.Assert(i0.InstanceStatus.Details.ImpairedSince, gocheck.Equals, "2010-08-17T01:15:18.000Z")
}

func (s *S) TestDescribeAddressesPublicIPExample(c *gocheck.C) {
testServer.Response(200, nil, DescribeAddressesExample)

Expand Down
42 changes: 42 additions & 0 deletions ec2/responses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,48 @@ var DescribeInstancesExample2 = `
</DescribeInstancesResponse>
`

// http://goo.gl/2FBTdS
var DescribeInstanceStatusExample = `
<DescribeInstanceStatusResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<instanceStatusSet>
<item>
<instanceId>i-c7cd56ad</instanceId>
<availabilityZone>us-east-1b</availabilityZone>
<eventsSet>
<item>
<code>instance-reboot</code>
<description>example description</description>
<notBefore>2010-08-17T01:15:18.000Z</notBefore>
<notAfter>2010-08-17T01:15:18.000Z</notAfter>
</item>
</eventsSet>
<instanceState>
<code>16</code>
<name>running</name>
</instanceState>
<systemStatus>
<status>ok</status>
<details>
<name>reachability</name>
<status>passed</status>
<impairedSince>2010-08-17T01:15:18.000Z</impairedSince>
</details>
</systemStatus>
<instanceStatus>
<status>ok</status>
<details>
<name>reachability</name>
<status>passed</status>
<impairedSince>2010-08-17T01:15:18.000Z</impairedSince>
</details>
</instanceStatus>
</item>
</instanceStatusSet>
<nextToken>exampleToken</nextToken>
</DescribeInstanceStatusResponse>
`

// http://goo.gl/icuXh5
var ModifyInstanceExample = `
<ModifyImageAttributeResponse xmlns="http://ec2.amazonaws.com/doc/2013-06-15/">
Expand Down

0 comments on commit dd9237c

Please sign in to comment.