Handle the case where an incoming filter is a string (#4)

Ideally we'd ensure that an incoming filter is a string, but some
clients, such as the CLI, don't have a good way to require this without
it being very onerous to the user.

Eventually we can handle this via allowing the plugin to declare a
schema and perform type validation in the handler, but for now, work
around this within the plugin.
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-11-12 19:47:46 -05:00
1 parent 563a2b9931
commit 1c7bacafa7
2 files changed
+70 -124

No files matched your search

+13 -2
View File
@@ -311,7 +311,7 @@ func (p *AwsPlugin) OnUpdateSet(ctx context.Context, req *pb.OnUpdateSetRequest)
}
if set.GetAttributes() == nil {
return nil, errors.New("set missing attributes")
return nil, errors.New("new set missing attributes")
}
setAttrs, err := getSetAttributes(set.GetAttributes())
if err != nil {
@@ -534,7 +534,18 @@ func getTimeValue(in *structpb.Struct, k string) (time.Time, error) {
func getSetAttributes(in *structpb.Struct) (*SetAttributes, error) {
var setAttrs SetAttributes
if err := mapstructure.Decode(in.AsMap(), &setAttrs); err != nil {
// Mapstructure complains if it expects a slice as output and sees a scalar
// value. Rather than use WeakDecode and risk unintended consequences, I'm
// manually making this change if necessary.
inMap := in.AsMap()
if filtersRaw, ok := inMap[constDescribeInstancesFilters]; ok {
switch filterVal := filtersRaw.(type) {
case string:
inMap[constDescribeInstancesFilters] = []string{filterVal}
}
}
if err := mapstructure.Decode(inMap, &setAttrs); err != nil {
return nil, fmt.Errorf("error decoding set attributes: %w", err)
}