You have no doubt heard the saying that ‘once you know one language you will be able to pick and learn the next language quicker.’ I’ve heard this both for learning to speak another language and also in the coding world. I wanted to put this to the test. Before I start, when I say “I know a programming language” it does not mean I am an expert. What I mean is, I can use a programming language to accomplish tasks that my role benefits from.
Polyglot programming
Polyglot programming is the practice of writing code
in multiple languages to capture additional
functionality and efficiency not available in a single language
In my case, I was able to achieve the desired results and goals in Python, but I wanted to learn something new, and see if I could apply what I learnt when learning Python into learning Golang. There is great advice out there for why Python and Golang are similar, the pros and cons, and which one you should learn if you are new to programming/coding.
Starting off, day one
To start learning Golang, I built my local environment and also played with A tour of Go – Then, picking up the basics, such as Variables, Primitives, Constants and Arrays and Slices. Just being comfortable with the format and outputs. It took a little while for this to sink in, I tried to put an hour per day at least in writing the code going through examples, I would watch a few videos in the evening also just get some ideas and see how others build the code and explained this. My good buddy Nicolas Leiva made this great repo Go programming language online resources on his GitHub account which has a ton of great learning links for Golang. It has both videos and walk-through examples which are really helpful, and I did not need to trawl the internet looking for content.
Using API’s with Golang
Once I had some idea of what I was doing with Golang, I wanted to leverage Golang in some of my daily tasks. I am currently working on a few things – one is my workshop content for DevNet Create , and the other is new DevNet Learning Labs content for Cisco SD-WAN. I wanted to see if I could write the Golang code for vManage REST APIs. I had done this in Python before.
The Cisco SD-WAN software provides a REST API, which is a programmatic interface for controlling, configuring, and monitoring the Cisco SD-WAN devices in an overlay network. You access the REST API through the vManage web server.
A REST API is a web service API that adheres to the REST (Representational State Transfer) architecture. The REST architecture uses a stateless, client–server, cache-able communications protocol. The Cisco SD-WAN vManage NMS web server uses HTTP and its secure counterpart, HTTPS, as the communications protocol. REST applications communicate over HTTP or HTTPS using standard HTTP methods to make calls between network devices. These standard HTTP methods include:
- GET—Retrieve or read information.
- PUT—Update an object.
- POST—Create an object.
- DELETE—Remove an object.
REST is a simpler alternative to mechanisms such as remote procedure calls (RPCs) and web services such as Simple Object Access Protocol (SOAP) and Web Service Definition Language (WSDL). The REST architecture has not been formally defined by any standards bodies.
Here’s a link for more information about REST.
The Golang Code
When I built this code, I broke it down into sections and steps. Below is how I did this – with each example being a checkpoint, and the code following this the next step. This helped me build this code.
example.one.api.cookie
– When you use a program or script to transfer data from a vManage web server, or perform operations on the server, you must first establish an HTTPS session to the server. To do this, you send a call to log in to the server with the following parameters: URL to send the request to—Usehttps://{vmanage-ip-address/j_security_check
, which performs the login operation and security check on the vManage web server at the specified IP address. Request method—Specify aPost
request. The data contains the username and password in the formatj_username=username
&j_password=password
.
I added the HTTP Response Status Codes. When making http requests with Go it is almost always necessary to check the status code of the response which is returned. Generally, if the status code is between 200 and 300 you can treat as successful. But anything except a 200-300 status, we often need to fix something.
example.two.api.func
This breaks theexample.api.cookie
into two functionsmain()
function will now utilize all the functions creating acookiejar
to store cookies required while logging into the URL/vManage
example.three.api.output
adds an additionalGet
request forhttps://{vmanage-ip-address/dataservice/device
this display all Cisco SD-WAN devices in the overlay network that are connected to the vManage NMS.
example.four.api.final
Printing the final output into a more human readable format and removing unwanted information the import “encoding/json” has been used. interface makes the code more flexible, scalable and it’s a way to achieve polymorphism in Go. Instead of requiring a particular type, interfaces allow to specify that only some behavior is needed.
ProTip: It’s not working…help!
It will happen. Unless you have God-like powers, or you are Chuck Norris of course, at some stage (or in my case many stages!) your code will not work or be broken. I got stuck, some/many parts of the code I wrote here. This had me scratching my head, stroking my beard, and hoping wisdom would kick in. Whilst learning python I was reading a book by Zed Shaw. He said, “you will not fix problems in your code just by staring at it.” Of course, look at what you wrote – did you make a simple typo, or is something really wrong here – but do not just sit for hours rerunning the same code expecting it to work just by magic! Start looking for the answer. Let Stackoverflow or Google etc. be your best friend. You won’t be the first to face an issue nor will you be the last. Take a break, get some air, get some coffee…it will help.
There is also the “it was working” pain. You either made a change by adding something, removing something, or simply tried to clean up the code and removed the commented out lines. This is a learning curve for anyone in engineering, no matter the disciple and job role. I am often met with the, “it was working a minute ago!” Followed by the painful, “what did i do?” Then pressing ctrl + z a bunch of time on Atom and rerunning again until I’m back at the working state I was in ten minutes beforehand. In these cases, I am often reminded of one of my favorite quotes by Thomas Edison – “I have not failed. I’ve just found 10,000 ways that won’t work.”
Both the above examples remind us that learning by failure is an important part of learning something new. If and when I am totally stuck, have looked for the answer myself, and tried many attempts to get the code to run, I find that speaking with people around me helps. Even if they do not know the programming language themselves, getting that second opinion can help me work over the parts I was stuck on. (For this particular example let me now thank Matt J, Jock, and Nicolas for their help!)
Get this Code
Give it a try! You can grab this code and run this yourself by following this link to Cisco Exchange SD-WAN API Example w/ Golang Not used Code Exchange before? No problem, Code Exchange is an online, curated set of code repositories that help you develop applications with/on Cisco platforms and APIs. Inside Code Exchange, you will find hundreds of code repositories – code created and maintained by Cisco engineering teams, ecosystem partners, technology and open source communities, and individual developers. Anyone can use this code to jumpstart their app development with Cisco platforms, products, application programming interfaces (APIs), and software development kits (SDKs).
Want to learn more?
I am please to see that my workshop session “Getting Started with Golang” was selected for DevNet Create – DevNet Create will take place April 24-25, 2019 at the Computer History Museum in Mountain View, California. If you cannot attend I will publish the slide deck and code after the event on Code Exchange also.
Get your free DevNet account for free access to application development resources, learning labs, and sandboxes.
CONNECT WITH CISCO
LET US HELP
Call us: 1.800.553.6387 - Ext 118
US/Can | 5am-5pm Pacific Other Countries