Skip to content

Adding functionality to assign one API to multiple resources #472

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

Merged
merged 1 commit into from
Jan 16, 2024
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
6 changes: 3 additions & 3 deletions pkg/config/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type OperationConfig struct {
OutputWrapperFieldPath string `json:"output_wrapper_field_path,omitempty"`
// Override for resource name in case of heuristic failure
// An example of this is correcting stutter when the resource logic doesn't properly determine the resource name
ResourceName string `json:"resource_name"`
ResourceName StringArray `json:"resource_name"`
// Override for operation type in case of heuristic failure
// An example of this is `Put...` or `Register...` API operations not being correctly classified as `Create` op type
// OperationType []string `json:"operation_type"`
Expand All @@ -59,8 +59,8 @@ func (c *Config) OperationIsIgnored(operation *awssdkmodel.Operation) bool {
return util.InStrings(operation.ExportedName, c.Ignore.Operations)
}

// UnmarshalJSON parses input for a either a string or
// or a list and returns a StringArray.
// UnmarshalJSON parses input for either a string or
// a list and returns a StringArray.
func (a *StringArray) UnmarshalJSON(b []byte) error {
var multi []string
err := json.Unmarshal(b, &multi)
Expand Down
39 changes: 23 additions & 16 deletions pkg/model/sdk_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ func (a *SDKAPI) GetOperationMap(cfg *ackgenconfig.Config) *OperationMap {
// create an index of Operations by operation types and resource name
opMap := OperationMap{}
for opID, op := range a.API.Operations {
opTypeArray, resName := getOpTypeAndResourceName(opID, cfg)
for _, opType := range opTypeArray {
opTypes, opResourceNames := getOpTypesAndResourcesMapping(opID, cfg)
for _, opType := range opTypes {
if _, found := opMap[opType]; !found {
opMap[opType] = map[string]*awssdkmodel.Operation{}
}
opMap[opType][resName] = op
for _, resName := range opResourceNames {
opMap[opType][resName] = op
}
}
}

Expand All @@ -98,7 +100,7 @@ func (a *SDKAPI) GetOperationMap(cfg *ackgenconfig.Config) *OperationMap {
//
// see: https://github.com/aws-controllers-k8s/community/issues/1555
for opID, opCfg := range cfg.Operations {
if opCfg.ResourceName == "" {
if len(opCfg.ResourceName) == 0 {
continue
}
op, found := a.API.Operations[opID]
Expand All @@ -107,7 +109,9 @@ func (a *SDKAPI) GetOperationMap(cfg *ackgenconfig.Config) *OperationMap {
}
for _, operationType := range opCfg.OperationType {
opType := OpTypeFromString(operationType)
opMap[opType][opCfg.ResourceName] = op
for _, resName := range opCfg.ResourceName {
opMap[opType][resName] = op
}
}
}
a.opMap = &opMap
Expand Down Expand Up @@ -360,20 +364,23 @@ func NewSDKAPI(api *awssdkmodel.API, apiGroupSuffix string) *SDKAPI {
}

// Override the operation type and/or resource name, if specified in config
func getOpTypeAndResourceName(opID string, cfg *ackgenconfig.Config) ([]OpType, string) {
func getOpTypesAndResourcesMapping(opID string, cfg *ackgenconfig.Config) ([]OpType, []string) {
opType, resName := GetOpTypeAndResourceNameFromOpID(opID, cfg)
opTypes := []OpType{opType}
opConfig, exists := cfg.GetOperationConfig(opID)

if operationConfig, exists := cfg.GetOperationConfig(opID); exists {
if operationConfig.ResourceName != "" {
resName = operationConfig.ResourceName
}
for _, operationType := range operationConfig.OperationType {
opType = OpTypeFromString(operationType)
opTypes = append(opTypes, opType)
}
// The existance of the operation in the config file is not enough to
// override the operation type and/or resource name. The operation type
// and/or resource name must be specified in the config file.
if !exists || len(opConfig.ResourceName) == 0 || len(opConfig.OperationType) == 0 {
return []OpType{opType}, []string{resName}
}
Comment on lines +371 to +376
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Vandita2020 You approach is correct, we only missed this last bits of code ^. When a user provides an Operation configuration without ResourceName nor OperationType directives - this block doesn't get called. Causing the opTypes list to reset (L378)


opTypes := []OpType{}
for _, operationType := range opConfig.OperationType {
opType = OpTypeFromString(operationType)
opTypes = append(opTypes, opType)
}
return opTypes, resName
return []OpType{opType}, opConfig.ResourceName
}

// getMemberByPath returns a ShapeRef given a root Shape and a dot-notation
Expand Down