-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #225 from nimrodshn/add_resume_hibernate_commands
Add support for hibernate/resume cluster
- Loading branch information
Showing
5 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package cluster | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
c "github.com/openshift-online/ocm-cli/pkg/cluster" | ||
"github.com/openshift-online/ocm-cli/pkg/ocm" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "cluster {NAME|ID|EXTERNAL_ID}", | ||
Short: "Initiate cluster hibernation", | ||
Long: "Initiates cluster hibernation. While hibernating a cluster will not consume any cloud provider infrastructure" + | ||
"but will be counted for quota.", | ||
RunE: run, | ||
} | ||
|
||
func run(cmd *cobra.Command, argv []string) error { | ||
// Check that there is exactly one cluster name, identifir or external identifier in the | ||
// command line arguments: | ||
if len(argv) != 1 { | ||
fmt.Fprintf( | ||
os.Stderr, | ||
"Expected exactly one cluster name, identifier or external identifier "+ | ||
"is required\n", | ||
) | ||
os.Exit(1) | ||
} | ||
|
||
// Check that the cluster key (name, identifier or external identifier) given by the user | ||
// is reasonably safe so that there is no risk of SQL injection: | ||
clusterKey := argv[0] | ||
if !c.IsValidClusterKey(clusterKey) { | ||
return fmt.Errorf( | ||
"Cluster name, identifier or external identifier '%s' isn't valid: it "+ | ||
"must contain only letters, digits, dashes and underscores", | ||
clusterKey, | ||
) | ||
} | ||
|
||
// Create the client for the OCM API: | ||
connection, err := ocm.NewConnection().Build() | ||
if err != nil { | ||
return fmt.Errorf("Failed to create OCM connection: %v", err) | ||
} | ||
defer connection.Close() | ||
|
||
// Get the client for the cluster management api | ||
clusterCollection := connection.ClustersMgmt().V1().Clusters() | ||
|
||
// Verify the cluster exists in OCM. | ||
cluster, err := c.GetCluster(clusterCollection, clusterKey) | ||
if err != nil { | ||
return fmt.Errorf("Failed to get cluster '%s': %v", clusterKey, err) | ||
} | ||
|
||
_, err = clusterCollection.Cluster(cluster.ID()).Hibernate().Send() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package hibernate | ||
|
||
import ( | ||
"github.com/openshift-online/ocm-cli/cmd/ocm/hibernate/cluster" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "hibernate [flags] RESOURCE", | ||
Short: "Hibernate a specific resource (currently only supported for clusters)", | ||
Long: "Hibernate a specific resource (currently only supported for clusters)", | ||
} | ||
|
||
func init() { | ||
Cmd.AddCommand(cluster.Cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cluster | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
c "github.com/openshift-online/ocm-cli/pkg/cluster" | ||
"github.com/openshift-online/ocm-cli/pkg/ocm" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "cluster {NAME|ID|EXTERNAL_ID}", | ||
Short: "Resume a cluster from hibernation", | ||
Long: "Resumes cluster hibernation. The cluster will return to a `Ready` state, and all actions will be enabled.", | ||
RunE: run, | ||
} | ||
|
||
func run(cmd *cobra.Command, argv []string) error { | ||
// Check that there is exactly one cluster name, identifir or external identifier in the | ||
// command line arguments: | ||
if len(argv) != 1 { | ||
fmt.Fprintf( | ||
os.Stderr, | ||
"Expected exactly one cluster name, identifier or external identifier "+ | ||
"is required\n", | ||
) | ||
os.Exit(1) | ||
} | ||
|
||
// Check that the cluster key (name, identifier or external identifier) given by the user | ||
// is reasonably safe so that there is no risk of SQL injection: | ||
clusterKey := argv[0] | ||
if !c.IsValidClusterKey(clusterKey) { | ||
return fmt.Errorf( | ||
"Cluster name, identifier or external identifier '%s' isn't valid: it "+ | ||
"must contain only letters, digits, dashes and underscores", | ||
clusterKey, | ||
) | ||
} | ||
|
||
// Create the client for the OCM API: | ||
connection, err := ocm.NewConnection().Build() | ||
if err != nil { | ||
return fmt.Errorf("Failed to create OCM connection: %v", err) | ||
} | ||
defer connection.Close() | ||
|
||
// Get the client for the cluster management api | ||
clusterCollection := connection.ClustersMgmt().V1().Clusters() | ||
|
||
// Verify the cluster exists in OCM. | ||
cluster, err := c.GetCluster(clusterCollection, clusterKey) | ||
if err != nil { | ||
return fmt.Errorf("Failed to get cluster '%s': %v", clusterKey, err) | ||
} | ||
_, err = clusterCollection.Cluster(cluster.ID()).Resume().Send() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package resume | ||
|
||
import ( | ||
"github.com/openshift-online/ocm-cli/cmd/ocm/resume/cluster" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "resume [flags] RESOURCE", | ||
Short: "Resumes a hibernating resource (currently only supported for clusters)", | ||
Long: "Resumes a hibernating resource (currently only supported for clusters)", | ||
} | ||
|
||
func init() { | ||
Cmd.AddCommand(cluster.Cmd) | ||
} |