Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force create of services.TrustedCluster no longer fails. #1997

Merged
merged 1 commit into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/services/trustedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,9 @@ func (c *TrustedClusterV2) CanChangeStateTo(t TrustedCluster) error {

if c.GetEnabled() == t.GetEnabled() {
if t.GetEnabled() == true {
return trace.BadParameter("trusted cluster is already enabled")
return trace.AlreadyExists("trusted cluster is already enabled")
}
return trace.BadParameter("trusted cluster state is already disabled")
return trace.AlreadyExists("trusted cluster state is already disabled")
}

return nil
Expand Down
28 changes: 22 additions & 6 deletions tool/tctl/common/resource_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,20 @@ func (u *ResourceCommand) createTrustedCluster(client auth.ClientI, raw services

out, err := client.UpsertTrustedCluster(tc)
if err != nil {
return trace.Wrap(err)
// If force is used and UpsertTrustedCluster returns trace.AlreadyExists,
// this means the user tried to upsert a cluster whose exact match already
// exists in the backend, nothing needs to occur other than happy message
// that the trusted cluster has been created.
if u.force && trace.IsAlreadyExists(err) {
out = tc
} else {
return trace.Wrap(err)
}
}
if out.GetName() != tc.GetName() {
fmt.Printf("WARNING: trusted cluster %q resource has been renamed to match remote cluster name %q\n", name, out.GetName())
}
fmt.Printf("trusted cluster %q has been %v\n", out.GetName(), UpsertVerb(exists))
fmt.Printf("trusted cluster %q has been %v\n", out.GetName(), UpsertVerb(exists, u.force))
return nil
}

Expand All @@ -224,7 +232,7 @@ func (u *ResourceCommand) createGithubConnector(client auth.ClientI, raw service
return trace.Wrap(err)
}
fmt.Printf("github connector %q has been %s\n",
connector.GetName(), UpsertVerb(exists))
connector.GetName(), UpsertVerb(exists, u.force))
return nil
}

Expand Down Expand Up @@ -433,10 +441,18 @@ const (
formatJSON = "json"
)

func UpsertVerb(exists bool) string {
if exists {
func UpsertVerb(exists bool, force bool) string {
switch {
case exists == true && force == true:
return "created"
case exists == false && force == true:
return "created"
case exists == true && force == false:
return "updated"
} else {
case exists == false && force == false:
return "created"
}

// Can never reach here, but the compiler complains.
return "unknown"
}