项目作者: 2spmohanty

项目描述 :
vCenter Automation GO way.
高级语言: Go
项目地址: git://github.com/2spmohanty/gosphere.git
创建时间: 2019-05-01T17:02:33Z
项目社区:https://github.com/2spmohanty/gosphere

开源协议:Apache License 2.0

下载


gosphere

The gosphere repository contains codes written in Go Language that can be used to perform automation task on VMWare vCenter. These codes are wrapper on govmomi and exposes easy Methods. Contributors are welcome.

  1. func main() {
  2. vc := flag.String("vc", "VC_IP", "Enter vCenter IP/ FQDN")
  3. user := flag.String("user", "Administrator@vsphere.local", "vCenter User")
  4. pass := flag.String("pass", "XXXXX", "Enter vCenter pass")
  5. flag.Parse()
  6. vcenter := operation.NewVCenter(*vc, *user, *pass)
  7. ctx, cancel := context.WithCancel(context.Background())
  8. defer cancel()
  9. err := vcenter.Connect(ctx)
  10. if err != nil {
  11. fmt.Printf("Failed to connect to vCenter: %s\n", err)
  12. return
  13. }
  14. fmt.Printf("Connected to vCenter: %s\n", *vc)
  15. //Get Datacenter Operation Level object
  16. dcops := operation.DatacenterOperation{Context: ctx, Vcenter: vcenter}
  17. //Get Cluster Operation Level object
  18. clops := operation.ClusterOperation{Context: ctx, Vcenter: vcenter}
  19. datacenters, err := vcenter.GetAllDatacenter(ctx)
  20. if err != nil {
  21. fmt.Printf("Datacenters errors: %s", err)
  22. return
  23. }
  24. for _, dc := range datacenters {
  25. dcName := dc.Name()
  26. fmt.Printf(" Datacenter %s\n", dcName)
  27. standalonehosts := dcops.GetStandAloneHosts(dc)
  28. if standalonehosts != nil {
  29. fmt.Printf("Standalone Hosts on Datacenter %s\n", dcName)
  30. for _, hostmor := range standalonehosts {
  31. fmt.Println(hostmor.Name)
  32. }
  33. }
  34. var cls []mo.ClusterComputeResource
  35. cls, _ = dcops.GetAllCluster(dc)
  36. if cls != nil {
  37. for _, clsref := range cls {
  38. fmt.Printf("Datcenter Clusters ***** %s ******\n", clsref.Name)
  39. var hosts []mo.HostSystem
  40. hosts, _ = clops.GetAllClusterHosts(clsref, "")
  41. if hosts != nil {
  42. fmt.Printf("Cluster Hosts")
  43. for _, hostref := range hosts {
  44. fmt.Printf("**** %s ****\n", hostref.Name)
  45. }
  46. }
  47. }
  48. }
  49. }
  50. }

Philosophy

The code must be

  1. - simple
  2. - readable
  3. - maintainable
  4. - Do exactly one task.