Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert service model name argument #215

Merged
merged 2 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions cmd/ack-generate/command/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ const (
)

var (
optGenVersion string
apisVersionPath string
optGenVersion string
optAPIsInputPath string
apisVersionPath string
)

// apiCmd is the command that generates service API types
Expand Down Expand Up @@ -96,13 +97,10 @@ func generateAPIs(cmd *cobra.Command, args []string) error {
if err := ensureSDKRepo(ctx, optCacheDir, optRefreshCache); err != nil {
return err
}
if optModelName == "" {
optModelName = svcAlias
}
sdkHelper := ackmodel.NewSDKHelper(sdkDir)
sdkAPI, err := sdkHelper.API(optModelName)
sdkAPI, err := sdkHelper.API(svcAlias)
if err != nil {
newSvcAlias, err := FallBackFindServiceID(sdkDir, optModelName)
newSvcAlias, err := FallBackFindServiceID(sdkDir, svcAlias)
if err != nil {
return err
}
Expand All @@ -112,7 +110,7 @@ func generateAPIs(cmd *cobra.Command, args []string) error {
}
}
model, err := ackmodel.New(
sdkAPI, svcAlias, optGenVersion, optGeneratorConfigPath, ackgenerate.DefaultConfig,
sdkAPI, optGenVersion, optGeneratorConfigPath, ackgenerate.DefaultConfig,
)
if err != nil {
return err
Expand Down
9 changes: 3 additions & 6 deletions cmd/ack-generate/command/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,10 @@ func generateController(cmd *cobra.Command, args []string) error {
if err := ensureSDKRepo(ctx, optCacheDir, optRefreshCache); err != nil {
return err
}
if optModelName == "" {
optModelName = svcAlias
}
sdkHelper := ackmodel.NewSDKHelper(sdkDir)
sdkAPI, err := sdkHelper.API(optModelName)
sdkAPI, err := sdkHelper.API(svcAlias)
if err != nil {
newSvcAlias, err := FallBackFindServiceID(sdkDir, optModelName)
newSvcAlias, err := FallBackFindServiceID(sdkDir, svcAlias)
if err != nil {
return err
}
Expand All @@ -82,7 +79,7 @@ func generateController(cmd *cobra.Command, args []string) error {
return err
}
m, err := ackmodel.New(
sdkAPI, svcAlias, latestAPIVersion, optGeneratorConfigPath, ackgenerate.DefaultConfig,
sdkAPI, latestAPIVersion, optGeneratorConfigPath, ackgenerate.DefaultConfig,
)
if err != nil {
return err
Expand Down
14 changes: 6 additions & 8 deletions cmd/ack-generate/command/crossplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/spf13/cobra"

cpgenerate "github.com/aws-controllers-k8s/code-generator/pkg/generate/crossplane"
"github.com/aws-controllers-k8s/code-generator/pkg/model"
ackmodel "github.com/aws-controllers-k8s/code-generator/pkg/model"
)

Expand Down Expand Up @@ -55,20 +56,17 @@ func generateCrossplane(_ *cobra.Command, args []string) error {
return err
}
svcAlias := strings.ToLower(args[0])
if optModelName == "" {
optModelName = svcAlias
}
sdkHelper := ackmodel.NewSDKHelper(sdkDir)
sdkHelper := model.NewSDKHelper(sdkDir)
sdkHelper.APIGroupSuffix = "aws.crossplane.io"
sdkAPI, err := sdkHelper.API(optModelName)
sdkAPI, err := sdkHelper.API(svcAlias)
if err != nil {
newSvcAlias, err := FallBackFindServiceID(sdkDir, optModelName)
newSvcAlias, err := FallBackFindServiceID(sdkDir, svcAlias)
if err != nil {
return err
}
sdkAPI, err = sdkHelper.API(newSvcAlias) // retry with serviceID
if err != nil {
return fmt.Errorf("service %s not found", svcAlias)
return fmt.Errorf("cannot get the API model for service %s", svcAlias)
}
}
cfgPath := filepath.Join(providerDir, "apis", svcAlias, optGenVersion, "generator-config.yaml")
Expand All @@ -80,7 +78,7 @@ func generateCrossplane(_ *cobra.Command, args []string) error {
cfgPath = ""
}
m, err := ackmodel.New(
sdkAPI, svcAlias, optGenVersion, cfgPath, cpgenerate.DefaultConfig,
sdkAPI, optGenVersion, cfgPath, cpgenerate.DefaultConfig,
)
if err != nil {
return err
Expand Down
9 changes: 3 additions & 6 deletions cmd/ack-generate/command/olm.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,10 @@ func generateOLMAssets(cmd *cobra.Command, args []string) error {
if err := ensureSDKRepo(ctx, optCacheDir, optRefreshCache); err != nil {
return err
}
if optModelName == "" {
optModelName = svcAlias
}
sdkHelper := ackmodel.NewSDKHelper(sdkDir)
sdkAPI, err := sdkHelper.API(optModelName)
sdkAPI, err := sdkHelper.API(svcAlias)
if err != nil {
newSvcAlias, err := FallBackFindServiceID(sdkDir, optModelName)
newSvcAlias, err := FallBackFindServiceID(sdkDir, svcAlias)
if err != nil {
return err
}
Expand All @@ -107,7 +104,7 @@ func generateOLMAssets(cmd *cobra.Command, args []string) error {
return err
}
m, err := ackmodel.New(
sdkAPI, svcAlias, latestAPIVersion, optGeneratorConfigPath, ackgenerate.DefaultConfig,
sdkAPI, latestAPIVersion, optGeneratorConfigPath, ackgenerate.DefaultConfig,
)
if err != nil {
return err
Expand Down
9 changes: 3 additions & 6 deletions cmd/ack-generate/command/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,10 @@ func generateRelease(cmd *cobra.Command, args []string) error {
if err := ensureSDKRepo(ctx, optCacheDir, optRefreshCache); err != nil {
return err
}
if optModelName == "" {
optModelName = svcAlias
}
sdkHelper := ackmodel.NewSDKHelper(sdkDir)
sdkAPI, err := sdkHelper.API(optModelName)
sdkAPI, err := sdkHelper.API(svcAlias)
if err != nil {
newSvcAlias, err := FallBackFindServiceID(sdkDir, optModelName)
newSvcAlias, err := FallBackFindServiceID(sdkDir, svcAlias)
if err != nil {
return err
}
Expand All @@ -90,7 +87,7 @@ func generateRelease(cmd *cobra.Command, args []string) error {
}
}
m, err := ackmodel.New(
sdkAPI, svcAlias, "", optGeneratorConfigPath, ackgenerate.DefaultConfig,
sdkAPI, "", optGeneratorConfigPath, ackgenerate.DefaultConfig,
)
if err != nil {
return err
Expand Down
4 changes: 0 additions & 4 deletions cmd/ack-generate/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ var (
optCacheDir string
optRefreshCache bool
optAWSSDKGoVersion string
optModelName string
defaultTemplateDirs []string
optTemplateDirs []string
defaultServicesDir string
Expand Down Expand Up @@ -122,9 +121,6 @@ func init() {
rootCmd.PersistentFlags().StringVar(
&optAWSSDKGoVersion, "aws-sdk-go-version", "", "Version of github.com/aws/aws-sdk-go used to generate apis and controllers files",
)
rootCmd.PersistentFlags().StringVar(
&optModelName, "model-name", "", "the name of the service model package",
)
}

// Execute adds all child commands to the root command and sets flags
Expand Down
6 changes: 3 additions & 3 deletions pkg/generate/templateset/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type MetaVars struct {
// alias does not match the ServiceID. e.g. The AWS Step Functions API has
// a ServiceID of "SFN" and a service alias of "states"...
ServiceAlias string
// ServiceID is the exact string that appears in the AWS service API's
// api-2.json descriptor file under `metadata.serviceId`
ServiceID string
// ServiceIDClean is the ServiceID lowercased and stripped of any
// non-alphanumeric characters
ServiceIDClean string
Expand All @@ -31,9 +34,6 @@ type MetaVars struct {
// for custom resources, e.g. "sns.services.k8s.aws" or
// "sfn.services.k8s.aws"
APIGroup string
// AWSSDKModelServiceID is the exact string that appears in the AWS service API's
// api-2.json descriptor file under `metadata.serviceId`
AWSSDKModelServiceID string
// SDKAPIInterfaceTypeName is the name of the interface type used by the
// aws-sdk-go services/$SERVICE/api.go file
SDKAPIInterfaceTypeName string
Expand Down
50 changes: 16 additions & 34 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ var (
// Model contains the ACK model for the generator to process and apply
// templates against.
type Model struct {
SDKAPI *SDKAPI
servicePackage string
serviceAlias string
apiVersion string
crds []*CRD
typeDefs []*TypeDef
typeImports map[string]string
typeRenames map[string]string
SDKAPI *SDKAPI
serviceAlias string
apiVersion string
crds []*CRD
typeDefs []*TypeDef
typeImports map[string]string
typeRenames map[string]string
// Instructions to the code generator how to handle the API and its
// resources
cfg *ackgenconfig.Config
Expand All @@ -52,10 +51,10 @@ type Model struct {
func (m *Model) MetaVars() templateset.MetaVars {
return templateset.MetaVars{
ServiceAlias: m.serviceAlias,
ServiceIDClean: m.ServiceIDClean(),
APIGroup: m.APIGroup(),
ServiceID: m.SDKAPI.ServiceID(),
ServiceIDClean: m.SDKAPI.ServiceIDClean(),
APIGroup: m.SDKAPI.APIGroup(),
APIVersion: m.apiVersion,
AWSSDKModelServiceID: m.SDKAPI.AWSSDKModelServiceID(),
SDKAPIInterfaceTypeName: m.SDKAPI.SDKAPIInterfaceTypeName(),
CRDNames: m.crdNames(),
}
Expand Down Expand Up @@ -692,29 +691,11 @@ func (m *Model) GetConfig() *ackgenconfig.Config {
return m.cfg
}

// APIGroup returns the normalized Kubernetes APIGroup for the AWS service API,
// e.g. "sns.services.k8s.aws"
func (m *Model) APIGroup() string {
serviceID := m.servicePackage
suffix := "services.k8s.aws"
if m.SDKAPI.apiGroupSuffix != "" {
suffix = m.SDKAPI.apiGroupSuffix
}
return fmt.Sprintf("%s.%s", serviceID, suffix)
}

// ServiceIDClean returns a lowercased, whitespace-stripped ServiceID
func (m *Model) ServiceIDClean() string {
serviceID := strings.ToLower(m.servicePackage)
return strings.Replace(serviceID, " ", "", -1)
}

// New returns a new Model struct for a supplied API model.
// Optionally, pass a file path to a generator config file that can be used to
// instruct the code generator how to handle the API properly
func New(
SDKAPI *SDKAPI,
servicePackage string,
apiVersion string,
configPath string,
defaultConfig ackgenconfig.Config,
Expand All @@ -724,11 +705,12 @@ func New(
return nil, err
}
m := &Model{
SDKAPI: SDKAPI,
servicePackage: servicePackage,
serviceAlias: SDKAPI.AWSSDKModelServiceID(),
apiVersion: apiVersion,
cfg: &cfg,
SDKAPI: SDKAPI,
// TODO(jaypipes): Handle cases where service alias and service ID
// don't match (Step Functions)
serviceAlias: SDKAPI.ServiceID(),
apiVersion: apiVersion,
cfg: &cfg,
}
m.ApplyShapeIgnoreRules()
return m, nil
Expand Down
4 changes: 1 addition & 3 deletions pkg/model/multiversion/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func NewAPIVersionManager(
sdkCacheDir string,
metadataPath string,
serviceAlias string,
serviceModelName string,
hubVersion string,
apisInfo map[string]ackmetadata.APIInfo,
defaultConfig ackgenconfig.Config,
Expand Down Expand Up @@ -95,14 +94,13 @@ func NewAPIVersionManager(
return nil, err
}

SDKAPI, err := SDKAPIHelper.API(serviceModelName)
SDKAPI, err := SDKAPIHelper.API(serviceAlias)
if err != nil {
return nil, err
}

i, err := ackmodel.New(
SDKAPI,
serviceAlias,
version.APIVersion,
apiInfo.GeneratorConfigPath,
defaultConfig,
Expand Down
29 changes: 22 additions & 7 deletions pkg/model/sdk_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ func (a *SDKAPI) GetOutputShapeRef(
}

// getMemberByPath returns a ShapeRef given a root Shape and a dot-notation
// object search path. Given the explicit type check for list type members
// both ".." and "." notations work currently.
// object search path. Given the explicit type check for list type members
// both ".." and "." notations work currently.
// TODO: Add support for other types such as map.
func getMemberByPath(
shape *awssdkmodel.Shape,
Expand Down Expand Up @@ -353,24 +353,39 @@ func (a *SDKAPI) HasConflictingTypeName(typeName string, cfg *ackgenconfig.Confi
util.InStrings(cleanTypeName, crdListResourceNames)
}

// AWSSDKModelServiceID returns the exact `metadata.serviceId` attribute for the AWS
// service APi's api-2.json file.
// This MAY NOT MATCH the AWS SDK Go package used by the service. For example:
// AWS SDK Go uses `opensearchservice` whereas the service ID is `opensearch`
func (a *SDKAPI) AWSSDKModelServiceID() string {
// ServiceID returns the exact `metadata.serviceId` attribute for the AWS
// service APi's api-2.json file
func (a *SDKAPI) ServiceID() string {
if a == nil || a.API == nil {
return ""
}
return awssdkmodel.ServiceID(a.API)
}

// ServiceIDClean returns a lowercased, whitespace-stripped ServiceID
func (a *SDKAPI) ServiceIDClean() string {
serviceID := strings.ToLower(a.ServiceID())
return strings.Replace(serviceID, " ", "", -1)
}

func (a *SDKAPI) GetServiceFullName() string {
if a == nil || a.API == nil {
return ""
}
return a.API.Metadata.ServiceFullName
}

// APIGroup returns the normalized Kubernetes APIGroup for the AWS service API,
// e.g. "sns.services.k8s.aws"
func (a *SDKAPI) APIGroup() string {
serviceID := a.ServiceIDClean()
suffix := "services.k8s.aws"
if a.apiGroupSuffix != "" {
suffix = a.apiGroupSuffix
}
return fmt.Sprintf("%s.%s", serviceID, suffix)
}

// SDKAPIInterfaceTypeName returns the name of the aws-sdk-go primary API
// interface type name.
func (a *SDKAPI) SDKAPIInterfaceTypeName() string {
Expand Down
Loading