github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/monitor.go (about)

     1  /*
     2   * Copyright 2021 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     3   */
     4  
     5  // Contains auxiliary functions to show library entities structure.
     6  // Used for debugging and testing.
     7  package govcd
     8  
     9  import (
    10  	"encoding/json"
    11  	"fmt"
    12  	"time"
    13  
    14  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    15  	"github.com/vmware/go-vcloud-director/v2/util"
    16  )
    17  
    18  // For each library {entity}, we have two functions: Show{Entity} and Log{Entity}
    19  // The first one shows the contents of the entity on screen
    20  // The second one logs into the default util.Logger
    21  // Both functions use JSON as the entity format
    22  
    23  // Available entities:
    24  // org
    25  // adminOrg
    26  // vdc
    27  // catalog
    28  // catalogItem
    29  // adminCatalog
    30  // network
    31  // externalNetwork
    32  // vapp
    33  // vm
    34  // task
    35  // Edge Gateway service configuration
    36  
    37  func out(destination, format string, args ...interface{}) {
    38  	switch destination {
    39  	case "screen":
    40  		fmt.Printf(format, args...)
    41  	case "log":
    42  		util.Logger.Printf(format, args...)
    43  	default:
    44  		fmt.Printf("Unhandled destination: %s\n", destination)
    45  	}
    46  }
    47  
    48  // Returns a vApp structure as JSON
    49  func prettyVapp(vapp types.VApp) string {
    50  	byteBuf, err := json.MarshalIndent(vapp, " ", " ")
    51  	if err == nil {
    52  		return fmt.Sprintf("%s\n", string(byteBuf))
    53  	}
    54  	return ""
    55  }
    56  
    57  // Returns a VM structure as JSON
    58  func prettyVm(vm types.Vm) string {
    59  	byteBuf, err := json.MarshalIndent(vm, " ", " ")
    60  	if err == nil {
    61  		return fmt.Sprintf("%s\n", string(byteBuf))
    62  	}
    63  	return ""
    64  }
    65  
    66  // Returns an OrgUser structure as JSON
    67  func prettyUser(user types.User) string {
    68  	byteBuf, err := json.MarshalIndent(user, " ", " ")
    69  	if err == nil {
    70  		return fmt.Sprintf("%s\n", string(byteBuf))
    71  	}
    72  	return ""
    73  }
    74  
    75  // Returns a VDC structure as JSON
    76  func prettyVdc(vdc types.Vdc) string {
    77  	byteBuf, err := json.MarshalIndent(vdc, " ", " ")
    78  	if err == nil {
    79  		return fmt.Sprintf("%s\n", string(byteBuf))
    80  	}
    81  	return ""
    82  }
    83  
    84  // Returns a Catalog Item structure as JSON
    85  func prettyCatalogItem(catalogItem types.CatalogItem) string {
    86  	byteBuf, err := json.MarshalIndent(catalogItem, " ", " ")
    87  	if err == nil {
    88  		return fmt.Sprintf("%s\n", string(byteBuf))
    89  	}
    90  	return ""
    91  }
    92  
    93  // Returns a Catalog structure as JSON
    94  func prettyCatalog(catalog types.Catalog) string {
    95  	byteBuf, err := json.MarshalIndent(catalog, " ", " ")
    96  	if err == nil {
    97  		return fmt.Sprintf("%s\n", string(byteBuf))
    98  	}
    99  	return ""
   100  }
   101  
   102  // Returns an Admin Catalog structure as JSON
   103  func prettyAdminCatalog(catalog types.AdminCatalog) string {
   104  	byteBuf, err := json.MarshalIndent(catalog, " ", " ")
   105  	if err == nil {
   106  		return fmt.Sprintf("%s\n", string(byteBuf))
   107  	}
   108  	return ""
   109  }
   110  
   111  // Returns an Org structure as JSON
   112  func prettyOrg(org types.Org) string {
   113  	byteBuf, err := json.MarshalIndent(org, " ", " ")
   114  	if err == nil {
   115  		return fmt.Sprintf("%s\n", string(byteBuf))
   116  	}
   117  	return ""
   118  }
   119  
   120  // Returns an Admin Org structure as JSON
   121  func prettyAdminOrg(org types.AdminOrg) string {
   122  	byteBuf, err := json.MarshalIndent(org, " ", " ")
   123  	if err == nil {
   124  		return fmt.Sprintf("%s\n", string(byteBuf))
   125  	}
   126  	return ""
   127  }
   128  
   129  // Returns a Disk structure as JSON
   130  func prettyDisk(disk types.Disk) string {
   131  	byteBuf, err := json.MarshalIndent(disk, " ", " ")
   132  	if err == nil {
   133  		return fmt.Sprintf("%s\n", string(byteBuf))
   134  	}
   135  	return ""
   136  }
   137  
   138  // Returns an External Network structure as JSON
   139  func prettyExternalNetwork(network types.ExternalNetwork) string {
   140  	byteBuf, err := json.MarshalIndent(network, " ", " ")
   141  	if err == nil {
   142  		return fmt.Sprintf("%s\n", string(byteBuf))
   143  	}
   144  	return ""
   145  }
   146  
   147  // Returns a Network structure as JSON
   148  func prettyNetworkConf(conf types.OrgVDCNetwork) string {
   149  	byteBuf, err := json.MarshalIndent(conf, " ", " ")
   150  	if err == nil {
   151  		return fmt.Sprintf("%s\n", string(byteBuf))
   152  	}
   153  	return ""
   154  }
   155  
   156  // Returns a Task structure as JSON
   157  func prettyTask(task *types.Task) string {
   158  	byteBuf, err := json.MarshalIndent(task, " ", " ")
   159  	if err == nil {
   160  		return fmt.Sprintf("%s\n", string(byteBuf))
   161  	}
   162  	return ""
   163  }
   164  
   165  // Returns an Edge Gateway service configuration structure as JSON
   166  // func prettyEdgeGatewayServiceConfiguration(conf types.EdgeGatewayServiceConfiguration) string {
   167  func prettyEdgeGateway(egw types.EdgeGateway) string {
   168  	result := ""
   169  	byteBuf, err := json.MarshalIndent(egw, " ", " ")
   170  	if err == nil {
   171  		result += fmt.Sprintf("%s\n", string(byteBuf))
   172  	}
   173  	return result
   174  }
   175  
   176  func LogNetwork(conf types.OrgVDCNetwork) {
   177  	out("log", prettyNetworkConf(conf))
   178  }
   179  
   180  func ShowNetwork(conf types.OrgVDCNetwork) {
   181  	out("screen", prettyNetworkConf(conf))
   182  }
   183  
   184  func LogExternalNetwork(network types.ExternalNetwork) {
   185  	out("log", prettyExternalNetwork(network))
   186  }
   187  
   188  func ShowExternalNetwork(network types.ExternalNetwork) {
   189  	out("screen", prettyExternalNetwork(network))
   190  }
   191  
   192  func LogVapp(vapp types.VApp) {
   193  	out("log", prettyVapp(vapp))
   194  }
   195  
   196  func ShowVapp(vapp types.VApp) {
   197  	out("screen", prettyVapp(vapp))
   198  }
   199  
   200  func LogVm(vm types.Vm) {
   201  	out("log", prettyVm(vm))
   202  }
   203  
   204  func ShowVm(vm types.Vm) {
   205  	out("screen", prettyVm(vm))
   206  }
   207  func ShowOrg(org types.Org) {
   208  	out("screen", prettyOrg(org))
   209  }
   210  
   211  func LogOrg(org types.Org) {
   212  	out("log", prettyOrg(org))
   213  }
   214  
   215  func ShowAdminOrg(org types.AdminOrg) {
   216  	out("screen", prettyAdminOrg(org))
   217  }
   218  
   219  func LogAdminOrg(org types.AdminOrg) {
   220  	out("log", prettyAdminOrg(org))
   221  }
   222  
   223  func ShowVdc(vdc types.Vdc) {
   224  	out("screen", prettyVdc(vdc))
   225  }
   226  
   227  func LogVdc(vdc types.Vdc) {
   228  	out("log", prettyVdc(vdc))
   229  }
   230  
   231  func ShowUser(user types.User) {
   232  	out("screen", prettyUser(user))
   233  }
   234  
   235  func LogUser(user types.User) {
   236  	out("log", prettyUser(user))
   237  }
   238  
   239  func ShowDisk(disk types.Disk) {
   240  	out("screen", prettyDisk(disk))
   241  }
   242  
   243  func LogDisk(disk types.Disk) {
   244  	out("log", prettyDisk(disk))
   245  }
   246  func ShowCatalog(catalog types.Catalog) {
   247  	out("screen", prettyCatalog(catalog))
   248  }
   249  
   250  func LogCatalog(catalog types.Catalog) {
   251  	out("log", prettyCatalog(catalog))
   252  }
   253  
   254  func ShowCatalogItem(catalogItem types.CatalogItem) {
   255  	out("screen", prettyCatalogItem(catalogItem))
   256  }
   257  
   258  func LogCatalogItem(catalogItem types.CatalogItem) {
   259  	out("log", prettyCatalogItem(catalogItem))
   260  }
   261  
   262  func ShowAdminCatalog(catalog types.AdminCatalog) {
   263  	out("screen", prettyAdminCatalog(catalog))
   264  }
   265  
   266  func LogAdminCatalog(catalog types.AdminCatalog) {
   267  	out("log", prettyAdminCatalog(catalog))
   268  }
   269  
   270  func LogEdgeGateway(edgeGateway types.EdgeGateway) {
   271  	out("log", prettyEdgeGateway(edgeGateway))
   272  }
   273  
   274  func ShowEdgeGateway(edgeGateway types.EdgeGateway) {
   275  	out("screen", prettyEdgeGateway(edgeGateway))
   276  }
   277  
   278  // Auxiliary function to monitor a task
   279  // It can be used in association with WaitInspectTaskCompletion
   280  func outTask(destination string, task *types.Task, howManyTimes int, elapsed time.Duration, first, last bool) {
   281  	if task == nil {
   282  		out(destination, "Task is null\n")
   283  		return
   284  	}
   285  	out(destination, prettyTask(task))
   286  
   287  	out(destination, "progress: [%s:%d] %d%%\n", elapsed.Round(1*time.Second), howManyTimes, task.Progress)
   288  	out(destination, "-------------------------------\n")
   289  }
   290  
   291  func simpleOutTask(destination string, task *types.Task, howManyTimes int, elapsed time.Duration, first, last bool) {
   292  	if task == nil {
   293  		out(destination, "Task is null\n")
   294  		return
   295  	}
   296  	out(destination, "%s (%s) - elapsed: [%s:%d] - progress: %d%%\n", task.OperationName, task.Status, elapsed.Round(1*time.Second), howManyTimes, task.Progress)
   297  }
   298  
   299  func LogTask(task *types.Task, howManyTimes int, elapsed time.Duration, first, last bool) {
   300  	outTask("log", task, howManyTimes, elapsed, first, last)
   301  }
   302  
   303  func ShowTask(task *types.Task, howManyTimes int, elapsed time.Duration, first, last bool) {
   304  	outTask("screen", task, howManyTimes, elapsed, first, last)
   305  }
   306  
   307  func SimpleShowTask(task *types.Task, howManyTimes int, elapsed time.Duration, first, last bool) {
   308  	simpleOutTask("screen", task, howManyTimes, elapsed, first, last)
   309  }
   310  
   311  func MinimalShowTask(task *types.Task, howManyTimes int, elapsed time.Duration, first, last bool) {
   312  	marker := "."
   313  	if task.Status == "success" {
   314  		marker = "+"
   315  	}
   316  	if task.Status == "error" {
   317  		marker = "-"
   318  	}
   319  	fmt.Print(marker)
   320  	if last {
   321  		fmt.Println()
   322  	}
   323  }
   324  
   325  func SimpleLogTask(task *types.Task, howManyTimes int, elapsed time.Duration, first, last bool) {
   326  	simpleOutTask("log", task, howManyTimes, elapsed, first, last)
   327  }