Skip to content

Commit

Permalink
feat: report cluster id and platform id in namespaces/deployments/con…
Browse files Browse the repository at this point in the history
…nectors sub command
  • Loading branch information
lburgazzoli committed Dec 13, 2022
1 parent 4da5f25 commit c66852a
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 96 deletions.
28 changes: 20 additions & 8 deletions rhoc/pkg/cmd/connectors/get/get.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
31 changes: 26 additions & 5 deletions rhoc/pkg/cmd/connectors/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down Expand Up @@ -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")
Expand Down
51 changes: 34 additions & 17 deletions rhoc/pkg/cmd/connectors/list/list_dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand All @@ -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),
}
Expand All @@ -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),
}
Expand All @@ -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,
}
Expand All @@ -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),
}
Expand All @@ -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 = ""
Expand All @@ -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),
}
Expand All @@ -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),
}
Expand All @@ -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)
}
26 changes: 18 additions & 8 deletions rhoc/pkg/cmd/deployments/get/get.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
28 changes: 24 additions & 4 deletions rhoc/pkg/cmd/deployments/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down
Loading

0 comments on commit c66852a

Please sign in to comment.