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