@@ -11,6 +11,7 @@ import (
11
11
type (
12
12
IArgoRuntimeAPI interface {
13
13
List () ([]model.Runtime , error )
14
+ Create (runtimeName string ) (* model.RuntimeCreationResponse , error )
14
15
}
15
16
argoRuntime struct {
16
17
codefresh * codefresh
@@ -21,19 +22,73 @@ type (
21
22
}
22
23
Errors []graphqlError
23
24
}
25
+
26
+ graphQlRuntimeCreationResponse struct {
27
+ Data struct {
28
+ Runtime model.RuntimeCreationResponse
29
+ }
30
+ Errors []graphqlError
31
+ }
24
32
)
25
33
26
34
func newArgoRuntimeAPI (codefresh * codefresh ) IArgoRuntimeAPI {
27
35
return & argoRuntime {codefresh : codefresh }
28
36
}
29
- func (r * argoRuntime ) List () ([]model.Runtime , error ) {
30
37
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 ) {
31
84
jsonData := map [string ]interface {}{
32
85
"query" : `
33
86
{
34
- runtimes(
87
+ runtimes
88
+ (
35
89
pagination: {}
36
- project: "") {
90
+ project: ""
91
+ ) {
37
92
edges {
38
93
node {
39
94
metadata {
@@ -55,21 +110,25 @@ func (r *argoRuntime) List() ([]model.Runtime, error) {
55
110
path : "/argo/api/graphql" ,
56
111
body : jsonData ,
57
112
})
58
- defer response .Body .Close ()
59
113
if err != nil {
60
114
fmt .Printf ("The HTTP request failed with error %s\n " , err )
61
115
return nil , err
62
116
}
117
+ defer response .Body .Close ()
118
+
63
119
data , err := ioutil .ReadAll (response .Body )
64
120
if err != nil {
65
121
fmt .Printf ("failed to read from response body" )
66
122
return nil , err
67
123
}
124
+
68
125
res := graphqlRuntimesResponse {}
69
126
err = json .Unmarshal (data , & res )
127
+
70
128
if err != nil {
71
129
return nil , err
72
130
}
131
+
73
132
runtimes := make ([]model.Runtime , len (res .Data .Runtimes .Edges ))
74
133
for i := range res .Data .Runtimes .Edges {
75
134
runtimes [i ] = * res .Data .Runtimes .Edges [i ].Node
@@ -80,5 +139,4 @@ func (r *argoRuntime) List() ([]model.Runtime, error) {
80
139
}
81
140
82
141
return runtimes , nil
83
-
84
142
}
0 commit comments