dubee commented on a change in pull request #479: Trigger parameter issue
URL: https://github.com/apache/openwhisk-cli/pull/479#discussion_r383983553
 
 

 ##########
 File path: commands/trigger.go
 ##########
 @@ -431,52 +435,141 @@ func (t *Trigger) Create(Client *whisk.Client, args 
[]string) error {
                return whiskErr
        }
 
+       //1. if the command line arguments user provides contains only --param 
flags
+       //2. if the command line arguments user provides contains no --param 
flags at all
+       //we should process the trigger create command in the old way.
+       if userIndicatesToUseOldTriggerCommand() {
+               triggerName, err := NewQualifiedName(args[0])
+               if err != nil {
+                       return NewQualifiedNameError(args[0], err)
+               }
+
+               //if user also issued new trigger command then we stop execution
+               if triggerUsageErr := userIssuedNewTriggerCmd(); 
triggerUsageErr != nil {
+                       return triggerUsageErr
+               }
+
+               annotationArray := Flags.common.annotation
+               authToken := Client.Config.AuthToken
+
+               // if a feed is specified, create additional parameters which 
must be passed to the feed
+               feedQualifiedName, additionalFeedParams := 
feedParameters(Flags.common.feed, FEED_CREATE, triggerName, authToken)
+
+               // if a feed is specified, add feed annotation the annotations 
declared on the command line
+               // TODO: add test to ensure that generated annotation has 
precedence
+               if feedQualifiedName != nil {
+                       annotationArray = append(annotationArray, 
getFormattedJSON("feed", feedQualifiedName.GetFullQualifiedName()))
+               }
+               annotations := getParameters(annotationArray, true, true)
+
+               // the feed receives all the parameters that are specified on 
the command line so we merge
+               // the feed lifecycle parameters with the command line ones
+               parameters := getParameters(append(Flags.common.param, 
additionalFeedParams...), feedQualifiedName == nil, false)
+
+               trigger := &whisk.Trigger{
+                       Name:        triggerName.GetEntityName(),
+                       Annotations: annotations.(whisk.KeyValueArr),
+               }
+
+               if feedQualifiedName == nil {
+                       // parameters are only attached to the trigger in there 
is no feed, otherwise
+                       // parameters are passed to the feed instead
+                       trigger.Parameters = parameters.(whisk.KeyValueArr)
+               }
+
+               createOrUpdate(Client, triggerName, trigger, false)
+
+               // Invoke the specified feed action to configure the trigger 
feed
+               if feedQualifiedName != nil {
+                       res, err := invokeAction(*feedQualifiedName, 
parameters, true, false)
+                       if err != nil {
+                               whisk.Debug(whisk.DbgError, "Failed configuring 
feed '%s' failed: %s\n", feedQualifiedName.GetFullQualifiedName(), err)
+
+                               // TODO: should we do this at all? Keeping for 
now.
+                               
printFailedBlockingInvocationResponse(*feedQualifiedName, false, res, err)
+
+                               reason := wski18n.T(FEED_CONFIGURATION_FAILURE, 
map[string]interface{}{"feedname": feedQualifiedName.GetFullQualifiedName(), 
"err": err})
+                               errStr := wski18n.T("Unable to create trigger 
'{{.name}}': {{.err}}",
+                                       map[string]interface{}{"name": 
trigger.Name, "err": reason})
+                               werr := 
whisk.MakeWskErrorFromWskError(errors.New(errStr), err, 
whisk.EXIT_CODE_ERR_GENERAL, whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)
+
+                               // Delete trigger that was created for this feed
+                               err = deleteTrigger(triggerName.GetEntityName())
+                               if err != nil {
+                                       whisk.Debug(whisk.DbgWarn, "Ignoring 
deleteTrigger(%s) failure: %s\n", triggerName.GetEntityName(), err)
+                               }
+
+                               return werr
+                       } else {
+                               whisk.Debug(whisk.DbgInfo, "Successfully 
configured trigger feed via feed action '%s'\n", Flags.common.feed)
+
+                               // preserve existing behavior where output of 
feed activation is emitted to console
+                               printInvocationMsg(*feedQualifiedName, true, 
true, res, color.Output)
+                       }
+               }
+
+               fmt.Fprintf(color.Output,
+                       wski18n.T("{{.ok}} created trigger {{.name}}\n",
+                               map[string]interface{}{"ok": 
color.GreenString("ok:"), "name": boldString(trigger.Name)}))
+               return nil
+       }
+       //1. if user's input command line argument contains either --feed-param 
or --trigger-param
+       //2. if user's input command line argument contains both --feed-param 
and --trigger-param
+       //then we process trigger create command in a different way
+       return CreateExtendedVersion(Client, args)
+}
+
+//CreateExtendedVersion only executes when users indicate to create triggers 
with --feed-param
+//or --trigger-param flags.
+func CreateExtendedVersion(Client *whisk.Client, args []string) error {
        triggerName, err := NewQualifiedName(args[0])
        if err != nil {
                return NewQualifiedNameError(args[0], err)
        }
 
-       paramArray := Flags.common.param
        annotationArray := Flags.common.annotation
-       feedParam := Flags.common.feed
        authToken := Client.Config.AuthToken
 
        // if a feed is specified, create additional parameters which must be 
passed to the feed
-       feedName, feedParams := feedParameters(feedParam, FEED_CREATE, 
triggerName, authToken)
-       // the feed receives all the parameters that are specified on the 
command line so we merge
-       // the feed lifecycle parameters with the command line ones
-       parameters := getParameters(append(paramArray, feedParams...), feedName 
== nil, false)
+       feedQualifiedName, additionalFeedParams := 
feedParameters(Flags.common.feed, FEED_CREATE, triggerName, authToken)
 
        // if a feed is specified, add feed annotation the annotations declared 
on the command line
        // TODO: add test to ensure that generated annotation has precedence
-       if feedName != nil {
-               annotationArray = append(annotationArray, 
getFormattedJSON("feed", feedName.GetFullQualifiedName()))
+       if feedQualifiedName != nil {
+               annotationArray = append(annotationArray, 
getFormattedJSON("feed", feedQualifiedName.GetFullQualifiedName()))
        }
+
        annotations := getParameters(annotationArray, true, true)
 
+       //if trigger contains no feed but user tries to update feed parameter, 
then we issue error.
+       if feedQualifiedName == nil && len(Flags.trigger.feedParam) > 0 {
+               whisk.Debug(whisk.DbgError, "Incorrect usage. trigger without a 
feed cannot have feed parameters")
 
 Review comment:
   This error string does not match what is in `en_US.all.json`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to