summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/k8splugin/api/defhandler.go6
-rw-r--r--src/k8splugin/api/defhandler_test.go10
-rw-r--r--src/k8splugin/internal/rb/definition.go43
-rw-r--r--src/k8splugin/internal/rb/definition_test.go89
4 files changed, 128 insertions, 20 deletions
diff --git a/src/k8splugin/api/defhandler.go b/src/k8splugin/api/defhandler.go
index 21e5e768..44521dd1 100644
--- a/src/k8splugin/api/defhandler.go
+++ b/src/k8splugin/api/defhandler.go
@@ -54,12 +54,6 @@ func (h rbDefinitionHandler) createHandler(w http.ResponseWriter, r *http.Reques
return
}
- // Chart Name is required
- if v.ChartName == "" {
- http.Error(w, "Missing chart name in POST request", http.StatusBadRequest)
- return
- }
-
ret, err := h.client.Create(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
diff --git a/src/k8splugin/api/defhandler_test.go b/src/k8splugin/api/defhandler_test.go
index 20ef537e..48e2406c 100644
--- a/src/k8splugin/api/defhandler_test.go
+++ b/src/k8splugin/api/defhandler_test.go
@@ -125,16 +125,6 @@ func TestRBDefCreateHandler(t *testing.T) {
expectedCode: http.StatusBadRequest,
rbDefClient: &mockRBDefinition{},
},
- {
- label: "Missing Chart Name in Request Body",
- reader: bytes.NewBuffer([]byte(`{
- "name":"testresourcebundle",
- "description":"test description",
- "service-type":"firewall"
- }`)),
- expectedCode: http.StatusBadRequest,
- rbDefClient: &mockRBDefinition{},
- },
}
for _, testCase := range testCases {
diff --git a/src/k8splugin/internal/rb/definition.go b/src/k8splugin/internal/rb/definition.go
index 19844990..4eaa9578 100644
--- a/src/k8splugin/internal/rb/definition.go
+++ b/src/k8splugin/internal/rb/definition.go
@@ -19,8 +19,12 @@ package rb
import (
"bytes"
"encoding/base64"
- "k8splugin/internal/db"
+ "io/ioutil"
"log"
+ "os"
+ "path/filepath"
+
+ "k8splugin/internal/db"
uuid "github.com/hashicorp/go-uuid"
pkgerrors "github.com/pkg/errors"
@@ -142,8 +146,8 @@ func (v *DefinitionClient) Delete(id string) error {
// Upload the contents of resource bundle into database
func (v *DefinitionClient) Upload(id string, inp []byte) error {
- //ignore the returned data here
- _, err := v.Get(id)
+ //Check if definition metadata exists
+ def, err := v.Get(id)
if err != nil {
return pkgerrors.Errorf("Invalid Definition ID provided: %s", err.Error())
}
@@ -153,6 +157,39 @@ func (v *DefinitionClient) Upload(id string, inp []byte) error {
return pkgerrors.Errorf("Error in file format: %s", err.Error())
}
+ //Detect chart name from data if it was not provided originally
+ if def.ChartName == "" {
+ path, err := ExtractTarBall(bytes.NewBuffer(inp))
+ if err != nil {
+ return pkgerrors.Wrap(err, "Detecting chart name")
+ }
+
+ finfo, err := ioutil.ReadDir(path)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Detecting chart name")
+ }
+
+ //Store the first directory with Chart.yaml found as the chart name
+ for _, f := range finfo {
+ if f.IsDir() {
+ //Check if Chart.yaml exists
+ if _, err = os.Stat(filepath.Join(path, f.Name(), "Chart.yaml")); err == nil {
+ def.ChartName = f.Name()
+ break
+ }
+ }
+ }
+
+ if def.ChartName == "" {
+ return pkgerrors.New("Unable to detect chart name")
+ }
+
+ _, err = v.Create(def)
+ if err != nil {
+ return pkgerrors.Wrap(err, "Storing updated chart metadata")
+ }
+ }
+
//Encode given byte stream to text for storage
encodedStr := base64.StdEncoding.EncodeToString(inp)
err = db.DBconn.Create(v.storeName, id, v.tagContent, encodedStr)
diff --git a/src/k8splugin/internal/rb/definition_test.go b/src/k8splugin/internal/rb/definition_test.go
index f720b6a9..b1875fd7 100644
--- a/src/k8splugin/internal/rb/definition_test.go
+++ b/src/k8splugin/internal/rb/definition_test.go
@@ -281,8 +281,95 @@ func TestUploadDefinition(t *testing.T) {
mockdb *db.MockDB
}{
{
- label: "Upload Resource Bundle Definition",
+ label: "Upload With Chart Name Detection",
inp: "123e4567-e89b-12d3-a456-426655440000",
+ //Binary format for testchart/Chart.yaml
+ content: []byte{
+ 0x1f, 0x8b, 0x08, 0x08, 0xb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67, 0x8a, 0x65, 0x1c, 0x17, 0xa9, 0xdd,
+ 0xe4, 0xc3, 0xfa, 0x5e, 0x1e, 0xe2, 0xaa, 0x97,
+ 0x43, 0x7b, 0xc8, 0xeb, 0x3a, 0xc9, 0xe3, 0xf6,
+ 0x25, 0xa6, 0x21, 0xee, 0x7b, 0xa6, 0x18, 0x42,
+ 0x1f, 0x67, 0x72, 0xea, 0x9e, 0x62, 0x77, 0xbc,
+ 0x44, 0x97, 0xa6, 0xd4, 0xc5, 0x5f, 0x7f, 0x0b,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0xb8, 0x09, 0x1f, 0xae,
+ 0x48, 0xfe, 0xe8, 0x00, 0x28, 0x00, 0x00,
+ },
+ mockdb: &db.MockDB{
+ Items: map[string]map[string][]byte{
+ "123e4567-e89b-12d3-a456-426655440000": {
+ "metadata": []byte(
+ "{\"name\":\"testresourcebundle\"," +
+ "\"description\":\"testresourcebundle\"," +
+ "\"uuid\":\"123e4567-e89b-12d3-a456-426655440000\"," +
+ "\"service-type\":\"firewall\"}"),
+ },
+ },
+ },
+ },
+ {
+ label: "Upload With Chart Name",
+ inp: "123e4567-e89b-12d3-a456-426655440000",
+ content: []byte{
+ 0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x6b, 0xf4, 0x5b,
+ 0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
+ 0x61, 0x72, 0x00, 0xed, 0xce, 0x41, 0x0a, 0xc2,
+ 0x30, 0x10, 0x85, 0xe1, 0xac, 0x3d, 0x45, 0x4e,
+ 0x50, 0x12, 0xd2, 0xc4, 0xe3, 0x48, 0xa0, 0x01,
+ 0x4b, 0x52, 0x0b, 0xed, 0x88, 0x1e, 0xdf, 0x48,
+ 0x11, 0x5c, 0x08, 0xa5, 0x8b, 0x52, 0x84, 0xff,
+ 0xdb, 0xbc, 0x61, 0x66, 0x16, 0x4f, 0xd2, 0x2c,
+ 0x8d, 0x3c, 0x45, 0xed, 0xc8, 0x54, 0x21, 0xb4,
+ 0xef, 0xb4, 0x67, 0x6f, 0xbe, 0x73, 0x61, 0x9d,
+ 0xb2, 0xce, 0xd5, 0x55, 0xf0, 0xde, 0xd7, 0x3f,
+ 0xdb, 0xd6, 0x49, 0x69, 0xb3, 0x67, 0xa9, 0x8f,
+ 0xfb, 0x2c, 0x71, 0xd2, 0x5a, 0xc5, 0xee, 0x92,
+ 0x73, 0x8e, 0x43, 0x7f, 0x4b, 0x3f, 0xff, 0xd6,
+ 0xee, 0x7f, 0xea, 0x9a, 0x4a, 0x19, 0x1f, 0xe3,
+ 0x54, 0xba, 0xd3, 0xd1, 0x55, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x1b, 0xbc, 0x00, 0xb5, 0xe8,
+ 0x4a, 0xf9, 0x00, 0x28, 0x00, 0x00,
+ },
+ mockdb: &db.MockDB{
+ Items: map[string]map[string][]byte{
+ "123e4567-e89b-12d3-a456-426655440000": {
+ "metadata": []byte(
+ "{\"name\":\"testresourcebundle\"," +
+ "\"description\":\"testresourcebundle\"," +
+ "\"chart-name\":\"testchart\"," +
+ "\"uuid\":\"123e4567-e89b-12d3-a456-426655440000\"," +
+ "\"service-type\":\"firewall\"}"),
+ },
+ },
+ },
+ },
+ {
+ label: "Upload Without Chart.yaml",
+ inp: "123e4567-e89b-12d3-a456-426655440000",
+ expectedError: "Unable to detect chart name",
content: []byte{
0x1f, 0x8b, 0x08, 0x08, 0xb0, 0x6b, 0xf4, 0x5b,
0x00, 0x03, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,