From 44feecd13e0566f74a4499de8c1f5ca267eb88d3 Mon Sep 17 00:00:00 2001 From: Hussein Galal Date: Tue, 10 Dec 2024 23:20:32 +0200 Subject: [PATCH] Add external rancher mgiration (#9) * Add the ability to migrate objects between rancher environments * Add user migrate and status * fix * fix setting description for active directory users Signed-off-by: galal-hussein * fixes Signed-off-by: galal-hussein --------- Signed-off-by: galal-hussein --- cli/cmds/interactive/interactive.go | 5 +++-- cli/cmds/migrate/migrate.go | 5 +++-- cli/cmds/status/status.go | 5 +++-- pkg/cluster/cluster.go | 4 ++-- pkg/cluster/clusterroletemplatebinding.go | 15 +++++++++++++++ 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/cli/cmds/interactive/interactive.go b/cli/cmds/interactive/interactive.go index b20a95f..0838ac8 100644 --- a/cli/cmds/interactive/interactive.go +++ b/cli/cmds/interactive/interactive.go @@ -2,6 +2,7 @@ package interactive import ( "context" + "errors" "fmt" "galal-hussein/cattle-drive/cli/cmds" "galal-hussein/cattle-drive/pkg/client" @@ -85,7 +86,7 @@ func migrate(clx *cli.Context) error { cmds.Spinner.Start() if source == "" || target == "" { - return fmt.Errorf("source or target is not specified") + return errors.New("source or target is not specified") } var clusters v3.ClusterList @@ -116,7 +117,7 @@ func migrate(clx *cli.Context) error { } } if sourceCluster == nil || targetCluster == nil { - return fmt.Errorf("failed to find source or target cluster") + return errors.New("failed to find source or target cluster") } // initiate client for the cluster scConfig := *restConfig diff --git a/cli/cmds/migrate/migrate.go b/cli/cmds/migrate/migrate.go index 691928f..58ea74e 100644 --- a/cli/cmds/migrate/migrate.go +++ b/cli/cmds/migrate/migrate.go @@ -2,6 +2,7 @@ package migrate import ( "context" + "errors" "fmt" "galal-hussein/cattle-drive/cli/cmds" "galal-hussein/cattle-drive/pkg/client" @@ -26,7 +27,7 @@ var ( Destination: &source, Aliases: []string{"s"}, }, - &cli.StringFlag{ + &cli.StringFlag{https://github.com/rancherlabs/cattle-drive/pull/9/conflict?name=cli%252Fcmds%252Fmigrate%252Fmigrate.go&ancestor_oid=38dfd762b78fafcff245ce52f24be4266a5b7830&base_oid=505ab5277a8713ea812d2ab04429496d3b10d096&head_oid=691928f200247163bfa64d6991e3d7dbf93445bf Name: "target", Usage: "name of the target cluster", Destination: &target, @@ -77,7 +78,7 @@ func migrate(clx *cli.Context) error { cmds.Spinner.Start() if source == "" || target == "" { - return fmt.Errorf("source or target is not specified") + return errors.New("source or target is not specified") } var clusters v3.ClusterList diff --git a/cli/cmds/status/status.go b/cli/cmds/status/status.go index f7b2fe1..757a203 100644 --- a/cli/cmds/status/status.go +++ b/cli/cmds/status/status.go @@ -2,6 +2,7 @@ package status import ( "context" + "errors" "fmt" "galal-hussein/cattle-drive/cli/cmds" "galal-hussein/cattle-drive/pkg/client" @@ -75,7 +76,7 @@ func status(clx *cli.Context) error { cmds.Spinner.Start() if source == "" || target == "" { - return fmt.Errorf("source or target is not specified") + return errors.New("source or target is not specified") } var clusters v3.ClusterList @@ -107,7 +108,7 @@ func status(clx *cli.Context) error { } } if sourceCluster == nil || targetCluster == nil { - return fmt.Errorf("failed to find source or target cluster") + return errors.New("failed to find source or target cluster") } // initiate client for the cluster scConfig := *restConfig diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 7a557fc..01d3682 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -344,7 +344,7 @@ func (c *Cluster) Migrate(ctx context.Context, client *client.Clients, tc *Clust var user v3.User if err := client.Users.Get(ctx, "", userID, &user, v1.GetOptions{}); err != nil { if apierrors.IsNotFound(err) { - return fmt.Errorf("user [%s] does not exists, please migrate user first", userID) + return errors.New("user " + userID + " does not exists, please migrate user first") } } @@ -374,7 +374,7 @@ func (c *Cluster) Migrate(ctx context.Context, client *client.Clients, tc *Clust var user v3.User if err := client.Users.Get(ctx, "", userID, &user, v1.GetOptions{}); err != nil { if apierrors.IsNotFound(err) { - return fmt.Errorf("user [%s] does not exists, please migrate user first", userID) + return errors.New("user " + userID + " does not exists, please migrate user first") } } diff --git a/pkg/cluster/clusterroletemplatebinding.go b/pkg/cluster/clusterroletemplatebinding.go index 4b5de48..cd83d76 100644 --- a/pkg/cluster/clusterroletemplatebinding.go +++ b/pkg/cluster/clusterroletemplatebinding.go @@ -58,6 +58,21 @@ func (c *ClusterRoleTemplateBinding) Mutate(tc *Cluster) { } } func (c *ClusterRoleTemplateBinding) SetDescription(ctx context.Context, client *client.Clients) error { + // check if this is a group + if c.Obj.UserName == "" && c.Obj.UserPrincipalName == "" { + groupName := c.Obj.GroupName + if groupName == "" { + // handling external auth providers + groupName = c.Obj.GroupPrincipalName + } + c.Description = fmt.Sprintf("%s permission for group %s", c.Obj.RoleTemplateName, groupName) + return nil + } + // setting description for external users + if c.Obj.UserPrincipalName != "" { + c.Description = fmt.Sprintf("%s permission for user %s", c.Obj.RoleTemplateName, c.Obj.UserPrincipalName) + return nil + } var user v3.User userID := c.Obj.UserName if err := client.Users.Get(ctx, "", userID, &user, v1.GetOptions{}); err != nil {