diff --git a/main.go b/main.go
index 69979c3..92064c6 100644
--- a/main.go
+++ b/main.go
@@ -1,10 +1,13 @@
package main
import (
+ "bytes"
"context"
"flag"
"fmt"
+ "html/template"
"log"
+ "net/url"
"os"
"reflect"
"strings"
@@ -32,7 +35,6 @@ var (
)
type BusSharingResults struct {
- Vcenter string
VmName string
ClusterName string
ControllerName string
@@ -119,7 +121,7 @@ func getScsiBusSharingVMs(client *govmomi.Client) error {
// Iterate through VMs and check for SCSI bus sharing
for _, vm := range vmList {
- fmt.Printf("vm : %s [%s]\n", vm.Name, vm.Summary.Runtime.Host)
+ //fmt.Printf("vm : %s [%s]\n", vm.Name, vm.Summary.Runtime.Host)
//fmt.Printf("vm parent: %v\n", vm.Parent)
/*
@@ -154,7 +156,7 @@ func getScsiBusSharingVMs(client *govmomi.Client) error {
for _, device := range vm.Config.Hardware.Device {
//fmt.Printf("device: %v\n", device)
if scsi, ok := device.(types.BaseVirtualSCSIController); ok {
- fmt.Printf("scsi: %v\n", scsi)
+ //fmt.Printf("scsi: %v\n", scsi)
controller := scsi.GetVirtualSCSIController()
//fmt.Printf("controller: %s\n", device.GetVirtualDevice().DeviceInfo.GetDescription().Label)
fmt.Printf("VM %s is using SCSI bus sharing mode: %s\n", vm.Name, string(controller.SharedBus))
@@ -178,6 +180,46 @@ func getScsiBusSharingVMs(client *govmomi.Client) error {
return nil
}
+func generateBusSharingTable() string {
+ // Define the HTML template
+ htmlTemplate := `
+
+
+ Vm Name |
+ Cluster Name |
+ Controller Name |
+ Sharing Type |
+
+ {{range .}}
+
+ {{.VmName}} |
+ {{.ClusterName}} |
+ {{.ControllerName}} |
+ {{.SharingType}} |
+
+ {{end}}
+
+ `
+
+ // Create a new template and parse the HTML template
+ tmpl := template.Must(template.New("table").Parse(htmlTemplate))
+
+ // Create a buffer to store the HTML output
+ var buf bytes.Buffer
+
+ // Execute the template with the results and write to the buffer
+ err := tmpl.Execute(&buf, busSharingResults)
+ if err != nil {
+ panic(err)
+ }
+
+ // Convert the buffer to a string
+ htmlString := buf.String()
+
+ return htmlString
+
+}
+
func updateHtml(htmlContent string, heading string, newTable string) string {
// Load the HTML content into a goquery document
doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlContent))
@@ -199,24 +241,28 @@ func updateHtml(htmlContent string, heading string, newTable string) string {
}
})
+ // Render the html
h, err := doc.Html()
if err != nil {
fmt.Println(err)
return htmlContent
}
- return h
+ // goquery produces implicit nodes when parsing the input html, remove them now
+ h = strings.Replace("", h, "", 1)
+ h = strings.Replace("", h, "", 1)
+ return h
}
func main() {
// Command line flags
- /*
- vURL := flag.String("url", "", "The URL of a vCenter server, eg https://server.domain.example/sdk")
- vUser := flag.String("user", "", "The username to use when connecting to vCenter")
- vPass := flag.String("password", "", "The password to use when connecting to vCenter")
- vInsecure := flag.Bool("insecure", true, "Allow insecure connections to vCenter")
- */
+
+ vURL := flag.String("url", "", "The URL of a vCenter server, eg https://server.domain.example/sdk")
+ vUser := flag.String("user", "", "The username to use when connecting to vCenter")
+ vPass := flag.String("password", "", "The password to use when connecting to vCenter")
+ vInsecure := flag.Bool("insecure", true, "Allow insecure connections to vCenter")
+
vTZ := flag.String("tz", "Australia/Sydney", "The timezone to use when converting vCenter UTC times")
cURL := flag.String("confluence-url", "https://confluence.yourdomain.com/wiki/rest/api", "The URL to your confluence rest API endpoint")
@@ -244,33 +290,40 @@ func main() {
fmt.Fprintf(os.Stderr, "Error setting timezone to %s : %s\n", *vTZ, err)
os.Exit(1)
}
- /*
- u, err := url.Parse(*vURL)
- if err != nil {
- fmt.Fprintf(os.Stderr, "Error parsing url %s : %s\n", *vURL, err)
- os.Exit(1)
- } else {
- if !strings.HasSuffix(u.Path, "/sdk") {
- u.Path, _ = url.JoinPath(u.Path, "/sdk")
- log.Printf("Updated vCenter URL to '%v'\n", u)
- }
+
+ u, err := url.Parse(*vURL)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Error parsing url %s : %s\n", *vURL, err)
+ os.Exit(1)
+ } else {
+ if !strings.HasSuffix(u.Path, "/sdk") {
+ u.Path, _ = url.JoinPath(u.Path, "/sdk")
+ log.Printf("Updated vCenter URL to '%v'\n", u)
}
+ }
- log.Printf("Connecting to vCenter %s\n", u)
- u.User = url.UserPassword(*vUser, *vPass)
+ log.Printf("Connecting to vCenter %s\n", u)
+ u.User = url.UserPassword(*vUser, *vPass)
- ctx, cancel = context.WithCancel(context.Background())
- defer cancel()
+ ctx, cancel = context.WithCancel(context.Background())
+ defer cancel()
- // Login to vcenter
- c, err = govmomi.NewClient(ctx, u, *vInsecure)
- if err != nil {
- fmt.Fprintf(os.Stderr, "Logging in error: %s\n", err)
- os.Exit(1)
- }
- defer c.Logout(ctx)
- */
+ // Login to vcenter
+ c, err = govmomi.NewClient(ctx, u, *vInsecure)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Logging in error: %s\n", err)
+ os.Exit(1)
+ }
+ defer c.Logout(ctx)
+ // Find scsi bus sharing VMs
+ err = getScsiBusSharingVMs(c)
+ if err != nil {
+ log.Printf("Error retrieving list of VMs with SCSI Bus Sharing : %s\n", err)
+ return
+ }
+
+ // Connect to confluence
api, err := goconfluence.NewAPI(*cURL, "", *cToken)
if err != nil {
@@ -279,12 +332,14 @@ func main() {
}
// get current user information
- currentUser, err := api.CurrentUser()
- if err != nil {
- fmt.Println(err)
- return
- }
- fmt.Printf("%+v\n", currentUser)
+ /*
+ currentUser, err := api.CurrentUser()
+ if err != nil {
+ fmt.Println(err)
+ return
+ }
+ fmt.Printf("%+v\n", currentUser)
+ */
// get content by content id
c, err := api.GetContentByID(*cPageId, goconfluence.ContentQuery{
@@ -297,11 +352,14 @@ func main() {
}
//fmt.Printf("%+v\n", c)
+ // Generate new content for confluence
+
fmt.Printf("Current content: %s\n", c.Body.Storage.Value)
- dummyTable := "VM Name | Cluster Name |
---|
VM1 | Cluster1 |
"
- newContent := updateHtml(c.Body.Storage.Value, "wsdc-vc-npr.srv.westpac.com.au", dummyTable)
- fmt.Printf("newContent: %v\n", newContent)
+ //dummyTable := "VM Name | Cluster Name |
---|
VM1 | Cluster1 |
"
+ newTable := generateBusSharingTable()
+ newContent := updateHtml(c.Body.Storage.Value, "wsdc-vc-npr.srv.westpac.com.au", newTable)
+ fmt.Printf("New Content: %v\n", newContent)
/*
c.Body.Storage.Value = newContent