fix setter clear set-by test

This commit is contained in:
Phillip Wittrock
2020-02-26 20:17:54 -08:00
parent cf61a360e0
commit 3c776b3435
3 changed files with 19 additions and 4 deletions

View File

@@ -109,7 +109,6 @@ openAPI:
setter: setter:
name: replicas name: replicas
value: "4" value: "4"
setBy: me
`, `,
expectedResources: ` expectedResources: `
apiVersion: apps/v1 apiVersion: apps/v1

View File

@@ -106,7 +106,11 @@ type SetterDefinition struct {
Count int `yaml:"count,omitempty"` Count int `yaml:"count,omitempty"`
// EnumValues is a map of possible setter values to actual field values. // EnumValues is a map of possible setter values to actual field values.
// May be used for t-shirt sizing values -- e.g. set cpu to small, medium, large // If EnumValues is specified, then the value set the by user 1) MUST
// be present in the enumValues map as a key, and 2) the map entry value
// MUST be used as the value to set in the configuration (rather than the key)
// Example -- may be used for t-shirt sizing values by allowing cpu to be
// set to small, medium or large, and then mapping these values to cpu values -- 0.5, 2, 8
EnumValues map[string]string `yaml:"enumValues,omitempty"` EnumValues map[string]string `yaml:"enumValues,omitempty"`
} }

View File

@@ -88,7 +88,8 @@ func (s *Set) substitute(field *yaml.RNode, ext *cliExtension) (bool, error) {
} }
if val, found := subSetter.Setter.EnumValues[subSetter.Setter.Value]; found { if val, found := subSetter.Setter.EnumValues[subSetter.Setter.Value]; found {
// resolve enum for substitution // the setter has an enum-map. we should replace the marker with the
// enum value looked up from the map rather than the enum key
p = strings.ReplaceAll(p, v.Marker, val) p = strings.ReplaceAll(p, v.Marker, val)
} else { } else {
// substitute the setters current value into the substitution pattern // substitute the setters current value into the substitution pattern
@@ -121,6 +122,8 @@ func (s *Set) set(field *yaml.RNode, ext *cliExtension) bool {
// TODO(pwittrock): validate the field value // TODO(pwittrock): validate the field value
if val, found := ext.Setter.EnumValues[ext.Setter.Value]; found { if val, found := ext.Setter.EnumValues[ext.Setter.Value]; found {
// the setter has an enum-map. we should replace the marker with the
// enum value looked up from the map rather than the enum key
field.YNode().Value = val field.YNode().Value = val
return true return true
} }
@@ -158,22 +161,31 @@ func (s SetOpenAPI) Filter(object *yaml.RNode) (*yaml.RNode, error) {
return nil, errors.Errorf("no setter %s found", s.Name) return nil, errors.Errorf("no setter %s found", s.Name)
} }
// if the setter is an enumeration, then make sure the value matches // if the setter contains an enumValues map, then ensure the set value appears
// as a key in the map
if values, err := def.Pipe(yaml.Lookup("enumValues")); err != nil { if values, err := def.Pipe(yaml.Lookup("enumValues")); err != nil {
// error looking up the enumValues
return nil, err return nil, err
} else if values != nil { } else if values != nil {
// contains enumValues map -- validate the set value against the map entries
// get the enumValues keys
fields, err := values.Fields() fields, err := values.Fields()
if err != nil { if err != nil {
return nil, err return nil, err
} }
// search for the user provided value in the set of allowed values
var match bool var match bool
for i := range fields { for i := range fields {
if fields[i] == s.Value { if fields[i] == s.Value {
// found a match, we are good
match = true match = true
break break
} }
} }
if !match { if !match {
// no match found -- provide an informative error to the user
return nil, errors.Errorf("%s does not match the possible values for %s: [%s]", return nil, errors.Errorf("%s does not match the possible values for %s: [%s]",
s.Value, s.Name, strings.Join(fields, ",")) s.Value, s.Name, strings.Join(fields, ","))
} }