Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/app/enaptercli/cmd_base_pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type paginateRespProcesor struct {

func (p *paginateRespProcesor) Process(resp *http.Response) error {
if resp.StatusCode != http.StatusOK {
return cli.Exit("Unexpected response status: "+resp.Status, 1)
return cli.Exit(parseRespErrorMessage(resp), 1)
}

var pageBody map[string]json.RawMessage
Expand Down
86 changes: 82 additions & 4 deletions internal/app/enaptercli/cmd_rule_engine_rule_list.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package enaptercli

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/urfave/cli/v3"
Expand Down Expand Up @@ -37,15 +41,89 @@ func (c *cmdRuleEngineRuleList) Flags() []cli.Flag {
}

func (c *cmdRuleEngineRuleList) do(ctx context.Context) error {
doPaginateRequestParams := paginateHTTPRequestParams{
body, err := c.doProbeRequest(ctx)
if err != nil {
return err
}

if len(body.Rules) == 0 {
return c.printEmptyResponse()
}

if body.TotalCount == 0 {
return c.doLegacyUnpaginatedPath(body)
}

return c.doPaginatedPath(ctx)
}

type ruleListResponse struct {
Rules []json.RawMessage `json:"rules"`
TotalCount int `json:"total_count"`
}

func (c *cmdRuleEngineRuleList) doProbeRequest(ctx context.Context) (*ruleListResponse, error) {
var body ruleListResponse
err := c.doHTTPRequest(ctx, doHTTPRequestParams{
Method: http.MethodGet,
Path: "",
RespProcessor: func(resp *http.Response) error {
if resp.StatusCode != http.StatusOK {
return cli.Exit(parseRespErrorMessage(resp), 1)
}
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
return fmt.Errorf("decode response: %w", err)
}
return nil
},
})
if err != nil {
return nil, err
}
return &body, nil
}

func (c *cmdRuleEngineRuleList) printEmptyResponse() error {
respBytes, err := json.Marshal(map[string]any{
"rules": []json.RawMessage{},
"total_count": 0,
})
if err != nil {
return fmt.Errorf("marshal empty response: %w", err)
}

return c.defaultRespProcessor(&http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(respBytes)),
})
}

func (c *cmdRuleEngineRuleList) doLegacyUnpaginatedPath(body *ruleListResponse) error {
body.TotalCount = len(body.Rules)

if c.limit > 0 && len(body.Rules) > c.limit {
body.Rules = body.Rules[:c.limit]
}

respBytes, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("marshal legacy response: %w", err)
}

return c.defaultRespProcessor(&http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(respBytes)),
})
}

func (c *cmdRuleEngineRuleList) doPaginatedPath(ctx context.Context) error {
return c.doPaginateRequest(ctx, paginateHTTPRequestParams{
ObjectName: "rules",
Limit: c.limit,
DoFn: c.doHTTPRequest,
BaseParams: doHTTPRequestParams{
Method: http.MethodGet,
Path: "",
},
}

return c.doPaginateRequest(ctx, doPaginateRequestParams)
})
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Method": "GET",
"URL": "/v3/sites/my-site/rule_engine/rules?limit=50\u0026offset=0",
"URL": "/v3/sites/my-site/rule_engine/rules",
"Header": {
"Accept-Encoding": [
"gzip"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Method": "GET",
"URL": "/v3/sites/my-site/rule_engine/rules?limit=50\u0026offset=1",
"URL": "/v3/sites/my-site/rule_engine/rules?limit=50\u0026offset=0",
"Header": {
"Accept-Encoding": [
"gzip"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"Method": "GET",
"URL": "/v3/sites/my-site/rule_engine/rules?limit=50\u0026offset=1",
"Header": {
"Accept-Encoding": [
"gzip"
],
"Content-Type": [
""
],
"User-Agent": [
"enapter-cli/"
],
"X-Enapter-Auth-Token": [
"enapter_api_test_token"
]
},
"Body": ""
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"rules":[],"total_count":1}
{"rules":[{"id":"rule-1"}],"total_count":1}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rules":[],"total_count":1}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
enapter3 connection add --name my-conn --url {{.URL}} --token {{.Token}} --site-id my-site
enapter3 rule-engine rule list --connection my-conn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rules":[{"id":"rule-1"}],"total_count":1}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"Method": "GET",
"URL": "/v3/sites/my-site/rule_engine/rules",
"Header": {
"Accept-Encoding": [
"gzip"
],
"Content-Type": [
""
],
"User-Agent": [
"enapter-cli/"
],
"X-Enapter-Auth-Token": [
"enapter_api_test_token"
]
},
"Body": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"rules":[{"id":"rule-1"}]}
Loading