8000 fix(cdsctl): create admin curl command and fix template bulk test (#3… · ovh/cds@d91ddcb · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit d91ddcb

Browse files
authored
fix(cdsctl): create admin curl command and fix template bulk test (#3887)
1 parent 78798ca commit d91ddcb

File tree

4 files changed

+106
-13
lines changed

4 files changed

+106
-13
lines changed

cli/cdsctl/admin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func adminCommands() []*cobra.Command {
2222
adminPlugins(),
2323
adminBroadcasts(),
2424
adminErrors(),
25+
adminCurl(),
2526
}
2627
}
2728

cli/cdsctl/admin_curl.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"fmt"
7+
"io"
8+
"net/http"
9+
10+
"github.com/spf13/cobra"
11+
12+
"github.com/ovh/cds/cli"
13+
"github.com/ovh/cds/sdk/cdsclient"
14+
)
15+
16+
func adminCurl() *cobra.Command {
17+
return cli.NewCommand(adminCurlCmd, adminCurlFunc, nil)
18+
}
19+
20+
var adminCurlCmd = cli.Command{
21+
Name: "curl",
22+
Short: "Execute request to CDS api",
23+
Args: []cli.Arg{
24+
{
25+
Name: "path",
26+
},
27+
},
28+
Flags: []cli.Flag{
29+
{
30+
Type: cli.FlagString,
31+
Name: "request",
32+
ShortHand: "X",
33+
Default: http.MethodGet,
34+
},
35+
{
36+
Type: cli.FlagString,
37+
Name: "data",
38+
ShortHand: "d",
39+
},
40+
},
41+
}
42+
43+
func adminCurlFunc(v cli.Values) error {
44+
var rdata io.Reader
45+
46+
data := v.GetString("data")
47+
if data != "" {
48+
rdata = bytes.NewReader([]byte(data))
49+
}
50+
51+
res, _, _, err := client.(cdsclient.Raw).Request(context.Background(), v.GetString("request"), v.GetString("path"), rdata)
52+
if err != nil {
53+
return err
54+
}
55+
56+
fmt.Println(string(res))
57+
58+
return nil
59+
}

cli/cdsctl/version.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
56

67
"github.com/spf13/cobra"
8+
yaml "gopkg.in/yaml.v2"
79

810
"github.com/ovh/cds/cli"
911
"github.com/ovh/cds/sdk"
1214
var versionCmd = cli.Command{
1315
Name: "version",
1416
Short: "show cdsctl version",
17+
Flags: []cli.Flag{
18+
{
19+
Type: cli.FlagString,
20+
Name: "format",
21+
Usage: "Specify out format (json or yaml)",
22+
},
23+
},
1524
}
1625

1726
func version() *cobra.Command {
1827
return cli.NewCommand(versionCmd, versionRun, nil, cli.CommandWithoutExtraFlags)
1928
}
2029

2130
func versionRun(v cli.Values) error {
22-
fmt.Println(sdk.VersionString())
2331
version, err := client.Version()
2432
if err != nil {
2533
return err
2634
}
27-
fmt.Printf("CDS api version: %s\n", version.Version)
28-
fmt.Printf("CDS URL: %s\n", client.APIURL())
35+
36+
m := map[string]interface{}{
37+
"version": sdk.VersionString(),
38+
"api-version": version.Version,
39+
"api-url": client.APIURL(),
40+
}
41+
42+
format := v.GetString("format")
43+
if format == "" {
44+
fmt.Println(m["version"])
45+
fmt.Printf("CDS api version: %s\n", m["api-version"])
46+
fmt.Printf("CDS URL: %s\n", m["api-url"])
47+
return nil
48+
}
49+
50+
var buf []byte
51+
52+
switch format {
53+
case "json":
54+
buf, err = json.Marshal(m)
55+
case "yaml":
56+
buf, err = yaml.Marshal(m)
57+
default:
58+
return fmt.Errorf("invalid given format")
59+
}
60+
if err != nil {
61+
return err
62+
}
63+
64+
fmt.Println(string(buf))
65+
2966
return nil
3067
}

tests/clictl_template_bulk.yml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,12 @@ testcases:
2222

2323
- name: check bulk request status
2424
steps:
25-
- type: http
26-
method: GET
27-
url: {{.cds.build.api}}/template/shared.infra/example-simple/bulk/{{.sendTemplateBulkRequest.bulkID}}
28-
headers:
29-
Session-Token: {{.cds.build.token}}
25+
- script: {{.cds.build.cdsctl}} admin curl /template/shared.infra/example-simple/bulk/{{.sendTemplateBulkRequest.bulkID}}
3026
retry: 10
3127
delay: 2
3228
assertions:
33-
- result.statuscode ShouldEqual 200
34-
- result.bodyjson.operations.operations0.status ShouldEqual 2
35-
- result.bodyjson.operations.operations1.status ShouldEqual 2
36-
- result.bodyjson.operations.operations2.status ShouldEqual 3
37-
- result.bodyjson.operations.operations2.error ShouldEqual 'Unsupported when condition ok'
29+
- result.code ShouldEqual 0
30+
- result.systemoutjson.operations.operations0.status ShouldEqual 2
31+
- result.systemoutjson.operations.operations1.status ShouldEqual 2
32+
- result.systemoutjson.operations.operations2.status ShouldEqual 3
33+
- result.systemoutjson.operations.operations2.error ShouldEqual 'Unsupported when condition ok'

0 commit comments

Comments
 (0)
0