|
| 1 | +package codefresh |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + |
| 8 | + "github.com/codefresh-io/argo-platform/libs/ql/graph/model" |
| 9 | +) |
| 10 | + |
| 11 | +type ( |
| 12 | + IArgoRuntimeAPI interface { |
| 13 | + List() ([]model.Runtime, error) |
| 14 | + } |
| 15 | + argoRuntime struct { |
| 16 | + codefresh *codefresh |
| 17 | + } |
| 18 | + graphqlRuntimesResponse struct { |
| 19 | + Data struct { |
| 20 | + Runtimes model.RuntimePage |
| 21 | + } |
| 22 | + Errors []graphqlError |
| 23 | + } |
| 24 | +) |
| 25 | + |
| 26 | +func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI { |
| 27 | + return &argoRuntime{codefresh: codefresh} |
| 28 | +} |
| 29 | +func (r *argoRuntime) List() ([]model.Runtime, error) { |
| 30 | + |
| 31 | + jsonData := map[string]interface{}{ |
| 32 | + "query": ` |
| 33 | + { |
| 34 | + runtimes{ |
| 35 | + edges{ |
| 36 | + node { |
| 37 | + id |
| 38 | + namespace |
| 39 | + objectMeta { |
| 40 | + name |
| 41 | + description |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + `, |
| 48 | + } |
| 49 | + |
| 50 | + response, err := r.codefresh.requestAPI(&requestOptions{ |
| 51 | + method: "POST", |
| 52 | + path: "/argo/api/graphql", |
| 53 | + body: jsonData, |
| 54 | + }) |
| 55 | + defer response.Body.Close() |
| 56 | + if err != nil { |
| 57 | + fmt.Printf("The HTTP request failed with error %s\n", err) |
| 58 | + return nil, err |
| 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 := graphqlRuntimesResponse{} |
| 66 | + err = json.Unmarshal(data, &res) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 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 | + if len(res.Errors) > 0 { |
| 76 | + return nil, graphqlErrorResponse{errors: res.Errors} |
| 77 | + } |
| 78 | + |
| 79 | + return runtimes, nil |
| 80 | + |
| 81 | +} |
0 commit comments