VMware Cloud Community
SantoshBB
Contributor
Contributor

REST API for vCenter management

Hi,

I am writing a REST API based GO program, and finding the below program.

Did not get any sample code to connect to vcenter and fetch the VM Properties.

Can anyone please guide me to the right tutorials.

I have pasted the code which as below.

package main

import (

   "fmt"
  "io/ioutil"
  "log"
  "net/http"
)

func main() {

   var username string = "administrator@vsphere.local"
   var passwd string = "MyPassword@123"
   client := &http.Client{}

  req, err := http.NewRequest("POST", "https://IP_ADDRESS/rest/com/vmware/cis/session", nil)

  req.SetBasicAuth(username, passwd)

  resp, err := client.Do(req)

   if err != nil{

  log.Fatal(err)

  }

  bodyText, err := ioutil.ReadAll(resp.Body)

  s := string(bodyText)

  fmt.Println(s)

}

I am getting the below error for the above program

2019/07/08 21:43:19 Post https://IP_ADDRESS/rest/com/vmware/cis/session: x509: certificate signed by unknown authority

exit status 1

I am trying to connect to vCenter 6.7. Connecting vCenter from browser works fine, but with the URL "https://IP_ADDRESS/rest/com/vmware/cis/session" i get the error {"name":"com.vmware.vapi.rest.httpNotFound","localizableMessages":[{"defaultMessage":"Not found.","id":"com.vmware.vapi.rest.httpNotFound"}],"majorErrorCode":404}

Can anyone please help ?

0 Kudos
5 Replies
daphnissov
Immortal
Immortal

When talking to the API endpoint, you need to use fully-qualified domain names.

0 Kudos
SantoshBB
Contributor
Contributor

Hi,

Thanks for replying.

I am able to connect to the same vcenter with ipaddress when using govmomi call, does it mean the fully qualified domain name is necessary for REST calls only.

0 Kudos
daphnissov
Immortal
Immortal

0 Kudos
sjesse
Leadership
Leadership

Why not just use the fqdn regardless, using just the ip limits portability, and if you ever need to rebuild the vcenter with a  new ip or something similar you don't need to go into the script and update the fqdn. I have a software that I have to manage disaster recovery. for, and its very irrartating to go into there configuration files and find the ips with a script and change them, especially when they change the syntax in updates.

0 Kudos
rdindokar
VMware Employee
VMware Employee

Skip the certificate verification to avoid x509: certificate signed by unknown authority error

url := "https://"+<ip>+"/rest/com/vmware/cis/session"
req, err := http.NewRequest("POST", url, nil)

tr := &http.Transport{

  TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

client := &http.Client{Transport: tr}

req.SetBasicAuth(config.Username,config.Password)

resp, err := client.Do(req)

if err != nil{

   log.Fatal(err)

   return "",err

}

bodyText, err := ioutil.ReadAll(resp.Body)

0 Kudos