Skip to content

Commit 5a78644

Browse files
interpolated mutation
1 parent a3a62a2 commit 5a78644

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

pkg/codefresh/argo_runtime.go

Lines changed: 52 additions & 4 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,65 @@ type (
2122
}
2223
Errors []graphqlError
2324
}
25+
26+
graphQlRuntimeCreationResponse struct {
27+
Data struct {
28+
Runtime model.RuntimeCreationResponse
29+
}
30+
// TODO: Errors
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) { // TODO: should also return error
39+
type forJsonData interface{}
40+
41+
// the newlines are necessary
42+
var interpolatedMutation forJsonData = fmt.Sprintf("mutation {\n runtime(name: \"%s\") {\n id\n newAccessToken\n }\n}\n", runtimeName)
43+
44+
jsonData := map[string]interface{}{
45+
"query": interpolatedMutation,
46+
}
47+
48+
response, err := r.codefresh.requestAPI(&requestOptions{
49+
method: "POST",
50+
path: "/argo/api/graphql",
51+
body: jsonData,
52+
})
53+
54+
if err != nil {
55+
fmt.Printf("The HTTP request failed with error %s\n", err)
56+
return nil, err
57+
}
58+
defer response.Body.Close()
59+
60+
data, err := ioutil.ReadAll(response.Body)
61+
if err != nil {
62+
fmt.Printf("failed to read from response body")
63+
return nil, err
64+
}
65+
res := graphQlRuntimeCreationResponse{}
66+
67+
err = json.Unmarshal(data, &res)
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
return &res.Data.Runtime, nil
73+
}
74+
75+
func (r *argoRuntime) List() ([]model.Runtime, error) {
3176
jsonData := map[string]interface{}{
3277
"query": `
3378
{
34-
runtimes(
79+
runtimes
80+
(
3581
pagination: {}
36-
project: "") {
82+
project: ""
83+
) {
3784
edges {
3885
node {
3986
metadata {
@@ -55,11 +102,12 @@ func (r *argoRuntime) List() ([]model.Runtime, error) {
55102
path: "/argo/api/graphql",
56103
body: jsonData,
57104
})
58-
defer response.Body.Close()
59105
if err != nil {
60106
fmt.Printf("The HTTP request failed with error %s\n", err)
61107
return nil, err
62108
}
109+
defer response.Body.Close()
110+
63111
data, err := ioutil.ReadAll(response.Body)
64112
if err != nil {
65113
fmt.Printf("failed to read from response body")

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)