try generating real table
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
140
main.go
140
main.go
@@ -1,10 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -32,7 +35,6 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type BusSharingResults struct {
|
type BusSharingResults struct {
|
||||||
Vcenter string
|
|
||||||
VmName string
|
VmName string
|
||||||
ClusterName string
|
ClusterName string
|
||||||
ControllerName string
|
ControllerName string
|
||||||
@@ -119,7 +121,7 @@ func getScsiBusSharingVMs(client *govmomi.Client) error {
|
|||||||
|
|
||||||
// Iterate through VMs and check for SCSI bus sharing
|
// Iterate through VMs and check for SCSI bus sharing
|
||||||
for _, vm := range vmList {
|
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)
|
//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 {
|
for _, device := range vm.Config.Hardware.Device {
|
||||||
//fmt.Printf("device: %v\n", device)
|
//fmt.Printf("device: %v\n", device)
|
||||||
if scsi, ok := device.(types.BaseVirtualSCSIController); ok {
|
if scsi, ok := device.(types.BaseVirtualSCSIController); ok {
|
||||||
fmt.Printf("scsi: %v\n", scsi)
|
//fmt.Printf("scsi: %v\n", scsi)
|
||||||
controller := scsi.GetVirtualSCSIController()
|
controller := scsi.GetVirtualSCSIController()
|
||||||
//fmt.Printf("controller: %s\n", device.GetVirtualDevice().DeviceInfo.GetDescription().Label)
|
//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))
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func generateBusSharingTable() string {
|
||||||
|
// Define the HTML template
|
||||||
|
htmlTemplate := `
|
||||||
|
<table><tbody>
|
||||||
|
<tr>
|
||||||
|
<th>Vm Name</th>
|
||||||
|
<th>Cluster Name</th>
|
||||||
|
<th>Controller Name</th>
|
||||||
|
<th>Sharing Type</th>
|
||||||
|
</tr>
|
||||||
|
{{range .}}
|
||||||
|
<tr>
|
||||||
|
<td>{{.VmName}}</td>
|
||||||
|
<td>{{.ClusterName}}</td>
|
||||||
|
<td>{{.ControllerName}}</td>
|
||||||
|
<td>{{.SharingType}}</td>
|
||||||
|
</tr>
|
||||||
|
{{end}}
|
||||||
|
</tbody></table>
|
||||||
|
`
|
||||||
|
|
||||||
|
// 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 {
|
func updateHtml(htmlContent string, heading string, newTable string) string {
|
||||||
// Load the HTML content into a goquery document
|
// Load the HTML content into a goquery document
|
||||||
doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlContent))
|
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()
|
h, err := doc.Html()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return htmlContent
|
return htmlContent
|
||||||
}
|
}
|
||||||
|
|
||||||
return h
|
// goquery produces implicit nodes when parsing the input html, remove them now
|
||||||
|
h = strings.Replace("<html><head></head><body>", h, "", 1)
|
||||||
|
h = strings.Replace("</body></html>", h, "", 1)
|
||||||
|
|
||||||
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Command line flags
|
// Command line flags
|
||||||
/*
|
|
||||||
vURL := flag.String("url", "", "The URL of a vCenter server, eg https://server.domain.example/sdk")
|
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")
|
vUser := flag.String("user", "", "The username to use when connecting to vCenter")
|
||||||
vPass := flag.String("password", "", "The password 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")
|
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")
|
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")
|
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)
|
fmt.Fprintf(os.Stderr, "Error setting timezone to %s : %s\n", *vTZ, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
u, err := url.Parse(*vURL)
|
u, err := url.Parse(*vURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error parsing url %s : %s\n", *vURL, err)
|
fmt.Fprintf(os.Stderr, "Error parsing url %s : %s\n", *vURL, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
} else {
|
} else {
|
||||||
if !strings.HasSuffix(u.Path, "/sdk") {
|
if !strings.HasSuffix(u.Path, "/sdk") {
|
||||||
u.Path, _ = url.JoinPath(u.Path, "/sdk")
|
u.Path, _ = url.JoinPath(u.Path, "/sdk")
|
||||||
log.Printf("Updated vCenter URL to '%v'\n", u)
|
log.Printf("Updated vCenter URL to '%v'\n", u)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("Connecting to vCenter %s\n", u)
|
log.Printf("Connecting to vCenter %s\n", u)
|
||||||
u.User = url.UserPassword(*vUser, *vPass)
|
u.User = url.UserPassword(*vUser, *vPass)
|
||||||
|
|
||||||
ctx, cancel = context.WithCancel(context.Background())
|
ctx, cancel = context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// Login to vcenter
|
// Login to vcenter
|
||||||
c, err = govmomi.NewClient(ctx, u, *vInsecure)
|
c, err = govmomi.NewClient(ctx, u, *vInsecure)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Logging in error: %s\n", err)
|
fmt.Fprintf(os.Stderr, "Logging in error: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
defer c.Logout(ctx)
|
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)
|
api, err := goconfluence.NewAPI(*cURL, "", *cToken)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -279,12 +332,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get current user information
|
// get current user information
|
||||||
currentUser, err := api.CurrentUser()
|
/*
|
||||||
if err != nil {
|
currentUser, err := api.CurrentUser()
|
||||||
fmt.Println(err)
|
if err != nil {
|
||||||
return
|
fmt.Println(err)
|
||||||
}
|
return
|
||||||
fmt.Printf("%+v\n", currentUser)
|
}
|
||||||
|
fmt.Printf("%+v\n", currentUser)
|
||||||
|
*/
|
||||||
|
|
||||||
// get content by content id
|
// get content by content id
|
||||||
c, err := api.GetContentByID(*cPageId, goconfluence.ContentQuery{
|
c, err := api.GetContentByID(*cPageId, goconfluence.ContentQuery{
|
||||||
@@ -297,11 +352,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
//fmt.Printf("%+v\n", c)
|
//fmt.Printf("%+v\n", c)
|
||||||
|
|
||||||
|
// Generate new content for confluence
|
||||||
|
|
||||||
fmt.Printf("Current content: %s\n", c.Body.Storage.Value)
|
fmt.Printf("Current content: %s\n", c.Body.Storage.Value)
|
||||||
|
|
||||||
dummyTable := "<table><tbody><tr><th>VM Name</th><th>Cluster Name</th></tr><tr><td>VM1</td><td>Cluster1</td></tr></tbody></table>"
|
//dummyTable := "<table><tbody><tr><th>VM Name</th><th>Cluster Name</th></tr><tr><td>VM1</td><td>Cluster1</td></tr></tbody></table>"
|
||||||
newContent := updateHtml(c.Body.Storage.Value, "wsdc-vc-npr.srv.westpac.com.au", dummyTable)
|
newTable := generateBusSharingTable()
|
||||||
fmt.Printf("newContent: %v\n", newContent)
|
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
|
c.Body.Storage.Value = newContent
|
||||||
|
Reference in New Issue
Block a user