Skip to content

Commit 56601f2

Browse files
authored
Merge pull request #34 from codefresh-io/CR-4369
CR-4369 - components list
2 parents 39e472b + de93f9d commit 56601f2

File tree

7 files changed

+198
-136
lines changed

7 files changed

+198
-136
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.31.2
1+
0.32.0

pkg/codefresh/argo_runtime.go

+54-87
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package codefresh
22

33
import (
4-
"encoding/json"
4+
"context"
55
"fmt"
6-
"io/ioutil"
76

87
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
98
)
109

1110
type (
12-
IArgoRuntimeAPI interface {
13-
List() ([]model.Runtime, error)
14-
Create(runtimeName, cluster, runtimeVersion string) (*model.RuntimeCreationResponse, error)
11+
IRuntimeAPI interface {
12+
List(ctx context.Context) ([]model.Runtime, error)
13+
Create(ctx context.Context, runtimeName, cluster, runtimeVersion string) (*model.RuntimeCreationResponse, error)
1514
}
15+
1616
argoRuntime struct {
1717
codefresh *codefresh
1818
}
19+
1920
graphqlRuntimesResponse struct {
2021
Data struct {
2122
Runtimes model.RuntimePage
@@ -31,113 +32,79 @@ type (
3132
}
3233
)
3334

34-
var qlEndPoint = "/2.0/api/graphql"
35-
36-
func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI {
35+
func newArgoRuntimeAPI(codefresh *codefresh) IRuntimeAPI {
3736
return &argoRuntime{codefresh: codefresh}
3837
}
3938

40-
func (r *argoRuntime) Create(runtimeName, cluster, runtimeVersion string) (*model.RuntimeCreationResponse, error) {
39+
func (r *argoRuntime) List(ctx context.Context) ([]model.Runtime, error) {
4140
jsonData := map[string]interface{}{
42-
"query": `mutation CreateRuntime($name: String!, $cluster: String!, $runtimeVersion: String!) {
43-
runtime(name: $name, cluster: $cluster, runtimeVersion: $runtimeVersion) {
44-
name
45-
newAccessToken
46-
}
47-
}`,
48-
"variables": map[string]interface{}{
49-
"name": runtimeName,
50-
"cluster": cluster,
51-
"runtimeVersion": runtimeVersion,
52-
},
53-
}
54-
55-
response, err := r.codefresh.requestAPI(&requestOptions{
56-
method: "POST",
57-
path: qlEndPoint,
58-
body: jsonData,
59-
})
60-
61-
if err != nil {
62-
fmt.Printf("The HTTP request failed with error %s\n", err)
63-
return nil, err
64-
}
65-
66-
defer response.Body.Close()
67-
68-
data, err := ioutil.ReadAll(response.Body)
69-
if err != nil {
70-
fmt.Printf("failed to read from response body")
71-
return nil, err
41+
"query": `{
42+
runtimes {
43+
edges {
44+
node {
45+
metadata {
46+
name
47+
namespace
48+
}
49+
self {
50+
healthStatus
51+
version
52+
}
53+
cluster
54+
}
55+
}
56+
}
57+
}`,
7258
}
7359

74-
res := graphQlRuntimeCreationResponse{}
75-
err = json.Unmarshal(data, &res)
60+
res := &graphqlRuntimesResponse{}
61+
err := r.codefresh.graphqlAPI(ctx, jsonData, res)
7662
if err != nil {
77-
return nil, err
63+
return nil, fmt.Errorf("failed getting runtime list: %w", err)
7864
}
7965

8066
if len(res.Errors) > 0 {
8167
return nil, graphqlErrorResponse{errors: res.Errors}
8268
}
8369

84-
return &res.Data.Runtime, nil
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+
return runtimes, nil
8576
}
8677

87-
func (r *argoRuntime) List() ([]model.Runtime, error) {
78+
func (r *argoRuntime) Create(ctx context.Context, runtimeName, cluster, runtimeVersion string) (*model.RuntimeCreationResponse, error) {
8879
jsonData := map[string]interface{}{
89-
"query": `{
90-
runtimes {
91-
edges {
92-
node {
93-
metadata {
94-
name
95-
namespace
80+
"query": `
81+
mutation CreateRuntime(
82+
$name: String!
83+
$cluster: String!
84+
$runtimeVersion: String!
85+
) {
86+
runtime(name: $name, cluster: $cluster, runtimeVersion: $runtimeVersion) {
87+
name
88+
newAccessToken
9689
}
97-
self {
98-
healthStatus
99-
version
100-
}
101-
cluster
102-
}
10390
}
104-
}
105-
}
106-
`,
107-
}
108-
109-
response, err := r.codefresh.requestAPI(&requestOptions{
110-
method: "POST",
111-
path: qlEndPoint,
112-
body: jsonData,
113-
})
114-
if err != nil {
115-
fmt.Printf("The HTTP request failed with error %s\n", err)
116-
return nil, err
117-
}
118-
defer response.Body.Close()
119-
120-
data, err := ioutil.ReadAll(response.Body)
121-
if err != nil {
122-
fmt.Printf("failed to read from response body")
123-
return nil, err
91+
`,
92+
"variables": map[string]interface{}{
93+
"name": runtimeName,
94+
"cluster": cluster,
95+
"runtimeVersion": runtimeVersion,
96+
},
12497
}
12598

126-
res := graphqlRuntimesResponse{}
127-
err = json.Unmarshal(data, &res)
128-
99+
res := &graphQlRuntimeCreationResponse{}
100+
err := r.codefresh.graphqlAPI(ctx, jsonData, res)
129101
if err != nil {
130-
return nil, err
102+
return nil, fmt.Errorf("failed getting runtime list: %w", err)
131103
}
132104

133105
if len(res.Errors) > 0 {
134106
return nil, graphqlErrorResponse{errors: res.Errors}
135107
}
136108

137-
runtimes := make([]model.Runtime, len(res.Data.Runtimes.Edges))
138-
for i := range res.Data.Runtimes.Edges {
139-
runtimes[i] = *res.Data.Runtimes.Edges[i].Node
140-
}
141-
142-
return runtimes, nil
109+
return &res.Data.Runtime, nil
143110
}

pkg/codefresh/codefresh.go

+40-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"io/ioutil"
910
"net/http"
@@ -28,8 +29,13 @@ type (
2829
Users() UsersAPI
2930
Argo() ArgoAPI
3031
Gitops() GitopsAPI
31-
ArgoRuntime() IArgoRuntimeAPI
32+
V2() V2API
33+
}
34+
35+
V2API interface {
36+
Runtime() IRuntimeAPI
3237
GitSource() IGitSourceAPI
38+
Component() IComponentAPI
3339
}
3440
)
3541

@@ -86,14 +92,22 @@ func (c *codefresh) Gitops() GitopsAPI {
8692
return newGitopsAPI(c)
8793
}
8894

89-
func (c *codefresh) ArgoRuntime() IArgoRuntimeAPI {
95+
func (c *codefresh) V2() V2API {
96+
return c
97+
}
98+
99+
func (c *codefresh) Runtime() IRuntimeAPI {
90100
return newArgoRuntimeAPI(c)
91101
}
92102

93103
func (c *codefresh) GitSource() IGitSourceAPI {
94104
return newGitSourceAPI(c)
95105
}
96106

107+
func (c *codefresh) Component() IComponentAPI {
108+
return newComponentAPI(c)
109+
}
110+
97111
func (c *codefresh) requestAPI(opt *requestOptions) (*http.Response, error) {
98112
return c.requestAPIWithContext(context.Background(), opt)
99113
}
@@ -122,6 +136,30 @@ func (c *codefresh) requestAPIWithContext(ctx context.Context, opt *requestOptio
122136
return response, nil
123137
}
124138

139+
func (c *codefresh) graphqlAPI(ctx context.Context, body map[string]interface{}, res interface{}) error {
140+
response, err := c.requestAPIWithContext(ctx, &requestOptions{
141+
method: "POST",
142+
path: "/2.0/api/graphql",
143+
body: body,
144+
})
145+
if err != nil {
146+
return fmt.Errorf("The HTTP request failed: %w", err)
147+
}
148+
defer response.Body.Close()
149+
150+
statusOK := response.StatusCode >= 200 && response.StatusCode < 300
151+
if !statusOK {
152+
return errors.New(response.Status)
153+
}
154+
155+
data, err := ioutil.ReadAll(response.Body)
156+
if err != nil {
157+
return fmt.Errorf("failed to read from response body: %w", err)
158+
}
159+
160+
return json.Unmarshal(data, res)
161+
}
162+
125163
func buildQSFromMap(qs map[string]string) string {
126164
var arr = []string{}
127165
for k, v := range qs {

pkg/codefresh/component.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
version
42+
self {
43+
status {
44+
syncStatus
45+
healthStatus
46+
}
47+
}
48+
}
49+
}
50+
}
51+
}`,
52+
"variables": map[string]interface{}{
53+
"runtime": runtimeName,
54+
},
55+
}
56+
57+
res := &graphqlComponentsResponse{}
58+
err := r.codefresh.graphqlAPI(ctx, jsonData, res)
59+
if err != nil {
60+
return nil, fmt.Errorf("failed getting components list: %w", err)
61+
}
62+
63+
if len(res.Errors) > 0 {
64+
return nil, graphqlErrorResponse{errors: res.Errors}
65+
}
66+
67+
components := make([]model.Component, len(res.Data.Components.Edges))
68+
for i := range res.Data.Components.Edges {
69+
components[i] = *res.Data.Components.Edges[i].Node
70+
}
71+
72+
return components, nil
73+
}

0 commit comments

Comments
 (0)