Skip to content

Commit 67ee829

Browse files
committed
added component list
refactored code into codefresh.go
1 parent 39e472b commit 67ee829

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

pkg/codefresh/argo_runtime.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ type (
3131
}
3232
)
3333

34-
var qlEndPoint = "/2.0/api/graphql"
35-
3634
func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI {
3735
return &argoRuntime{codefresh: codefresh}
3836
}

pkg/codefresh/codefresh.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"strings"
1111

12+
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
1213
"github.com/google/go-querystring/query"
1314
)
1415

@@ -30,9 +31,19 @@ type (
3031
Gitops() GitopsAPI
3132
ArgoRuntime() IArgoRuntimeAPI
3233
GitSource() IGitSourceAPI
34+
Component() IComponentAPI
35+
}
36+
37+
graphqlResponse struct {
38+
Data struct {
39+
Runtimes model.RuntimePage
40+
}
41+
Errors []graphqlError
3342
}
3443
)
3544

45+
var qlEndPoint = "/2.0/api/graphql"
46+
3647
func New(opt *ClientOptions) Codefresh {
3748
httpClient := &http.Client{}
3849
if opt.Client != nil {
@@ -94,6 +105,10 @@ func (c *codefresh) GitSource() IGitSourceAPI {
94105
return newGitSourceAPI(c)
95106
}
96107

108+
func (c *codefresh) Component() IComponentAPI {
109+
return newComponentAPI(c)
110+
}
111+
97112
func (c *codefresh) requestAPI(opt *requestOptions) (*http.Response, error) {
98113
return c.requestAPIWithContext(context.Background(), opt)
99114
}
@@ -122,6 +137,25 @@ func (c *codefresh) requestAPIWithContext(ctx context.Context, opt *requestOptio
122137
return response, nil
123138
}
124139

140+
func (c *codefresh) graphqlAPI(ctx context.Context, body map[string]interface{}, res interface{}) error {
141+
response, err := c.requestAPIWithContext(ctx, &requestOptions{
142+
method: "POST",
143+
path: qlEndPoint,
144+
body: body,
145+
})
146+
if err != nil {
147+
return fmt.Errorf("The HTTP request failed: %w", err)
148+
}
149+
defer response.Body.Close()
150+
151+
data, err := ioutil.ReadAll(response.Body)
152+
if err != nil {
153+
return fmt.Errorf("failed to read from response body: %w", err)
154+
}
155+
156+
return json.Unmarshal(data, res)
157+
}
158+
125159
func buildQSFromMap(qs map[string]string) string {
126160
var arr = []string{}
127161
for k, v := range qs {

pkg/codefresh/component.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package codefresh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
8+
)
9+
10+
type (
11+
IComponentAPI interface {
12+
List(ctx context.Context, runtimeName string) ([]model.Component, error)
13+
}
14+
15+
component struct {
16+
codefresh *codefresh
17+
}
18+
19+
graphqlComponentsResponse struct {
20+
Data struct {
21+
Components model.ComponentPage
22+
}
23+
Errors []graphqlError
24+
}
25+
)
26+
27+
func newComponentAPI(codefresh *codefresh) IComponentAPI {
28+
return &component{codefresh: codefresh}
29+
}
30+
31+
func (r *component) List(ctx context.Context, runtimeName string) ([]model.Component, error) {
32+
jsonData := map[string]interface{}{
33+
"query": `
34+
query Components($runtime: String!) {
35+
components(runtime: $runtime) {
36+
edges {
37+
node {
38+
metadata {
39+
name
40+
}
41+
self {
42+
status {
43+
syncStatus
44+
healthStatus
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}`,
51+
"variables": map[string]interface{}{
52+
"runtime": runtimeName,
53+
},
54+
}
55+
56+
res := &graphqlComponentsResponse{}
57+
err := r.codefresh.graphqlAPI(ctx, jsonData, res)
58+
if err != nil {
59+
return nil, fmt.Errorf("failed getting components list: %w", err)
60+
}
61+
62+
if len(res.Errors) > 0 {
63+
return nil, graphqlErrorResponse{errors: res.Errors}
64+
}
65+
66+
components := make([]model.Component, len(res.Data.Components.Edges))
67+
for i := range res.Data.Components.Edges {
68+
components[i] = *res.Data.Components.Edges[i].Node
69+
}
70+
71+
return components, nil
72+
}

0 commit comments

Comments
 (0)