From 0a0ea6701d38bb91c14906a3147ad0754b938721 Mon Sep 17 00:00:00 2001 From: Geoff Macartney Date: Mon, 2 May 2016 22:13:01 +0100 Subject: [PATCH] Support getting arguments from URL or file. --- api/application/applications.go | 4 +-- api/catalog/catalog.go | 4 +-- api/entities/entities.go | 4 +-- commands/add-catalog.go | 4 +-- commands/add-children.go | 2 +- commands/deploy.go | 6 ++--- net/net.go | 43 ++++++++++++++++++++++++++++----- 7 files changed, 49 insertions(+), 18 deletions(-) diff --git a/api/application/applications.go b/api/application/applications.go index 7f44ce0..f46ebac 100644 --- a/api/application/applications.go +++ b/api/application/applications.go @@ -48,10 +48,10 @@ func Applications(network *net.Network) ([]models.ApplicationSummary, error) { return appSummary, err } -func Create(network *net.Network, filePath string) (models.TaskSummary, error) { +func Create(network *net.Network, resource string) (models.TaskSummary, error) { url := "/v1/applications" var response models.TaskSummary - body, err := network.SendPostFileRequest(url, filePath, "application/json") + body, err := network.SendPostResourceRequest(url, resource, "application/json") if err != nil { return response, err } diff --git a/api/catalog/catalog.go b/api/catalog/catalog.go index b087530..dd5b75a 100644 --- a/api/catalog/catalog.go +++ b/api/catalog/catalog.go @@ -161,9 +161,9 @@ func Locations(network *net.Network) (models.CatalogLocationSummary, error) { return catalogLocation, err } -func AddCatalog(network *net.Network, filePath string) (string, error) { +func AddCatalog(network *net.Network, resource string) (string, error) { url := "/v1/catalog" - body, err := network.SendPostFileRequest(url, filePath, "application/json") + body, err := network.SendPostResourceRequest(url, resource, "application/json") if err != nil { return "", err } diff --git a/api/entities/entities.go b/api/entities/entities.go index f132827..90dbb49 100644 --- a/api/entities/entities.go +++ b/api/entities/entities.go @@ -60,10 +60,10 @@ func Children(network *net.Network, application, entity string) ([]models.Entity return entityList, err } -func AddChildren(network *net.Network, application, entity, filePath string) (models.TaskSummary, error) { +func AddChildren(network *net.Network, application, entity, resource string) (models.TaskSummary, error) { urlStr := fmt.Sprintf("/v1/applications/%s/entities/%s/children", application, entity) var response models.TaskSummary - body, err := network.SendPostFileRequest(urlStr, filePath, "application/json") + body, err := network.SendPostResourceRequest(urlStr, resource, "application/json") if err != nil { return response, err } diff --git a/commands/add-catalog.go b/commands/add-catalog.go index bf79f23..a7d00bc 100644 --- a/commands/add-catalog.go +++ b/commands/add-catalog.go @@ -41,8 +41,8 @@ func NewAddCatalog(network *net.Network) (cmd *AddCatalog) { 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", + Description: "* Add a new catalog item from the supplied YAML (a file or http URL)", + Usage: "BROOKLYN_NAME add-catalog ( FILEPATH | URL )", Flags: []cli.Flag{}, } } diff --git a/commands/add-children.go b/commands/add-children.go index 6492044..b9cb8de 100644 --- a/commands/add-children.go +++ b/commands/add-children.go @@ -43,7 +43,7 @@ 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 SCOPE add-children FILEPATH", + Usage: "BROOKLYN_NAME SCOPE add-children ( FILEPATH | URL )", Flags: []cli.Flag{}, } } diff --git a/commands/deploy.go b/commands/deploy.go index 3b0607c..8a60248 100644 --- a/commands/deploy.go +++ b/commands/deploy.go @@ -45,8 +45,8 @@ func NewDeploy(network *net.Network) (cmd *Deploy) { func (cmd *Deploy) Metadata() command_metadata.CommandMetadata { return command_metadata.CommandMetadata{ Name: "deploy", - Description: "Deploy a new application from the given YAML (read from file or stdin)", - Usage: "BROOKLYN_NAME deploy ( | - )", + Description: "Deploy a new application from the given YAML (read from file or URL, or stdin)", + Usage: "BROOKLYN_NAME deploy ( FILE | URL | '-' )", Flags: []cli.Flag{}, } } @@ -60,7 +60,7 @@ func (cmd *Deploy) Run(scope scope.Scope, c *cli.Context) { var err error var blueprint []byte if c.Args().First() == "" { - error_handler.ErrorExit("A filename or '-' must be provided as the first argument", error_handler.CLIUsageErrorExitCode) + error_handler.ErrorExit("A filename or URL or '-' must be provided as the first argument", error_handler.CLIUsageErrorExitCode) } if c.Args().First() == "-" { blueprint, err = ioutil.ReadAll(os.Stdin) diff --git a/net/net.go b/net/net.go index f910a17..d0383f4 100644 --- a/net/net.go +++ b/net/net.go @@ -147,18 +147,49 @@ func (net *Network) SendPostRequest(urlStr string, data []byte) ([]byte, error) return body, err } -func (net *Network) SendPostFileRequest(url, filePath string, contentType string) ([]byte, error) { +func (net *Network) SendPostResourceRequest(restUrl string, resourceUrl string, contentType string) ([]byte, error) { + resource, err := openResource(resourceUrl) + defer resource.Close() + req := net.NewPostRequest(restUrl, resource) + req.Header.Set("Content-Type", contentType) + body, err := net.SendRequest(req) + return body, err +} + +func openResource(resourceUrl string) (io.ReadCloser, error) { + u, err := url.Parse(resourceUrl) + if err != nil { + return nil, err + } + if "" == u.Scheme || "file" == u.Scheme { + return openFileResource(u) + + } else if "http" == u.Scheme || "https" == u.Scheme { + return openHttpResource(resourceUrl) + + } else { + return nil, errors.New("Unrecognised protocol scheme: " + u.Scheme) + } +} + +func openFileResource(url *url.URL) (io.ReadCloser, error) { + filePath := url.Path; file, err := os.Open(filepath.Clean(filePath)) if err != nil { return nil, err } - defer file.Close() - req := net.NewPostRequest(url, file) - req.Header.Set("Content-Type", contentType) - body, err := net.SendRequest(req) - return body, err + return file, nil } +func openHttpResource(resourceUrl string) (io.ReadCloser, error) { + resp, err := http.Get(resourceUrl) + if err != nil { + return nil, err + } + return resp.Body, nil +} + + func VerifyLoginURL(network *Network) error { url, err := url.Parse(network.BrooklynUrl) if err != nil {