1package imds
2
3import (
4	"context"
5	"fmt"
6	"io"
7
8	"github.com/aws/smithy-go/middleware"
9	smithyhttp "github.com/aws/smithy-go/transport/http"
10)
11
12const getMetadataPath = "/latest/meta-data"
13
14// GetMetadata uses the path provided to request information from the Amazon
15// EC2 Instance Metadata Service. The content will be returned as a string, or
16// error if the request failed.
17func (c *Client) GetMetadata(ctx context.Context, params *GetMetadataInput, optFns ...func(*Options)) (*GetMetadataOutput, error) {
18	if params == nil {
19		params = &GetMetadataInput{}
20	}
21
22	result, metadata, err := c.invokeOperation(ctx, "GetMetadata", params, optFns,
23		addGetMetadataMiddleware,
24	)
25	if err != nil {
26		return nil, err
27	}
28
29	out := result.(*GetMetadataOutput)
30	out.ResultMetadata = metadata
31	return out, nil
32}
33
34// GetMetadataInput provides the input parameters for the GetMetadata
35// operation.
36type GetMetadataInput struct {
37	// The relative metadata path to retrieve. Can be empty string to retrieve
38	// a response containing a new line separated list of metadata resources
39	// available.
40	//
41	// Must not include the metadata base path.
42	//
43	// May include leading slash. If Path includes trailing slash the trailing slash
44	// will be included in the request for the resource.
45	Path string
46}
47
48// GetMetadataOutput provides the output parameters for the GetMetadata
49// operation.
50type GetMetadataOutput struct {
51	Content io.ReadCloser
52
53	ResultMetadata middleware.Metadata
54}
55
56func addGetMetadataMiddleware(stack *middleware.Stack, options Options) error {
57	return addAPIRequestMiddleware(stack,
58		options,
59		buildGetMetadataPath,
60		buildGetMetadataOutput)
61}
62
63func buildGetMetadataPath(params interface{}) (string, error) {
64	p, ok := params.(*GetMetadataInput)
65	if !ok {
66		return "", fmt.Errorf("unknown parameter type %T", params)
67	}
68
69	return appendURIPath(getMetadataPath, p.Path), nil
70}
71
72func buildGetMetadataOutput(resp *smithyhttp.Response) (interface{}, error) {
73	return &GetMetadataOutput{
74		Content: resp.Body,
75	}, nil
76}
77