Skip to content

Commit

Permalink
Add initial implementation of scopes.
Browse files Browse the repository at this point in the history
This is a work in progress but sufficient for review.

TODO:
Rename the "config" and "activity" commands to avoid name clash with their scope specifiers.
Replace "list" commands with their equivalents from "RESTish" syntax.
Add tests.

The scope specifiers (and their aliases) are:
application (app, a)
entity (ent, e)
effector (eff, f)
config (conf, con, c)
activity (act, v)

For example:
br app myweb ent mycluster entity-children
br a myweb e mycluster config
br a myweb e myweb activity RJRwVqXR activity-children
br a myweb e mycluster list entities
br app myweb status
  • Loading branch information
geomacy committed Dec 4, 2015
1 parent 67d892b commit 32686c1
Show file tree
Hide file tree
Showing 37 changed files with 291 additions and 140 deletions.
9 changes: 6 additions & 3 deletions brooklyn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/brooklyncentral/brooklyn-cli/io"
"os"
"path/filepath"
"github.com/brooklyncentral/brooklyn-cli/scope"
)

func getNetworkCredentialsFromConfig(yamlMap map[string]interface{}) (string, string, string){
Expand All @@ -32,8 +33,10 @@ func main() {
//target, username, password := "http://192.168.50.101:8081", "brooklyn", "Sns4Hh9j7l"
network := net.NewNetwork(target, username, password)
cmdFactory := command_factory.NewFactory(network, config)
cmdRunner := command_runner.NewRunner(cmdFactory)

args, scope := scope.ScopeArguments(os.Args)
cmdRunner := command_runner.NewRunner(scope, cmdFactory)
metaDatas := cmdFactory.CommandMetadatas()
theApp := app.NewApp(filepath.Base(os.Args[0]), cmdRunner, metaDatas...)
theApp.Run(os.Args)
theApp := app.NewApp(filepath.Base(args[0]), cmdRunner, metaDatas...)
theApp.Run(args)
}
3 changes: 2 additions & 1 deletion command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package command
import (
"github.com/codegangsta/cli"
"github.com/brooklyncentral/brooklyn-cli/command_metadata"
"github.com/brooklyncentral/brooklyn-cli/scope"
)

type Command interface {
Metadata() command_metadata.CommandMetadata
Run(context *cli.Context)
Run(scope scope.Scope, context *cli.Context)
}
78 changes: 43 additions & 35 deletions command_factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,58 @@ type concreteFactory struct {
subCommands map[string]map[string]command.Command
}


func NewFactory(network *net.Network, config *io.Config) (factory concreteFactory) {
factory.cmdsByName = make(map[string]command.Command)
factory.subCommands = make(map[string]map[string]command.Command)

factory.cmdsByName["access"] = commands.NewAccess(network)
//factory.cmdsByName["activities"] = commands.NewActivities(network)
factory.cmdsByName["activity"] = commands.NewActivity(network)
factory.cmdsByName["activity-children"] = commands.NewActivityChildren(network)
factory.cmdsByName["activity-stream"] = commands.NewActivityStream(network)
factory.cmdsByName["add-catalog"] = commands.NewAddCatalog(network)
factory.cmdsByName["add-children"] = commands.NewAddChildren(network)
factory.cmdsByName["application"] = commands.NewApplication(network)
factory.cmdsByName["catalog"] = commands.NewCatalog(network)
factory.cmdsByName["config"] = commands.NewConfig(network)
factory.cmdsByName["create"] = commands.NewCreate(network)
factory.cmdsByName["delete"] = commands.NewDelete(network)
factory.cmdsByName["destroy-policy"] = commands.NewDestroyPolicy(network)
factory.cmdsByName["entity-children"] = commands.NewChildren(network)
factory.command(commands.NewAccess(network))
//factory.command(commands.NewActivities(network))
factory.command(commands.NewActivity(network))
factory.command(commands.NewActivityChildren(network))
factory.command(commands.NewActivityStream(network))
factory.command(commands.NewAddCatalog(network))
factory.command(commands.NewAddChildren(network))
factory.command(commands.NewApplication(network))
factory.command(commands.NewCatalog(network))
factory.command(commands.NewConfig(network))
factory.command(commands.NewCreate(network))
factory.command(commands.NewDelete(network))
factory.command(commands.NewDestroyPolicy(network))
factory.command(commands.NewChildren(network))
listCommand := commands.NewList(network)
factory.cmdsByName["list"] = listCommand;
factory.subCommand("list", "application", listCommand.SubCommand(commands.ListApplicationCommand))
factory.subCommand("list", "effector", listCommand.SubCommand(commands.ListEffectorCommand))
factory.subCommand("list", "entity", listCommand.SubCommand(commands.ListEntityCommand))
factory.subCommand("list", "sensor", listCommand.SubCommand(commands.ListSensorCommand))
factory.cmdsByName["locations"] = commands.NewLocations(network)
factory.cmdsByName["login"] = commands.NewLogin(network, config)
factory.cmdsByName["policies"] = commands.NewPolicies(network)
factory.cmdsByName["policy"] = commands.NewPolicy(network)
factory.cmdsByName["rename-entity"] = commands.NewRename(network)
factory.cmdsByName["sensor"] = commands.NewSensor(network)
factory.cmdsByName["set-config"] = commands.NewSetConfig(network)
factory.cmdsByName["spec"] = commands.NewSpec(network)
factory.cmdsByName["start-policy"] = commands.NewStartPolicy(network)
factory.cmdsByName["stop-policy"] = commands.NewStopPolicy(network)
factory.cmdsByName["tree"] = commands.NewTree(network)
factory.cmdsByName["version"] = commands.NewVersion(network)
factory.command(listCommand);
factory.subCommand(listCommand, commands.ListApplicationCommand)
factory.subCommand(listCommand, commands.ListEffectorCommand)
factory.subCommand(listCommand, commands.ListEntityCommand)
factory.subCommand(listCommand, commands.ListSensorCommand)
factory.command(commands.NewLocations(network))
factory.command(commands.NewLogin(network, config))
factory.command(commands.NewPolicies(network))
factory.command(commands.NewPolicy(network))
factory.command(commands.NewRename(network))
factory.command(commands.NewSensor(network))
factory.command(commands.NewSetConfig(network))
factory.command(commands.NewSpec(network))
factory.command(commands.NewStartPolicy(network))
factory.command(commands.NewStopPolicy(network))
factory.command(commands.NewTree(network))
factory.command(commands.NewVersion(network))

return factory
}

func (factory concreteFactory) subCommand(commandName string, subCommandName string, subCommand command.Command) {
if nil == factory.subCommands[commandName] {
factory.subCommands[commandName] = make(map[string]command.Command)

func (factory *concreteFactory) command(command command.Command) {
factory.cmdsByName[command.Metadata().Name] = command
}

// TODO make this more generic - instead of List use a generic Command type
func (factory concreteFactory) subCommand(listCommand *commands.List, subCommandName string) {
if nil == factory.subCommands[listCommand.Metadata().Name] {
factory.subCommands[listCommand.Metadata().Name] = make(map[string]command.Command)
}
factory.subCommands[commandName][subCommandName] = subCommand
factory.subCommands[listCommand.Metadata().Name][subCommandName] = listCommand.SubCommand(subCommandName)
}

func (f concreteFactory) GetByCmdName(cmdName string) (cmd command.Command, err error) {
Expand All @@ -84,6 +91,7 @@ func (f concreteFactory) GetByCmdName(cmdName string) (cmd command.Command, err
}

func (f concreteFactory) GetBySubCmdName(cmdName string, subCmdName string) (cmd command.Command, err error) {

_, hasPrimary := f.subCommands[cmdName]
if hasPrimary {
cmd, found := f.subCommands[cmdName][subCmdName]
Expand Down
9 changes: 6 additions & 3 deletions command_runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/codegangsta/cli"
"github.com/brooklyncentral/brooklyn-cli/command_factory"
"github.com/brooklyncentral/brooklyn-cli/scope"
)

type Runner interface {
Expand All @@ -13,10 +14,12 @@ type Runner interface {

type ConcreteRunner struct {
cmdFactory command_factory.Factory
scope scope.Scope
}

func NewRunner(cmdFactory command_factory.Factory) (runner ConcreteRunner) {
func NewRunner(scope scope.Scope, cmdFactory command_factory.Factory) (runner ConcreteRunner) {
runner.cmdFactory = cmdFactory
runner.scope = scope
return
}

Expand All @@ -27,7 +30,7 @@ func (runner ConcreteRunner) RunCmdByName(cmdName string, c *cli.Context) error
return err
}

cmd.Run(c)
cmd.Run(runner.scope, c)
return nil
}

Expand All @@ -38,6 +41,6 @@ func (runner ConcreteRunner) RunSubCmdByName(cmdName string, subCommand string,
return err
}

cmd.Run(c)
cmd.Run(runner.scope, c)
return nil
}
5 changes: 3 additions & 2 deletions commands/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/brooklyncentral/brooklyn-cli/api/access_control"
"github.com/brooklyncentral/brooklyn-cli/command_metadata"
"github.com/brooklyncentral/brooklyn-cli/net"
"github.com/brooklyncentral/brooklyn-cli/scope"
)

type Access struct {
Expand All @@ -22,12 +23,12 @@ func (cmd *Access) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "access",
Description: "Show access control",
Usage: "BROOKLYN_NAME access",
Usage: "BROOKLYN_NAME [ SCOPE ] access",
Flags: []cli.Flag{},
}
}

func (cmd *Access) Run(c *cli.Context) {
func (cmd *Access) Run(scope scope.Scope, c *cli.Context) {
access := access_control.Access(cmd.network)
fmt.Println("Location Provisioning Allowed:", access.LocationProvisioningAllowed)
}
8 changes: 4 additions & 4 deletions commands/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ func (cmd *Activities) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "activities",
Description: "Show the activities for an entity",
Usage: "BROOKLYN_NAME activities APPLICATION ENTITY",
Usage: "BROOKLYN_NAME [ SCOPE ] activities",
Flags: []cli.Flag{},
}
}
//WIP
//func (cmd *Activities) Run(c *cli.Context) {
// activityList := activities.ActivityList(cmd.network, c.Args()[0], c.Args()[1])
//func (cmd *Activities) Run(scope scope.Scope, c *cli.Context) {
// activityList := activities.ActivityList(cmd.network, c.Args().First(), c.Args()[1])
// table := terminal.NewTable([]string{"Id", "Task", "Submitted", "Status"})
// for _, activity := range activityList {
// table.Add(activity.Id, activity.DisplayName, time.Unix(activity.SubmitTimeUtc/1000, 0).Format(time.UnixDate), activity.CurrentStatus)
// }
// table.Print()
//}
//}
7 changes: 4 additions & 3 deletions commands/activity-children.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/brooklyncentral/brooklyn-cli/net"
"github.com/brooklyncentral/brooklyn-cli/terminal"
"time"
"github.com/brooklyncentral/brooklyn-cli/scope"
)

type ActivityChildren struct {
Expand All @@ -23,13 +24,13 @@ func (cmd *ActivityChildren) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "activity-children",
Description: "Show the child activities for an entity",
Usage: "BROOKLYN_NAME activity-children ACTIVITY",
Usage: "BROOKLYN_NAME [ SCOPE ] activity-children",
Flags: []cli.Flag{},
}
}

func (cmd *ActivityChildren) Run(c *cli.Context) {
activityList := activities.ActivityChildren(cmd.network, c.Args()[0])
func (cmd *ActivityChildren) Run(scope scope.Scope, c *cli.Context) {
activityList := activities.ActivityChildren(cmd.network, scope.Activity)
table := terminal.NewTable([]string{"Id", "Task", "Submitted", "Status"})
for _, activity := range activityList {
table.Add(activity.Id, activity.DisplayName, time.Unix(activity.SubmitTimeUtc/1000, 0).Format(time.UnixDate), activity.CurrentStatus)
Expand Down
7 changes: 4 additions & 3 deletions commands/activity-stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/brooklyncentral/brooklyn-cli/api/activities"
"github.com/brooklyncentral/brooklyn-cli/command_metadata"
"github.com/brooklyncentral/brooklyn-cli/net"
"github.com/brooklyncentral/brooklyn-cli/scope"
)

type ActivityStream struct {
Expand All @@ -22,12 +23,12 @@ func (cmd *ActivityStream) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "activity-stream",
Description: "Show the stream for a given activity",
Usage: "BROOKLYN_NAME activity-stream ACTIVITY STREAM_ID",
Usage: "BROOKLYN_NAME [ SCOPE ] activity-stream STREAM_ID",
Flags: []cli.Flag{},
}
}

func (cmd *ActivityStream) Run(c *cli.Context) {
activityStream := activities.ActivityStream(cmd.network, c.Args()[0], c.Args()[1])
func (cmd *ActivityStream) Run(scope scope.Scope, c *cli.Context) {
activityStream := activities.ActivityStream(cmd.network, scope.Activity, c.Args().First())
fmt.Println(activityStream)
}
7 changes: 4 additions & 3 deletions commands/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/brooklyncentral/brooklyn-cli/net"
"github.com/brooklyncentral/brooklyn-cli/terminal"
"time"
"github.com/brooklyncentral/brooklyn-cli/scope"
)

type Activity struct {
Expand All @@ -23,13 +24,13 @@ func (cmd *Activity) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "activity",
Description: "Show the activity for an entity",
Usage: "BROOKLYN_NAME activity ACTIVITY",
Usage: "BROOKLYN_NAME [ SCOPE ] activity ACTIVITYID",
Flags: []cli.Flag{},
}
}

func (cmd *Activity) Run(c *cli.Context) {
activity := activities.Activity(cmd.network, c.Args()[0])
func (cmd *Activity) Run(scope scope.Scope, c *cli.Context) {
activity := activities.Activity(cmd.network, c.Args().First())
table := terminal.NewTable([]string{"Id", "Task", "Submitted", "Status"})
table.Add(activity.Id, activity.DisplayName, time.Unix(activity.SubmitTimeUtc/1000, 0).Format(time.UnixDate), activity.CurrentStatus)

Expand Down
7 changes: 4 additions & 3 deletions commands/add-catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/brooklyncentral/brooklyn-cli/api/catalog"
"github.com/brooklyncentral/brooklyn-cli/command_metadata"
"github.com/brooklyncentral/brooklyn-cli/net"
"github.com/brooklyncentral/brooklyn-cli/scope"
)

type AddCatalog struct {
Expand All @@ -22,12 +23,12 @@ func (cmd *AddCatalog) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "add-catalog",
Description: "Add a new catalog item from the supplied YAML",
Usage: "BROOKLYN_NAME add-catalog FILEPATH",
Usage: "BROOKLYN_NAME [ SCOPE ] add-catalog FILEPATH",
Flags: []cli.Flag{},
}
}

func (cmd *AddCatalog) Run(c *cli.Context) {
create := catalog.AddCatalog(cmd.network, c.Args()[0])
func (cmd *AddCatalog) Run(scope scope.Scope, c *cli.Context) {
create := catalog.AddCatalog(cmd.network, c.Args().First())
fmt.Println(create)
}
7 changes: 4 additions & 3 deletions commands/add-children.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/brooklyncentral/brooklyn-cli/net"
"github.com/brooklyncentral/brooklyn-cli/terminal"
"time"
"github.com/brooklyncentral/brooklyn-cli/scope"
)

type AddChildren struct {
Expand All @@ -23,13 +24,13 @@ func (cmd *AddChildren) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "add-children",
Description: "Add a child or children to this entity from the supplied YAML",
Usage: "BROOKLYN_NAME add-children FILEPATH",
Usage: "BROOKLYN_NAME [ SCOPE ] add-children FILEPATH",
Flags: []cli.Flag{},
}
}

func (cmd *AddChildren) Run(c *cli.Context) {
activity := entities.AddChildren(cmd.network, c.Args()[0], c.Args()[1], c.Args()[2])
func (cmd *AddChildren) Run(scope scope.Scope, c *cli.Context) {
activity := entities.AddChildren(cmd.network, scope.Application, scope.Entity, c.Args().First())
table := terminal.NewTable([]string{"Id", "Task", "Submitted", "Status"})
table.Add(activity.Id, activity.DisplayName, time.Unix(activity.SubmitTimeUtc/1000, 0).Format(time.UnixDate), activity.CurrentStatus)

Expand Down
5 changes: 3 additions & 2 deletions commands/add-policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
//"github.com/brooklyncentral/brooklyn-cli/api/entity_policies"
"github.com/brooklyncentral/brooklyn-cli/command_metadata"
"github.com/brooklyncentral/brooklyn-cli/net"
"github.com/brooklyncentral/brooklyn-cli/scope"
)

type AddPolicy struct {
Expand All @@ -21,11 +22,11 @@ func (cmd *AddPolicy) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "add-policy",
Description: "Add a new policy",
Usage: "BROOKLYN_NAME add-policy APPLICATION ENTITY POLICY_TYPE",
Usage: "BROOKLYN_NAME [ SCOPE ] add-policy APPLICATION ENTITY POLICY_TYPE",
Flags: []cli.Flag{},
}
}

func (cmd *AddPolicy) Run(c *cli.Context) {
func (cmd *AddPolicy) Run(scope scope.Scope, c *cli.Context) {
// Todo
}
5 changes: 3 additions & 2 deletions commands/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/brooklyncentral/brooklyn-cli/terminal"
"strings"
"github.com/brooklyncentral/brooklyn-cli/command_metadata"
"github.com/brooklyncentral/brooklyn-cli/scope"
)

type ListApplicationSubCommand struct {
Expand All @@ -23,12 +24,12 @@ func (cmd *ListApplicationSubCommand) Metadata() command_metadata.CommandMetadat
return command_metadata.CommandMetadata{
Name: "application",
Description: "Show the status and location of running applications",
Usage: "list application",
Usage: "BROOKLYN_NAME [ SCOPE ] application",
Flags: []cli.Flag{},
}
}

func (cmd *ListApplicationSubCommand) Run(c *cli.Context) {
func (cmd *ListApplicationSubCommand) Run(scope scope.Scope, c *cli.Context) {
applications := application.Applications(cmd.network)

table := terminal.NewTable([]string{"Id", "Name", "Status", "Location"})
Expand Down
Loading

0 comments on commit 32686c1

Please sign in to comment.