From 29b2eb40a0aa4931b44407bd52686ada2547c1ad Mon Sep 17 00:00:00 2001 From: Dennis Ahaus Date: Mon, 5 May 2025 08:12:25 +0200 Subject: [PATCH 1/5] Save work --- cmd/instance_table.go | 7 ++++++- director/vms.go | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/cmd/instance_table.go b/cmd/instance_table.go index 0d284a5fe..afc4ceae4 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 + Prefix 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"), + Prefix: boshtbl.NewValueString("Prefix"), Deployment: boshtbl.NewValueString("Deployment"), // Details @@ -105,6 +107,8 @@ func (t InstanceTable) Headers() []boshtbl.Header { func (t InstanceTable) ForVMInfo(i boshdir.VMInfo) InstanceTableValues { + fmt.Println(i.IPs) + var vmInfoIndex boshtbl.ValueInt if i.Index != nil { @@ -134,6 +138,7 @@ func (t InstanceTable) ForVMInfo(i boshdir.VMInfo) InstanceTableValues { VMType: boshtbl.NewValueString(i.VMType), Active: boshtbl.NewValueString(activeStatus), IPs: boshtbl.NewValueStrings(i.IPs), + Prefix: boshtbl.NewValueStrings(i.Prefix), Deployment: boshtbl.NewValueString(i.Deployment), // Details @@ -212,7 +217,7 @@ func (t InstanceTable) AsValues(v InstanceTableValues) []boshtbl.Value { result = append(result, v.Process) } - result = append(result, []boshtbl.Value{v.ProcessState, v.AZ, v.IPs}...) + result = append(result, []boshtbl.Value{v.ProcessState, v.AZ, v.IPs, v.Prefix}...) if t.DeploymentDetails { result = append(result, v.Deployment) diff --git a/director/vms.go b/director/vms.go index 6a0c6f307..3538a6667 100644 --- a/director/vms.go +++ b/director/vms.go @@ -22,6 +22,7 @@ type VMInfo struct { Bootstrap bool IPs []string `json:"ips"` + Prefix []string `json:"prefix"` Deployment string `json:"deployment_name"` AZ string `json:"az"` From 5854db3ff88c16b25cf6ec21eb0ea57d10934c4b Mon Sep 17 00:00:00 2001 From: Saumya Dudeja Date: Mon, 5 May 2025 17:07:43 +0530 Subject: [PATCH 2/5] add cidr flag on instances --- cmd/instance_table.go | 13 ++++++++++--- cmd/instances.go | 1 + cmd/opts/opts.go | 1 + director/vms.go | 1 + 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cmd/instance_table.go b/cmd/instance_table.go index afc4ceae4..269cfab58 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 Prefix boshtbl.Value Deployment boshtbl.Value @@ -59,6 +60,7 @@ var InstanceTableHeader = InstanceTableValues{ VMType: boshtbl.NewValueString("VM Type"), Active: boshtbl.NewValueString("Active"), IPs: boshtbl.NewValueString("IPs"), + IPs_cidr: boshtbl.NewValueString("IPs"), Prefix: boshtbl.NewValueString("Prefix"), Deployment: boshtbl.NewValueString("Deployment"), @@ -93,7 +95,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 { @@ -108,6 +110,7 @@ func (t InstanceTable) Headers() []boshtbl.Header { func (t InstanceTable) ForVMInfo(i boshdir.VMInfo) InstanceTableValues { fmt.Println(i.IPs) + fmt.Println(i.IPs_cidr) var vmInfoIndex boshtbl.ValueInt @@ -138,6 +141,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), Prefix: boshtbl.NewValueStrings(i.Prefix), Deployment: boshtbl.NewValueString(i.Deployment), @@ -216,8 +220,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, v.Prefix}...) + if t.Cidr { + result = append(result, []boshtbl.Value{v.ProcessState, v.AZ, v.IPs_cidr, v.Prefix}...) + } else { + result = append(result, []boshtbl.Value{v.ProcessState, v.AZ, v.IPs, v.Prefix}...) + } 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..159446895 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:"CIDR of the network to filter VMs by"` Deployment string cmd } diff --git a/director/vms.go b/director/vms.go index 3538a6667..470c2719b 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"` Prefix []string `json:"prefix"` Deployment string `json:"deployment_name"` From 17100432ace7fc1db22ae85faa643d5835b2b8ba Mon Sep 17 00:00:00 2001 From: Saumya Dudeja Date: Mon, 5 May 2025 17:11:24 +0530 Subject: [PATCH 3/5] remove all refernces of Prefix --- cmd/instance_table.go | 10 ++-------- director/vms.go | 1 - 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/cmd/instance_table.go b/cmd/instance_table.go index 269cfab58..2dd8e3c7d 100644 --- a/cmd/instance_table.go +++ b/cmd/instance_table.go @@ -19,7 +19,6 @@ type InstanceTableValues struct { Active boshtbl.Value IPs boshtbl.Value IPs_cidr boshtbl.Value - Prefix boshtbl.Value Deployment boshtbl.Value // Details @@ -61,7 +60,6 @@ var InstanceTableHeader = InstanceTableValues{ Active: boshtbl.NewValueString("Active"), IPs: boshtbl.NewValueString("IPs"), IPs_cidr: boshtbl.NewValueString("IPs"), - Prefix: boshtbl.NewValueString("Prefix"), Deployment: boshtbl.NewValueString("Deployment"), // Details @@ -109,9 +107,6 @@ func (t InstanceTable) Headers() []boshtbl.Header { func (t InstanceTable) ForVMInfo(i boshdir.VMInfo) InstanceTableValues { - fmt.Println(i.IPs) - fmt.Println(i.IPs_cidr) - var vmInfoIndex boshtbl.ValueInt if i.Index != nil { @@ -142,7 +137,6 @@ func (t InstanceTable) ForVMInfo(i boshdir.VMInfo) InstanceTableValues { Active: boshtbl.NewValueString(activeStatus), IPs: boshtbl.NewValueStrings(i.IPs), IPs_cidr: boshtbl.NewValueStrings(i.IPs_cidr), - Prefix: boshtbl.NewValueStrings(i.Prefix), Deployment: boshtbl.NewValueString(i.Deployment), // Details @@ -221,9 +215,9 @@ func (t InstanceTable) AsValues(v InstanceTableValues) []boshtbl.Value { result = append(result, v.Process) } if t.Cidr { - result = append(result, []boshtbl.Value{v.ProcessState, v.AZ, v.IPs_cidr, v.Prefix}...) + result = append(result, []boshtbl.Value{v.ProcessState, v.AZ, v.IPs_cidr}...) } else { - result = append(result, []boshtbl.Value{v.ProcessState, v.AZ, v.IPs, v.Prefix}...) + result = append(result, []boshtbl.Value{v.ProcessState, v.AZ, v.IPs}...) } if t.DeploymentDetails { diff --git a/director/vms.go b/director/vms.go index 470c2719b..b90d923ae 100644 --- a/director/vms.go +++ b/director/vms.go @@ -23,7 +23,6 @@ type VMInfo struct { IPs []string `json:"ips"` IPs_cidr []string `json:"ips_cidr"` - Prefix []string `json:"prefix"` Deployment string `json:"deployment_name"` AZ string `json:"az"` From 6c40396213eaecd713484872ff70ba1ec0eeb8e0 Mon Sep 17 00:00:00 2001 From: Saumya Dudeja Date: Mon, 5 May 2025 18:51:51 +0530 Subject: [PATCH 4/5] allow orphaned-vms to have cidr flag and notation --- cmd/cmd.go | 2 +- cmd/opts/opts.go | 4 +++- cmd/orphaned_vms.go | 16 ++++++++++++---- cmd/vms.go | 1 + director/director.go | 26 ++++++++++++++------------ director/interfaces.go | 13 +++++++------ 6 files changed, 38 insertions(+), 24 deletions(-) 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/opts/opts.go b/cmd/opts/opts.go index 159446895..b5c6d64e5 100644 --- a/cmd/opts/opts.go +++ b/cmd/opts/opts.go @@ -807,7 +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:"CIDR of the network to filter VMs by"` + Cidr bool `long:"cidr" description:"show the CIDR notation of the network IP Addresses"` Deployment string cmd } @@ -816,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 } @@ -845,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/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 { From 81eec7420e4937393530f4550395f470844c788e Mon Sep 17 00:00:00 2001 From: Dennis Ahaus Date: Mon, 12 May 2025 11:59:22 +0200 Subject: [PATCH 5/5] Fix tests --- cmd/orphaned_vms_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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")) })