Skip to content

Commit e6f164e

Browse files
Merge pull request #20 from codefresh-io/CR-5223
support runtime list
2 parents 38b1d0c + 5a77eae commit e6f164e

File tree

6 files changed

+130
-1
lines changed

6 files changed

+130
-1
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.26.2
1+
0.26.3

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module github.com/codefresh-io/go-sdk
22

33
require (
44
github.com/BurntSushi/toml v0.3.1 // indirect
5+
github.com/codefresh-io/argo-platform v1.13.0
56
github.com/dustin/go-humanize v1.0.0
67
github.com/google/go-querystring v1.1.0
78
github.com/inconshreveable/mousetrap v1.0.0 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
33
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
4+
github.com/codefresh-io/argo-platform v1.13.0 h1:eWAWkxtO0nGZuekrQJsfFT5KfBMdWby0cOucy4yTExs=
5+
github.com/codefresh-io/argo-platform v1.13.0/go.mod h1:u/eLWAySJ1nRzNWnB5baeyIc5vqINghwYJvydU+EC3c=
46
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
57
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
68
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=

pkg/codefresh/argo_runtime.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package codefresh
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
8+
"github.com/codefresh-io/argo-platform/libs/ql/graph/model"
9+
)
10+
11+
type (
12+
IArgoRuntimeAPI interface {
13+
List() ([]model.Runtime, error)
14+
}
15+
argoRuntime struct {
16+
codefresh *codefresh
17+
}
18+
graphqlRuntimesResponse struct {
19+
Data struct {
20+
Runtimes model.RuntimePage
21+
}
22+
Errors []graphqlError
23+
}
24+
)
25+
26+
func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI {
27+
return &argoRuntime{codefresh: codefresh}
28+
}
29+
func (r *argoRuntime) List() ([]model.Runtime, error) {
30+
31+
jsonData := map[string]interface{}{
32+
"query": `
33+
{
34+
runtimes{
35+
edges{
36+
node {
37+
id
38+
namespace
39+
objectMeta {
40+
name
41+
description
42+
}
43+
}
44+
}
45+
}
46+
}
47+
`,
48+
}
49+
50+
response, err := r.codefresh.requestAPI(&requestOptions{
51+
method: "POST",
52+
path: "/argo/api/graphql",
53+
body: jsonData,
54+
})
55+
defer response.Body.Close()
56+
if err != nil {
57+
fmt.Printf("The HTTP request failed with error %s\n", err)
58+
return nil, err
59+
}
60+
data, err := ioutil.ReadAll(response.Body)
61+
if err != nil {
62+
fmt.Printf("failed to read from response body")
63+
return nil, err
64+
}
65+
res := graphqlRuntimesResponse{}
66+
err = json.Unmarshal(data, &res)
67+
if err != nil {
68+
return nil, err
69+
}
70+
runtimes := make([]model.Runtime, len(res.Data.Runtimes.Edges))
71+
for i := range res.Data.Runtimes.Edges {
72+
runtimes[i] = *res.Data.Runtimes.Edges[i].Node
73+
}
74+
75+
if len(res.Errors) > 0 {
76+
return nil, graphqlErrorResponse{errors: res.Errors}
77+
}
78+
79+
return runtimes, nil
80+
81+
}

pkg/codefresh/codefresh.go

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type (
2828
Users() UsersAPI
2929
Argo() ArgoAPI
3030
Gitops() GitopsAPI
31+
ArgoRuntime() IArgoRuntimeAPI
3132
}
3233
)
3334

@@ -84,6 +85,10 @@ func (c *codefresh) Gitops() GitopsAPI {
8485
return newGitopsAPI(c)
8586
}
8687

88+
func (c *codefresh) ArgoRuntime() IArgoRuntimeAPI {
89+
return newArgoRuntimeAPI(c)
90+
}
91+
8792
func (c *codefresh) requestAPI(opt *requestOptions) (*http.Response, error) {
8893
return c.requestAPIWithContext(context.Background(), opt)
8994
}

pkg/codefresh/common.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package codefresh
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
)
8+
9+
type graphqlError struct {
10+
Message string
11+
Locations [] struct {
12+
Line int
13+
Column int
14+
}
15+
Extensions struct {
16+
Code string
17+
Exception struct {
18+
Stacktrace []string
19+
}
20+
}
21+
}
22+
23+
type graphqlErrorResponse struct {
24+
errors []graphqlError
25+
concatenatedErrors string
26+
}
27+
28+
29+
func (e graphqlErrorResponse) Error() string {
30+
31+
if e.concatenatedErrors != "" {
32+
return e.concatenatedErrors
33+
}
34+
var sb strings.Builder
35+
for _, err := range e.errors {
36+
sb.WriteString(fmt.Sprintln(err.Message))
37+
}
38+
e.concatenatedErrors = sb.String()
39+
return e.concatenatedErrors
40+
}

0 commit comments

Comments
 (0)