Skip to content

Commit

Permalink
feat: Support for fqdn endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dploeger committed Sep 28, 2023
1 parent 4417e34 commit a67ef2f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ Return all VM names and their IDs

### GET

Return all assigned tags and their categories for the specified vm `:vm`.
Return all assigned tags and their categories for the specified vm `:vm`.

## /vms/:vm/fqdn

### GET

Return the fqdn of the specified vm `:vm` from the VMware guest tools
38 changes: 38 additions & 0 deletions internal/api/vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,41 @@ func GetVMTags(c internal.Config, username string, password string, VMID string)
}
}
}

type GuestNetworkingResponseDNSValues struct {
DomainName string `json:"domain_name"`
HostName string `json:"host_name"`
}

type GuestNetworkingResponse struct {
DNSValues GuestNetworkingResponseDNSValues `json:"dns_values"`
}

// GetFQDN uses the VMware guest tools to get the fqdn of a VM (if possible)
func GetFQDN(c internal.Config, username string, password string, VMID string) (string, error) {
if s, err := GetSession(c, username, password); err != nil {
return "", err
} else {
logrus.Debugf("Trying to figure out the fqdn for vm %s from %s for %s", VMID, c.Resty.BaseURL, username)

var guestNetworkingResponse GuestNetworkingResponse
if r, err := c.Resty.
R().
SetHeader("vmware-api-session-id", s).
SetResult(&guestNetworkingResponse).
SetPathParam("vm", VMID).
Get("/api/vcenter/vm/{vm}/guest/networking"); err != nil {
logrus.Error(err)
return "", err
} else {
if r.IsError() {
return "", fmt.Errorf("can not get FQDN (%s): %s", r.Status(), r.Body())
}
return fmt.Sprintf(
"%s.%s",
guestNetworkingResponse.DNSValues.HostName,
guestNetworkingResponse.DNSValues.DomainName,
), nil
}
}
}
22 changes: 22 additions & 0 deletions internal/endpoints/vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (V *VMSEndpoint) Register(engine *gin.Engine, config internal.Config) {
V.config = config
engine.GET("/vms", V.getVMS)
engine.GET("/vms/:vm/tags", V.getVMTags)
engine.GET("/vms/:vm/fqdn", V.getFQDN)
}

// getVMS exposes all vms of the vCenter at /vms
Expand Down Expand Up @@ -67,3 +68,24 @@ func (V *VMSEndpoint) getVMTags(context *gin.Context) {
}
}
}

func (V *VMSEndpoint) getFQDN(context *gin.Context) {
if r, ok := HandleRequest(context); ok {
var vm VMBinding
if err := context.ShouldBindUri(&vm); err != nil {
context.AbortWithStatusJSON(400, gin.H{
"error": fmt.Sprintf("Missing VM id in path: %s", err),
})
return
}
if fqdn, err := api.GetFQDN(V.config, r.Username, r.Password, vm.ID); err != nil {
context.AbortWithStatusJSON(500, gin.H{
"error": fmt.Sprintf("Error getting tags: %s", err),
})
} else {
context.JSON(200, gin.H{
"fqdn": fqdn,
})
}
}
}

0 comments on commit a67ef2f

Please sign in to comment.