1package customizations
2
3import (
4	"context"
5	"fmt"
6
7	"github.com/aws/smithy-go"
8	"github.com/aws/smithy-go/middleware"
9	smithyhttp "github.com/aws/smithy-go/transport/http"
10)
11
12// AddSanitizeURLMiddlewareOptions provides the options for Route53SanitizeURL middleware setup
13type AddSanitizeURLMiddlewareOptions struct {
14	// functional pointer to sanitize hosted zone id member
15	// The function is intended to take an input value,
16	// look for hosted zone id input member and sanitize the value
17	// to strip out an excess `/hostedzone/` prefix that can be present in
18	// the hosted zone id input member.
19	//
20	// returns an error if any.
21	SanitizeHostedZoneIDInput func(interface{}) error
22}
23
24// AddSanitizeURLMiddleware add the middleware necessary to modify Route53 input before op serialization.
25func AddSanitizeURLMiddleware(stack *middleware.Stack, options AddSanitizeURLMiddlewareOptions) error {
26	return stack.Serialize.Insert(&sanitizeURL{
27		sanitizeHostedZoneIDInput: options.SanitizeHostedZoneIDInput,
28	}, "OperationSerializer", middleware.Before)
29}
30
31// sanitizeURL cleans up potential formatting issues in the Route53 path.
32//
33// Notably it will strip out an excess `/hostedzone/` prefix that can be present in
34// the hosted zone id input member. That excess prefix is there because some route53 apis return
35// the id in that format, so this middleware enables round-tripping those values.
36type sanitizeURL struct {
37	sanitizeHostedZoneIDInput func(interface{}) error
38}
39
40// ID returns the id for the middleware.
41func (*sanitizeURL) ID() string {
42	return "Route53:SanitizeURL"
43}
44
45// HandleSerialize implements the SerializeMiddleware interface.
46func (m *sanitizeURL) HandleSerialize(
47	ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler,
48) (
49	out middleware.SerializeOutput, metadata middleware.Metadata, err error,
50) {
51	_, ok := in.Request.(*smithyhttp.Request)
52	if !ok {
53		return out, metadata, &smithy.SerializationError{
54			Err: fmt.Errorf("unknown request type %T", in.Request),
55		}
56	}
57
58	if err := m.sanitizeHostedZoneIDInput(in.Parameters); err != nil {
59		return out, metadata, err
60	}
61
62	return next.HandleSerialize(ctx, in)
63}
64