Visualizing Apps Using Giant Swarm’s APIs

On By Kord Campbell in tech

Giant Swarm’s core values consist of transparency, enabling others to do their best work and providing reliable infrastructure. Our vision is to build a trusted collaborative compute model which provides resources when and where they are needed for helping you run your microservices in a scalable, reliable and secure way.

The key tenants of collaborative coding are documentation and discussion, so we are pleased as punch to announce the launch of Giant Swarm’s API Reference and our Google Group to enable discussions around building and running applications. The API reference is Open Source and may be checked out from the docs-content repo on Github. Pull requests and issues are welcomed!

Video APIs

The average page visit lasts a little less than a minute, so I figured it would be nice to have a video overview of our API where I demonstrate grabbing an auth token and starting an application:

The Basics

Let’s do a couple of simple queries to the APIs to get a list of applications. We start by grabbing our authentication token:

$ curl -sS \
-H "Content-Type: application/json" \
-X POST \
--data '{"password":"'"$(echo -n f00bar | base64)"'"}' \
https://api.giantswarm.io/v1/user/terminal/login \
| python -mjson.tool

{
    "data": {
        "Created": "2015-05-21 18:23:59.469752274 +0000 UTC",
        "Id": "8b8bfa15-539b-4f4e-9e91-0b3e108b52bc",
        "UserId": "40e6afca-53f7-4736-8a4a-4b696279f1c6"
    },
    "status_code": 10000,
    "status_text": "success"
}

The 8b8bfa15-539b-4f4e-9e91-0b3e108b52bc string under the Id key name is the token we will use to authenticate ourselves in a second.

You may cut and paste the example above exactly as you see it and then do the following to substitute a valid <username> and <password> for your account:

$ !!:gs/f00bar/<password>
$ !!:gs/terminal/<username>

Now we have a token, let’s use it to get a list of applications in the terminal/dev environment:

$ curl -sS \
    -X GET \
    -H "Authorization: giantswarm 8b8bfa15-539b-4f4e-9e91-0b3e108b52bc" \
    https://api.giantswarm.io/v1/org/bantic/env/dev/app/ \
    | python -mjson.tool

{
    "data": [
        {
            "app": "helloworld",
            "company": "startup",
            "created": "2015-05-17 20:15:04",
            "env": "dev",
            "org": "startup"
        }
    ],
    "status_code": 10000,
    "status_text": "success"
}

Again, you may substitute the <token> and <org> in the curl request above:

!!:gs/8b8bfa15-539b-4f4e-9e91-0b3e108b52bc/<token>
!!:gs/bantic/<org>

Now we’ve seen how to authenticate and call the API, let’s take a look at a real application, written in Python.

Visualize Your Apps with the APIs

I’m a big fan of using visual perception, so I thought it would be useful to put together a sample application which used a few of our API calls to fetch the usage of applications running in your Giant Swarm account and visualize them with Mike Bostock’s Cubism library. The API methods and the code the app uses are:

How about a screenshot?

All the code for running this yourself is available in the swarm-cubism application formation on Github. Assuming you have the swarm client and boot2docker installed, deploy the app by doing the following:

swarm login
swarm env

Be sure the output of swarm env gives you a valid organization and environment for your account, and has at least one running application. Once you’ve done that, do the following to deploy the application:

git clone https://github.com/giantswarm/swarm-cubism.git
cd swarm-cubism
make up

The last line of the output should say something like this:

Your app 'cubism' is running at http://cubism-kord-dev.gigantic.io/"

Note the inclusion of my orginization (kord) and environment (dev) in the URL. You can view a demo of my app stats here.

Secure Your Code

Given I have tokens in this post and I love being secure, my last example shows how to delete the tokens from the Giant Swarm service:

$ curl -sS \
    -X POST \
    -H "Authorization: giantswarm 8b8bfa15-539b-4f4e-9e91-0b3e108b52bc" \
    -H "Content-Type: application/json" \
    https://api.giantswarm.io/v1/token/logout \
    | python -mjson.tool

{
  "status_code": 10007,
  "status_text": "resource deleted"
}

Be sure and ping me on Twitter if you do something cool with our APIs: I’m happy to tweet it out to our users!

Picture of Kord Campbell
Kord Campbell
Cowboy coder in the business of digital evangelism, cloud computing, cryptocurrencies, transparency & the coming age of trust. @utterio.

Let’s start your journey towards microservices

Contact us Now