]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/aws/aws-sdk-go/aws/ec2metadata/api.go
Initial transfer of provider code
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / aws / aws-sdk-go / aws / ec2metadata / api.go
1 package ec2metadata
2
3 import (
4 "encoding/json"
5 "fmt"
6 "net/http"
7 "path"
8 "strings"
9 "time"
10
11 "github.com/aws/aws-sdk-go/aws/awserr"
12 "github.com/aws/aws-sdk-go/aws/request"
13 )
14
15 // GetMetadata uses the path provided to request information from the EC2
16 // instance metdata service. The content will be returned as a string, or
17 // error if the request failed.
18 func (c *EC2Metadata) GetMetadata(p string) (string, error) {
19 op := &request.Operation{
20 Name: "GetMetadata",
21 HTTPMethod: "GET",
22 HTTPPath: path.Join("/", "meta-data", p),
23 }
24
25 output := &metadataOutput{}
26 req := c.NewRequest(op, nil, output)
27
28 return output.Content, req.Send()
29 }
30
31 // GetUserData returns the userdata that was configured for the service. If
32 // there is no user-data setup for the EC2 instance a "NotFoundError" error
33 // code will be returned.
34 func (c *EC2Metadata) GetUserData() (string, error) {
35 op := &request.Operation{
36 Name: "GetUserData",
37 HTTPMethod: "GET",
38 HTTPPath: path.Join("/", "user-data"),
39 }
40
41 output := &metadataOutput{}
42 req := c.NewRequest(op, nil, output)
43 req.Handlers.UnmarshalError.PushBack(func(r *request.Request) {
44 if r.HTTPResponse.StatusCode == http.StatusNotFound {
45 r.Error = awserr.New("NotFoundError", "user-data not found", r.Error)
46 }
47 })
48
49 return output.Content, req.Send()
50 }
51
52 // GetDynamicData uses the path provided to request information from the EC2
53 // instance metadata service for dynamic data. The content will be returned
54 // as a string, or error if the request failed.
55 func (c *EC2Metadata) GetDynamicData(p string) (string, error) {
56 op := &request.Operation{
57 Name: "GetDynamicData",
58 HTTPMethod: "GET",
59 HTTPPath: path.Join("/", "dynamic", p),
60 }
61
62 output := &metadataOutput{}
63 req := c.NewRequest(op, nil, output)
64
65 return output.Content, req.Send()
66 }
67
68 // GetInstanceIdentityDocument retrieves an identity document describing an
69 // instance. Error is returned if the request fails or is unable to parse
70 // the response.
71 func (c *EC2Metadata) GetInstanceIdentityDocument() (EC2InstanceIdentityDocument, error) {
72 resp, err := c.GetDynamicData("instance-identity/document")
73 if err != nil {
74 return EC2InstanceIdentityDocument{},
75 awserr.New("EC2MetadataRequestError",
76 "failed to get EC2 instance identity document", err)
77 }
78
79 doc := EC2InstanceIdentityDocument{}
80 if err := json.NewDecoder(strings.NewReader(resp)).Decode(&doc); err != nil {
81 return EC2InstanceIdentityDocument{},
82 awserr.New("SerializationError",
83 "failed to decode EC2 instance identity document", err)
84 }
85
86 return doc, nil
87 }
88
89 // IAMInfo retrieves IAM info from the metadata API
90 func (c *EC2Metadata) IAMInfo() (EC2IAMInfo, error) {
91 resp, err := c.GetMetadata("iam/info")
92 if err != nil {
93 return EC2IAMInfo{},
94 awserr.New("EC2MetadataRequestError",
95 "failed to get EC2 IAM info", err)
96 }
97
98 info := EC2IAMInfo{}
99 if err := json.NewDecoder(strings.NewReader(resp)).Decode(&info); err != nil {
100 return EC2IAMInfo{},
101 awserr.New("SerializationError",
102 "failed to decode EC2 IAM info", err)
103 }
104
105 if info.Code != "Success" {
106 errMsg := fmt.Sprintf("failed to get EC2 IAM Info (%s)", info.Code)
107 return EC2IAMInfo{},
108 awserr.New("EC2MetadataError", errMsg, nil)
109 }
110
111 return info, nil
112 }
113
114 // Region returns the region the instance is running in.
115 func (c *EC2Metadata) Region() (string, error) {
116 resp, err := c.GetMetadata("placement/availability-zone")
117 if err != nil {
118 return "", err
119 }
120
121 // returns region without the suffix. Eg: us-west-2a becomes us-west-2
122 return resp[:len(resp)-1], nil
123 }
124
125 // Available returns if the application has access to the EC2 Metadata service.
126 // Can be used to determine if application is running within an EC2 Instance and
127 // the metadata service is available.
128 func (c *EC2Metadata) Available() bool {
129 if _, err := c.GetMetadata("instance-id"); err != nil {
130 return false
131 }
132
133 return true
134 }
135
136 // An EC2IAMInfo provides the shape for unmarshaling
137 // an IAM info from the metadata API
138 type EC2IAMInfo struct {
139 Code string
140 LastUpdated time.Time
141 InstanceProfileArn string
142 InstanceProfileID string
143 }
144
145 // An EC2InstanceIdentityDocument provides the shape for unmarshaling
146 // an instance identity document
147 type EC2InstanceIdentityDocument struct {
148 DevpayProductCodes []string `json:"devpayProductCodes"`
149 AvailabilityZone string `json:"availabilityZone"`
150 PrivateIP string `json:"privateIp"`
151 Version string `json:"version"`
152 Region string `json:"region"`
153 InstanceID string `json:"instanceId"`
154 BillingProducts []string `json:"billingProducts"`
155 InstanceType string `json:"instanceType"`
156 AccountID string `json:"accountId"`
157 PendingTime time.Time `json:"pendingTime"`
158 ImageID string `json:"imageId"`
159 KernelID string `json:"kernelId"`
160 RamdiskID string `json:"ramdiskId"`
161 Architecture string `json:"architecture"`
162 }