Skip to content

Commit

Permalink
add new commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Moss committed Apr 1, 2015
1 parent f47a6d8 commit 3ccebba
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 8 deletions.
23 changes: 23 additions & 0 deletions api/access_control/access_control.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package access_control

import(
"encoding/json"
"fmt"
"github.com/robertgmoss/brooklyn-cli/models"
"github.com/robertgmoss/brooklyn-cli/net"
)

func Access(network *net.Network) models.AccessSummary {
url := fmt.Sprintf("/v1/access")
body, err := network.SendGetRequest(url)
if err != nil {
fmt.Println(err)
}

var access models.AccessSummary
err = json.Unmarshal(body, &access)
if err != nil {
fmt.Println(err)
}
return access
}
9 changes: 9 additions & 0 deletions api/activities/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@ func ActivityChildren(network *net.Network, activity string) []models.TaskSummar
}
return tasks
}

func ActivityStream(network *net.Network, activity, streamId string) string {
url := fmt.Sprintf("/v1/activities/%s/stream/%s", activity, streamId)
body, err := network.SendGetRequest(url)
if err != nil {
fmt.Println(err)
}
return string(body)
}
27 changes: 19 additions & 8 deletions api/entities/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ import (
"fmt"
"github.com/robertgmoss/brooklyn-cli/models"
"github.com/robertgmoss/brooklyn-cli/net"
"net/url"
)

func Spec(network *net.Network, application, entity string) string {
url := fmt.Sprintf("/v1/applications/%s/entities/%s/spec", application, entity)
body, err := network.SendGetRequest(url)
urlStr := fmt.Sprintf("/v1/applications/%s/entities/%s/spec", application, entity)
body, err := network.SendGetRequest(urlStr)
if err != nil {
fmt.Println(err)
}
return string(body)
}

func EntityList(network *net.Network, application string) []models.EntitySummary {
url := fmt.Sprintf("/v1/applications/%s/entities", application)
body, err := network.SendGetRequest(url)
urlStr := fmt.Sprintf("/v1/applications/%s/entities", application)
body, err := network.SendGetRequest(urlStr)
if err != nil {
fmt.Println(err)
}
Expand All @@ -32,8 +33,8 @@ func EntityList(network *net.Network, application string) []models.EntitySummary
}

func Children(network *net.Network, application, entity string) []models.EntitySummary {
url := fmt.Sprintf("/v1/applications/%s/entities/%s/children", application, entity)
body, err := network.SendGetRequest(url)
urlStr := fmt.Sprintf("/v1/applications/%s/entities/%s/children", application, entity)
body, err := network.SendGetRequest(urlStr)
if err != nil {
fmt.Println(err)
}
Expand All @@ -47,8 +48,8 @@ func Children(network *net.Network, application, entity string) []models.EntityS
}

func AddChildren(network *net.Network, application, entity, filePath string) models.TaskSummary {
url := fmt.Sprintf("/v1/applications/%s/entities/%s/children", application, entity)
body, err := network.SendPostFileRequest(url, filePath)
urlStr := fmt.Sprintf("/v1/applications/%s/entities/%s/children", application, entity)
body, err := network.SendPostFileRequest(urlStr, filePath)
if err != nil {
fmt.Println(err)
}
Expand All @@ -60,3 +61,13 @@ func AddChildren(network *net.Network, application, entity, filePath string) mod
}
return response
}

func Rename(network *net.Network, application, entity, newName string) string {
urlStr := fmt.Sprintf("/v1/applications/%s/entities/%s/name?name=%s", application, entity, url.QueryEscape(newName))
body, err := network.SendEmptyPostRequest(urlStr)
if err != nil {
fmt.Println(err)
}

return string(body)
}
11 changes: 11 additions & 0 deletions api/entity_config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ func ConfigList(network *net.Network, application, entity string) []models.Confi
return configList
}

func SetConfig(network *net.Network, application, entity, config, value string) string {
url := fmt.Sprintf("/v1/applications/%s/entities/%s/config/%s", application, entity, config)
val := []byte(value)
body, err := network.SendPostRequest(url, val)
if err != nil {
fmt.Println(err)
}

return string(body)
}

func ConfigValue(network *net.Network, application, entity, config string) string {
url := fmt.Sprintf("/v1/applications/%s/entities/%s/config/%s", application, entity, config)
body, err := network.SendGetRequest(url)
Expand Down
4 changes: 4 additions & 0 deletions command_factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type concreteFactory struct {

func NewFactory(network *net.Network, config *io.Config) (factory concreteFactory) {
factory.cmdsByName = make(map[string]command.Command)
factory.cmdsByName["access"] = commands.NewAccess(network)
factory.cmdsByName["login"] = commands.NewLogin(network, config)
factory.cmdsByName["tree"] = commands.NewTree(network)
factory.cmdsByName["entities"] = commands.NewEntities(network)
Expand All @@ -42,11 +43,14 @@ func NewFactory(network *net.Network, config *io.Config) (factory concreteFactor
factory.cmdsByName["stop-policy"] = commands.NewStopPolicy(network)
factory.cmdsByName["destroy-policy"] = commands.NewDestroyPolicy(network)
factory.cmdsByName["config"] = commands.NewConfig(network)
factory.cmdsByName["set-config"] = commands.NewSetConfig(network)
factory.cmdsByName["locations"] = commands.NewLocations(network)
factory.cmdsByName["activity"] = commands.NewActivity(network)
factory.cmdsByName["activity-children"] = commands.NewActivityChildren(network)
factory.cmdsByName["activity-stream"] = commands.NewActivityStream(network)
factory.cmdsByName["activities"] = commands.NewActivities(network)
factory.cmdsByName["spec"] = commands.NewSpec(network)
factory.cmdsByName["rename-entity"] = commands.NewRename(network)
return factory
}

Expand Down
33 changes: 33 additions & 0 deletions commands/access.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package commands

import (
"fmt"
"github.com/codegangsta/cli"
"github.com/robertgmoss/brooklyn-cli/api/access_control"
"github.com/robertgmoss/brooklyn-cli/command_metadata"
"github.com/robertgmoss/brooklyn-cli/net"
)

type Access struct {
network *net.Network
}

func NewAccess(network *net.Network) (cmd *Access) {
cmd = new(Access)
cmd.network = network
return
}

func (cmd *Access) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "access",
Description: "Show access control",
Usage: "BROOKLYN_NAME access",
Flags: []cli.Flag{},
}
}

func (cmd *Access) Run(c *cli.Context) {
access := access_control.Access(cmd.network)
fmt.Println("Location Provisioning Allowed:", access.LocationProvisioningAllowed)
}
33 changes: 33 additions & 0 deletions commands/activity-stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package commands

import (
"fmt"
"github.com/codegangsta/cli"
"github.com/robertgmoss/brooklyn-cli/api/activities"
"github.com/robertgmoss/brooklyn-cli/command_metadata"
"github.com/robertgmoss/brooklyn-cli/net"
)

type ActivityStream struct {
network *net.Network
}

func NewActivityStream(network *net.Network) (cmd *ActivityStream) {
cmd = new(ActivityStream)
cmd.network = network
return
}

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",
Flags: []cli.Flag{},
}
}

func (cmd *ActivityStream) Run(c *cli.Context) {
activityStream := activities.ActivityStream(cmd.network, c.Args()[0], c.Args()[1])
fmt.Println(activityStream)
}
33 changes: 33 additions & 0 deletions commands/rename-entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package commands

import (
"fmt"
"github.com/codegangsta/cli"
"github.com/robertgmoss/brooklyn-cli/api/entities"
"github.com/robertgmoss/brooklyn-cli/command_metadata"
"github.com/robertgmoss/brooklyn-cli/net"
)

type Rename struct {
network *net.Network
}

func NewRename(network *net.Network) (cmd *Rename) {
cmd = new(Rename)
cmd.network = network
return
}

func (cmd *Rename) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "rename-entity",
Description: "Rename an entity",
Usage: "BROOKLYN_NAME rename-entity APPLICATION ENTITY",
Flags: []cli.Flag{},
}
}

func (cmd *Rename) Run(c *cli.Context) {
rename := entities.Rename(cmd.network, c.Args()[0], c.Args()[1], c.Args()[2])
fmt.Println(rename)
}
33 changes: 33 additions & 0 deletions commands/set-config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package commands

import (
"fmt"
"github.com/codegangsta/cli"
"github.com/robertgmoss/brooklyn-cli/api/entity_config"
"github.com/robertgmoss/brooklyn-cli/command_metadata"
"github.com/robertgmoss/brooklyn-cli/net"
)

type SetConfig struct {
network *net.Network
}

func NewSetConfig(network *net.Network) (cmd *SetConfig) {
cmd = new(SetConfig)
cmd.network = network
return
}

func (cmd *SetConfig) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "set-config",
Description: "Set config for an entity",
Usage: "BROOKLYN_NAME set-config APPLICATION ENTITY CONFIG_KEY VALUE",
Flags: []cli.Flag{},
}
}

func (cmd *SetConfig) Run(c *cli.Context) {
response := entity_config.SetConfig(cmd.network, c.Args()[0], c.Args()[1], c.Args()[2], c.Args()[3])
fmt.Println(response)
}
6 changes: 6 additions & 0 deletions models/access.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package models

type AccessSummary struct {
Links map[string]URI `json:"links"`
LocationProvisioningAllowed bool `json:"locationProvisioningAllowed"`
}
8 changes: 8 additions & 0 deletions net/net.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net

import (
"bytes"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -72,6 +73,13 @@ func (net *Network) SendEmptyPostRequest(url string) ([]byte, error) {
return body, err
}

func (net *Network) SendPostRequest(urlStr string, data []byte) ([]byte, error) {
req := net.NewPostRequest(urlStr, bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
body, err := net.SendRequest(req)
return body, err
}

func (net *Network) SendPostFileRequest(url, filePath string) ([]byte, error) {
file, err := os.Open(filepath.Clean(filePath))
if err != nil {
Expand Down

0 comments on commit 3ccebba

Please sign in to comment.