This is an automated email from the ASF dual-hosted git repository.

rabbah pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwhisk-client-go.git


The following commit(s) were added to refs/heads/master by this push:
     new 1ca8976  Adds a new method for getting the number of activations in 
the namespace. (#155)
1ca8976 is described below

commit 1ca897633f2d98b80c48fbce6e3a21254b8fa3fc
Author: Davi DeBarros <[email protected]>
AuthorDate: Fri Oct 14 07:27:04 2022 -0400

    Adds a new method for getting the number of activations in the namespace. 
(#155)
---
 whisk/activation.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 56 insertions(+), 3 deletions(-)

diff --git a/whisk/activation.go b/whisk/activation.go
index 8579ffa..2082bfa 100644
--- a/whisk/activation.go
+++ b/whisk/activation.go
@@ -20,11 +20,12 @@ package whisk
 import (
        "errors"
        "fmt"
-       "github.com/apache/openwhisk-client-go/wski18n"
        "net/http"
        "net/url"
        "strconv"
        "time"
+
+       "github.com/apache/openwhisk-client-go/wski18n"
 )
 
 type ActivationService struct {
@@ -48,6 +49,10 @@ type Activation struct {
        Publish      *bool       `json:"publish,omitempty"`
 }
 
+type ActivationCount struct {
+       Activations int64 `json:"activations"`
+}
+
 type ActivationFilteredRow struct {
        Row       Activation
        HeaderFmt string
@@ -72,7 +77,15 @@ type ActivationListOptions struct {
        Docs  bool   `url:"docs,omitempty"`
 }
 
-//MWD - This structure may no longer be needed as the log format is now a 
string and not JSON
+type ActivationCountOptions struct {
+       Name  string `url:"name,omitempty"`
+       Skip  int    `url:"skip"`
+       Since int64  `url:"since,omitempty"`
+       Upto  int64  `url:"upto,omitempty"`
+       Count bool   `url:"count,omitempty"`
+}
+
+// MWD - This structure may no longer be needed as the log format is now a 
string and not JSON
 type Log struct {
        Log    string `json:"log,omitempty"`
        Stream string `json:"stream,omitempty"`
@@ -118,7 +131,9 @@ func TruncateStr(str string, maxlen int) string {
 }
 
 // ToSummaryRowString() returns a compound string of required parameters for 
printing
-//   from CLI command `wsk activation list`.
+//
+//     from CLI command `wsk activation list`.
+//
 // ***Method of type Sortable***
 func (activation ActivationFilteredRow) ToSummaryRowString() string {
        s := time.Unix(0, activation.Row.Start*1000000)
@@ -189,6 +204,44 @@ func (s *ActivationService) List(options 
*ActivationListOptions) ([]Activation,
        return activations, resp, nil
 }
 
+func (s *ActivationService) Count(options *ActivationCountOptions) 
(*ActivationCount, *http.Response, error) {
+       // TODO :: for some reason /activations only works with "_" as namespace
+       s.client.Namespace = "_"
+       route := "activations"
+
+       options.Count = true
+       routeUrl, err := addRouteOptions(route, options)
+
+       if err != nil {
+               Debug(DbgError, "addRouteOptions(%s, %#v) error: '%s'\n", 
route, options, err)
+               errStr := wski18n.T("Unable to append options '{{.options}}' to 
URL route '{{.route}}': {{.err}}",
+                       map[string]interface{}{"options": fmt.Sprintf("%#v", 
options), "route": route, "err": err})
+               werr := MakeWskErrorFromWskError(errors.New(errStr), err, 
EXIT_CODE_ERR_GENERAL, DISPLAY_MSG, NO_DISPLAY_USAGE)
+               return nil, nil, werr
+       }
+
+       req, err := s.client.NewRequestUrl("GET", routeUrl, nil, 
IncludeNamespaceInUrl, AppendOpenWhiskPathPrefix, EncodeBodyAsJson, 
AuthRequired)
+       if err != nil {
+               Debug(DbgError, "http.NewRequestUrl(GET, %s, nil, 
IncludeNamespaceInUrl, AppendOpenWhiskPathPrefix, EncodeBodyAsJson, 
AuthRequired) error: '%s'\n", route, err)
+               errStr := wski18n.T("Unable to create HTTP request for GET 
'{{.route}}': {{.err}}",
+                       map[string]interface{}{"route": route, "err": err})
+               werr := MakeWskErrorFromWskError(errors.New(errStr), err, 
EXIT_CODE_ERR_GENERAL, DISPLAY_MSG, NO_DISPLAY_USAGE)
+               return nil, nil, werr
+       }
+
+       Debug(DbgInfo, "Sending HTTP request - URL '%s'; req %#v\n", 
req.URL.String(), req)
+
+       count := new(ActivationCount)
+       resp, err := s.client.Do(req, &count, ExitWithSuccessOnTimeout)
+
+       if err != nil {
+               Debug(DbgError, "s.client.Do() error - HTTP req %s; error 
'%s'\n", req.URL.String(), err)
+               return nil, resp, err
+       }
+
+       return count, resp, nil
+}
+
 func (s *ActivationService) Get(activationID string) (*Activation, 
*http.Response, error) {
        // TODO :: for some reason /activations/:id only works with "_" as 
namespace
        s.client.Namespace = "_"

Reply via email to