diff --git a/VERSION b/VERSION index 83b4730..697f087 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.27.1 +0.28.0 diff --git a/pkg/codefresh/argo_runtime.go b/pkg/codefresh/argo_runtime.go index f3131ab..9b3f4e3 100644 --- a/pkg/codefresh/argo_runtime.go +++ b/pkg/codefresh/argo_runtime.go @@ -11,6 +11,7 @@ import ( type ( IArgoRuntimeAPI interface { List() ([]model.Runtime, error) + Create(runtimeName string) (*model.RuntimeCreationResponse, error) } argoRuntime struct { codefresh *codefresh @@ -21,19 +22,73 @@ type ( } Errors []graphqlError } + + graphQlRuntimeCreationResponse struct { + Data struct { + Runtime model.RuntimeCreationResponse + } + Errors []graphqlError + } ) func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI { return &argoRuntime{codefresh: codefresh} } -func (r *argoRuntime) List() ([]model.Runtime, error) { +func (r *argoRuntime) Create(runtimeName string) (*model.RuntimeCreationResponse, error) { + interpolatedMutation := fmt.Sprintf(`mutation { + runtime(name: "%s") { + id + newAccessToken + } + } + `, runtimeName) + + jsonData := map[string]interface{}{ + "query": interpolatedMutation, + } + + response, err := r.codefresh.requestAPI(&requestOptions{ + method: "POST", + path: "/argo/api/graphql", + body: jsonData, + }) + + if err != nil { + fmt.Printf("The HTTP request failed with error %s\n", err) + return nil, err + } + + defer response.Body.Close() + + data, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Printf("failed to read from response body") + return nil, err + } + + res := graphQlRuntimeCreationResponse{} + err = json.Unmarshal(data, &res) + if err != nil { + return nil, err + } + + if len(res.Errors) > 0 { + return nil, graphqlErrorResponse{errors: res.Errors} + } + + return &res.Data.Runtime, nil +} + +func (r *argoRuntime) List() ([]model.Runtime, error) { jsonData := map[string]interface{}{ "query": ` { - runtimes( + runtimes + ( pagination: {} - project: "") { + project: "" + ) { edges { node { metadata { @@ -55,21 +110,25 @@ func (r *argoRuntime) List() ([]model.Runtime, error) { path: "/argo/api/graphql", body: jsonData, }) - defer response.Body.Close() if err != nil { fmt.Printf("The HTTP request failed with error %s\n", err) return nil, err } + defer response.Body.Close() + data, err := ioutil.ReadAll(response.Body) if err != nil { fmt.Printf("failed to read from response body") return nil, err } + res := graphqlRuntimesResponse{} err = json.Unmarshal(data, &res) + if err != nil { return nil, err } + runtimes := make([]model.Runtime, len(res.Data.Runtimes.Edges)) for i := range res.Data.Runtimes.Edges { runtimes[i] = *res.Data.Runtimes.Edges[i].Node @@ -80,5 +139,4 @@ func (r *argoRuntime) List() ([]model.Runtime, error) { } return runtimes, nil - } diff --git a/pkg/codefresh/codefresh.go b/pkg/codefresh/codefresh.go index 808666f..4d432c3 100644 --- a/pkg/codefresh/codefresh.go +++ b/pkg/codefresh/codefresh.go @@ -108,6 +108,7 @@ func (c *codefresh) requestAPIWithContext(ctx context.Context, opt *requestOptio } request.Header.Set("Authorization", c.token) request.Header.Set("Content-Type", "application/json") + request.Header.Set("origin", c.host) response, err := c.client.Do(request) if err != nil {