Skip to content

Commit

Permalink
Merge pull request #349 from pratikmallya/add_orchestration_commands
Browse files Browse the repository at this point in the history
[rfr]Add orchestration commands
  • Loading branch information
jrperritt committed Oct 1, 2015
2 parents df6d769 + cdfaed1 commit 5d31171
Show file tree
Hide file tree
Showing 53 changed files with 5,527 additions and 27 deletions.
6 changes: 6 additions & 0 deletions auth/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ func authFromScratch(credsResult *CredentialsResult, serviceType string, urlType
Availability: urlType,
})
break
case "orchestration":
sc, err = rackspace.NewOrchestrationV1(pc, gophercloud.EndpointOpts{
Region: region,
Availability: urlType,
})
break
}
if err != nil {
return nil, err
Expand Down
13 changes: 13 additions & 0 deletions commands/orchestrationcommands/buildinfocommands/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package buildinfocommands

import "github.com/rackspace/rack/internal/github.com/codegangsta/cli"

var commandPrefix = "orchestration build-info"
var serviceClientType = "orchestration"

// Get returns all the commands allowed for an `orchestration build-info` request.
func Get() []cli.Command {
return []cli.Command{
get,
}
}
66 changes: 66 additions & 0 deletions commands/orchestrationcommands/buildinfocommands/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package buildinfocommands

import (
"github.com/rackspace/rack/commandoptions"
"github.com/rackspace/rack/handler"
"github.com/rackspace/rack/internal/github.com/codegangsta/cli"
"github.com/rackspace/rack/internal/github.com/fatih/structs"
"github.com/rackspace/rack/internal/github.com/rackspace/gophercloud/rackspace/orchestration/v1/buildinfo"
"github.com/rackspace/rack/util"
)

var get = cli.Command{
Name: "get",
Usage: util.Usage(commandPrefix, "get", ""),
Description: "Retrieve build information for a Heat deployment",
Action: actionGet,
Flags: commandoptions.CommandFlags(flagsGet, nil),
BashComplete: func(c *cli.Context) {
commandoptions.CompleteFlags(commandoptions.CommandFlags(flagsGet, keysGet))
},
}

var keysGet = []string{"API", "Engine", "FusionAPI"}

type commandGet handler.Command

func flagsGet() []cli.Flag {
return []cli.Flag{}
}

func actionGet(c *cli.Context) {
command := &commandGet{
Ctx: &handler.Context{
CLIContext: c,
},
}
handler.Handle(command)
}

func (command *commandGet) Context() *handler.Context {
return command.Ctx
}

func (command *commandGet) Keys() []string {
return keysGet
}

func (command *commandGet) ServiceClientType() string {
return serviceClientType
}

func (command *commandGet) HandleFlags(resource *handler.Resource) error {
return nil
}

func (command *commandGet) Execute(resource *handler.Resource) {
info, err := buildinfo.Get(command.Ctx.ServiceClient).Extract()
if err != nil {
resource.Err = err
}
result := structs.Map(info)
for k, v := range result {
result[k] = v.(map[string]interface{})["Revision"]
}
resource.Result = result
}
60 changes: 60 additions & 0 deletions commands/orchestrationcommands/buildinfocommands/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package buildinfocommands

import (
"flag"
"fmt"
"net/http"
"testing"

"github.com/rackspace/rack/handler"
"github.com/rackspace/rack/internal/github.com/codegangsta/cli"
th "github.com/rackspace/rack/internal/github.com/rackspace/gophercloud/testhelper"
"github.com/rackspace/rack/internal/github.com/rackspace/gophercloud/testhelper/client"
)

func TestGetContext(t *testing.T) {
app := cli.NewApp()
flagset := flag.NewFlagSet("flags", 1)
c := cli.NewContext(app, flagset, nil)
cmd := &commandGet{
Ctx: &handler.Context{
CLIContext: c,
},
}
expected := cmd.Ctx
actual := cmd.Context()
th.AssertDeepEquals(t, expected, actual)
}

func TestGetKeys(t *testing.T) {
cmd := &commandGet{}
expected := keysGet
actual := cmd.Keys()
th.AssertDeepEquals(t, expected, actual)
}

func TestGetServiceClientType(t *testing.T) {
cmd := &commandGet{}
expected := serviceClientType
actual := cmd.ServiceClientType()
th.AssertEquals(t, expected, actual)
}

func TestGetExecute(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
th.Mux.HandleFunc("/build_info", func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json")
fmt.Fprint(w, `{"api": {"revision": "{api_build_revision}"}}`)
})
cmd := &commandGet{
Ctx: &handler.Context{
ServiceClient: client.ServiceClient(),
},
}
actual := &handler.Resource{}
cmd.Execute(actual)
th.AssertNoErr(t, actual.Err)
}
43 changes: 43 additions & 0 deletions commands/orchestrationcommands/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package orchestrationcommands

import (
"github.com/rackspace/rack/commands/orchestrationcommands/buildinfocommands"
"github.com/rackspace/rack/commands/orchestrationcommands/stackcommands"
"github.com/rackspace/rack/commands/orchestrationcommands/stackeventcommands"
"github.com/rackspace/rack/commands/orchestrationcommands/stackresourcecommands"
"github.com/rackspace/rack/commands/orchestrationcommands/stacktemplatecommands"
"github.com/rackspace/rack/internal/github.com/codegangsta/cli"
)

var serviceClientType = "orchestration"

// Get returns all the commands allowed for an `orchestration` request.
func Get() []cli.Command {
return []cli.Command{
{
Name: "build-info",
Usage: "Build information.",
Subcommands: buildinfocommands.Get(),
},
{
Name: "stack",
Usage: "Stack management.",
Subcommands: stackcommands.Get(),
},
{
Name: "event",
Usage: "Stack event queries.",
Subcommands: stackeventcommands.Get(),
},
{
Name: "resource",
Usage: "Stack resource queries.",
Subcommands: stackresourcecommands.Get(),
},
{
Name: "template",
Usage: "Stack template queries.",
Subcommands: stacktemplatecommands.Get(),
},
}
}
144 changes: 144 additions & 0 deletions commands/orchestrationcommands/stackcommands/abandon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package stackcommands

import (
"encoding/json"

"github.com/rackspace/rack/commandoptions"
"github.com/rackspace/rack/handler"
"github.com/rackspace/rack/internal/github.com/codegangsta/cli"
"github.com/rackspace/rack/internal/github.com/rackspace/gophercloud/rackspace/orchestration/v1/stacks"
"github.com/rackspace/rack/util"
)

var abandon = cli.Command{
Name: "abandon",
Usage: util.Usage(commandPrefix, "abandon", "[--name <stackName> | --id <stackID> | --stdin name]"),
Description: "Deletes an existing stack, but leaves resources intact",
Action: actionAbandon,
Flags: commandoptions.CommandFlags(flagsAbandon, keysAbandon),
BashComplete: func(c *cli.Context) {
commandoptions.CompleteFlags(commandoptions.CommandFlags(flagsAbandon, keysAbandon))
},
}

func flagsAbandon() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "id",
Usage: "[optional; required if `stdin` or `name` isn't provided] The ID of the stack.",
},
cli.StringFlag{
Name: "name",
Usage: "[optional; required if `id` or `stdin` isn't provided] The name of the stack.",
},
cli.StringFlag{
Name: "stdin",
Usage: "[optional; required if `id` or `name` isn't provided] The field being piped into STDIN. Valid values are: name.",
},
}
}

var keysAbandon = []string{"Status", "Name", "Template", "Action", "ID", "Resources", "Files", "StackUserProjectID", "ProjectID", "Environment"}

type paramsAbandon struct {
stackName string
stackID string
}

type commandAbandon handler.Command

func actionAbandon(c *cli.Context) {
command := &commandAbandon{
Ctx: &handler.Context{
CLIContext: c,
},
}
handler.Handle(command)
}

func (command *commandAbandon) Context() *handler.Context {
return command.Ctx
}

func (command *commandAbandon) Keys() []string {
return keysAbandon
}

func (command *commandAbandon) ServiceClientType() string {
return serviceClientType
}

func (command *commandAbandon) HandleFlags(resource *handler.Resource) error {
return nil
}

func (command *commandAbandon) HandlePipe(resource *handler.Resource, item string) error {
name, id, err := IDAndName(command.Ctx.ServiceClient, item, "")
if err != nil {
return err
}
resource.Params.(*paramsAbandon).stackName = name
resource.Params.(*paramsAbandon).stackID = id
return nil
}

func (command *commandAbandon) HandleSingle(resource *handler.Resource) error {
c := command.Ctx.CLIContext
name := c.String("name")
id := c.String("id")
name, id, err := IDAndName(command.Ctx.ServiceClient, name, id)
if err != nil {
return err
}
resource.Params = &paramsAbandon{
stackName: name,
stackID: id,
}

resource.Params.(*paramsAbandon).stackName = name
resource.Params.(*paramsAbandon).stackID = id
return nil
}

func (command *commandAbandon) Execute(resource *handler.Resource) {

params := resource.Params.(*paramsAbandon)
stackName := params.stackName
stackID := params.stackID
res := stacks.Abandon(command.Ctx.ServiceClient, stackName, stackID)
resStack, err := res.Extract()
if err != nil {
resource.Err = err
return
}
resource.Result = resStack
}

func (command *commandAbandon) StdinField() string {
return "name"
}

func (command *commandAbandon) PreTable(resource *handler.Resource) error {
resource.Result = stackSingle(resource.Result)
return nil
}

func (command *commandAbandon) PreJSON(resource *handler.Resource) error {
var resInterface map[string]interface{}
res, err := json.Marshal(resource.Result)
if err != nil {
return err
}
err = json.Unmarshal(res, &resInterface)
if err != nil {
return err
}
resource.Result = resInterface
resource.Keys = []string{"status", "name", "template", "action", "id", "resources", "files", "stack_user_project_id", "project_id", "environment"}
return nil
}

func (command *commandAbandon) PreCSV(resource *handler.Resource) error {
command.PreTable(resource)
return nil
}
Loading

0 comments on commit 5d31171

Please sign in to comment.