Get Adobe Flash player

Cloud Foundry

So this week I started tinkering with the cloud foundry API. In all honesty I did have to get a little help from one of our developers because I was doing something wrong. Forgetting that in my lab I don’t have trusted certificates I wasn’t using a flag in the headers below to ignore that. Anyway just a slight glitch.

First you need to get your endpoint URL by asking for info from your CF API

 

Get Endpoint:

Get https://API.pcfsys.domain.local/info

This should give you a payload with the endpoint listed. Using the endpoint url now post a payload like the one below substituting your domain and your admin user/password.

 

Get token:

POST https://login.pcfsys.domain.local/oauth/token

Headers:

Content-Type:application/x-www-form-urlencoded

Accept:application/json;charset=utf-8

Authorization:Basic Y2Y6

X-UAA-Endpoint:https://login.domain.local

rejectUnauthorized:false

requestCert:false

agent:false

Body:

grant_type=password&username=admin&password=somepassword

 

At this point you should be presented with payload with your access token listed. Now you can use the access token to Get stuff. Like a list of the CF orgs.

 

List orgs:

Get https://API.pcfsys.domain.local/v2/organizations

Headers:

Accept:application/json;charset=utf-8

Authorization:bearer eyJhbGciOiJSUzI1…………..

 

List Spaces:

Get https://API.pcfsys.domain.local/v2/spaces

Headers = same as orgs

 

List Apps:

Get https://API.pcfsys.domain.local/v2/apps

Headers = same as orgs

 

List Users:

Get https://API.pcfsys.domain.local/v2/users

Headers = same as orgs

 

OK how about something a bit more like a query? How about we list all the space for a given user?

When you called the list of users you should be presented with a payload that also includes the guid for each user. Using this guid you can list the spaces for that user.

 

List spaces for specific user using the guid:

Get https://API.pcfsys.domain.local/v2/users/94aa5e3b-86e1-4ca0-90b1-4734d6555ef2/spaces

Headers = same as orgs

 

Simple right… watch this space for more on the Cloud Foundry API

So I wanted to test a Go Web running on Cloud Foundry and I’m still homing my skills in this area. I needed a simple hello world web app and found some Go examples on the web that use the net/http package.

 

package main

import (

“fmt”

“net/http”

)

func handler(w http.ResponseWriter, r *http.Request) {

fmt.Fprintf(w, “Hi there, this is a Go App running on CF %s!”, r.URL.Path[1:])

}

func main() {

http.HandleFunc(“/”, handler)

http.ListenAndServe(“:8080”, nil)

}


I ran into an issue which was a school boy error and fundamental to Cloud Foundry. If you view the above code you will notice I hard coded the http listeners to port 8080. WaWARRR.. The mistake was presuming I could get a route bag to my CF container from the outside world on a specific port number.

 

CF will give each instance of your app container a dynamical port number. The key in your code to make use of an environment variable called “PORT”. To do that you need to first import the “os” package so you can make use of the “Getenv” function to retrieve the value of the assigned port. Now my code looks like this:

 

package main

import (

“fmt”

“net/http”

“os”

)

func handler(w http.ResponseWriter, r *http.Request) {

fmt.Fprintf(w, “Hi there Ricky, this is a Go App running on CF %s!”, r.URL.Path[1:])

}

func main() {

http.HandleFunc(“/”, handler)

http.ListenAndServe(“:”+os.Getenv(“PORT”), nil)

}

 

 

Now when you access your application through cloud foundry, CF will route you through to the correct port.

While setting up PCF in my home lab I noticed a quirk. You have to create a network to be used and specify a subnet for PCF to assign IPs from. Personally I would rather specify a range rather than a whole subnet. As a result you have to exclude out ranges that might be used by other VMs/Systems/Devices on your network. I did so for different subnets and stopped at .254 knowing that .255 is the broadcast address for that subnet.  Whooops I think I found a bug, because any other software would know not to use the broadcast address. Time to ping my mates at Pivotal.

2016-03-31_19-48-08

Hello All.

Welcome to this new blog. I am a veteran blogger in the virtualisation space but I started to reskill in Cloud Native technologies like Cloud Foundry and Docker. So I decided to start blogging about my experience. Watch this space for education and interesting bits on Cloud Native tech.

Banner Adverts