Skip to content
Permalink
Browse files
Added the ability to upload a zip via the CLI
  • Loading branch information
graeme.miller committed Apr 7, 2017
1 parent df47be8 commit 1cf07d53241c80e1552b8af224f634ce5c7f8170
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 5 deletions.
@@ -23,6 +23,14 @@ import (
"fmt"
"github.com/apache/brooklyn-client/cli/models"
"github.com/apache/brooklyn-client/cli/net"
"net/url"
"path/filepath"
"errors"
"os"
"strings"
"archive/zip"
"io/ioutil"
"bytes"
)

func Icon(network *net.Network, itemId string) ([]byte, error) {
@@ -170,15 +178,102 @@ func Locations(network *net.Network) ([]models.CatalogItemSummary, error) {
return catalogLocations, err
}


func ZipResource(resource string) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
writer := zip.NewWriter(buf)
defer writer.Close()

walkFn := func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}

relativePath, err := filepath.Rel(resource, path)
if err != nil {
return err
}
f, err := writer.Create(relativePath)
if err != nil {
return err
}

fileBytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}

_, err = f.Write(fileBytes)
if err != nil {
return err
}
return nil
}

err := filepath.Walk(resource, walkFn)

return buf, err;
}

func AddCatalog(network *net.Network, resource string) (map[string]models.CatalogEntitySummary, error) {
url := "/v1/catalog"
urlString := "/v1/catalog"
var entities map[string]models.CatalogEntitySummary
body, err := network.SendPostResourceRequest(url, resource, "application/json")

//Assume application/json. This is correct for http/file resources.
//Zips will need application/x-zip
contentType := "application/json"
u, err := url.Parse(resource)
if err != nil {
return nil, err
}

//Only deal with the below file types
if "" != u.Scheme && "file" != u.Scheme && "http" != u.Scheme && "https" != u.Scheme{
return nil, errors.New("Unrecognised protocol scheme: " + u.Scheme)
}

if "" == u.Scheme || "file" == u.Scheme {
file, err := os.Open(filepath.Clean(resource))
if err != nil {
return nil, err
}

fileStat, err := file.Stat()
if err != nil {
return nil, err
}

if fileStat.IsDir() {
//A dir is a special case, we need to zip it up, and call a different network method
buf, err := ZipResource(resource)
if err != nil {
return nil, err
}
body, err := network.SendPostRequestWithContentType(urlString, buf.Bytes(), "application/x-zip")
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &entities)
return entities, err
} else {
extension := filepath.Ext(resource)
lowercaseExtension := strings.ToLower(extension)
if lowercaseExtension == ".zip" {
contentType = "application/x-zip"
} else if lowercaseExtension == ".jar" {
contentType = "application/x-jar"
}
}

}

body, err := network.SendPostResourceRequest(urlString, resource, contentType)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &entities)
return entities, nil

return entities, err
}

func Reset(network *net.Network) (string, error) {
@@ -166,13 +166,17 @@ func (net *Network) SendEmptyPostRequest(url string) ([]byte, error) {
return body, err
}

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

func (net *Network) SendPostRequest(urlStr string, data []byte) ([]byte, error) {
return net.SendPostRequestWithContentType(urlStr, data, "application/json")
}

func (net *Network) SendPostResourceRequest(restUrl string, resourceUrl string, contentType string) ([]byte, error) {
resource, err := net.openResource(resourceUrl)
if err != nil {

0 comments on commit 1cf07d5

Please sign in to comment.