Browse Source

bin/conch| clone the "global datacenter" commands into the root

sungo 9 months ago
parent
commit
c3d7dcbdac
3 changed files with 396 additions and 0 deletions
  1. 2 0
      cmd/conch/conch.go
  2. 87 0
      pkg/commands/datacenter/init.go
  3. 307 0
      pkg/commands/datacenter/main.go

+ 2 - 0
cmd/conch/conch.go

@@ -12,6 +12,7 @@ import (
 	"github.com/joyent/conch-shell/pkg/cmd/conch1"
 	"github.com/joyent/conch-shell/pkg/commands/admin"
 	"github.com/joyent/conch-shell/pkg/commands/api"
+	"github.com/joyent/conch-shell/pkg/commands/datacenter"
 	"github.com/joyent/conch-shell/pkg/commands/devices"
 	"github.com/joyent/conch-shell/pkg/commands/global"
 	"github.com/joyent/conch-shell/pkg/commands/hardware"
@@ -29,6 +30,7 @@ func main() {
 
 	api.Init(app)
 	admin.Init(app)
+	datacenter.Init(app)
 	devices.Init(app)
 	global.Init(app)
 	hardware.Init(app)

+ 87 - 0
pkg/commands/datacenter/init.go

@@ -0,0 +1,87 @@
+// Copyright Joyent, Inc.
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+package datacenter
+
+import (
+	"github.com/jawher/mow.cli"
+	"github.com/joyent/conch-shell/pkg/conch/uuid"
+	"github.com/joyent/conch-shell/pkg/util"
+)
+
+// GdcUUID is the UUID of the global datacenter provided by the user
+var GdcUUID uuid.UUID
+
+// Init loads up the commands
+func Init(app *cli.Cli) {
+
+	app.Command(
+		"datacenters dcs",
+		"Operate on all datacenters",
+		func(cmd *cli.Cmd) {
+			cmd.Before = util.BuildAPIAndVerifyLogin
+			cmd.Command(
+				"get",
+				"Get all datacenters",
+				dcGetAll,
+			)
+
+			cmd.Command(
+				"create",
+				"Create a datacenter",
+				dcCreate,
+			)
+		},
+	)
+
+	app.Command(
+		"datacenter dc",
+		"Operate on individual datacenters",
+		func(cmd *cli.Cmd) {
+			var gdcIDStr = cmd.StringArg("ID", "", "The UUID of the datacenter")
+
+			cmd.Spec = "ID"
+			cmd.Before = func() {
+				util.BuildAPIAndVerifyLogin()
+				id, err := util.MagicDatacenterID(*gdcIDStr)
+				if err != nil {
+					util.Bail(err)
+				}
+				GdcUUID = id
+			}
+
+			cmd.Command(
+				"get",
+				"Get a datacenter",
+				dcGet,
+			)
+
+			cmd.Command(
+				"delete rm",
+				"Delete a datacenter",
+				dcDelete,
+			)
+
+			cmd.Command(
+				"update",
+				"Update a datacenter",
+				dcUpdate,
+			)
+
+			cmd.Command(
+				"rooms",
+				"Get all rooms assigned to a datacenter",
+				dcGetAllRooms,
+			)
+
+			cmd.Command(
+				"layout-tree",
+				"Get a tree of the datacenter, its rooms, racks, and layouts",
+				dcAllTheThingsTree,
+			)
+		},
+	)
+}

+ 307 - 0
pkg/commands/datacenter/main.go

@@ -0,0 +1,307 @@
+// Copyright Joyent, Inc.
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+package datacenter
+
+import (
+	"fmt"
+
+	gotree "github.com/DiSiqueira/GoTree"
+	"github.com/jawher/mow.cli"
+	"github.com/joyent/conch-shell/pkg/conch"
+	"github.com/joyent/conch-shell/pkg/util"
+	"sort"
+)
+
+func dcGetAll(app *cli.Cmd) {
+	app.Action = func() {
+		d, err := util.API.GetDatacenters()
+		if err != nil {
+			util.Bail(err)
+		}
+
+		if util.JSON {
+			util.JSONOut(d)
+			return
+		}
+		table := util.GetMarkdownTable()
+		table.SetHeader([]string{
+			"ID",
+			"Region",
+			"Vendor",
+			"Vendor Name",
+			"Location",
+		})
+
+		for _, dc := range d {
+			table.Append([]string{
+				dc.ID.String(),
+				dc.Region,
+				dc.Vendor,
+				dc.VendorName,
+				dc.Location,
+			})
+		}
+
+		table.Render()
+	}
+}
+
+func dcGet(app *cli.Cmd) {
+	app.Action = func() {
+		d, err := util.API.GetDatacenter(GdcUUID)
+		if err != nil {
+			util.Bail(err)
+		}
+
+		if util.JSON {
+			util.JSONOut(d)
+			return
+		}
+
+		fmt.Printf(`
+ID: %s
+Region: %s
+Vendor: %s
+Vendor Name: %s
+Location: %s
+
+Created: %s
+Updated: %s
+
+`,
+			d.ID.String(),
+			d.Region,
+			d.Vendor,
+			d.VendorName,
+			d.Location,
+			util.TimeStr(d.Created),
+			util.TimeStr(d.Updated),
+		)
+	}
+}
+
+func dcCreate(app *cli.Cmd) {
+	var (
+		regionOpt     = app.StringOpt("region", "", "Region identifier")
+		vendorOpt     = app.StringOpt("vendor", "", "Vendor")
+		vendorNameOpt = app.StringOpt("vendor-name", "", "Vendor Name")
+		locationOpt   = app.StringOpt("location", "", "Location")
+	)
+	app.Spec = "--region --vendor --location [OPTIONS]"
+
+	app.Action = func() {
+		d := conch.Datacenter{
+			Region:     *regionOpt,
+			Vendor:     *vendorOpt,
+			VendorName: *vendorNameOpt,
+			Location:   *locationOpt,
+		}
+
+		err := util.API.SaveDatacenter(&d)
+		if err != nil {
+			util.Bail(err)
+		}
+
+		if util.JSON {
+			util.JSONOut(d)
+			return
+		}
+
+		fmt.Printf(`
+ID: %s
+Region: %s
+Vendor: %s
+Vendor Name: %s
+Location: %s
+
+Created: %s
+Updated: %s
+
+`,
+			d.ID.String(),
+			d.Region,
+			d.Vendor,
+			d.VendorName,
+			d.Location,
+			util.TimeStr(d.Created),
+			util.TimeStr(d.Updated),
+		)
+
+	}
+}
+
+func dcDelete(app *cli.Cmd) {
+	app.Action = func() {
+		if err := util.API.DeleteDatacenter(GdcUUID); err != nil {
+			util.Bail(err)
+		}
+	}
+}
+
+func dcUpdate(app *cli.Cmd) {
+	var (
+		regionOpt     = app.StringOpt("region", "", "Region identifier")
+		vendorOpt     = app.StringOpt("vendor", "", "Vendor")
+		vendorNameOpt = app.StringOpt("vendor-name", "", "Vendor Name")
+		locationOpt   = app.StringOpt("location", "", "Location")
+	)
+
+	app.Action = func() {
+		d, err := util.API.GetDatacenter(GdcUUID)
+		if err != nil {
+			util.Bail(err)
+		}
+
+		if *regionOpt != "" {
+			d.Region = *regionOpt
+		}
+		if *vendorOpt != "" {
+			d.Vendor = *vendorOpt
+		}
+		if *vendorNameOpt != "" {
+			d.VendorName = *vendorNameOpt
+		}
+		if *locationOpt != "" {
+			d.Location = *locationOpt
+		}
+
+		if err := util.API.SaveDatacenter(&d); err != nil {
+			util.Bail(err)
+		}
+
+		if util.JSON {
+			util.JSONOut(d)
+			return
+		}
+
+		fmt.Printf(`
+ID: %s
+Region: %s
+Vendor: %s
+Vendor Name: %s
+Location: %s
+
+Created: %s
+Updated: %s
+
+`,
+			d.ID.String(),
+			d.Region,
+			d.Vendor,
+			d.VendorName,
+			d.Location,
+			util.TimeStr(d.Created),
+			util.TimeStr(d.Updated),
+		)
+
+	}
+
+}
+
+func dcGetAllRooms(app *cli.Cmd) {
+	app.Action = func() {
+		d, err := util.API.GetDatacenter(GdcUUID)
+		if err != nil {
+			util.Bail(err)
+		}
+
+		rs, err := util.API.GetDatacenterRooms(d)
+		if err != nil {
+			util.Bail(err)
+		}
+
+		if util.JSON {
+			util.JSONOut(rs)
+			return
+		}
+
+		table := util.GetMarkdownTable()
+		table.SetHeader([]string{
+			"ID",
+			"AZ",
+			"Alias",
+			"Vendor Name",
+		})
+
+		for _, r := range rs {
+			table.Append([]string{
+				r.ID.String(),
+				r.AZ,
+				r.Alias,
+				r.VendorName,
+			})
+		}
+		table.Render()
+	}
+}
+
+func dcAllTheThingsTree(app *cli.Cmd) {
+	app.Action = func() {
+		hwProds := make(map[string]conch.HardwareProduct)
+
+		d, err := util.API.GetDatacenter(GdcUUID)
+		if err != nil {
+			util.Bail(err)
+		}
+
+		tree := gotree.GTStructure{}
+		tree.Name = fmt.Sprintf("DC: %s (%s)", d.Region, d.ID)
+
+		rs, err := util.API.GetDatacenterRooms(d)
+		if err != nil {
+			util.Bail(err)
+		}
+
+		for _, room := range rs {
+			roomTree := gotree.GTStructure{}
+			roomTree.Name = fmt.Sprintf("Room: %s (%s)", room.AZ, room.ID)
+
+			racks, err := util.API.GetRoomRacks(room)
+			if err != nil {
+				util.Bail(err)
+			}
+
+			for _, rack := range racks {
+				rackTree := gotree.GTStructure{}
+				rackTree.Name = fmt.Sprintf("Rack: %s (%s)", rack.Name, rack.ID)
+
+				ls, err := util.API.GetRackLayout(rack)
+				if err != nil {
+					util.Bail(err)
+				}
+
+				sort.Sort(ls)
+				for _, layout := range ls {
+					var hw conch.HardwareProduct
+
+					hw, ok := hwProds[layout.ProductID.String()]
+					if !ok {
+						hw, err = util.API.GetHardwareProduct(layout.ProductID)
+						if err != nil {
+							util.Bail(err)
+						}
+						hwProds[layout.ProductID.String()] = hw
+					}
+
+					layoutTree := gotree.GTStructure{}
+					layoutTree.Name = fmt.Sprintf(
+						"RU: %d | Product: %s",
+						layout.RUStart,
+						hw.Name,
+					)
+					rackTree.Items = append(rackTree.Items, layoutTree)
+				}
+
+				roomTree.Items = append(roomTree.Items, rackTree)
+			}
+
+			tree.Items = append(tree.Items, roomTree)
+		}
+
+		gotree.PrintTree(tree)
+	}
+}