For the Golang developer, this chapter shows how to install the govmomi SDK and run example programs against vSphere APIs.
Installing the govmomi SDK for vSphere
govmomi
library is most convenient. To install
govmomi, open a terminal window and use these commands:
- mkdir govmomi
- cd govmomi
- go mod init example/govmomi
- go get -u github.com/vmware/govmomi
Basic Authentication with the VIM API Using govmomi
The govmomi
SDK simplifies the connection code with the govmomi.NewClient
function. The function accepts a service URL containing basic user credentials, opens a session with the server, and returns a client
object containing the ServiceContent
object. ServiceContent
contains references to specific managed objects needed to use VIM API features.
package main import ( "context" "fmt" "net/url" "github.com/vmware/govmomi" "github.com/vmware/govmomi/vim25/methods" "github.com/vmware/govmomi/vim25/types" "github.com/vmware/govmomi/vapi/rest" "github.com/vmware/govmomi/vapi/tags" ) // Connection parameters. var hostname = "vc1.example.com" var username = "[email protected]" var password = "TooSecret2C!" func main() { fmt.Println("Creating a VIM/SOAP session.") vcURL := "https://" + username + ":" + password + "@" + hostname + "/sdk" u, err := url.Parse(vcURL) if err != nil { fmt.Printf("Error parsing url %s\n", vcURL) return } ctx, cancel := context.WithCancel(context.Background()) defer cancel() vimClient, err := govmomi.NewClient(ctx, u, true) if err != nil { fmt.Printf("Error logging in: %s\n", err.Error()) return } fmt.Println("Login successful") ...
At this point, your session is established and the session ID is stored in the client
object. The session ID will be passed to the server with future requests.
Creating a Hello Folder with the VIM API Using govmomi
The ServiceContent
object contains a reference to the root folder of the managed object hierarchy. You can invoke the CreateFolder()
method on the remote rootFolder
object to create a subfolder named Hello Folder
.
... fmt.Println("Using VIM to create Hello Folder.") req := types.CreateFolder{ This: vimClient.Client.ServiceContent.RootFolder, Name: "Hello Folder", } res, err := methods.CreateFolder(ctx, vimClient.Client, &req) if err != nil { fmt.Println(err) return } fmt.Println(res.Returnval.Type, "created") } helloFolder := res.Returnval fmt.Println(helloFolder) ...
Basic Authentication with the Automation API Using govmomi
To create an authenticated session with the Automation API, you can use code like the following.
... fmt.Println("Creating an Automation API session.") vapiClient := rest.NewClient(vimClient.Client) err = vapiClient.Login(ctx, url.UserPassword(username, password)) if err != nil { fmt.Println(err) return } fmt.Println("VAPI session created.") ...
Tagging the Hello World Folder with the Automation API Using govmomi
To tag a folder, start by creating a tag category, then create a tag in that category, and finally associate the tag with the folder.
... fmt.Println("VAPI creating a tag category.") m := tags.NewManager(vapiClient) catId, err := m.CreateCategory(ctx, &tags.Category{ AssociableTypes: []string{"Folder"}, Cardinality: "SINGLE", Description: "example of tagging category from govmomi", Name: "hello_category", }) if err != nil { fmt.Println(err) return } fmt.Println("New category created: ", catId) fmt.Println("VAPI creating a tag in new category.") tagId, err := m.CreateTag(ctx, &tags.Tag{ CategoryID: catId, Description: "example of a tag from govmomi", Name: "hello_tag", }) if err != nil { fmt.Println(err) return } fmt.Println("New tag created: ", tagId) fmt.Println("VAPI creating association of tag with Hello Folder.") err = m.AttachTag(ctx, tagId, helloFolder) if err != nil { fmt.Println(err) return } fmt.Println("Tag attached to folder: ", tagId, helloFolder) }