When developing with OpenWhisk, it’s useful to use separate environments for working locally or on the cloud for development, staging and production of your application. In OpenWhisk terms, this means setting the host and the API key for your wsk command line application.

$ wsk property set --apihost $host --auth $key

For a Vagrant install of OpenWhisk, the host is 192.168.33.13 and the key can be found inside the VM. On Bluemix, the host is always this is most easily done using separate “spaces” as each space has its own key.

To avoid having to keep looking up the correct keys, I wrote a simple Bash function in my .bash_profile file:

function owenv() {
    switches=""
    space=$1
    case "$space" in
        "dev")
            host=openwhisk.ng.bluemix.net
            key="aaa-bbb-ccc-ddd-eee:fff"
            ;;
        "uat")
            host=openwhisk.ng.bluemix.net
            key="bbb-ccc-ddd-eee-fff:ggg"
            ;;
        "live")
            host=openwhisk.ng.bluemix.net
            key="ccc-ddd-eee-fff-ggg:hhh"
            ;;
        "local")
            host=http://192.168.33.13:10001
            key=`cat ~/Projects/openwhisk/openwhisk/ansible/files/auth.guest`
            ;;
        "localssl")
            switches="-i" # insecure switch required as cert is self-signed
            host=192.168.33.13
            key=`cat ~/Projects/openwhisk/openwhisk/ansible/files/auth.guest`
            ;;
        *)
            echo "Error. No space set: choose dev, uat, live, local or localssl"
            return
    esac

    wsk $switches property set --apihost $host --auth $key > /dev/null && 
    wsk $switches property unset --namespace > /dev/null

    # display some information
    bold=$(tput bold)
    normal=$(tput sgr0)
    apihost=`wsk $switches property get --apihost | rev | cut -f1 | rev`
    namespace=`wsk $switches namespace list | tail -n1`
    echo "Host       ${bold}${apihost}${normal}"
    echo "Namespace ${bold}${namespace}${normal}"
}

(Actual keys redacted!)

This code uses a case statement to set up the right host and key to use. I’m lazy, so just hardcode my Bluemix keys. There’s probably a better way to that though. For the local Vagrant instance, I get the key directly from the file used by Ansible for provisioning. Again, due to laziness, I’ve hardcoded the Vagrant VM’s IP address.

Lastly I set display the current host and namespace – tput is my new favourite bash command!

Switching environments

I can then use the function like this:

$ owenv local
Host      http://192.168.33.13:10001
Namespace guest

To switch to my Vagrant install of OpenWhisk or:

$ owenv dev
Host      openwhisk.ng.bluemix.net
Namespace 19FT_dev

To swap back to the 19FT_dev environment.

Source: AKRABAT

By Rob