@@ -11,6 +11,7 @@ import (
1111type (
1212 IArgoRuntimeAPI interface {
1313 List () ([]model.Runtime , error )
14+ Create (runtimeName string ) (* model.RuntimeCreationResponse , error )
1415 }
1516 argoRuntime struct {
1617 codefresh * codefresh
@@ -21,19 +22,73 @@ type (
2122 }
2223 Errors []graphqlError
2324 }
25+
26+ graphQlRuntimeCreationResponse struct {
27+ Data struct {
28+ Runtime model.RuntimeCreationResponse
29+ }
30+ Errors []graphqlError
31+ }
2432)
2533
2634func newArgoRuntimeAPI (codefresh * codefresh ) IArgoRuntimeAPI {
2735 return & argoRuntime {codefresh : codefresh }
2836}
29- func (r * argoRuntime ) List () ([]model.Runtime , error ) {
3037
38+ func (r * argoRuntime ) Create (runtimeName string ) (* model.RuntimeCreationResponse , error ) {
39+ interpolatedMutation := fmt .Sprintf (`mutation {
40+ runtime(name: "%s") {
41+ id
42+ newAccessToken
43+ }
44+ }
45+ ` , runtimeName )
46+
47+ jsonData := map [string ]interface {}{
48+ "query" : interpolatedMutation ,
49+ }
50+
51+ response , err := r .codefresh .requestAPI (& requestOptions {
52+ method : "POST" ,
53+ path : "/argo/api/graphql" ,
54+ body : jsonData ,
55+ })
56+
57+ if err != nil {
58+ fmt .Printf ("The HTTP request failed with error %s\n " , err )
59+ return nil , err
60+ }
61+
62+ defer response .Body .Close ()
63+
64+ data , err := ioutil .ReadAll (response .Body )
65+ if err != nil {
66+ fmt .Printf ("failed to read from response body" )
67+ return nil , err
68+ }
69+
70+ res := graphQlRuntimeCreationResponse {}
71+ err = json .Unmarshal (data , & res )
72+ if err != nil {
73+ return nil , err
74+ }
75+
76+ if len (res .Errors ) > 0 {
77+ return nil , graphqlErrorResponse {errors : res .Errors }
78+ }
79+
80+ return & res .Data .Runtime , nil
81+ }
82+
83+ func (r * argoRuntime ) List () ([]model.Runtime , error ) {
3184 jsonData := map [string ]interface {}{
3285 "query" : `
3386 {
34- runtimes(
87+ runtimes
88+ (
3589 pagination: {}
36- project: "") {
90+ project: ""
91+ ) {
3792 edges {
3893 node {
3994 metadata {
@@ -55,21 +110,25 @@ func (r *argoRuntime) List() ([]model.Runtime, error) {
55110 path : "/argo/api/graphql" ,
56111 body : jsonData ,
57112 })
58- defer response .Body .Close ()
59113 if err != nil {
60114 fmt .Printf ("The HTTP request failed with error %s\n " , err )
61115 return nil , err
62116 }
117+ defer response .Body .Close ()
118+
63119 data , err := ioutil .ReadAll (response .Body )
64120 if err != nil {
65121 fmt .Printf ("failed to read from response body" )
66122 return nil , err
67123 }
124+
68125 res := graphqlRuntimesResponse {}
69126 err = json .Unmarshal (data , & res )
127+
70128 if err != nil {
71129 return nil , err
72130 }
131+
73132 runtimes := make ([]model.Runtime , len (res .Data .Runtimes .Edges ))
74133 for i := range res .Data .Runtimes .Edges {
75134 runtimes [i ] = * res .Data .Runtimes .Edges [i ].Node
@@ -80,5 +139,4 @@ func (r *argoRuntime) List() ([]model.Runtime, error) {
80139 }
81140
82141 return runtimes , nil
83-
84142}
0 commit comments