11package codefresh
22
33import (
4- "encoding/json "
4+ "context "
55 "fmt"
6- "io/ioutil"
76
87 "github.com/codefresh-io/go-sdk/pkg/codefresh/model"
98)
109
1110type (
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}
0 commit comments