diff --git a/rhoc/pkg/cmd/connectors/get/get.go b/rhoc/pkg/cmd/connectors/get/get.go index 9c68516..b2a5372 100644 --- a/rhoc/pkg/cmd/connectors/get/get.go +++ b/rhoc/pkg/cmd/connectors/get/get.go @@ -1,9 +1,9 @@ package get import ( + "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/api/admin" "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/service" "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/cmdutil" - "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/response" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" "github.com/spf13/cobra" @@ -20,6 +20,12 @@ type options struct { f *factory.Factory } +type connectorDetail struct { + admin.ConnectorAdminView `json:",inline" yaml:",inline"` + ClusterID string `json:"cluster_id,omitempty" yaml:"cluster_id,omitempty"` + PlatformID string `json:"platform_id,omitempty" yaml:"platform_id,omitempty"` +} + func NewGetCommand(f *factory.Factory) *cobra.Command { opts := options{ f: f, @@ -54,15 +60,21 @@ func run(opts *options) error { return err } - result, httpRes, err := c.Clusters().GetConnector(opts.f.Context, opts.id).Execute() - if httpRes != nil { - defer func() { - _ = httpRes.Body.Close() - }() + connector, err := service.GetConnectorByID(c, opts.id) + if err != nil { + return err } + + cluster, err := service.GetClusterForNamespace(c, connector.NamespaceId) if err != nil { - return response.Error(err, httpRes) + return err + } + + detail := connectorDetail{ + ConnectorAdminView: *connector, + ClusterID: cluster.Id, + PlatformID: cluster.Status.Platform.Id, } - return dump.Formatted(opts.f.IOStreams.Out, opts.outputFormat, result) + return dump.Formatted(opts.f.IOStreams.Out, opts.outputFormat, detail) } diff --git a/rhoc/pkg/cmd/connectors/list/list.go b/rhoc/pkg/cmd/connectors/list/list.go index f113bd6..d60266a 100644 --- a/rhoc/pkg/cmd/connectors/list/list.go +++ b/rhoc/pkg/cmd/connectors/list/list.go @@ -3,7 +3,6 @@ package list import ( "errors" "fmt" - "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/api/admin" "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/service" "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/cmdutil" @@ -29,6 +28,12 @@ type options struct { f *factory.Factory } +type connectorDetail struct { + admin.ConnectorAdminView `json:",inline" yaml:",inline"` + ClusterID string `json:"cluster_id,omitempty" yaml:"cluster_id,omitempty"` + PlatformID string `json:"platform_id,omitempty" yaml:"platform_id,omitempty"` +} + func NewListCommand(f *factory.Factory) *cobra.Command { opts := options{ @@ -82,24 +87,40 @@ func run(opts *options) error { return err } - var items admin.ConnectorAdminViewList + var connectors admin.ConnectorAdminViewList + var cluster *admin.ConnectorClusterAdminView switch { case opts.clusterID != "": - items, err = service.ListConnectorsForCluster(c, opts.ListOptions, opts.clusterID) + connectors, err = service.ListConnectorsForCluster(c, opts.ListOptions, opts.clusterID) + if err == nil { + cluster, err = service.GetClusterByID(c, opts.clusterID) + } case opts.namespaceID != "": - items, err = service.ListConnectorsForNamespace(c, opts.ListOptions, opts.namespaceID) + connectors, err = service.ListConnectorsForNamespace(c, opts.ListOptions, opts.namespaceID) + if err == nil { + cluster, err = service.GetClusterForNamespace(c, opts.namespaceID) + } } if err != nil { return err } - if len(items.Items) == 0 && opts.outputFormat == "" { + if len(connectors.Items) == 0 && opts.outputFormat == "" { _, _ = fmt.Fprint(opts.f.IOStreams.Out, "No result\n") return nil } + items := make([]connectorDetail, len(connectors.Items)) + for i := range connectors.Items { + items[i] = connectorDetail{ + ConnectorAdminView: connectors.Items[i], + ClusterID: cluster.Id, + PlatformID: cluster.Status.Platform.Id, + } + } + switch opts.outputFormat { case dump.EmptyFormat: _, _ = fmt.Fprint(opts.f.IOStreams.Out, "\n") diff --git a/rhoc/pkg/cmd/connectors/list/list_dumper.go b/rhoc/pkg/cmd/connectors/list/list_dumper.go index 1d8a158..e680b83 100644 --- a/rhoc/pkg/cmd/connectors/list/list_dumper.go +++ b/rhoc/pkg/cmd/connectors/list/list_dumper.go @@ -5,21 +5,20 @@ import ( "strconv" "time" - "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/api/admin" "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/dumper" "github.com/olekukonko/tablewriter" "k8s.io/apimachinery/pkg/util/duration" ) -func dumpAsTable(out io.Writer, items admin.ConnectorAdminViewList, wide bool, style dumper.TableStyle) { - config := dumper.TableConfig[admin.ConnectorAdminView]{ +func dumpAsTable(out io.Writer, items []connectorDetail, wide bool, style dumper.TableStyle) { + config := dumper.TableConfig[connectorDetail]{ Style: style, Wide: wide, - Columns: []dumper.Column[admin.ConnectorAdminView]{ + Columns: []dumper.Column[connectorDetail]{ { Name: "ID", Wide: false, - Getter: func(in *admin.ConnectorAdminView) dumper.Row { + Getter: func(in *connectorDetail) dumper.Row { return dumper.Row{ Value: in.Id, } @@ -28,16 +27,34 @@ func dumpAsTable(out io.Writer, items admin.ConnectorAdminViewList, wide bool, s { Name: "NamespaceId", Wide: false, - Getter: func(in *admin.ConnectorAdminView) dumper.Row { + Getter: func(in *connectorDetail) dumper.Row { return dumper.Row{ Value: in.NamespaceId, } }, }, + { + Name: "ClusterId", + Wide: true, + Getter: func(in *connectorDetail) dumper.Row { + return dumper.Row{ + Value: in.ClusterID, + } + }, + }, + { + Name: "PlatformId", + Wide: true, + Getter: func(in *connectorDetail) dumper.Row { + return dumper.Row{ + Value: in.PlatformID, + } + }, + }, { Name: "Name", Wide: true, - Getter: func(in *admin.ConnectorAdminView) dumper.Row { + Getter: func(in *connectorDetail) dumper.Row { return dumper.Row{ Value: in.Name, } @@ -46,7 +63,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorAdminViewList, wide bool, s { Name: "Owner", Wide: false, - Getter: func(in *admin.ConnectorAdminView) dumper.Row { + Getter: func(in *connectorDetail) dumper.Row { return dumper.Row{ Value: in.Owner, } @@ -55,7 +72,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorAdminViewList, wide bool, s { Name: "ConnectorTypeId", Wide: false, - Getter: func(in *admin.ConnectorAdminView) dumper.Row { + Getter: func(in *connectorDetail) dumper.Row { return dumper.Row{ Value: in.ConnectorTypeId, } @@ -64,7 +81,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorAdminViewList, wide bool, s { Name: "DesiredState", Wide: false, - Getter: func(in *admin.ConnectorAdminView) dumper.Row { + Getter: func(in *connectorDetail) dumper.Row { return dumper.Row{ Value: string(in.DesiredState), } @@ -73,7 +90,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorAdminViewList, wide bool, s { Name: "State", Wide: false, - Getter: func(in *admin.ConnectorAdminView) dumper.Row { + Getter: func(in *connectorDetail) dumper.Row { r := dumper.Row{ Value: string(in.Status.State), } @@ -93,7 +110,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorAdminViewList, wide bool, s { Name: "Reason", Wide: true, - Getter: func(in *admin.ConnectorAdminView) dumper.Row { + Getter: func(in *connectorDetail) dumper.Row { return dumper.Row{ Value: in.Status.Error, } @@ -102,7 +119,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorAdminViewList, wide bool, s { Name: "Version", Wide: false, - Getter: func(in *admin.ConnectorAdminView) dumper.Row { + Getter: func(in *connectorDetail) dumper.Row { return dumper.Row{ Value: strconv.FormatInt(in.ResourceVersion, 10), } @@ -111,7 +128,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorAdminViewList, wide bool, s { Name: "Age", Wide: false, - Getter: func(in *admin.ConnectorAdminView) dumper.Row { + Getter: func(in *connectorDetail) dumper.Row { age := duration.HumanDuration(time.Since(in.CreatedAt)) if in.CreatedAt.IsZero() { age = "" @@ -125,7 +142,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorAdminViewList, wide bool, s { Name: "CreatedAt", Wide: true, - Getter: func(in *admin.ConnectorAdminView) dumper.Row { + Getter: func(in *connectorDetail) dumper.Row { return dumper.Row{ Value: in.CreatedAt.Format(time.RFC3339), } @@ -134,7 +151,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorAdminViewList, wide bool, s { Name: "ModifiedAt", Wide: true, - Getter: func(in *admin.ConnectorAdminView) dumper.Row { + Getter: func(in *connectorDetail) dumper.Row { return dumper.Row{ Value: in.ModifiedAt.Format(time.RFC3339), } @@ -143,5 +160,5 @@ func dumpAsTable(out io.Writer, items admin.ConnectorAdminViewList, wide bool, s }, } - dumper.DumpTable(config, out, items.Items) + dumper.DumpTable(config, out, items) } diff --git a/rhoc/pkg/cmd/deployments/get/get.go b/rhoc/pkg/cmd/deployments/get/get.go index 80eae22..2b5dc14 100644 --- a/rhoc/pkg/cmd/deployments/get/get.go +++ b/rhoc/pkg/cmd/deployments/get/get.go @@ -1,9 +1,9 @@ package get import ( + "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/api/admin" "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/service" "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/cmdutil" - "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/response" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" "github.com/spf13/cobra" @@ -21,6 +21,11 @@ type options struct { f *factory.Factory } +type deploymentDetail struct { + admin.ConnectorDeploymentAdminView `json:",inline" yaml:",inline"` + PlatformID string `json:"platform_id,omitempty" yaml:"platform_id,omitempty"` +} + func NewGetCommand(f *factory.Factory) *cobra.Command { opts := options{ f: f, @@ -56,15 +61,20 @@ func run(opts *options) error { return err } - result, httpRes, err := c.Clusters().GetConnectorDeployment(opts.f.Context, opts.clusterID, opts.id).Execute() - if httpRes != nil { - defer func() { - _ = httpRes.Body.Close() - }() + deployment, err := service.GetDeploymentByID(c, opts.clusterID, opts.id) + if err != nil { + return err } + + cluster, err := service.GetClusterByID(c, deployment.Spec.ClusterId) if err != nil { - return response.Error(err, httpRes) + return err + } + + detail := deploymentDetail{ + ConnectorDeploymentAdminView: *deployment, + PlatformID: cluster.Status.Platform.Id, } - return dump.Formatted(opts.f.IOStreams.Out, opts.outputFormat, result) + return dump.Formatted(opts.f.IOStreams.Out, opts.outputFormat, detail) } diff --git a/rhoc/pkg/cmd/deployments/list/list.go b/rhoc/pkg/cmd/deployments/list/list.go index ba34ea5..f124b9c 100644 --- a/rhoc/pkg/cmd/deployments/list/list.go +++ b/rhoc/pkg/cmd/deployments/list/list.go @@ -29,6 +29,11 @@ type options struct { f *factory.Factory } +type deploymentDetail struct { + admin.ConnectorDeploymentAdminView `json:",inline" yaml:",inline"` + PlatformID string `json:"platform_id,omitempty" yaml:"platform_id,omitempty"` +} + func NewListCommand(f *factory.Factory) *cobra.Command { opts := options{ f: f, @@ -80,24 +85,39 @@ func run(opts *options) error { return err } - var items admin.ConnectorDeploymentAdminViewList + var deployments admin.ConnectorDeploymentAdminViewList + var cluster *admin.ConnectorClusterAdminView switch { case opts.clusterID != "": - items, err = service.ListDeploymentsForCluster(c, opts.ListDeploymentsOptions, opts.clusterID) + deployments, err = service.ListDeploymentsForCluster(c, opts.ListDeploymentsOptions, opts.clusterID) + if err == nil { + cluster, err = service.GetClusterByID(c, opts.clusterID) + } case opts.namespaceID != "": - items, err = service.ListDeploymentsForNamespace(c, opts.ListOptions, opts.namespaceID) + deployments, err = service.ListDeploymentsForNamespace(c, opts.ListOptions, opts.namespaceID) + if err == nil { + cluster, err = service.GetClusterForNamespace(c, opts.namespaceID) + } } if err != nil { return err } - if len(items.Items) == 0 && opts.outputFormat == "" { + if len(deployments.Items) == 0 && opts.outputFormat == "" { _, _ = fmt.Fprint(opts.f.IOStreams.Out, "No result\n") return nil } + items := make([]deploymentDetail, len(deployments.Items)) + for i := range deployments.Items { + items[i] = deploymentDetail{ + ConnectorDeploymentAdminView: deployments.Items[i], + PlatformID: cluster.Status.Platform.Id, + } + } + switch opts.outputFormat { case dump.EmptyFormat: _, _ = fmt.Fprint(opts.f.IOStreams.Out, "\n") diff --git a/rhoc/pkg/cmd/deployments/list/list_dumper.go b/rhoc/pkg/cmd/deployments/list/list_dumper.go index a244e6e..7c02ca8 100644 --- a/rhoc/pkg/cmd/deployments/list/list_dumper.go +++ b/rhoc/pkg/cmd/deployments/list/list_dumper.go @@ -5,22 +5,21 @@ import ( "strconv" "time" - "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/api/admin" "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/dumper" "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/request" "github.com/olekukonko/tablewriter" "k8s.io/apimachinery/pkg/util/duration" ) -func dumpAsTable(out io.Writer, items admin.ConnectorDeploymentAdminViewList, options request.ListDeploymentsOptions, wide bool, style dumper.TableStyle) { - config := dumper.TableConfig[admin.ConnectorDeploymentAdminView]{ +func dumpAsTable(out io.Writer, items []deploymentDetail, options request.ListDeploymentsOptions, wide bool, style dumper.TableStyle) { + config := dumper.TableConfig[deploymentDetail]{ Style: style, Wide: wide, - Columns: []dumper.Column[admin.ConnectorDeploymentAdminView]{ + Columns: []dumper.Column[deploymentDetail]{ { Name: "ID", Wide: false, - Getter: func(in *admin.ConnectorDeploymentAdminView) dumper.Row { + Getter: func(in *deploymentDetail) dumper.Row { return dumper.Row{ Value: in.Id, } @@ -29,7 +28,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorDeploymentAdminViewList, op { Name: "ConnectorID", Wide: false, - Getter: func(in *admin.ConnectorDeploymentAdminView) dumper.Row { + Getter: func(in *deploymentDetail) dumper.Row { return dumper.Row{ Value: in.Spec.ConnectorId, } @@ -38,7 +37,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorDeploymentAdminViewList, op { Name: "NamespaceID", Wide: false, - Getter: func(in *admin.ConnectorDeploymentAdminView) dumper.Row { + Getter: func(in *deploymentDetail) dumper.Row { return dumper.Row{ Value: in.Spec.NamespaceId, } @@ -47,16 +46,25 @@ func dumpAsTable(out io.Writer, items admin.ConnectorDeploymentAdminViewList, op { Name: "ClusterID", Wide: false, - Getter: func(in *admin.ConnectorDeploymentAdminView) dumper.Row { + Getter: func(in *deploymentDetail) dumper.Row { return dumper.Row{ Value: in.Spec.ClusterId, } }, }, + { + Name: "PlatformId", + Wide: true, + Getter: func(in *deploymentDetail) dumper.Row { + return dumper.Row{ + Value: in.PlatformID, + } + }, + }, { Name: "Type", Wide: true, - Getter: func(in *admin.ConnectorDeploymentAdminView) dumper.Row { + Getter: func(in *deploymentDetail) dumper.Row { return dumper.Row{ Value: in.Spec.ConnectorTypeId, } @@ -65,7 +73,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorDeploymentAdminViewList, op { Name: "TypeRevision", Wide: !options.ChannelUpdate, - Getter: func(in *admin.ConnectorDeploymentAdminView) dumper.Row { + Getter: func(in *deploymentDetail) dumper.Row { if typeRevision, ok := in.Spec.ShardMetadata["connector_revision"]; ok { floatRevision, isfloat64 := typeRevision.(float64) if isfloat64 { @@ -86,7 +94,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorDeploymentAdminViewList, op { Name: "UpdatableTypeRevision", Wide: !options.ChannelUpdate, - Getter: func(in *admin.ConnectorDeploymentAdminView) dumper.Row { + Getter: func(in *deploymentDetail) dumper.Row { var updatableTypeRevision string if in.Status.ShardMetadata.Available.Revision == 0 { updatableTypeRevision = "-" @@ -101,7 +109,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorDeploymentAdminViewList, op { Name: "TypeImage", Wide: true, - Getter: func(in *admin.ConnectorDeploymentAdminView) dumper.Row { + Getter: func(in *deploymentDetail) dumper.Row { if image, ok := in.Spec.ShardMetadata["connector_image"]; ok { return dumper.Row{ Value: image.(string), @@ -119,7 +127,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorDeploymentAdminViewList, op { Name: "DesiredState", Wide: false, - Getter: func(in *admin.ConnectorDeploymentAdminView) dumper.Row { + Getter: func(in *deploymentDetail) dumper.Row { return dumper.Row{ Value: string(in.Spec.DesiredState), } @@ -128,7 +136,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorDeploymentAdminViewList, op { Name: "State", Wide: false, - Getter: func(in *admin.ConnectorDeploymentAdminView) dumper.Row { + Getter: func(in *deploymentDetail) dumper.Row { r := dumper.Row{ Value: string(in.Status.Phase), } @@ -148,7 +156,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorDeploymentAdminViewList, op { Name: "Version", Wide: true, - Getter: func(in *admin.ConnectorDeploymentAdminView) dumper.Row { + Getter: func(in *deploymentDetail) dumper.Row { return dumper.Row{ Value: strconv.FormatInt(in.Metadata.ResourceVersion, 10), } @@ -157,7 +165,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorDeploymentAdminViewList, op { Name: "DeploymentVersion", Wide: true, - Getter: func(in *admin.ConnectorDeploymentAdminView) dumper.Row { + Getter: func(in *deploymentDetail) dumper.Row { r := dumper.Row{ Value: strconv.FormatInt(in.Status.ResourceVersion, 10), } @@ -178,7 +186,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorDeploymentAdminViewList, op { Name: "Age", Wide: false, - Getter: func(in *admin.ConnectorDeploymentAdminView) dumper.Row { + Getter: func(in *deploymentDetail) dumper.Row { age := duration.HumanDuration(time.Since(in.Metadata.CreatedAt)) if in.Metadata.CreatedAt.IsZero() { age = "" @@ -192,7 +200,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorDeploymentAdminViewList, op { Name: "CreatedAt", Wide: true, - Getter: func(in *admin.ConnectorDeploymentAdminView) dumper.Row { + Getter: func(in *deploymentDetail) dumper.Row { return dumper.Row{ Value: in.Metadata.CreatedAt.Format(time.RFC3339), } @@ -201,7 +209,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorDeploymentAdminViewList, op { Name: "ModifiedAt", Wide: true, - Getter: func(in *admin.ConnectorDeploymentAdminView) dumper.Row { + Getter: func(in *deploymentDetail) dumper.Row { return dumper.Row{ Value: in.Metadata.UpdatedAt.Format(time.RFC3339), } @@ -210,5 +218,5 @@ func dumpAsTable(out io.Writer, items admin.ConnectorDeploymentAdminViewList, op }, } - dumper.DumpTable(config, out, items.Items) + dumper.DumpTable(config, out, items) } diff --git a/rhoc/pkg/cmd/namespaces/get/get.go b/rhoc/pkg/cmd/namespaces/get/get.go index 027b564..68d9abc 100644 --- a/rhoc/pkg/cmd/namespaces/get/get.go +++ b/rhoc/pkg/cmd/namespaces/get/get.go @@ -1,9 +1,9 @@ package get import ( + "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/api/admin" "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/service" "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/cmdutil" - "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/response" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump" "github.com/redhat-developer/app-services-cli/pkg/shared/factory" "github.com/spf13/cobra" @@ -20,6 +20,11 @@ type options struct { f *factory.Factory } +type namespaceDetail struct { + admin.ConnectorNamespace `json:",inline" yaml:",inline"` + PlatformID string `json:"platform_id,omitempty" yaml:"platform_id,omitempty"` +} + func NewGetCommand(f *factory.Factory) *cobra.Command { opts := options{ f: f, @@ -54,15 +59,20 @@ func run(opts *options) error { return err } - result, httpRes, err := c.Clusters().GetConnectorNamespace(opts.f.Context, opts.id).Execute() - if httpRes != nil { - defer func() { - _ = httpRes.Body.Close() - }() + namespace, err := service.GetNamespaceByID(c, opts.id) + if err != nil { + return err } + + cluster, err := service.GetClusterByID(c, namespace.ClusterId) if err != nil { - return response.Error(err, httpRes) + return err + } + + detail := namespaceDetail{ + ConnectorNamespace: *namespace, + PlatformID: cluster.Status.Platform.Id, } - return dump.Formatted(opts.f.IOStreams.Out, opts.outputFormat, result) + return dump.Formatted(opts.f.IOStreams.Out, opts.outputFormat, detail) } diff --git a/rhoc/pkg/cmd/namespaces/list/list.go b/rhoc/pkg/cmd/namespaces/list/list.go index 193c09d..6d247ca 100644 --- a/rhoc/pkg/cmd/namespaces/list/list.go +++ b/rhoc/pkg/cmd/namespaces/list/list.go @@ -26,6 +26,11 @@ type options struct { f *factory.Factory } +type namespaceDetail struct { + admin.ConnectorNamespace `json:",inline" yaml:",inline"` + PlatformID string `json:"platform_id,omitempty" yaml:"platform_id,omitempty"` +} + func NewListCommand(f *factory.Factory) *cobra.Command { opts := options{ f: f, @@ -66,24 +71,44 @@ func run(opts *options) error { return err } - var items admin.ConnectorNamespaceList + var namespaces admin.ConnectorNamespaceList switch { case opts.clusterID != "": - items, err = service.ListNamespacesForCluster(c, opts.ListOptions, opts.clusterID) + namespaces, err = service.ListNamespacesForCluster(c, opts.ListOptions, opts.clusterID) default: - items, err = service.ListNamespaces(c, opts.ListOptions) + namespaces, err = service.ListNamespaces(c, opts.ListOptions) } if err != nil { return err } - if len(items.Items) == 0 && opts.outputFormat == "" { + if len(namespaces.Items) == 0 && opts.outputFormat == "" { _, _ = fmt.Fprint(opts.f.IOStreams.Out, "No result\n") return nil } + clusters := make(map[string]*admin.ConnectorClusterAdminView) + + items := make([]namespaceDetail, len(namespaces.Items)) + for i := range namespaces.Items { + cluster := clusters[namespaces.Items[i].ClusterId] + if cluster == nil { + cluster, err = service.GetClusterByID(c, namespaces.Items[i].ClusterId) + if err != nil { + return err + } + + clusters[namespaces.Items[i].ClusterId] = cluster + } + + items[i] = namespaceDetail{ + ConnectorNamespace: namespaces.Items[i], + PlatformID: cluster.Status.Platform.Id, + } + } + switch opts.outputFormat { case dump.EmptyFormat: _, _ = fmt.Fprint(opts.f.IOStreams.Out, "\n") diff --git a/rhoc/pkg/cmd/namespaces/list/list_dumper.go b/rhoc/pkg/cmd/namespaces/list/list_dumper.go index ff35b3f..a180e18 100644 --- a/rhoc/pkg/cmd/namespaces/list/list_dumper.go +++ b/rhoc/pkg/cmd/namespaces/list/list_dumper.go @@ -2,25 +2,25 @@ package list import ( "fmt" + "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/api/admin" "io" "strconv" "time" - "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/api/admin" "github.com/bf2fc6cc711aee1a0c2a/cos-tools/rhoc/pkg/util/dumper" "github.com/olekukonko/tablewriter" "k8s.io/apimachinery/pkg/util/duration" ) -func dumpAsTable(out io.Writer, items admin.ConnectorNamespaceList, wide bool, style dumper.TableStyle) { - config := dumper.TableConfig[admin.ConnectorNamespace]{ +func dumpAsTable(out io.Writer, items []namespaceDetail, wide bool, style dumper.TableStyle) { + config := dumper.TableConfig[namespaceDetail]{ Style: style, Wide: wide, - Columns: []dumper.Column[admin.ConnectorNamespace]{ + Columns: []dumper.Column[namespaceDetail]{ { Name: "ID", Wide: false, - Getter: func(in *admin.ConnectorNamespace) dumper.Row { + Getter: func(in *namespaceDetail) dumper.Row { return dumper.Row{ Value: in.Id, } @@ -29,16 +29,25 @@ func dumpAsTable(out io.Writer, items admin.ConnectorNamespaceList, wide bool, s { Name: "ClusterID", Wide: false, - Getter: func(in *admin.ConnectorNamespace) dumper.Row { + Getter: func(in *namespaceDetail) dumper.Row { return dumper.Row{ Value: in.ClusterId, } }, }, + { + Name: "PlatformID", + Wide: true, + Getter: func(in *namespaceDetail) dumper.Row { + return dumper.Row{ + Value: in.PlatformID, + } + }, + }, { Name: "Name", Wide: true, - Getter: func(in *admin.ConnectorNamespace) dumper.Row { + Getter: func(in *namespaceDetail) dumper.Row { return dumper.Row{ Value: in.Name, } @@ -47,7 +56,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorNamespaceList, wide bool, s { Name: "Owner", Wide: false, - Getter: func(in *admin.ConnectorNamespace) dumper.Row { + Getter: func(in *namespaceDetail) dumper.Row { r := dumper.Row{ Value: in.Owner, } @@ -63,7 +72,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorNamespaceList, wide bool, s { Name: "TenantKind", Wide: true, - Getter: func(in *admin.ConnectorNamespace) dumper.Row { + Getter: func(in *namespaceDetail) dumper.Row { return dumper.Row{ Value: string(in.Tenant.Kind), } @@ -73,7 +82,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorNamespaceList, wide bool, s { Name: "TenantID", Wide: true, - Getter: func(in *admin.ConnectorNamespace) dumper.Row { + Getter: func(in *namespaceDetail) dumper.Row { return dumper.Row{ Value: in.Tenant.Id, } @@ -82,7 +91,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorNamespaceList, wide bool, s { Name: "State", Wide: false, - Getter: func(in *admin.ConnectorNamespace) dumper.Row { + Getter: func(in *namespaceDetail) dumper.Row { r := dumper.Row{ Value: string(in.Status.State), } @@ -107,7 +116,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorNamespaceList, wide bool, s { Name: "Version", Wide: true, - Getter: func(in *admin.ConnectorNamespace) dumper.Row { + Getter: func(in *namespaceDetail) dumper.Row { return dumper.Row{ Value: strconv.FormatInt(in.ResourceVersion, 10), } @@ -116,7 +125,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorNamespaceList, wide bool, s { Name: "Connectors", Wide: false, - Getter: func(in *admin.ConnectorNamespace) dumper.Row { + Getter: func(in *namespaceDetail) dumper.Row { return dumper.Row{ Value: fmt.Sprint(in.Status.ConnectorsDeployed), } @@ -125,7 +134,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorNamespaceList, wide bool, s { Name: "Expiration", Wide: true, - Getter: func(in *admin.ConnectorNamespace) dumper.Row { + Getter: func(in *namespaceDetail) dumper.Row { r := dumper.Row{ Value: in.Expiration, @@ -144,7 +153,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorNamespaceList, wide bool, s { Name: "Age", Wide: false, - Getter: func(in *admin.ConnectorNamespace) dumper.Row { + Getter: func(in *namespaceDetail) dumper.Row { age := duration.HumanDuration(time.Since(in.CreatedAt)) if in.CreatedAt.IsZero() { age = "" @@ -158,7 +167,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorNamespaceList, wide bool, s { Name: "CreatedAt", Wide: true, - Getter: func(in *admin.ConnectorNamespace) dumper.Row { + Getter: func(in *namespaceDetail) dumper.Row { return dumper.Row{ Value: in.CreatedAt.Format(time.RFC3339), } @@ -167,7 +176,7 @@ func dumpAsTable(out io.Writer, items admin.ConnectorNamespaceList, wide bool, s { Name: "ModifiedAt", Wide: true, - Getter: func(in *admin.ConnectorNamespace) dumper.Row { + Getter: func(in *namespaceDetail) dumper.Row { return dumper.Row{ Value: in.ModifiedAt.Format(time.RFC3339), } @@ -176,5 +185,5 @@ func dumpAsTable(out io.Writer, items admin.ConnectorNamespaceList, wide bool, s }, } - dumper.DumpTable(config, out, items.Items) + dumper.DumpTable(config, out, items) } diff --git a/rhoc/pkg/service/service_admin.go b/rhoc/pkg/service/service_admin.go index 1848ce2..0911640 100644 --- a/rhoc/pkg/service/service_admin.go +++ b/rhoc/pkg/service/service_admin.go @@ -77,14 +77,16 @@ func (api *AdminAPI) do(relativePath string, method, contentType string, body io r, w := io.Pipe() go func() { - defer w.Close() + defer func() { + _ = w.Close() + }() if _, err := io.Copy(w, body); err != nil { - w.CloseWithError(err) + _ = w.CloseWithError(err) return } if err := w.Close(); err != nil { - w.CloseWithError(err) + _ = w.CloseWithError(err) return } }() @@ -114,7 +116,11 @@ func (api *AdminAPI) do(relativePath string, method, contentType string, body io if resp.StatusCode > http.StatusBadRequest { return nil, resp, errors.New(resp.Status) } - defer resp.Body.Close() + if resp.Body != nil { + defer func() { + _ = resp.Body.Close() + }() + } b, err := io.ReadAll(resp.Body) if err != nil { diff --git a/rhoc/pkg/service/service_func.go b/rhoc/pkg/service/service_func.go index cf40173..94f7e00 100644 --- a/rhoc/pkg/service/service_func.go +++ b/rhoc/pkg/service/service_func.go @@ -377,3 +377,73 @@ func ListConnectorTypes(c *AdminAPI, opts request.ListOptions) (admin.ConnectorT return items, nil } + +func GetClusterForNamespace(c *AdminAPI, namespaceID string) (*admin.ConnectorClusterAdminView, error) { + result, httpRes, err := c.Clusters().GetConnectorNamespace(c.Context(), namespaceID).Execute() + if httpRes != nil { + defer func() { + _ = httpRes.Body.Close() + }() + } + if err != nil { + return nil, response.Error(err, httpRes) + } + + return GetClusterByID(c, result.ClusterId) +} + +func GetClusterByID(c *AdminAPI, clusterID string) (*admin.ConnectorClusterAdminView, error) { + result, httpRes, err := c.Clusters().GetConnectorCluster(c.Context(), clusterID).Execute() + if httpRes != nil { + defer func() { + _ = httpRes.Body.Close() + }() + } + if err != nil { + return nil, response.Error(err, httpRes) + } + + return result, nil +} + +func GetConnectorByID(c *AdminAPI, connectorID string) (*admin.ConnectorAdminView, error) { + result, httpRes, err := c.Clusters().GetConnector(c.Context(), connectorID).Execute() + if httpRes != nil { + defer func() { + _ = httpRes.Body.Close() + }() + } + if err != nil { + return nil, response.Error(err, httpRes) + } + + return result, nil +} + +func GetNamespaceByID(c *AdminAPI, namespaceID string) (*admin.ConnectorNamespace, error) { + result, httpRes, err := c.Clusters().GetConnectorNamespace(c.Context(), namespaceID).Execute() + if httpRes != nil { + defer func() { + _ = httpRes.Body.Close() + }() + } + if err != nil { + return nil, response.Error(err, httpRes) + } + + return result, nil +} + +func GetDeploymentByID(c *AdminAPI, clusterID string, deploymentID string) (*admin.ConnectorDeploymentAdminView, error) { + result, httpRes, err := c.Clusters().GetConnectorDeployment(c.Context(), clusterID, deploymentID).Execute() + if httpRes != nil { + defer func() { + _ = httpRes.Body.Close() + }() + } + if err != nil { + return nil, response.Error(err, httpRes) + } + + return result, nil +}