#+TITLE: LBaaS Testing #+AUTHOR: Adam Mohammed #+DATE: August 30, 2023 * API Testing :PROPERTIES: :header-args:shell: :session *bash3* :header-args: :results output verbatim :END: #+begin_src shell PS1="> " export PAPI_KEY="my-user-api-key" export PROJECT_ID=7c0d4b1d-4f21-4657-96d4-afe6236e361e #+end_src First let's exchange our user's API key for an infratographer JWT. #+begin_src shell export INFRA_TOK=$(curl -s -X POST -H"authorization: Bearer $PAPI_KEY" https://iam.metalctrl.io/api-keys/exchange | jq -M -r '.access_token' ) #+end_src #+RESULTS: If all went well, you should see a json object containing the =loadbalancers= key from this block. #+begin_src shell curl -s -H"Authorization: Bearer $INFRA_TOK" https://lb.metalctrl.io/v1/projects/${PROJECT_ID}/loadbalancers | jq -M #+end_src #+RESULTS: #+begin_example { "loadbalancers": [ { "created_at": "2023-08-30T18:26:19.534351Z", "id": "loadbal-9OhCaBNHUXo_f-gC7YKzW", "ips": [], "name": "test-graphql", "ports": [ { "id": "loadprt-8fN2XRnwY8C0SGs_T-zhp", "name": "public-http", "number": 8080 } ], "updated_at": "2023-08-30T18:26:19.534351Z" }, { "created_at": "2023-08-30T19:55:42.944273Z", "id": "loadbal-pLdVJLcAa3UdbPEmGWwvB", "ips": [], "name": "test-graphql", "ports": [ { "id": "loadprt-N8xRozMbxZwtG2yAPk7Wx", "name": "public-http", "number": 8080 } ], "updated_at": "2023-08-30T19:55:42.944273Z" } ] } #+end_example ** Creating a LB Here we'll create an empty LB with our newly exchanged API key. #+begin_src shell curl -s \ -H"Authorization: Bearer $INFRA_TOK" \ -H"content-type: application/json" \ -d '{"name": "test-graphql", "location_id": "metlloc-da", "provider_id":"loadpvd-gOB_-byp5ebFo7A3LHv2B"}' \ https://lb.metalctrl.io/v1/projects/${PROJECT_ID}/loadbalancers | jq -M #+end_src #+RESULTS: : : > > > { : "errors": null, : "id": "loadbal-ygZi9cUywLk5_oAoLGMxh" : } All we have is an ID now, but eventually we should get an IP back. #+begin_src shell RES=$(curl -s \ -H"Authorization: Bearer $INFRA_TOK" \ https://lb.metalctrl.io/v1/projects/${PROJECT_ID}/loadbalancers | tee ) export LOADBALANCER_ID=$(echo $RES | jq -r '.loadbalancers | sort_by(.created_at) | reverse | .[0].id' ) echo $LOADBALANCER_ID #+end_src #+RESULTS: : : > > > loadbal-ygZi9cUywLk5_oAoLGMxh ** Create the backends The load balancer requires a pool with an associated origin. #+begin_src shell export POOL_ID=$(curl -s -H"Authorization: Bearer $INFRA_TOK" \ -H"content-type: application/json" \ -d '{"name": "pool9", "protocol": "tcp"}' \ https://lb.metalctrl.io/v1/projects/${PROJECT_ID}/loadbalancers/pools | jq -r '.id') echo $POOL_ID #+end_src #+RESULTS: : : > > > loadpol-hC_UY3Woqjfyfw1Tzr5R2 Let's create a LB that points to =icanhazip.com= so we can see how we're proxying #+begin_src shell export TARGET_IP=$(dig +short icanhazip.com | head -1) data=$(jq -M -c -n --arg port_id $POOL_ID --arg target_ip "$TARGET_IP" '{"name": "icanhazip9", "target": $target_ip, "port_id": $port_id, "port_number": 80, "active": true}' | tee ) curl -s \ -H"Authorization: Bearer $INFRA_TOK" \ -H"content-type: application/json" \ -d "$data" \ https://lb.metalctrl.io/v1/loadbalancers/pools/${POOL_ID}/origins | jq -M #+end_src #+RESULTS: : : > > > > > { : "errors": null, : "id": "loadogn-zfbMfqtFKeQ75Tul52h4Q" : } #+begin_src shell curl -s \ -H"Authorization: Bearer $INFRA_TOK" \ -H"content-type: application/json" \ -d "$(jq -n -M -c -n --arg pool_id $POOL_ID '{"name": "public-http", "number": 8080, "pool_ids": [$pool_id]}')" \ https://lb.metalctrl.io/v1/loadbalancers/${LOADBALANCER_ID}/ports | jq -M #+end_src #+RESULTS: : : > > > { : "errors": null, : "id": "loadprt-IVrZB1sLUfKqdnDULd6Ix" : } ** Let's try out the LB now #+begin_src shell curl -s \ -H"Authorization: Bearer $INFRA_TOK" \ -H"content-type: application/json" \ https://lb.metalctrl.io/v1/loadbalancers/${LOADBALANCER_ID} | jq -M #+end_src #+RESULTS: #+begin_example > > { "created_at": "2023-08-30T20:10:59.389392Z", "id": "loadbal-ygZi9cUywLk5_oAoLGMxh", "ips": [], "name": "test-graphql", "ports": [ { "id": "loadprt-IVrZB1sLUfKqdnDULd6Ix", "name": "public-http", "number": 8080 } ], "provider": null, "updated_at": "2023-08-30T20:10:59.389392Z" } #+end_example