diff --git a/cmd/cmd.go b/cmd/cmd.go index e39acacad..967ba6193 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -323,7 +323,7 @@ func (c Cmd) Execute() (cmdErr error) { return NewVMsCmd(deps.UI, c.director(), c.BoshOpts.Parallel).Run(*opts) case *OrphanedVMsOpts: - return NewOrphanedVMsCmd(deps.UI, c.director()).Run() + return NewOrphanedVMsCmd(deps.UI, c.director()).Run(*opts) case *InstancesOpts: return NewInstancesCmd(deps.UI, c.director(), c.BoshOpts.Parallel).Run(*opts) diff --git a/cmd/instance_table.go b/cmd/instance_table.go index 0d284a5fe..2dd8e3c7d 100644 --- a/cmd/instance_table.go +++ b/cmd/instance_table.go @@ -18,6 +18,7 @@ type InstanceTableValues struct { VMType boshtbl.Value Active boshtbl.Value IPs boshtbl.Value + IPs_cidr boshtbl.Value Deployment boshtbl.Value // Details @@ -58,6 +59,7 @@ var InstanceTableHeader = InstanceTableValues{ VMType: boshtbl.NewValueString("VM Type"), Active: boshtbl.NewValueString("Active"), IPs: boshtbl.NewValueString("IPs"), + IPs_cidr: boshtbl.NewValueString("IPs"), Deployment: boshtbl.NewValueString("Deployment"), // Details @@ -91,7 +93,7 @@ var InstanceTableHeader = InstanceTableValues{ } type InstanceTable struct { - Processes, VMDetails, DeploymentDetails, Details, Stemcell, Vitals, CloudProperties bool + Processes, VMDetails, DeploymentDetails, Details, Stemcell, Vitals, CloudProperties, Cidr bool } func (t InstanceTable) Headers() []boshtbl.Header { @@ -134,6 +136,7 @@ func (t InstanceTable) ForVMInfo(i boshdir.VMInfo) InstanceTableValues { VMType: boshtbl.NewValueString(i.VMType), Active: boshtbl.NewValueString(activeStatus), IPs: boshtbl.NewValueStrings(i.IPs), + IPs_cidr: boshtbl.NewValueStrings(i.IPs_cidr), Deployment: boshtbl.NewValueString(i.Deployment), // Details @@ -211,8 +214,11 @@ func (t InstanceTable) AsValues(v InstanceTableValues) []boshtbl.Value { if t.Processes { result = append(result, v.Process) } - - result = append(result, []boshtbl.Value{v.ProcessState, v.AZ, v.IPs}...) + if t.Cidr { + result = append(result, []boshtbl.Value{v.ProcessState, v.AZ, v.IPs_cidr}...) + } else { + result = append(result, []boshtbl.Value{v.ProcessState, v.AZ, v.IPs}...) + } if t.DeploymentDetails { result = append(result, v.Deployment) diff --git a/cmd/instances.go b/cmd/instances.go index a35e67344..57f1e1e18 100644 --- a/cmd/instances.go +++ b/cmd/instances.go @@ -28,6 +28,7 @@ func (c InstancesCmd) Run(opts InstancesOpts) error { Processes: opts.Processes, Details: opts.Details, Vitals: opts.Vitals, + Cidr: opts.Cidr, } if len(opts.Deployment) > 0 { diff --git a/cmd/opts/opts.go b/cmd/opts/opts.go index 44b9e6d3f..b5c6d64e5 100644 --- a/cmd/opts/opts.go +++ b/cmd/opts/opts.go @@ -807,6 +807,7 @@ type InstancesOpts struct { Vitals bool `long:"vitals" description:"Show vitals"` Processes bool `long:"ps" short:"p" description:"Show processes"` Failing bool `long:"failing" short:"f" description:"Only show failing instances"` + Cidr bool `long:"cidr" description:"show the CIDR notation of the network IP Addresses"` Deployment string cmd } @@ -815,6 +816,7 @@ type VMsOpts struct { Vitals bool `long:"vitals" description:"Show vitals"` CloudProperties bool `long:"cloud-properties" description:"Show cloud properties"` Deployment string + Cidr bool `long:"cidr" description:"show the CIDR notation of the network IP Addresses"` cmd } @@ -844,6 +846,7 @@ type RecoverArgs struct { } type OrphanedVMsOpts struct { + Cidr bool `long:"cidr" description:"show the CIDR notation of the network IP Addresses"` cmd } diff --git a/cmd/orphaned_vms.go b/cmd/orphaned_vms.go index f9fbfc518..bb0d54924 100644 --- a/cmd/orphaned_vms.go +++ b/cmd/orphaned_vms.go @@ -1,6 +1,7 @@ package cmd import ( + . "github.com/cloudfoundry/bosh-cli/v7/cmd/opts" //nolint:staticcheck "github.com/cloudfoundry/bosh-cli/v7/director" "github.com/cloudfoundry/bosh-cli/v7/ui" "github.com/cloudfoundry/bosh-cli/v7/ui/table" @@ -15,18 +16,25 @@ func NewOrphanedVMsCmd(ui ui.UI, director director.Director) OrphanedVMsCmd { return OrphanedVMsCmd{ui: ui, director: director} } -func (c OrphanedVMsCmd) Run() error { +func (c OrphanedVMsCmd) Run(opts OrphanedVMsOpts) error { orphanedVMs, err := c.director.OrphanedVMs() if err != nil { return err } - printOrphanedVmTable(c.ui, orphanedVMs) + printOrphanedVmTable(c.ui, orphanedVMs, opts) return nil } -func printOrphanedVmTable(ui ui.UI, orphanedVMs []director.OrphanedVM) { +func formatIPAddress(orphanedVM director.OrphanedVM, opts OrphanedVMsOpts) []string { + if opts.Cidr { + return orphanedVM.IPAddressesCidr + } + return orphanedVM.IPAddresses +} + +func printOrphanedVmTable(ui ui.UI, orphanedVMs []director.OrphanedVM, opts OrphanedVMsOpts) { tbl := table.Table{ Content: "orphaned_vms", Header: []table.Header{ @@ -46,7 +54,7 @@ func printOrphanedVmTable(ui ui.UI, orphanedVMs []director.OrphanedVM) { table.NewValueString(vm.DeploymentName), table.NewValueString(vm.InstanceName), table.NewValueString(vm.AZName), - table.NewValueStrings(vm.IPAddresses), + table.NewValueStrings(formatIPAddress(vm, opts)), table.NewValueTime(vm.OrphanedAt), }) } diff --git a/cmd/orphaned_vms_test.go b/cmd/orphaned_vms_test.go index d649728c6..4aa7001a2 100644 --- a/cmd/orphaned_vms_test.go +++ b/cmd/orphaned_vms_test.go @@ -8,6 +8,7 @@ import ( . "github.com/onsi/gomega" "github.com/cloudfoundry/bosh-cli/v7/cmd" + "github.com/cloudfoundry/bosh-cli/v7/cmd/opts" boshdir "github.com/cloudfoundry/bosh-cli/v7/director" fakedir "github.com/cloudfoundry/bosh-cli/v7/director/directorfakes" fakeui "github.com/cloudfoundry/bosh-cli/v7/ui/fakes" @@ -57,7 +58,7 @@ var _ = Describe("OrphanedVMsCmd", func() { }) It("lists VMs for the deployment", func() { - Expect(command.Run()).ToNot(HaveOccurred()) + Expect(command.Run(opts.OrphanedVMsOpts{})).ToNot(HaveOccurred()) Expect(ui.Table).To(Equal(boshtbl.Table{ Content: "orphaned_vms", @@ -101,7 +102,7 @@ var _ = Describe("OrphanedVMsCmd", func() { }) It("returns an error", func() { - err := command.Run() + err := command.Run(opts.OrphanedVMsOpts{}) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("potato")) }) diff --git a/cmd/vms.go b/cmd/vms.go index 79f3dbf2e..b04027c5e 100644 --- a/cmd/vms.go +++ b/cmd/vms.go @@ -34,6 +34,7 @@ func (c VMsCmd) Run(opts VMsOpts) error { DeploymentDetails: false, Details: false, Stemcell: true, + Cidr: opts.Cidr, Vitals: opts.Vitals, CloudProperties: opts.CloudProperties, } diff --git a/director/director.go b/director/director.go index 1dc9457cc..0f1b4f311 100644 --- a/director/director.go +++ b/director/director.go @@ -14,12 +14,13 @@ type DirectorImpl struct { } type OrphanedVMResponse struct { - AZName string `json:"az"` - CID string `json:"cid"` - DeploymentName string `json:"deployment_name"` - IPAddresses []string `json:"ip_addresses"` - InstanceName string `json:"instance_name"` - OrphanedAt string `json:"orphaned_at"` + AZName string `json:"az"` + CID string `json:"cid"` + DeploymentName string `json:"deployment_name"` + IPAddresses []string `json:"ip_addresses"` + IPAddressesCidr []string `json:"ip_addresses_cidr"` + InstanceName string `json:"instance_name"` + OrphanedAt string `json:"orphaned_at"` } func (d DirectorImpl) WithContext(id string) Director { @@ -47,12 +48,13 @@ func transformOrphanedVMs(resps []OrphanedVMResponse) ([]OrphanedVM, error) { } orphanedVMs = append(orphanedVMs, OrphanedVM{ - CID: r.CID, - DeploymentName: r.DeploymentName, - InstanceName: r.InstanceName, - AZName: r.AZName, - IPAddresses: r.IPAddresses, - OrphanedAt: orphanedAt, + CID: r.CID, + DeploymentName: r.DeploymentName, + InstanceName: r.InstanceName, + AZName: r.AZName, + IPAddresses: r.IPAddresses, + IPAddressesCidr: r.IPAddressesCidr, + OrphanedAt: orphanedAt, }) } return orphanedVMs, nil diff --git a/director/interfaces.go b/director/interfaces.go index e0445105c..a77f3e394 100644 --- a/director/interfaces.go +++ b/director/interfaces.go @@ -330,12 +330,13 @@ type OrphanNetwork interface { } type OrphanedVM struct { - CID string - DeploymentName string - InstanceName string - AZName string - IPAddresses []string - OrphanedAt time.Time + CID string + DeploymentName string + InstanceName string + AZName string + IPAddresses []string + IPAddressesCidr []string + OrphanedAt time.Time } type EventsFilter struct { diff --git a/director/vms.go b/director/vms.go index 6a0c6f307..b90d923ae 100644 --- a/director/vms.go +++ b/director/vms.go @@ -22,6 +22,7 @@ type VMInfo struct { Bootstrap bool IPs []string `json:"ips"` + IPs_cidr []string `json:"ips_cidr"` Deployment string `json:"deployment_name"` AZ string `json:"az"`