brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [1/2] brooklyn-client git commit: Added the ability to upload a zip via the CLI
Date Mon, 24 Apr 2017 14:12:16 GMT
Repository: brooklyn-client
Updated Branches:
  refs/heads/master 95d1fb0d7 -> 3486f6ff4


Added the ability to upload a zip via the CLI


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-client/commit/1cf07d53
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-client/tree/1cf07d53
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-client/diff/1cf07d53

Branch: refs/heads/master
Commit: 1cf07d53241c80e1552b8af224f634ce5c7f8170
Parents: df47be8
Author: graeme.miller <graeme.miller@cloudsoftcorp.com>
Authored: Wed Apr 5 16:12:04 2017 +0100
Committer: graeme.miller <graeme.miller@cloudsoftcorp.com>
Committed: Fri Apr 7 11:51:13 2017 +0100

----------------------------------------------------------------------
 cli/api/catalog/catalog.go | 101 ++++++++++++++++++++++++++++++++++++++--
 cli/net/net.go             |   8 +++-
 2 files changed, 104 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-client/blob/1cf07d53/cli/api/catalog/catalog.go
----------------------------------------------------------------------
diff --git a/cli/api/catalog/catalog.go b/cli/api/catalog/catalog.go
index 2ff3a88..0928590 100644
--- a/cli/api/catalog/catalog.go
+++ b/cli/api/catalog/catalog.go
@@ -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) {

http://git-wip-us.apache.org/repos/asf/brooklyn-client/blob/1cf07d53/cli/net/net.go
----------------------------------------------------------------------
diff --git a/cli/net/net.go b/cli/net/net.go
index 73cfdea..b6ffb19 100644
--- a/cli/net/net.go
+++ b/cli/net/net.go
@@ -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 {


Mime
View raw message