|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package code |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "strings" |
| 19 | + |
| 20 | + "github.com/aws-controllers-k8s/code-generator/pkg/fieldpath" |
| 21 | + ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/generate/config" |
| 22 | + "github.com/aws-controllers-k8s/code-generator/pkg/model" |
| 23 | + |
| 24 | + awssdkmodel "github.com/aws/aws-sdk-go/private/model/api" |
| 25 | +) |
| 26 | + |
| 27 | +// ResourceIsSynced returns the Go code that verifies whether a resource is synced or |
| 28 | +// not. This code is generated using ack-generate configuration. |
| 29 | +// See ack-generate/pkg/config.SyncedConfiguration. |
| 30 | +// |
| 31 | +// Sample output: |
| 32 | +// |
| 33 | +// candidates0 := []string{"AVAILABLE", "ACTIVE"} |
| 34 | +// if !ackutil.InStrings(*r.ko.Status.TableStatus, candidates0) { |
| 35 | +// return false, nil |
| 36 | +// } |
| 37 | +// if r.ko.Spec.ProvisionedThroughput == nil { |
| 38 | +// return false, nil |
| 39 | +// } |
| 40 | +// if r.ko.Spec.ProvisionedThroughput.ReadCapacityUnits == nil { |
| 41 | +// return false, nil |
| 42 | +// } |
| 43 | +// candidates1 := []int{0, 10} |
| 44 | +// if !ackutil.InStrings(*r.ko.Spec.ProvisionedThroughput.ReadCapacityUnits, candidates1) { |
| 45 | +// return false, nil |
| 46 | +// } |
| 47 | +// candidates2 := []int{0} |
| 48 | +// if !ackutil.InStrings(*r.ko.Status.ItemCount, candidates2) { |
| 49 | +// return false, nil |
| 50 | +// } |
| 51 | +func ResourceIsSynced( |
| 52 | + cfg *ackgenconfig.Config, |
| 53 | + r *model.CRD, |
| 54 | + // String |
| 55 | + resVarName string, |
| 56 | + // Number of levels of indentation to use |
| 57 | + indentLevel int, |
| 58 | +) string { |
| 59 | + out := "\n" |
| 60 | + resConfig, ok := cfg.ResourceConfig(r.Names.Original) |
| 61 | + if !ok || resConfig.Synced == nil || len(resConfig.Synced.When) == 0 { |
| 62 | + return out |
| 63 | + } |
| 64 | + |
| 65 | + for _, condition := range resConfig.Synced.When { |
| 66 | + if condition.Path == nil || *condition.Path == "" { |
| 67 | + panic("Received an empty sync condition path. 'SyncCondition.Path' must be provided.") |
| 68 | + } |
| 69 | + if len(condition.In) == 0 { |
| 70 | + panic("'SyncCondition.In' must be provided.") |
| 71 | + } |
| 72 | + fp := fieldpath.FromString(*condition.Path) |
| 73 | + field, err := getTopLevelField(r, *condition.Path) |
| 74 | + if err != nil { |
| 75 | + msg := fmt.Sprintf("cannot find top level field of path '%s': %v", *condition.Path, err) |
| 76 | + panic(msg) |
| 77 | + } |
| 78 | + candidatesVarName := fmt.Sprintf("%sCandidates", field.Names.CamelLower) |
| 79 | + if fp.Size() == 2 { |
| 80 | + out += scalarFieldEqual(resVarName, candidatesVarName, field.ShapeRef.GoTypeElem(), condition) |
| 81 | + } else { |
| 82 | + out += fieldPathSafeEqual(resVarName, candidatesVarName, field, condition) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return out |
| 87 | +} |
| 88 | + |
| 89 | +func getTopLevelField(r *model.CRD, fieldPath string) (*model.Field, error) { |
| 90 | + fp := fieldpath.FromString(fieldPath) |
| 91 | + if fp.Size() < 2 { |
| 92 | + return nil, fmt.Errorf("fieldPath must contain at least two elements, received: %s", fieldPath) |
| 93 | + } |
| 94 | + |
| 95 | + head := fp.PopFront() |
| 96 | + fieldName := fp.PopFront() |
| 97 | + switch head { |
| 98 | + case "Spec": |
| 99 | + field, ok := r.Fields[fieldName] |
| 100 | + if !ok { |
| 101 | + return nil, fmt.Errorf("field not found in Spec: %v", fieldName) |
| 102 | + } |
| 103 | + return field, nil |
| 104 | + case "Status": |
| 105 | + field, ok := r.Fields[fieldName] |
| 106 | + if !ok { |
| 107 | + return nil, fmt.Errorf("field not found in Status: %v", fieldName) |
| 108 | + } |
| 109 | + return field, nil |
| 110 | + default: |
| 111 | + return nil, fmt.Errorf("fieldPath must start with 'Spec' or 'Status', received: %v", head) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// scalarFieldEqual returns Go code that compares a scalar field to a given set of values. |
| 116 | +func scalarFieldEqual(resVarName string, candidatesVarName string, goType string, condition ackgenconfig.SyncedCondition) string { |
| 117 | + out := "" |
| 118 | + fieldPath := fmt.Sprintf("%s.%s", resVarName, *condition.Path) |
| 119 | + |
| 120 | + valuesSlice := "" |
| 121 | + switch goType { |
| 122 | + case "string": |
| 123 | + // []string{"AVAILABLE", "ACTIVE"} |
| 124 | + valuesSlice = fmt.Sprintf("[]string{\"%s\"}", strings.Join(condition.In, "\", \"")) |
| 125 | + case "int64", "PositiveLongObject", "Long": |
| 126 | + // []int64{1, 2} |
| 127 | + valuesSlice = fmt.Sprintf("[]int{%s}", strings.Join(condition.In, ", ")) |
| 128 | + case "bool": |
| 129 | + // []bool{false} |
| 130 | + valuesSlice = fmt.Sprintf("[]bool{%s}", condition.In) |
| 131 | + default: |
| 132 | + panic("not supported type " + goType) |
| 133 | + } |
| 134 | + |
| 135 | + // candidates1 := []string{"AVAILABLE", "ACTIVE"} |
| 136 | + out += fmt.Sprintf( |
| 137 | + "\t%s := %v\n", |
| 138 | + candidatesVarName, |
| 139 | + valuesSlice, |
| 140 | + ) |
| 141 | + // if !ackutil.InStrings(*r.ko.Status.State, candidates1) { |
| 142 | + out += fmt.Sprintf( |
| 143 | + "\tif !ackutil.InStrings(*%s, %s) {\n", |
| 144 | + fieldPath, |
| 145 | + candidatesVarName, |
| 146 | + ) |
| 147 | + |
| 148 | + // return false, nil |
| 149 | + out += "\t\treturn false, nil\n" |
| 150 | + // } |
| 151 | + out += "\t}\n" |
| 152 | + return out |
| 153 | +} |
| 154 | + |
| 155 | +// fieldPathSafeEqual returns go code that safely compares a resource field to value |
| 156 | +func fieldPathSafeEqual( |
| 157 | + resVarName string, |
| 158 | + candidatesVarName string, |
| 159 | + field *model.Field, |
| 160 | + condition ackgenconfig.SyncedCondition, |
| 161 | +) string { |
| 162 | + out := "" |
| 163 | + rootPath := fmt.Sprintf("%s.%s", resVarName, strings.Split(*condition.Path, ".")[0]) |
| 164 | + knownShapesPath := strings.Join(strings.Split(*condition.Path, ".")[1:], ".") |
| 165 | + |
| 166 | + fp := fieldpath.FromString(knownShapesPath) |
| 167 | + shapes := fp.IterShapeRefs(field.ShapeRef) |
| 168 | + |
| 169 | + subFieldPath := rootPath |
| 170 | + for index, shape := range shapes { |
| 171 | + if index == len(shapes)-1 { |
| 172 | + // Some aws-sdk-go scalar shapes don't contain the real name of a shape |
| 173 | + // In this case we use the full path given in condition.Path |
| 174 | + subFieldPath = fmt.Sprintf("%s.%s", resVarName, *condition.Path) |
| 175 | + } else { |
| 176 | + subFieldPath += "." + shape.Shape.ShapeName |
| 177 | + } |
| 178 | + // if r.ko.Spec.ProvisionedThroughput == nil |
| 179 | + out += fmt.Sprintf("\tif %s == nil {\n", subFieldPath) |
| 180 | + // return false, nil |
| 181 | + out += "\t\treturn false, nil\n" |
| 182 | + // } |
| 183 | + out += "\t}\n" |
| 184 | + } |
| 185 | + out += scalarFieldEqual(resVarName, candidatesVarName, shapes[len(shapes)-1].GoTypeElem(), condition) |
| 186 | + return out |
| 187 | +} |
| 188 | + |
| 189 | +func fieldPathContainsMapOrArray(fieldPath string, shapeRef *awssdkmodel.ShapeRef) bool { |
| 190 | + c := fieldpath.FromString(fieldPath) |
| 191 | + sr := c.ShapeRef(shapeRef) |
| 192 | + |
| 193 | + if sr == nil { |
| 194 | + return false |
| 195 | + } |
| 196 | + if sr.ShapeName == "map" || sr.ShapeName == "list" { |
| 197 | + return true |
| 198 | + } |
| 199 | + if sr.ShapeName == "structure" { |
| 200 | + fieldName := c.PopFront() |
| 201 | + return fieldPathContainsMapOrArray(c.Copy().At(1), sr.Shape.MemberRefs[fieldName]) |
| 202 | + } |
| 203 | + return false |
| 204 | +} |
0 commit comments