Skip to content

Commit 7d26a50

Browse files
Merge pull request #23 from codefresh-io/CR-4771
CR-4771
2 parents a3a62a2 + 3fc03d0 commit 7d26a50

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.27.1
1+
0.28.0

pkg/codefresh/argo_runtime.go

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
type (
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

2634
func 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
}

pkg/codefresh/codefresh.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func (c *codefresh) requestAPIWithContext(ctx context.Context, opt *requestOptio
108108
}
109109
request.Header.Set("Authorization", c.token)
110110
request.Header.Set("Content-Type", "application/json")
111+
request.Header.Set("origin", c.host)
111112

112113
response, err := c.client.Do(request)
113114
if err != nil {

0 commit comments

Comments
 (0)