Skip to content

Extend getAttributeFromPath search to status fields as well. #512

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
Feb 19, 2024
Merged
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
45 changes: 31 additions & 14 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,38 +547,55 @@ func getAttributeFromPath(crd *CRD, fieldPath string, tdefs []*TypeDef) (parentT
}
// First part of nested reference fieldPath is the name of top level Spec
// field. Ex: For 'ResourcesVpcConfig.SecurityGroupIds' fieldpath the
// specFieldName is 'ResourcesVpcConfig'
specFieldName := fp.Front()
var specField *Field
// topLevelFieldName is 'ResourcesVpcConfig'
topLevelFieldName := fp.Front()
var topLevelField *Field
foundInSpec := false
foundInStatus := false
for fName, field := range crd.SpecFields {
if strings.EqualFold(fName, specFieldName) {
specField = field
if strings.EqualFold(fName, topLevelFieldName) {
topLevelField = field
foundInSpec = true
break
}
}
if specField == nil {
panic(fmt.Sprintf("Unable to find a spec field with name %s"+
" to add reference for %s", specFieldName,
if !foundInSpec {
for fName, field := range crd.StatusFields {
if strings.EqualFold(fName, topLevelFieldName) {
topLevelField = field
foundInStatus = true
}
}
}
if !foundInSpec && !foundInStatus {
panic(fmt.Sprintf("Unable to find a spec or status field with name %s"+
" to add get attribute from %s", topLevelFieldName,
fieldPath))
}
if foundInSpec && foundInStatus {
panic(fmt.Sprintf(
"error getting attributes from %s: found a field with name %s in both spec and status",
fieldPath, topLevelFieldName,
))
}

// Create a new fieldPath starting with ShapeName of Spec Field
// to determine the shape of typedef which will contain the reference
// attribute. We replace the spec-field Name with spec-field ShapeName in
// the beginning of field path and leave rest of nested member names as is.
// Ex: ResourcesVpcConfig.SecurityGroupIDs will become VPCConfigRequest.SecurityGroupIDs
// for Cluster resource in eks-controller.
specFieldShapeRef := specField.ShapeRef
specFieldShapeRef := topLevelField.ShapeRef
specFieldShapeName := specFieldShapeRef.ShapeName
switch shapeType := specFieldShapeRef.Shape.Type; shapeType {
case "list":
specFieldShapeName = specField.ShapeRef.Shape.MemberRef.ShapeName
specFieldShapeRef = &specField.ShapeRef.Shape.MemberRef
specFieldShapeName = topLevelField.ShapeRef.Shape.MemberRef.ShapeName
specFieldShapeRef = &topLevelField.ShapeRef.Shape.MemberRef
case "map":
specFieldShapeName = specField.ShapeRef.Shape.ValueRef.ShapeName
specFieldShapeRef = &specField.ShapeRef.Shape.ValueRef
specFieldShapeName = topLevelField.ShapeRef.Shape.ValueRef.ShapeName
specFieldShapeRef = &topLevelField.ShapeRef.Shape.ValueRef
}
fieldShapePath := strings.Replace(fieldPath, specFieldName, specFieldShapeName, 1)
fieldShapePath := strings.Replace(fieldPath, topLevelFieldName, specFieldShapeName, 1)
fsp := ackfp.FromString(fieldShapePath)

// "fieldName" is the member name for which reference field will be created.
Expand Down