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
+144 -68
View File
@@ -5,17 +5,18 @@ package api
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"text/template"
"unicode"
)
// SDKImportRoot is the root import path of the SDK.
const SDKImportRoot = "github.com/aws/aws-sdk-go"
// An API defines a service API's definition. and logic to serialize the definition.
type API struct {
Metadata Metadata
@@ -24,6 +25,7 @@ type API struct {
Waiters []Waiter
Documentation string
Examples Examples
SmokeTests SmokeTestSuite
// Set to true to avoid removing unused shapes
NoRemoveUnusedShapes bool
@@ -46,7 +48,7 @@ type API struct {
// Set to true to not generate struct field accessors
NoGenStructFieldAccessors bool
SvcClientImportPath string
BaseImportPath string
initialized bool
imports map[string]bool
@@ -54,6 +56,10 @@ type API struct {
path string
BaseCrosslinkURL string
HasEventStream bool `json:"-"`
EndpointDiscoveryOp *Operation
}
// A Metadata is the metadata about an API's definition.
@@ -67,21 +73,18 @@ type Metadata struct {
JSONVersion string
TargetPrefix string
Protocol string
ProtocolSettings ProtocolSettings
UID string
EndpointsID string
ServiceID string
NoResolveEndpoint bool
}
var serviceAliases map[string]string
func Bootstrap() error {
b, err := ioutil.ReadFile(filepath.Join("..", "models", "customizations", "service-aliases.json"))
if err != nil {
return err
}
return json.Unmarshal(b, &serviceAliases)
// ProtocolSettings define how the SDK should handle requests in the context
// of of a protocol.
type ProtocolSettings struct {
HTTP2 string `json:"h2,omitempty"`
}
// PackageName name of the API package
@@ -89,6 +92,11 @@ func (a *API) PackageName() string {
return strings.ToLower(a.StructName())
}
// ImportPath returns the client's full import path
func (a *API) ImportPath() string {
return path.Join(a.BaseImportPath, a.PackageName())
}
// InterfacePackageName returns the package name for the interface.
func (a *API) InterfacePackageName() string {
return a.PackageName() + "iface"
@@ -135,11 +143,6 @@ func (a *API) StructName() string {
// Strip out spaces.
name = strings.Replace(name, " ", "", -1)
// Swap out for alias name if one is defined.
if alias, ok := serviceAliases[strings.ToLower(name)]; ok {
name = alias
}
a.name = name
return a.name
}
@@ -217,8 +220,8 @@ func (a *API) ShapeNames() []string {
func (a *API) ShapeList() []*Shape {
list := make([]*Shape, 0, len(a.Shapes))
for _, n := range a.ShapeNames() {
// Ignore error shapes in list
if s := a.Shapes[n]; !s.IsError {
// Ignore non-eventstream exception shapes in list.
if s := a.Shapes[n]; !(s.Exception && len(s.EventFor) == 0) {
list = append(list, s)
}
}
@@ -230,7 +233,7 @@ func (a *API) ShapeListErrors() []*Shape {
list := []*Shape{}
for _, n := range a.ShapeNames() {
// Ignore error shapes in list
if s := a.Shapes[n]; s.IsError {
if s := a.Shapes[n]; s.Exception {
list = append(list, s)
}
}
@@ -239,9 +242,7 @@ func (a *API) ShapeListErrors() []*Shape {
// resetImports resets the import map to default values.
func (a *API) resetImports() {
a.imports = map[string]bool{
"github.com/aws/aws-sdk-go/aws": true,
}
a.imports = map[string]bool{}
}
// importsGoCode returns the generated Go import code.
@@ -293,22 +294,28 @@ var tplAPI = template.Must(template.New("api").Parse(`
{{ end }}
`))
// AddImport adds the import path to the generated file's import.
func (a *API) AddImport(v string) error {
a.imports[v] = true
return nil
}
// AddSDKImport adds a SDK package import to the generated file's import.
func (a *API) AddSDKImport(v ...string) error {
e := make([]string, 0, 5)
e = append(e, SDKImportRoot)
e = append(e, v...)
a.imports[path.Join(e...)] = true
return nil
}
// APIGoCode renders the API in Go code. Returning it as a string
func (a *API) APIGoCode() string {
a.resetImports()
a.imports["github.com/aws/aws-sdk-go/aws/awsutil"] = true
a.imports["github.com/aws/aws-sdk-go/aws/request"] = true
if a.OperationHasOutputPlaceholder() {
a.imports["github.com/aws/aws-sdk-go/private/protocol/"+a.ProtocolPackage()] = true
a.imports["github.com/aws/aws-sdk-go/private/protocol"] = true
}
for _, op := range a.Operations {
if op.AuthType == "none" {
a.imports["github.com/aws/aws-sdk-go/aws/credentials"] = true
break
}
}
a.AddSDKImport("aws")
a.AddSDKImport("aws/awsutil")
a.AddSDKImport("aws/request")
var buf bytes.Buffer
err := tplAPI.Execute(&buf, a)
@@ -407,7 +414,7 @@ var tplServiceDoc = template.Must(template.New("service docs").Funcs(template.Fu
//
// See the SDK's documentation for more information on how to use the SDK.
// https://docs.aws.amazon.com/sdk-for-go/api/
//
//
// See aws.Config documentation for more information on configuring SDK clients.
// https://docs.aws.amazon.com/sdk-for-go/api/aws/#Config
//
@@ -416,30 +423,62 @@ var tplServiceDoc = template.Must(template.New("service docs").Funcs(template.Fu
// https://docs.aws.amazon.com/sdk-for-go/api/service/{{ .PackageName }}/#New
`))
var serviceIDRegex = regexp.MustCompile("[^a-zA-Z0-9 ]+")
var prefixDigitRegex = regexp.MustCompile("^[0-9]+")
// ServiceID will return a unique identifier specific to a service.
func ServiceID(a *API) string {
if len(a.Metadata.ServiceID) > 0 {
return a.Metadata.ServiceID
}
name := a.Metadata.ServiceAbbreviation
if len(name) == 0 {
name = a.Metadata.ServiceFullName
}
name = strings.Replace(name, "Amazon", "", -1)
name = strings.Replace(name, "AWS", "", -1)
name = serviceIDRegex.ReplaceAllString(name, "")
name = prefixDigitRegex.ReplaceAllString(name, "")
name = strings.TrimSpace(name)
return name
}
// A tplService defines the template for the service generated code.
var tplService = template.Must(template.New("service").Funcs(template.FuncMap{
"ServiceNameConstValue": ServiceName,
"ServiceNameValue": func(a *API) string {
if a.NoConstServiceNames {
return fmt.Sprintf("%q", a.Metadata.EndpointPrefix)
if !a.NoConstServiceNames {
return "ServiceName"
}
return "ServiceName"
return fmt.Sprintf("%q", ServiceName(a))
},
"EndpointsIDConstValue": func(a *API) string {
if a.NoConstServiceNames {
return fmt.Sprintf("%q", a.Metadata.EndpointPrefix)
return fmt.Sprintf("%q", a.Metadata.EndpointsID)
}
if a.Metadata.EndpointPrefix == a.Metadata.EndpointsID {
if a.Metadata.EndpointsID == ServiceName(a) {
return "ServiceName"
}
return fmt.Sprintf("%q", a.Metadata.EndpointsID)
},
"EndpointsIDValue": func(a *API) string {
if a.NoConstServiceNames {
return fmt.Sprintf("%q", a.Metadata.EndpointPrefix)
return fmt.Sprintf("%q", a.Metadata.EndpointsID)
}
return "EndpointsID"
},
"ServiceIDVar": func(a *API) string {
if a.NoConstServiceNames {
return fmt.Sprintf("%q", ServiceID(a))
}
return "ServiceID"
},
"ServiceID": ServiceID,
}).Parse(`
// {{ .StructName }} provides the API operation methods for making requests to
// {{ .Metadata.ServiceFullName }}. See this package's package overview docs
@@ -449,6 +488,9 @@ var tplService = template.Must(template.New("service").Funcs(template.FuncMap{
// modify mutate any of the struct's properties though.
type {{ .StructName }} struct {
*client.Client
{{- if .EndpointDiscoveryOp }}
endpointCache *crr.EndpointCache
{{ end -}}
}
{{ if .UseInitMethods }}// Used for custom client initialization logic
@@ -462,8 +504,9 @@ var initRequest func(*request.Request)
{{ if not .NoConstServiceNames -}}
// Service information constants
const (
ServiceName = "{{ .Metadata.EndpointPrefix }}" // Service endpoint prefix API calls made to.
EndpointsID = {{ EndpointsIDConstValue . }} // Service ID for Regions and Endpoints metadata.
ServiceName = "{{ ServiceNameConstValue . }}" // Name of service.
EndpointsID = {{ EndpointsIDConstValue . }} // ID to lookup a service endpoint with.
ServiceID = "{{ ServiceID . }}" // ServiceID is a unique identifer of a specific service.
)
{{- end }}
@@ -504,14 +547,15 @@ func newClient(cfg aws.Config, handlers request.Handlers, endpoint, signingRegio
cfg,
metadata.ClientInfo{
ServiceName: {{ ServiceNameValue . }},
ServiceID : {{ ServiceIDVar . }},
SigningName: signingName,
SigningRegion: signingRegion,
Endpoint: endpoint,
APIVersion: "{{ .Metadata.APIVersion }}",
{{ if .Metadata.JSONVersion -}}
{{ if and (.Metadata.JSONVersion) (eq .Metadata.Protocol "json") -}}
JSONVersion: "{{ .Metadata.JSONVersion }}",
{{- end }}
{{ if .Metadata.TargetPrefix -}}
{{ if and (.Metadata.TargetPrefix) (eq .Metadata.Protocol "json") -}}
TargetPrefix: "{{ .Metadata.TargetPrefix }}",
{{- end }}
},
@@ -519,8 +563,22 @@ func newClient(cfg aws.Config, handlers request.Handlers, endpoint, signingRegio
),
}
{{- if .EndpointDiscoveryOp }}
svc.endpointCache = crr.NewEndpointCache(10)
{{- end }}
// Handlers
svc.Handlers.Sign.PushBackNamed({{if eq .Metadata.SignatureVersion "v2"}}v2{{else}}v4{{end}}.SignRequestHandler)
svc.Handlers.Sign.PushBackNamed(
{{- if eq .Metadata.SignatureVersion "v2" -}}
v2.SignRequestHandler
{{- else if or (eq .Metadata.SignatureVersion "s3") (eq .Metadata.SignatureVersion "s3v4") -}}
v4.BuildNamedHandler(v4.SignRequestHandler.Name, func(s *v4.Signer) {
s.DisableURIPathEscaping = true
})
{{- else -}}
v4.SignRequestHandler
{{- end -}}
)
{{- if eq .Metadata.SignatureVersion "v2" }}
svc.Handlers.Sign.PushBackNamed(corehandlers.BuildContentLengthHandler)
{{- end }}
@@ -528,6 +586,9 @@ func newClient(cfg aws.Config, handlers request.Handlers, endpoint, signingRegio
svc.Handlers.Unmarshal.PushBackNamed({{ .ProtocolPackage }}.UnmarshalHandler)
svc.Handlers.UnmarshalMeta.PushBackNamed({{ .ProtocolPackage }}.UnmarshalMetaHandler)
svc.Handlers.UnmarshalError.PushBackNamed({{ .ProtocolPackage }}.UnmarshalErrorHandler)
{{ if .HasEventStream }}
svc.Handlers.UnmarshalStream.PushBackNamed({{ .ProtocolPackage }}.UnmarshalHandler)
{{ end }}
{{ if .UseInitMethods }}// Run custom client initialization if present
if initClient != nil {
@@ -570,16 +631,20 @@ func (a *API) ServicePackageDoc() string {
// ServiceGoCode renders service go code. Returning it as a string.
func (a *API) ServiceGoCode() string {
a.resetImports()
a.imports["github.com/aws/aws-sdk-go/aws/client"] = true
a.imports["github.com/aws/aws-sdk-go/aws/client/metadata"] = true
a.imports["github.com/aws/aws-sdk-go/aws/request"] = true
a.AddSDKImport("aws")
a.AddSDKImport("aws/client")
a.AddSDKImport("aws/client/metadata")
a.AddSDKImport("aws/request")
if a.Metadata.SignatureVersion == "v2" {
a.imports["github.com/aws/aws-sdk-go/private/signer/v2"] = true
a.imports["github.com/aws/aws-sdk-go/aws/corehandlers"] = true
a.AddSDKImport("private/signer/v2")
a.AddSDKImport("aws/corehandlers")
} else {
a.imports["github.com/aws/aws-sdk-go/aws/signer/v4"] = true
a.AddSDKImport("aws/signer/v4")
}
a.AddSDKImport("private/protocol", a.ProtocolPackage())
if a.EndpointDiscoveryOp != nil {
a.AddSDKImport("aws/crr")
}
a.imports["github.com/aws/aws-sdk-go/private/protocol/"+a.ProtocolPackage()] = true
var buf bytes.Buffer
err := tplService.Execute(&buf, a)
@@ -607,9 +672,9 @@ func (a *API) ExampleGoCode() string {
"bytes",
"fmt",
"time",
"github.com/aws/aws-sdk-go/aws",
"github.com/aws/aws-sdk-go/aws/session",
path.Join(a.SvcClientImportPath, a.PackageName()),
SDKImportRoot+"/aws",
SDKImportRoot+"/aws/session",
a.ImportPath(),
)
for k := range imports {
code += fmt.Sprintf("%q\n", k)
@@ -665,7 +730,7 @@ var tplInterface = template.Must(template.New("interface").Parse(`
//
// It is important to note that this interface will have breaking changes
// when the service model is updated and adds new API operations, paginators,
// and waiters. Its suggested to use the pattern above for testing, or using
// and waiters. Its suggested to use the pattern above for testing, or using
// tooling to generate mocks to satisfy the interfaces.
type {{ .StructName }}API interface {
{{ range $_, $o := .OperationList }}
@@ -684,11 +749,9 @@ var _ {{ .StructName }}API = (*{{ .PackageName }}.{{ .StructName }})(nil)
// package than the service API's package.
func (a *API) InterfaceGoCode() string {
a.resetImports()
a.imports = map[string]bool{
"github.com/aws/aws-sdk-go/aws": true,
"github.com/aws/aws-sdk-go/aws/request": true,
path.Join(a.SvcClientImportPath, a.PackageName()): true,
}
a.AddSDKImport("aws")
a.AddSDKImport("aws/request")
a.AddImport(a.ImportPath())
var buf bytes.Buffer
err := tplInterface.Execute(&buf, a)
@@ -726,7 +789,6 @@ func resolveShapeValidations(s *Shape, ancestry ...*Shape) {
children := []string{}
for _, name := range s.MemberNames() {
ref := s.MemberRefs[name]
if s.IsRequired(name) && !s.Validations.Has(ref, ShapeValidationRequired) {
s.Validations = append(s.Validations, ShapeValidation{
Name: name, Ref: ref, Type: ShapeValidationRequired,
@@ -739,6 +801,12 @@ func resolveShapeValidations(s *Shape, ancestry ...*Shape) {
})
}
if !ref.CanBeEmpty() && !s.Validations.Has(ref, ShapeValidationMinVal) {
s.Validations = append(s.Validations, ShapeValidation{
Name: name, Ref: ref, Type: ShapeValidationMinVal,
})
}
switch ref.Shape.Type {
case "map", "list", "structure":
children = append(children, name)
@@ -806,7 +874,7 @@ func (a *API) APIErrorsGoCode() string {
// removeOperation removes an operation, its input/output shapes, as well as
// any references/shapes that are unique to this operation.
func (a *API) removeOperation(name string) {
fmt.Println("removing operation,", name)
debugLogger.Logln("removing operation,", name)
op := a.Operations[name]
delete(a.Operations, name)
@@ -820,7 +888,7 @@ func (a *API) removeOperation(name string) {
// shapes. Will also remove member reference targeted shapes if those shapes do
// not have any additional references.
func (a *API) removeShape(s *Shape) {
fmt.Println("removing shape,", s.ShapeName)
debugLogger.Logln("removing shape,", s.ShapeName)
delete(a.Shapes, s.ShapeName)
@@ -851,3 +919,11 @@ func (a *API) removeShapeRef(ref *ShapeRef) {
a.removeShape(ref.Shape)
}
}
func getDeprecatedMessage(msg string, name string) string {
if len(msg) == 0 {
return name + " has been deprecated"
}
return msg
}