Skip to content

Commit 43b45a0

Browse files
committed
Add WithSDKVersion and WithAPIVersion methods to ackmodel.SDKHelper
This patch adds a new methods to `ackmodel.SDKHelper` which will help in loading api files for different aws-sdk-go versions and different service api versions.
1 parent adeb7eb commit 43b45a0

File tree

1 file changed

+63
-15
lines changed

1 file changed

+63
-15
lines changed

pkg/model/sdk_helper.go

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ import (
1919
"io/ioutil"
2020
"os"
2121
"path/filepath"
22+
"sort"
2223
"strings"
2324

25+
"gopkg.in/src-d/go-git.v4"
26+
2427
ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/generate/config"
2528
"github.com/aws-controllers-k8s/code-generator/pkg/names"
2629
"github.com/aws-controllers-k8s/code-generator/pkg/util"
@@ -37,13 +40,19 @@ var (
3740
ErrServiceNotFound = errors.New(
3841
"no such service",
3942
)
43+
ErrAPIVersionNotFound = errors.New(
44+
"no such api version",
45+
)
4046
)
4147

4248
// SDKHelper is a helper struct that helps work with the aws-sdk-go models and
4349
// API model loader
4450
type SDKHelper struct {
45-
basePath string
46-
loader *awssdkmodel.Loader
51+
gitRepository *git.Repository
52+
basePath string
53+
loader *awssdkmodel.Loader
54+
// Default is set by `FirstAPIVersion`
55+
apiVersion string
4756
// Default is "services.k8s.aws"
4857
APIGroupSuffix string
4958
}
@@ -59,6 +68,29 @@ func NewSDKHelper(basePath string) *SDKHelper {
5968
}
6069
}
6170

71+
// WithSDKVersion checks out the sdk git repository to the provided version. To use
72+
// this function h.basePath should point to a git repository.
73+
func (h *SDKHelper) WithSDKVersion(version string) error {
74+
if h.gitRepository == nil {
75+
gitRepository, err := util.LoadRepository(h.basePath)
76+
if err != nil {
77+
return fmt.Errorf("error loading repository from %s: %v", h.basePath, err)
78+
}
79+
h.gitRepository = gitRepository
80+
}
81+
82+
err := util.CheckoutRepositoryTag(h.gitRepository, version)
83+
if err != nil {
84+
return fmt.Errorf("cannot checkout tag %s: %v", version, err)
85+
}
86+
return nil
87+
}
88+
89+
// WithAPIVersion sets the `apiVersion` field.
90+
func (h *SDKHelper) WithAPIVersion(apiVersion string) {
91+
h.apiVersion = apiVersion
92+
}
93+
6294
// API returns the aws-sdk-go API model for a supplied service alias
6395
func (h *SDKHelper) API(serviceAlias string) (*SDKAPI, error) {
6496
modelPath, _, err := h.ModelAndDocsPath(serviceAlias)
@@ -89,40 +121,56 @@ func (h *SDKHelper) API(serviceAlias string) (*SDKAPI, error) {
89121
func (h *SDKHelper) ModelAndDocsPath(
90122
serviceAlias string,
91123
) (string, string, error) {
92-
apiVersion, err := h.APIVersion(serviceAlias)
93-
if err != nil {
94-
return "", "", err
124+
if h.apiVersion == "" {
125+
apiVersion, err := h.FirstAPIVersion(serviceAlias)
126+
if err != nil {
127+
return "", "", err
128+
}
129+
h.apiVersion = apiVersion
95130
}
96131
versionPath := filepath.Join(
97-
h.basePath, "models", "apis", serviceAlias, apiVersion,
132+
h.basePath, "models", "apis", serviceAlias, h.apiVersion,
98133
)
99134
modelPath := filepath.Join(versionPath, "api-2.json")
100135
docsPath := filepath.Join(versionPath, "docs-2.json")
101136
return modelPath, docsPath, nil
102137
}
103138

104-
// APIVersion returns the API version (e.h. "2012-10-03") for a service API
105-
func (h *SDKHelper) APIVersion(serviceAlias string) (string, error) {
139+
// FirstAPIVersion returns the first found API version for a service API.
140+
// (e.h. "2012-10-03")
141+
func (h *SDKHelper) FirstAPIVersion(serviceAlias string) (string, error) {
142+
versions, err := h.GetAPIVersions(serviceAlias)
143+
if err != nil {
144+
return "", err
145+
}
146+
sort.Strings(versions)
147+
return versions[0], nil
148+
}
149+
150+
// GetAPIVersions returns the list of API Versions found in a service directory.
151+
func (h *SDKHelper) GetAPIVersions(serviceAlias string) ([]string, error) {
106152
apiPath := filepath.Join(h.basePath, "models", "apis", serviceAlias)
107153
versionDirs, err := ioutil.ReadDir(apiPath)
108154
if err != nil {
109-
return "", err
155+
return nil, err
110156
}
157+
versions := []string{}
111158
for _, f := range versionDirs {
112159
version := f.Name()
113160
fp := filepath.Join(apiPath, version)
114161
fi, err := os.Lstat(fp)
115162
if err != nil {
116-
return "", err
163+
return nil, err
117164
}
118165
if !fi.IsDir() {
119-
return "", ErrInvalidVersionDirectory
166+
return nil, fmt.Errorf("found %s: %v", version, ErrInvalidVersionDirectory)
120167
}
121-
// TODO(jaypipes): handle more than one version? doesn't seem like
122-
// there is ever more than one.
123-
return version, nil
168+
versions = append(versions, version)
169+
}
170+
if len(versions) == 0 {
171+
return nil, ErrNoValidVersionDirectory
124172
}
125-
return "", ErrNoValidVersionDirectory
173+
return versions, nil
126174
}
127175

128176
// SDKAPI contains an API model for a single AWS service API

0 commit comments

Comments
 (0)