Skip to content

Commit 59f427f

Browse files
authored
Merge pull request #472 from Vandita2020/resourceNameList
Adding functionality to assign one API to multiple resources
2 parents 3753ca3 + dd3c3f1 commit 59f427f

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

pkg/config/operation.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type OperationConfig struct {
4040
OutputWrapperFieldPath string `json:"output_wrapper_field_path,omitempty"`
4141
// Override for resource name in case of heuristic failure
4242
// An example of this is correcting stutter when the resource logic doesn't properly determine the resource name
43-
ResourceName string `json:"resource_name"`
43+
ResourceName StringArray `json:"resource_name"`
4444
// Override for operation type in case of heuristic failure
4545
// An example of this is `Put...` or `Register...` API operations not being correctly classified as `Create` op type
4646
// OperationType []string `json:"operation_type"`
@@ -59,8 +59,8 @@ func (c *Config) OperationIsIgnored(operation *awssdkmodel.Operation) bool {
5959
return util.InStrings(operation.ExportedName, c.Ignore.Operations)
6060
}
6161

62-
// UnmarshalJSON parses input for a either a string or
63-
// or a list and returns a StringArray.
62+
// UnmarshalJSON parses input for either a string or
63+
// a list and returns a StringArray.
6464
func (a *StringArray) UnmarshalJSON(b []byte) error {
6565
var multi []string
6666
err := json.Unmarshal(b, &multi)

pkg/model/sdk_api.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,14 @@ func (a *SDKAPI) GetOperationMap(cfg *ackgenconfig.Config) *OperationMap {
7878
// create an index of Operations by operation types and resource name
7979
opMap := OperationMap{}
8080
for opID, op := range a.API.Operations {
81-
opTypeArray, resName := getOpTypeAndResourceName(opID, cfg)
82-
for _, opType := range opTypeArray {
81+
opTypes, opResourceNames := getOpTypesAndResourcesMapping(opID, cfg)
82+
for _, opType := range opTypes {
8383
if _, found := opMap[opType]; !found {
8484
opMap[opType] = map[string]*awssdkmodel.Operation{}
8585
}
86-
opMap[opType][resName] = op
86+
for _, resName := range opResourceNames {
87+
opMap[opType][resName] = op
88+
}
8789
}
8890
}
8991

@@ -98,7 +100,7 @@ func (a *SDKAPI) GetOperationMap(cfg *ackgenconfig.Config) *OperationMap {
98100
//
99101
// see: https://github.com/aws-controllers-k8s/community/issues/1555
100102
for opID, opCfg := range cfg.Operations {
101-
if opCfg.ResourceName == "" {
103+
if len(opCfg.ResourceName) == 0 {
102104
continue
103105
}
104106
op, found := a.API.Operations[opID]
@@ -107,7 +109,9 @@ func (a *SDKAPI) GetOperationMap(cfg *ackgenconfig.Config) *OperationMap {
107109
}
108110
for _, operationType := range opCfg.OperationType {
109111
opType := OpTypeFromString(operationType)
110-
opMap[opType][opCfg.ResourceName] = op
112+
for _, resName := range opCfg.ResourceName {
113+
opMap[opType][resName] = op
114+
}
111115
}
112116
}
113117
a.opMap = &opMap
@@ -360,20 +364,23 @@ func NewSDKAPI(api *awssdkmodel.API, apiGroupSuffix string) *SDKAPI {
360364
}
361365

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

367-
if operationConfig, exists := cfg.GetOperationConfig(opID); exists {
368-
if operationConfig.ResourceName != "" {
369-
resName = operationConfig.ResourceName
370-
}
371-
for _, operationType := range operationConfig.OperationType {
372-
opType = OpTypeFromString(operationType)
373-
opTypes = append(opTypes, opType)
374-
}
371+
// The existance of the operation in the config file is not enough to
372+
// override the operation type and/or resource name. The operation type
373+
// and/or resource name must be specified in the config file.
374+
if !exists || len(opConfig.ResourceName) == 0 || len(opConfig.OperationType) == 0 {
375+
return []OpType{opType}, []string{resName}
376+
}
377+
378+
opTypes := []OpType{}
379+
for _, operationType := range opConfig.OperationType {
380+
opType = OpTypeFromString(operationType)
381+
opTypes = append(opTypes, opType)
375382
}
376-
return opTypes, resName
383+
return []OpType{opType}, opConfig.ResourceName
377384
}
378385

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

0 commit comments

Comments
 (0)