1
1
package codefresh
2
2
3
3
import (
4
- "encoding/json "
4
+ "context "
5
5
"fmt"
6
- "io/ioutil"
7
6
8
7
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
9
8
)
10
9
11
10
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 )
15
14
}
15
+
16
16
argoRuntime struct {
17
17
codefresh * codefresh
18
18
}
19
+
19
20
graphqlRuntimesResponse struct {
20
21
Data struct {
21
22
Runtimes model.RuntimePage
@@ -31,113 +32,79 @@ type (
31
32
}
32
33
)
33
34
34
- var qlEndPoint = "/2.0/api/graphql"
35
-
36
- func newArgoRuntimeAPI (codefresh * codefresh ) IArgoRuntimeAPI {
35
+ func newArgoRuntimeAPI (codefresh * codefresh ) IRuntimeAPI {
37
36
return & argoRuntime {codefresh : codefresh }
38
37
}
39
38
40
- func (r * argoRuntime ) Create ( runtimeName , cluster , runtimeVersion string ) (* model.RuntimeCreationResponse , error ) {
39
+ func (r * argoRuntime ) List ( ctx context. Context ) ([] model.Runtime , error ) {
41
40
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
+ }` ,
72
58
}
73
59
74
- res := graphQlRuntimeCreationResponse {}
75
- err = json . Unmarshal ( data , & res )
60
+ res := & graphqlRuntimesResponse {}
61
+ err := r . codefresh . graphqlAPI ( ctx , jsonData , res )
76
62
if err != nil {
77
- return nil , err
63
+ return nil , fmt . Errorf ( "failed getting runtime list: %w" , err )
78
64
}
79
65
80
66
if len (res .Errors ) > 0 {
81
67
return nil , graphqlErrorResponse {errors : res .Errors }
82
68
}
83
69
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
85
76
}
86
77
87
- func (r * argoRuntime ) List ( ) ([] model.Runtime , error ) {
78
+ func (r * argoRuntime ) Create ( ctx context. Context , runtimeName , cluster , runtimeVersion string ) (* model.RuntimeCreationResponse , error ) {
88
79
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
96
89
}
97
- self {
98
- healthStatus
99
- version
100
- }
101
- cluster
102
- }
103
90
}
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
+ },
124
97
}
125
98
126
- res := graphqlRuntimesResponse {}
127
- err = json .Unmarshal (data , & res )
128
-
99
+ res := & graphQlRuntimeCreationResponse {}
100
+ err := r .codefresh .graphqlAPI (ctx , jsonData , res )
129
101
if err != nil {
130
- return nil , err
102
+ return nil , fmt . Errorf ( "failed getting runtime list: %w" , err )
131
103
}
132
104
133
105
if len (res .Errors ) > 0 {
134
106
return nil , graphqlErrorResponse {errors : res .Errors }
135
107
}
136
108
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
143
110
}
0 commit comments