Press "Enter" to skip to content

Learning GO with Rubrik

bist 0

So, I have no clue what I am doing. I am in no means a developer. Always had the urge to understand at least one programming language. I decided on Golang. They always say that for not giving up a language is that you need a real project to build. Since Rubrik is what I am using on a daily base it might be a good idea to apply my Go learning to Rubrik.

Remember these are ramblings from someone with no knowledge about programming. This content might be uber simplistic.

What I learned so far:

  • All courses available are crap and only learn you syntax.
  • How the f… do you start writing your first code. Especially if you need to import modules…. clueless
  • All examples, guides assume you actually have a clue what you are doing…I do not.

Here goes. To initiate your first project and you are not going to publish it on github. You need to initiate a go.mod file that will contain your imported modules.

Do this by creating a folder for your project and run

go mod init example/rubrik

This will create a go.mod file.

To be able to use the Rubrik SDK module for go you will need to run

go get github.com/rubrikinc/rubrik-sdk-for-go/rubrikcdm

No shit sherlock. This will now be added to go.mod. — have a look

Now we are able to use this module in our code. The first code I will show you is getting the vmware vms from a existing SLA and show them on screen. But wait…….

I need to authenticate in a second within my go code. Reading the Rubrik github page and the Rubrik SDK kit it seems that a best practice is to have the username, password and CDM ip in a system variable……what the f….??

I am on mac os. The best way to do this is use export. Export that is in a full command:

  • export rubrik_cdm_node_ip=ipofyourcdmcluster
  • export rubrik_cdm_username=username
  • export rubrik_cdm_password=password

Cool. So if you type this the variables are stored……temporarely.

I have put this in a bash script which looks like this:

#!/bin/zsh

#execute this command to set the env variable -- eval "$(./env.sh)"

echo export rubrik_cdm_node_ip=ip
echo export rubrik_cdm_username=admin
echo export rubrik_cdm_password=password

Then execute the bash script with eval "$(./env.sh)

Alright. Can start writing code please?

Here we go:

package main

import (
	
	"fmt"
	"log"

	"github.com/rubrikinc/rubrik-sdk-for-go/rubrikcdm"
)

func main() {
	//This is the connection code
	rubrik, err := rubrikcdm.ConnectEnv()
	if err != nil {
		log.Fatal(err)
	}
	//This is the actual code for getting the VMs in the SLA daily_for_week
	slaName := "daily_for_week"

	getObjSLA, err := rubrik.GetSLAObjects(slaName, "vmware")
	if err != nil {
		log.Fatal(err)
	}
	//This is the command I added to print the output of the getObjSLA variable
	fmt.Println(getObjSLA)

}

If you are cool kid on the block I learned that you need to use VI to code. Or…..you use Neovim….(took a while to learn VI commands. But once you get the hang of it there really is nothing faster.

If setting up neovim is too hard at first….(you pussy) take a look at [email protected] and his Lunarvim project

Enjoy and don’t take anything to serious

Stayed tuned for more stuff I learned

Leave a Reply

Your email address will not be published.