api_op_GetRegion.go

 1package imds
 2
 3import (
 4	"context"
 5	"fmt"
 6
 7	"github.com/aws/smithy-go/middleware"
 8	smithyhttp "github.com/aws/smithy-go/transport/http"
 9)
10
11// GetRegion retrieves an identity document describing an
12// instance. Error is returned if the request fails or is unable to parse
13// the response.
14func (c *Client) GetRegion(
15	ctx context.Context, params *GetRegionInput, optFns ...func(*Options),
16) (
17	*GetRegionOutput, error,
18) {
19	if params == nil {
20		params = &GetRegionInput{}
21	}
22
23	result, metadata, err := c.invokeOperation(ctx, "GetRegion", params, optFns,
24		addGetRegionMiddleware,
25	)
26	if err != nil {
27		return nil, err
28	}
29
30	out := result.(*GetRegionOutput)
31	out.ResultMetadata = metadata
32	return out, nil
33}
34
35// GetRegionInput provides the input parameters for GetRegion operation.
36type GetRegionInput struct{}
37
38// GetRegionOutput provides the output parameters for GetRegion operation.
39type GetRegionOutput struct {
40	Region string
41
42	ResultMetadata middleware.Metadata
43}
44
45func addGetRegionMiddleware(stack *middleware.Stack, options Options) error {
46	return addAPIRequestMiddleware(stack,
47		options,
48		"GetRegion",
49		buildGetInstanceIdentityDocumentPath,
50		buildGetRegionOutput,
51	)
52}
53
54func buildGetRegionOutput(resp *smithyhttp.Response) (interface{}, error) {
55	out, err := buildGetInstanceIdentityDocumentOutput(resp)
56	if err != nil {
57		return nil, err
58	}
59
60	result, ok := out.(*GetInstanceIdentityDocumentOutput)
61	if !ok {
62		return nil, fmt.Errorf("unexpected instance identity document type, %T", out)
63	}
64
65	region := result.Region
66	if len(region) == 0 {
67		return "", fmt.Errorf("instance metadata did not return a region value")
68	}
69
70	return &GetRegionOutput{
71		Region: region,
72	}, nil
73}