|
14 | 14 | package code
|
15 | 15 |
|
16 | 16 | import (
|
| 17 | + "strings" |
| 18 | + |
17 | 19 | awssdkmodel "github.com/aws/aws-sdk-go/private/model/api"
|
| 20 | + "github.com/gertd/go-pluralize" |
18 | 21 |
|
| 22 | + ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/generate/config" |
19 | 23 | "github.com/aws-controllers-k8s/code-generator/pkg/model"
|
20 | 24 | "github.com/aws-controllers-k8s/code-generator/pkg/util"
|
21 | 25 | )
|
22 | 26 |
|
| 27 | +var ( |
| 28 | + PrimaryIdentifierARNOverride = "ARN" |
| 29 | +) |
| 30 | + |
23 | 31 | // FindIdentifiersInShape returns the identifier fields of a given shape which
|
24 | 32 | // can be singular or plural.
|
25 | 33 | func FindIdentifiersInShape(
|
@@ -49,6 +57,26 @@ func FindIdentifiersInShape(
|
49 | 57 | return identifiers
|
50 | 58 | }
|
51 | 59 |
|
| 60 | +// FindIdentifiersInShape returns the identifier fields of a given shape which |
| 61 | +// fit expect an ARN. |
| 62 | +func FindARNIdentifiersInShape( |
| 63 | + r *model.CRD, |
| 64 | + shape *awssdkmodel.Shape, |
| 65 | +) []string { |
| 66 | + var identifiers []string |
| 67 | + if r == nil || shape == nil { |
| 68 | + return identifiers |
| 69 | + } |
| 70 | + |
| 71 | + for _, memberName := range shape.MemberNames() { |
| 72 | + if r.IsPrimaryARNField(memberName) { |
| 73 | + identifiers = append(identifiers, memberName) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return identifiers |
| 78 | +} |
| 79 | + |
52 | 80 | // FindIdentifiersInCRD returns the identifier fields of a given CRD which
|
53 | 81 | // can be singular or plural. Note, these fields will be the *original* field
|
54 | 82 | // names from the API model shape, not renamed field names.
|
@@ -81,3 +109,114 @@ func FindIdentifiersInCRD(
|
81 | 109 |
|
82 | 110 | return identifiers
|
83 | 111 | }
|
| 112 | + |
| 113 | +// FindPluralizedIdentifiersInShape returns the name of a Spec OR Status field |
| 114 | +// that has a matching pluralized field in the given shape and the name of |
| 115 | +// the corresponding shape field name. This method will returns the original |
| 116 | +// field name - renames will need to be handled separately. |
| 117 | +// For example, DescribeVpcsInput has a `VpcIds` field which would be matched |
| 118 | +// to the `Status.VPCID` CRD field - the return value would be |
| 119 | +// "VPCID", "VpcIds". |
| 120 | +func FindPluralizedIdentifiersInShape( |
| 121 | + r *model.CRD, |
| 122 | + shape *awssdkmodel.Shape, |
| 123 | +) (crField string, shapeField string) { |
| 124 | + shapeIdentifiers := FindIdentifiersInShape(r, shape) |
| 125 | + crIdentifiers := FindIdentifiersInCRD(r) |
| 126 | + if len(shapeIdentifiers) == 0 || len(crIdentifiers) == 0 { |
| 127 | + return "", "" |
| 128 | + } |
| 129 | + |
| 130 | + pluralize := pluralize.NewClient() |
| 131 | + for _, si := range shapeIdentifiers { |
| 132 | + for _, ci := range crIdentifiers { |
| 133 | + if strings.EqualFold(pluralize.Singular(si), |
| 134 | + pluralize.Singular(ci)) { |
| 135 | + // The CRD identifiers being used for comparison reflect the |
| 136 | + // *original* field names in the API model shape. |
| 137 | + if crField == "" { |
| 138 | + crField = ci |
| 139 | + shapeField = si |
| 140 | + } else { |
| 141 | + // If there are multiple identifiers, then prioritize the |
| 142 | + // 'Id' identifier. Checking 'Id' to determine resource |
| 143 | + // creation should be safe as the field is usually |
| 144 | + // present in CR.Status. |
| 145 | + if !strings.HasSuffix(crField, "Id") || |
| 146 | + !strings.HasSuffix(crField, "Ids") { |
| 147 | + crField = ci |
| 148 | + shapeField = si |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + return crField, shapeField |
| 155 | +} |
| 156 | + |
| 157 | +// FindPrimaryIdentifierFieldNames returns the resource identifier field name |
| 158 | +// for the primary identifier used in a given operation and its corresponding |
| 159 | +// shape field name. |
| 160 | +func FindPrimaryIdentifierFieldNames( |
| 161 | + cfg *ackgenconfig.Config, |
| 162 | + r *model.CRD, |
| 163 | + op *awssdkmodel.Operation, |
| 164 | +) (crField string, shapeField string) { |
| 165 | + shape := op.InputRef.Shape |
| 166 | + |
| 167 | + // Attempt to fetch the primary identifier override from the configuration |
| 168 | + opConfig, ok := cfg.Operations[op.Name] |
| 169 | + if ok { |
| 170 | + shapeField = opConfig.PrimaryIdentifierFieldName |
| 171 | + } |
| 172 | + |
| 173 | + if shapeField == "" { |
| 174 | + // For ReadOne, search for a direct identifier |
| 175 | + if op == r.Ops.ReadOne { |
| 176 | + identifiers := FindIdentifiersInShape(r, shape) |
| 177 | + identifiers = append(identifiers, FindARNIdentifiersInShape(r, shape)...) |
| 178 | + |
| 179 | + switch len(identifiers) { |
| 180 | + case 0: |
| 181 | + break |
| 182 | + case 1: |
| 183 | + shapeField = identifiers[0] |
| 184 | + default: |
| 185 | + panic("Found multiple possible primary identifiers for " + |
| 186 | + r.Names.Original + ". Set " + |
| 187 | + "`primary_identifier_field_name` for the " + op.Name + |
| 188 | + " operation in the generator config.") |
| 189 | + } |
| 190 | + } else { |
| 191 | + // For ReadMany, search for pluralized identifiers |
| 192 | + crField, shapeField = FindPluralizedIdentifiersInShape(r, shape) |
| 193 | + } |
| 194 | + |
| 195 | + // Require override if still can't find any identifiers |
| 196 | + if shapeField == "" { |
| 197 | + panic("Could not find primary identifier for " + r.Names.Original + |
| 198 | + ". Set `primary_identifier_field_name` for the " + op.Name + |
| 199 | + " operation in the generator config.") |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + if r.IsPrimaryARNField(shapeField) || shapeField == PrimaryIdentifierARNOverride { |
| 204 | + return "", PrimaryIdentifierARNOverride |
| 205 | + } |
| 206 | + |
| 207 | + if crField == "" { |
| 208 | + renamedName, _ := r.InputFieldRename( |
| 209 | + op.Name, shapeField, |
| 210 | + ) |
| 211 | + |
| 212 | + _, inSpec := r.SpecFields[renamedName] |
| 213 | + _, inStatus := r.StatusFields[renamedName] |
| 214 | + if inSpec || inStatus { |
| 215 | + crField = renamedName |
| 216 | + } else { |
| 217 | + panic("Could not find corresponding spec or status field for primary identifier " + shapeField) |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + return crField, shapeField |
| 222 | +} |
0 commit comments