Update Go AWS SDK to the latest version

This commit is contained in:
Andrey Smirnov
2019-07-13 00:03:55 +03:00
committed by Andrey Smirnov
parent d08be990ef
commit 94a72b23ff
2183 changed files with 885887 additions and 228114 deletions
+261 -32
View File
@@ -13,18 +13,58 @@ import (
// An Operation defines a specific API Operation.
type Operation struct {
API *API `json:"-"`
ExportedName string
Name string
Documentation string
HTTP HTTPInfo
InputRef ShapeRef `json:"input"`
OutputRef ShapeRef `json:"output"`
ErrorRefs []ShapeRef `json:"errors"`
Paginator *Paginator
Deprecated bool `json:"deprecated"`
AuthType string `json:"authtype"`
imports map[string]bool
API *API `json:"-"`
ExportedName string
Name string
Documentation string
HTTP HTTPInfo
Host string `json:"host"`
InputRef ShapeRef `json:"input"`
OutputRef ShapeRef `json:"output"`
ErrorRefs []ShapeRef `json:"errors"`
Paginator *Paginator
Deprecated bool `json:"deprecated"`
DeprecatedMsg string `json:"deprecatedMessage"`
AuthType AuthType `json:"authtype"`
imports map[string]bool
CustomBuildHandlers []string
EventStreamAPI *EventStreamAPI
IsEndpointDiscoveryOp bool `json:"endpointoperation"`
EndpointDiscovery *EndpointDiscovery `json:"endpointdiscovery"`
Endpoint *EndpointTrait `json:"endpoint"`
}
// EndpointTrait provides the structure of the modeled enpdoint trait, and its
// properties.
type EndpointTrait struct {
// Specifies the hostPrefix template to prepend to the operation's request
// endpoint host.
HostPrefix string `json:"hostPrefix"`
}
// EndpointDiscovery represents a map of key values pairs that represents
// metadata about how a given API will make a call to the discovery endpoint.
type EndpointDiscovery struct {
// Required indicates that for a given operation that endpoint is required.
// Any required endpoint discovery operation cannot have endpoint discovery
// turned off.
Required bool `json:"required"`
}
// OperationForMethod returns the API operation name that corresponds to the
// client method name provided.
func (a *API) OperationForMethod(name string) *Operation {
for _, op := range a.Operations {
for _, m := range op.Methods() {
if m == name {
return op
}
}
}
return nil
}
// A HTTPInfo defines the method of HTTP request for the Operation.
@@ -34,6 +74,24 @@ type HTTPInfo struct {
ResponseCode uint
}
// Methods Returns a list of method names that will be generated.
func (o *Operation) Methods() []string {
methods := []string{
o.ExportedName,
o.ExportedName + "Request",
o.ExportedName + "WithContext",
}
if o.Paginator != nil {
methods = append(methods, []string{
o.ExportedName + "Pages",
o.ExportedName + "PagesWithContext",
}...)
}
return methods
}
// HasInput returns if the Operation accepts an input paramater
func (o *Operation) HasInput() bool {
return o.InputRef.ShapeName != ""
@@ -44,17 +102,27 @@ func (o *Operation) HasOutput() bool {
return o.OutputRef.ShapeName != ""
}
func (o *Operation) GetSigner() string {
if o.AuthType == "v4-unsigned-body" {
o.API.imports["github.com/aws/aws-sdk-go/aws/signer/v4"] = true
}
// AuthType provides the enumeration of AuthType trait.
type AuthType string
// Enumeration values for AuthType trait
const (
NoneAuthType AuthType = "none"
V4UnsignedBodyAuthType AuthType = "v4-unsigned-body"
)
// GetSigner returns the signer that should be used for a API request.
func (o *Operation) GetSigner() string {
buf := bytes.NewBuffer(nil)
switch o.AuthType {
case "none":
case NoneAuthType:
o.API.AddSDKImport("aws/credentials")
buf.WriteString("req.Config.Credentials = credentials.AnonymousCredentials")
case "v4-unsigned-body":
case V4UnsignedBodyAuthType:
o.API.AddSDKImport("aws/signer/v4")
buf.WriteString("req.Handlers.Sign.Remove(v4.SignRequestHandler)\n")
buf.WriteString("handler := v4.BuildNamedHandler(\"v4.CustomSignerHandler\", v4.WithUnsignedPayload)\n")
buf.WriteString("req.Handlers.Sign.PushFrontNamed(handler)")
@@ -64,16 +132,18 @@ func (o *Operation) GetSigner() string {
return buf.String()
}
// tplOperation defines a template for rendering an API Operation
var tplOperation = template.Must(template.New("operation").Funcs(template.FuncMap{
"GetCrosslinkURL": GetCrosslinkURL,
// operationTmpl defines a template for rendering an API Operation
var operationTmpl = template.Must(template.New("operation").Funcs(template.FuncMap{
"GetCrosslinkURL": GetCrosslinkURL,
"EnableStopOnSameToken": enableStopOnSameToken,
"GetDeprecatedMsg": getDeprecatedMessage,
}).Parse(`
const op{{ .ExportedName }} = "{{ .Name }}"
// {{ .ExportedName }}Request generates a "aws/request.Request" representing the
// client's request for the {{ .ExportedName }} operation. The "output" return
// value will be populated with the request's response once the request completes
// successfuly.
// successfully.
//
// Use "Send" method on the returned Request to send the API call to the service.
// the "output" return value is not valid until after Send returns without error.
@@ -93,16 +163,19 @@ const op{{ .ExportedName }} = "{{ .Name }}"
// fmt.Println(resp)
// }
{{ $crosslinkURL := GetCrosslinkURL $.API.BaseCrosslinkURL $.API.Metadata.UID $.ExportedName -}}
{{ if ne $crosslinkURL "" -}}
{{ if ne $crosslinkURL "" -}}
//
// See also, {{ $crosslinkURL }}
{{ end -}}
{{- if .Deprecated }}//
// Deprecated: {{ GetDeprecatedMsg .DeprecatedMsg .ExportedName }}
{{ end -}}
func (c *{{ .API.StructName }}) {{ .ExportedName }}Request(` +
`input {{ .InputRef.GoType }}) (req *request.Request, output {{ .OutputRef.GoType }}) {
{{ if (or .Deprecated (or .InputRef.Deprecated .OutputRef.Deprecated)) }}if c.Client.Config.Logger != nil {
c.Client.Config.Logger.Log("This operation, {{ .ExportedName }}, has been deprecated")
}
op := &request.Operation{ {{ else }} op := &request.Operation{ {{ end }}
op := &request.Operation{ {{ else }} op := &request.Operation{ {{ end }}
Name: op{{ .ExportedName }},
{{ if ne .HTTP.Method "" }}HTTPMethod: "{{ .HTTP.Method }}",
{{ end }}HTTPPath: {{ if ne .HTTP.RequestURI "" }}"{{ .HTTP.RequestURI }}"{{ else }}"/"{{ end }},
@@ -120,10 +193,57 @@ func (c *{{ .API.StructName }}) {{ .ExportedName }}Request(` +
}
output = &{{ .OutputRef.GoTypeElem }}{}
req = c.newRequest(op, input, output){{ if eq .OutputRef.Shape.Placeholder true }}
req.Handlers.Unmarshal.Remove({{ .API.ProtocolPackage }}.UnmarshalHandler)
req.Handlers.Unmarshal.PushBackNamed(protocol.UnmarshalDiscardBodyHandler){{ end }}
{{ if ne .AuthType "" }}{{ .GetSigner }}{{ end -}}
req = c.newRequest(op, input, output)
{{ if ne .AuthType "" }}{{ .GetSigner }}{{ end }}
{{- if .ShouldDiscardResponse -}}
{{- $_ := .API.AddSDKImport "private/protocol" -}}
{{- $_ := .API.AddSDKImport "private/protocol" .API.ProtocolPackage -}}
req.Handlers.Unmarshal.Swap({{ .API.ProtocolPackage }}.UnmarshalHandler.Name, protocol.UnmarshalDiscardBodyHandler)
{{ else if .OutputRef.Shape.EventStreamsMemberName -}}
{{- $_ := .API.AddSDKImport "private/protocol" .API.ProtocolPackage -}}
{{- $_ := .API.AddSDKImport "private/protocol/rest" -}}
req.Handlers.Send.Swap(client.LogHTTPResponseHandler.Name, client.LogHTTPResponseHeaderHandler)
req.Handlers.Unmarshal.Swap({{ .API.ProtocolPackage }}.UnmarshalHandler.Name, rest.UnmarshalHandler)
req.Handlers.Unmarshal.PushBack(output.runEventStreamLoop)
{{ if eq .API.Metadata.Protocol "json" -}}
req.Handlers.Unmarshal.PushBack(output.unmarshalInitialResponse)
{{ end -}}
{{ end -}}
{{ if .EndpointDiscovery -}}
{{if not .EndpointDiscovery.Required -}}
if aws.BoolValue(req.Config.EnableEndpointDiscovery) {
{{end -}}
de := discoverer{{ .API.EndpointDiscoveryOp.Name }}{
Required: {{ .EndpointDiscovery.Required }},
EndpointCache: c.endpointCache,
Params: map[string]*string{
"op": aws.String(req.Operation.Name),
{{ range $key, $ref := .InputRef.Shape.MemberRefs -}}
{{ if $ref.EndpointDiscoveryID -}}
"{{ $ref.OrigShapeName }}": input.{{ $key }},
{{ end -}}
{{- end }}
},
Client: c,
}
for k, v := range de.Params {
if v == nil {
delete(de.Params, k)
}
}
req.Handlers.Build.PushFrontNamed(request.NamedHandler{
Name: "crr.endpointdiscovery",
Fn: de.Handler,
})
{{if not .EndpointDiscovery.Required -}}
}
{{ end -}}
{{ end -}}
{{- range $_, $handler := $.CustomBuildHandlers -}}
req.Handlers.Build.PushBackNamed({{ $handler }})
{{ end -}}
return
}
@@ -151,9 +271,12 @@ func (c *{{ .API.StructName }}) {{ .ExportedName }}Request(` +
{{ end -}}
{{ end -}}
{{ $crosslinkURL := GetCrosslinkURL $.API.BaseCrosslinkURL $.API.Metadata.UID $.ExportedName -}}
{{ if ne $crosslinkURL "" -}}
{{ if ne $crosslinkURL "" -}}
// See also, {{ $crosslinkURL }}
{{ end -}}
{{- if .Deprecated }}//
// Deprecated: {{ GetDeprecatedMsg .DeprecatedMsg .ExportedName }}
{{ end -}}
func (c *{{ .API.StructName }}) {{ .ExportedName }}(` +
`input {{ .InputRef.GoType }}) ({{ .OutputRef.GoType }}, error) {
req, out := c.{{ .ExportedName }}Request(input)
@@ -169,6 +292,9 @@ func (c *{{ .API.StructName }}) {{ .ExportedName }}(` +
// the context is nil a panic will occur. In the future the SDK may create
// sub-contexts for http.Requests. See https://golang.org/pkg/context/
// for more information on using Contexts.
{{ if .Deprecated }}//
// Deprecated: {{ GetDeprecatedMsg .DeprecatedMsg (printf "%s%s" .ExportedName "WithContext") }}
{{ end -}}
func (c *{{ .API.StructName }}) {{ .ExportedName }}WithContext(` +
`ctx aws.Context, input {{ .InputRef.GoType }}, opts ...request.Option) ` +
`({{ .OutputRef.GoType }}, error) {
@@ -190,12 +316,15 @@ func (c *{{ .API.StructName }}) {{ .ExportedName }}WithContext(` +
// // Example iterating over at most 3 pages of a {{ .ExportedName }} operation.
// pageNum := 0
// err := client.{{ .ExportedName }}Pages(params,
// func(page {{ .OutputRef.GoType }}, lastPage bool) bool {
// func(page {{ .OutputRef.Shape.GoTypeWithPkgName }}, lastPage bool) bool {
// pageNum++
// fmt.Println(page)
// return pageNum <= 3
// })
//
{{ if .Deprecated }}//
// Deprecated: {{ GetDeprecatedMsg .DeprecatedMsg (printf "%s%s" .ExportedName "Pages") }}
{{ end -}}
func (c *{{ .API.StructName }}) {{ .ExportedName }}Pages(` +
`input {{ .InputRef.GoType }}, fn func({{ .OutputRef.GoType }}, bool) bool) error {
return c.{{ .ExportedName }}PagesWithContext(aws.BackgroundContext(), input, fn)
@@ -208,12 +337,17 @@ func (c *{{ .API.StructName }}) {{ .ExportedName }}Pages(` +
// the context is nil a panic will occur. In the future the SDK may create
// sub-contexts for http.Requests. See https://golang.org/pkg/context/
// for more information on using Contexts.
{{ if .Deprecated }}//
// Deprecated: {{ GetDeprecatedMsg .DeprecatedMsg (printf "%s%s" .ExportedName "PagesWithContext") }}
{{ end -}}
func (c *{{ .API.StructName }}) {{ .ExportedName }}PagesWithContext(` +
`ctx aws.Context, ` +
`input {{ .InputRef.GoType }}, ` +
`fn func({{ .OutputRef.GoType }}, bool) bool, ` +
`opts ...request.Option) error {
p := request.Pagination {
{{ if EnableStopOnSameToken .API.PackageName -}}EndPageOnSameToken: true,
{{ end -}}
NewRequest: func() (*request.Request, error) {
var inCpy {{ .InputRef.GoType }}
if input != nil {
@@ -234,12 +368,100 @@ func (c *{{ .API.StructName }}) {{ .ExportedName }}PagesWithContext(` +
return p.Err()
}
{{ end }}
{{ if .IsEndpointDiscoveryOp -}}
type discoverer{{ .ExportedName }} struct {
Client *{{ .API.StructName }}
Required bool
EndpointCache *crr.EndpointCache
Params map[string]*string
Key string
}
func (d *discoverer{{ .ExportedName }}) Discover() (crr.Endpoint, error) {
input := &{{ .API.EndpointDiscoveryOp.InputRef.ShapeName }}{
{{ if .API.EndpointDiscoveryOp.InputRef.Shape.HasMember "Operation" -}}
Operation: d.Params["op"],
{{ end -}}
{{ if .API.EndpointDiscoveryOp.InputRef.Shape.HasMember "Identifiers" -}}
Identifiers: d.Params,
{{ end -}}
}
resp, err := d.Client.{{ .API.EndpointDiscoveryOp.Name }}(input)
if err != nil {
return crr.Endpoint{}, err
}
endpoint := crr.Endpoint{
Key: d.Key,
}
for _, e := range resp.Endpoints {
if e.Address == nil {
continue
}
cachedInMinutes := aws.Int64Value(e.CachePeriodInMinutes)
u, err := url.Parse(*e.Address)
if err != nil {
continue
}
addr := crr.WeightedAddress{
URL: u,
Expired: time.Now().Add(time.Duration(cachedInMinutes) * time.Minute),
}
endpoint.Add(addr)
}
d.EndpointCache.Add(endpoint)
return endpoint, nil
}
func (d *discoverer{{ .ExportedName }}) Handler(r *request.Request) {
endpointKey := crr.BuildEndpointKey(d.Params)
d.Key = endpointKey
endpoint, err := d.EndpointCache.Get(d, endpointKey, d.Required)
if err != nil {
r.Error = err
return
}
if endpoint.URL != nil && len(endpoint.URL.String()) > 0 {
r.HTTPRequest.URL = endpoint.URL
}
}
{{ end -}}
`))
// GoCode returns a string of rendered GoCode for this Operation
func (o *Operation) GoCode() string {
var buf bytes.Buffer
err := tplOperation.Execute(&buf, o)
if len(o.OutputRef.Shape.EventStreamsMemberName) != 0 {
o.API.AddSDKImport("aws/client")
o.API.AddSDKImport("private/protocol")
o.API.AddSDKImport("private/protocol/rest")
o.API.AddSDKImport("private/protocol", o.API.ProtocolPackage())
}
if o.API.EndpointDiscoveryOp != nil {
o.API.AddSDKImport("aws/crr")
o.API.AddImport("time")
o.API.AddImport("net/url")
}
if o.Endpoint != nil && len(o.Endpoint.HostPrefix) != 0 {
setupEndpointHostPrefix(o)
}
err := operationTmpl.Execute(&buf, o)
if err != nil {
panic(err)
}
@@ -308,7 +530,7 @@ func (o *Operation) Example() string {
func (o *Operation) ExampleInput() string {
if len(o.InputRef.Shape.MemberRefs) == 0 {
if strings.Contains(o.InputRef.GoTypeElem(), ".") {
o.imports["github.com/aws/aws-sdk-go/service/"+strings.Split(o.InputRef.GoTypeElem(), ".")[0]] = true
o.imports[SDKImportRoot+"service/"+strings.Split(o.InputRef.GoTypeElem(), ".")[0]] = true
return fmt.Sprintf("var params *%s", o.InputRef.GoTypeElem())
}
return fmt.Sprintf("var params *%s.%s",
@@ -318,6 +540,13 @@ func (o *Operation) ExampleInput() string {
return "params := " + e.traverseAny(o.InputRef.Shape, false, false)
}
// ShouldDiscardResponse returns if the operation should discard the response
// returned by the service.
func (o *Operation) ShouldDiscardResponse() bool {
s := o.OutputRef.Shape
return s.Placeholder || len(s.MemberRefs) == 0
}
// A example provides
type example struct {
*Operation