Skip to content

Commit

Permalink
Merge pull request #34 from codefresh-io/CR-4369
Browse files Browse the repository at this point in the history
CR-4369 - components list
  • Loading branch information
ATGardner authored Aug 17, 2021
2 parents 39e472b + de93f9d commit 56601f2
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 136 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.31.2
0.32.0
141 changes: 54 additions & 87 deletions pkg/codefresh/argo_runtime.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package codefresh

import (
"encoding/json"
"context"
"fmt"
"io/ioutil"

"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
)

type (
IArgoRuntimeAPI interface {
List() ([]model.Runtime, error)
Create(runtimeName, cluster, runtimeVersion string) (*model.RuntimeCreationResponse, error)
IRuntimeAPI interface {
List(ctx context.Context) ([]model.Runtime, error)
Create(ctx context.Context, runtimeName, cluster, runtimeVersion string) (*model.RuntimeCreationResponse, error)
}

argoRuntime struct {
codefresh *codefresh
}

graphqlRuntimesResponse struct {
Data struct {
Runtimes model.RuntimePage
Expand All @@ -31,113 +32,79 @@ type (
}
)

var qlEndPoint = "/2.0/api/graphql"

func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI {
func newArgoRuntimeAPI(codefresh *codefresh) IRuntimeAPI {
return &argoRuntime{codefresh: codefresh}
}

func (r *argoRuntime) Create(runtimeName, cluster, runtimeVersion string) (*model.RuntimeCreationResponse, error) {
func (r *argoRuntime) List(ctx context.Context) ([]model.Runtime, error) {
jsonData := map[string]interface{}{
"query": `mutation CreateRuntime($name: String!, $cluster: String!, $runtimeVersion: String!) {
runtime(name: $name, cluster: $cluster, runtimeVersion: $runtimeVersion) {
name
newAccessToken
}
}`,
"variables": map[string]interface{}{
"name": runtimeName,
"cluster": cluster,
"runtimeVersion": runtimeVersion,
},
}

response, err := r.codefresh.requestAPI(&requestOptions{
method: "POST",
path: qlEndPoint,
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
"query": `{
runtimes {
edges {
node {
metadata {
name
namespace
}
self {
healthStatus
version
}
cluster
}
}
}
}`,
}

res := graphQlRuntimeCreationResponse{}
err = json.Unmarshal(data, &res)
res := &graphqlRuntimesResponse{}
err := r.codefresh.graphqlAPI(ctx, jsonData, res)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed getting runtime list: %w", err)
}

if len(res.Errors) > 0 {
return nil, graphqlErrorResponse{errors: res.Errors}
}

return &res.Data.Runtime, nil
runtimes := make([]model.Runtime, len(res.Data.Runtimes.Edges))
for i := range res.Data.Runtimes.Edges {
runtimes[i] = *res.Data.Runtimes.Edges[i].Node
}

return runtimes, nil
}

func (r *argoRuntime) List() ([]model.Runtime, error) {
func (r *argoRuntime) Create(ctx context.Context, runtimeName, cluster, runtimeVersion string) (*model.RuntimeCreationResponse, error) {
jsonData := map[string]interface{}{
"query": `{
runtimes {
edges {
node {
metadata {
name
namespace
"query": `
mutation CreateRuntime(
$name: String!
$cluster: String!
$runtimeVersion: String!
) {
runtime(name: $name, cluster: $cluster, runtimeVersion: $runtimeVersion) {
name
newAccessToken
}
self {
healthStatus
version
}
cluster
}
}
}
}
`,
}

response, err := r.codefresh.requestAPI(&requestOptions{
method: "POST",
path: qlEndPoint,
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
`,
"variables": map[string]interface{}{
"name": runtimeName,
"cluster": cluster,
"runtimeVersion": runtimeVersion,
},
}

res := graphqlRuntimesResponse{}
err = json.Unmarshal(data, &res)

res := &graphQlRuntimeCreationResponse{}
err := r.codefresh.graphqlAPI(ctx, jsonData, res)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed getting runtime list: %w", err)
}

if len(res.Errors) > 0 {
return nil, graphqlErrorResponse{errors: res.Errors}
}

runtimes := make([]model.Runtime, len(res.Data.Runtimes.Edges))
for i := range res.Data.Runtimes.Edges {
runtimes[i] = *res.Data.Runtimes.Edges[i].Node
}

return runtimes, nil
return &res.Data.Runtime, nil
}
42 changes: 40 additions & 2 deletions pkg/codefresh/codefresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -28,8 +29,13 @@ type (
Users() UsersAPI
Argo() ArgoAPI
Gitops() GitopsAPI
ArgoRuntime() IArgoRuntimeAPI
V2() V2API
}

V2API interface {
Runtime() IRuntimeAPI
GitSource() IGitSourceAPI
Component() IComponentAPI
}
)

Expand Down Expand Up @@ -86,14 +92,22 @@ func (c *codefresh) Gitops() GitopsAPI {
return newGitopsAPI(c)
}

func (c *codefresh) ArgoRuntime() IArgoRuntimeAPI {
func (c *codefresh) V2() V2API {
return c
}

func (c *codefresh) Runtime() IRuntimeAPI {
return newArgoRuntimeAPI(c)
}

func (c *codefresh) GitSource() IGitSourceAPI {
return newGitSourceAPI(c)
}

func (c *codefresh) Component() IComponentAPI {
return newComponentAPI(c)
}

func (c *codefresh) requestAPI(opt *requestOptions) (*http.Response, error) {
return c.requestAPIWithContext(context.Background(), opt)
}
Expand Down Expand Up @@ -122,6 +136,30 @@ func (c *codefresh) requestAPIWithContext(ctx context.Context, opt *requestOptio
return response, nil
}

func (c *codefresh) graphqlAPI(ctx context.Context, body map[string]interface{}, res interface{}) error {
response, err := c.requestAPIWithContext(ctx, &requestOptions{
method: "POST",
path: "/2.0/api/graphql",
body: body,
})
if err != nil {
return fmt.Errorf("The HTTP request failed: %w", err)
}
defer response.Body.Close()

statusOK := response.StatusCode >= 200 && response.StatusCode < 300
if !statusOK {
return errors.New(response.Status)
}

data, err := ioutil.ReadAll(response.Body)
if err != nil {
return fmt.Errorf("failed to read from response body: %w", err)
}

return json.Unmarshal(data, res)
}

func buildQSFromMap(qs map[string]string) string {
var arr = []string{}
for k, v := range qs {
Expand Down
73 changes: 73 additions & 0 deletions pkg/codefresh/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package codefresh

import (
"context"
"fmt"

"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
)

type (
IComponentAPI interface {
List(ctx context.Context, runtimeName string) ([]model.Component, error)
}

component struct {
codefresh *codefresh
}

graphqlComponentsResponse struct {
Data struct {
Components model.ComponentPage
}
Errors []graphqlError
}
)

func newComponentAPI(codefresh *codefresh) IComponentAPI {
return &component{codefresh: codefresh}
}

func (r *component) List(ctx context.Context, runtimeName string) ([]model.Component, error) {
jsonData := map[string]interface{}{
"query": `
query Components($runtime: String!) {
components(runtime: $runtime) {
edges {
node {
metadata {
name
}
version
self {
status {
syncStatus
healthStatus
}
}
}
}
}
}`,
"variables": map[string]interface{}{
"runtime": runtimeName,
},
}

res := &graphqlComponentsResponse{}
err := r.codefresh.graphqlAPI(ctx, jsonData, res)
if err != nil {
return nil, fmt.Errorf("failed getting components list: %w", err)
}

if len(res.Errors) > 0 {
return nil, graphqlErrorResponse{errors: res.Errors}
}

components := make([]model.Component, len(res.Data.Components.Edges))
for i := range res.Data.Components.Edges {
components[i] = *res.Data.Components.Edges[i].Node
}

return components, nil
}
Loading

0 comments on commit 56601f2

Please sign in to comment.