|
| 1 | +defmodule Core.Clients.Console do |
| 2 | + require Logger |
| 3 | + |
| 4 | + @clusters_q """ |
| 5 | + query { |
| 6 | + clusters(first: 100) { |
| 7 | + edges { node { name id distro metadata } } |
| 8 | + } |
| 9 | + } |
| 10 | + """ |
| 11 | + |
| 12 | + @create_svc_q """ |
| 13 | + mutation Create($clusterId: ID!, $attributes: ServiceDeploymentAttributes!) { |
| 14 | + createServiceDeployment(clusterId: $clusterId, attributes: $attributes) { |
| 15 | + id |
| 16 | + } |
| 17 | + } |
| 18 | + """ |
| 19 | + |
| 20 | + @delete_svc_q """ |
| 21 | + mutation Delete($id: ID!) { |
| 22 | + deleteServiceDeployment(id: $id) { |
| 23 | + id |
| 24 | + } |
| 25 | + } |
| 26 | + """ |
| 27 | + |
| 28 | + @update_svc_q """ |
| 29 | + mutation Update($id: ID!, $attributes: ServiceUpdateAttributes!) { |
| 30 | + updateServiceDeployment(id: $id) { |
| 31 | + id |
| 32 | + } |
| 33 | + } |
| 34 | + """ |
| 35 | + |
| 36 | + @repo_q """ |
| 37 | + query Repo($url: String!) { |
| 38 | + gitRepository(url: $url) { |
| 39 | + id |
| 40 | + } |
| 41 | + } |
| 42 | + """ |
| 43 | + |
| 44 | + def new(url, token) do |
| 45 | + Req.new(base_url: url, auth: "Token #{token}") |
| 46 | + |> AbsintheClient.attach() |
| 47 | + end |
| 48 | + |
| 49 | + def clusters(client) do |
| 50 | + Req.post(client, graphql: @clusters_q) |
| 51 | + |> case do |
| 52 | + {:ok, %Req.Response{body: %{"clusters" => %{"edges" => edges}}}} -> {:ok, Enum.map(edges, & &1["node"])} |
| 53 | + res -> |
| 54 | + Logger.warn "Failed to fetch clusters: #{inspect(res)}" |
| 55 | + {:error, "could not fetch clusters"} |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + def repo(client, url) do |
| 60 | + Req.post(client, graphql: {@repo_q, %{url: url}}) |
| 61 | + |> case do |
| 62 | + {:ok, %Req.Response{body: %{"gitRepository" => %{"id" => id}}}} -> {:ok, id} |
| 63 | + res -> |
| 64 | + Logger.warn "Failed to fetch clusters: #{inspect(res)}" |
| 65 | + {:error, "could not fetch repo"} |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + def create_service(client, cluster_id, attrs) do |
| 70 | + Req.post(client, graphql: {@create_svc_q, %{clusterId: cluster_id, attributes: attrs}}) |
| 71 | + |> service_resp("createServiceDeployment") |
| 72 | + end |
| 73 | + |
| 74 | + def update_service(client, id, attrs) do |
| 75 | + Req.post(client, graphql: {@update_svc_q, %{id: id, attributes: attrs}}) |
| 76 | + |> service_resp("updateServiceDeployment") |
| 77 | + end |
| 78 | + |
| 79 | + def delete_service(client, id) do |
| 80 | + Req.post(client, graphql: {@delete_svc_q, %{id: id}}) |
| 81 | + |> service_resp("deleteServiceDeployment") |
| 82 | + end |
| 83 | + |
| 84 | + defp service_resp({:ok, %Req.Response{status: 200, body: body}}, field) do |
| 85 | + case body[field] do |
| 86 | + %{"id" => id} -> {:ok, id} |
| 87 | + err -> |
| 88 | + Logger.warn "invalid console gql response: #{inspect(err)}" |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + defp service_resp(resp, _) do |
| 93 | + Logger.error "failed to fetch from console: #{inspect(resp)}" |
| 94 | + {:error, "console error"} |
| 95 | + end |
| 96 | +end |
0 commit comments