-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add capsule creation method Also add unit test and accept test Depends-On: #944 Change-Id: I29f9c56b0aa71ef4d20917cbc216b909a253f473 Signed-off-by: Kevin Zhao <kevin.zhao@arm.com> * Remove TE and Validate method Change-Id: I06f3295970f5945998ca75a2b5dc40b427eef8be Signed-off-by: Kevin Zhao <kevin.zhao@arm.com> * Remove the unused error type Change-Id: Ib6f016107d3c5c367ceeb968c699c981c3238643 Signed-off-by: Kevin Zhao <kevin.zhao@arm.com> * Remove the invalid change in orchestration Change-Id: Ib7a0e4da4c69f416e425229910b48d128facc010 Signed-off-by: Kevin Zhao <kevin.zhao@arm.com>
- Loading branch information
1 parent
27dfd3c
commit a3ac253
Showing
10 changed files
with
411 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package capsules | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gophercloud/gophercloud" | ||
) | ||
|
||
type ErrInvalidDataFormat struct { | ||
gophercloud.BaseError | ||
} | ||
|
||
func (e ErrInvalidDataFormat) Error() string { | ||
return fmt.Sprintf("Data in neither json nor yaml format.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package capsules | ||
|
||
// ValidJSONTemplate is a valid OpenStack Capsule template in JSON format | ||
const ValidJSONTemplate = ` | ||
{ | ||
"capsuleVersion": "beta", | ||
"kind": "capsule", | ||
"metadata": { | ||
"labels": { | ||
"app": "web", | ||
"app1": "web1" | ||
}, | ||
"name": "template" | ||
}, | ||
"restartPolicy": "Always", | ||
"spec": { | ||
"containers": [ | ||
{ | ||
"command": [ | ||
"/bin/bash" | ||
], | ||
"env": { | ||
"ENV1": "/usr/local/bin", | ||
"ENV2": "/usr/bin" | ||
}, | ||
"image": "ubuntu", | ||
"imagePullPolicy": "ifnotpresent", | ||
"ports": [ | ||
{ | ||
"containerPort": 80, | ||
"hostPort": 80, | ||
"name": "nginx-port", | ||
"protocol": "TCP" | ||
} | ||
], | ||
"resources": { | ||
"requests": { | ||
"cpu": 1, | ||
"memory": 1024 | ||
} | ||
}, | ||
"workDir": "/root" | ||
} | ||
] | ||
} | ||
} | ||
` | ||
|
||
// ValidYAMLTemplate is a valid OpenStack Capsule template in YAML format | ||
const ValidYAMLTemplate = ` | ||
capsuleVersion: beta | ||
kind: capsule | ||
metadata: | ||
name: template | ||
labels: | ||
app: web | ||
app1: web1 | ||
restartPolicy: Always | ||
spec: | ||
containers: | ||
- image: ubuntu | ||
command: | ||
- "/bin/bash" | ||
imagePullPolicy: ifnotpresent | ||
workDir: /root | ||
ports: | ||
- name: nginx-port | ||
containerPort: 80 | ||
hostPort: 80 | ||
protocol: TCP | ||
resources: | ||
requests: | ||
cpu: 1 | ||
memory: 1024 | ||
env: | ||
ENV1: /usr/local/bin | ||
ENV2: /usr/bin | ||
` | ||
|
||
// ValidJSONTemplateParsed is the expected parsed version of ValidJSONTemplate | ||
var ValidJSONTemplateParsed = map[string]interface{}{ | ||
"capsuleVersion": "beta", | ||
"kind": "capsule", | ||
"restartPolicy": "Always", | ||
"metadata": map[string]interface{}{ | ||
"name": "template", | ||
"labels": map[string]string{ | ||
"app": "web", | ||
"app1": "web1", | ||
}, | ||
}, | ||
"spec": map[string]interface{}{ | ||
"containers": []map[string]interface{}{ | ||
map[string]interface{}{ | ||
"image": "ubuntu", | ||
"command": []interface{}{ | ||
"/bin/bash", | ||
}, | ||
"imagePullPolicy": "ifnotpresent", | ||
"workDir": "/root", | ||
"ports": []interface{}{ | ||
map[string]interface{}{ | ||
"name": "nginx-port", | ||
"containerPort": float64(80), | ||
"hostPort": float64(80), | ||
"protocol": "TCP", | ||
}, | ||
}, | ||
"resources": map[string]interface{}{ | ||
"requests": map[string]interface{}{ | ||
"cpu": float64(1), | ||
"memory": float64(1024), | ||
}, | ||
}, | ||
"env": map[string]interface{}{ | ||
"ENV1": "/usr/local/bin", | ||
"ENV2": "/usr/bin", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
// ValidYAMLTemplateParsed is the expected parsed version of ValidYAMLTemplate | ||
var ValidYAMLTemplateParsed = map[string]interface{}{ | ||
"capsuleVersion": "beta", | ||
"kind": "capsule", | ||
"restartPolicy": "Always", | ||
"metadata": map[string]interface{}{ | ||
"name": "template", | ||
"labels": map[string]string{ | ||
"app": "web", | ||
"app1": "web1", | ||
}, | ||
}, | ||
"spec": map[interface{}]interface{}{ | ||
"containers": []map[interface{}]interface{}{ | ||
map[interface{}]interface{}{ | ||
"image": "ubuntu", | ||
"command": []interface{}{ | ||
"/bin/bash", | ||
}, | ||
"imagePullPolicy": "ifnotpresent", | ||
"workDir": "/root", | ||
"ports": []interface{}{ | ||
map[interface{}]interface{}{ | ||
"name": "nginx-port", | ||
"containerPort": 80, | ||
"hostPort": 80, | ||
"protocol": "TCP", | ||
}, | ||
}, | ||
"resources": map[interface{}]interface{}{ | ||
"requests": map[interface{}]interface{}{ | ||
"cpu": 1, | ||
"memory": 1024, | ||
}, | ||
}, | ||
"env": map[interface{}]interface{}{ | ||
"ENV1": "/usr/local/bin", | ||
"ENV2": "/usr/bin", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package capsules | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// Template is a structure that represents OpenStack Zun Capsule templates | ||
type Template struct { | ||
// Bin stores the contents of the template or environment. | ||
Bin []byte | ||
// Parsed contains a parsed version of Bin. Since there are 2 different | ||
// fields referring to the same value, you must be careful when accessing | ||
// this filed. | ||
Parsed map[string]interface{} | ||
} | ||
|
||
// Parse will parse the contents and then validate. The contents MUST be either JSON or YAML. | ||
func (t *Template) Parse() error { | ||
if jerr := json.Unmarshal(t.Bin, &t.Parsed); jerr != nil { | ||
if yerr := yaml.Unmarshal(t.Bin, &t.Parsed); yerr != nil { | ||
return ErrInvalidDataFormat{} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package capsules | ||
|
||
import ( | ||
"testing" | ||
|
||
th "github.com/gophercloud/gophercloud/testhelper" | ||
) | ||
|
||
func TestTemplateParsing(t *testing.T) { | ||
templateJSON := new(Template) | ||
templateJSON.Bin = []byte(ValidJSONTemplate) | ||
err := templateJSON.Parse() | ||
th.AssertNoErr(t, err) | ||
th.AssertDeepEquals(t, ValidJSONTemplateParsed, templateJSON.Parsed) | ||
|
||
templateYAML := new(Template) | ||
templateYAML.Bin = []byte(ValidYAMLTemplate) | ||
err = templateYAML.Parse() | ||
th.AssertNoErr(t, err) | ||
th.AssertDeepEquals(t, ValidYAMLTemplateParsed, templateYAML.Parsed) | ||
|
||
templateInvalid := new(Template) | ||
templateInvalid.Bin = []byte("Keep Austin Weird") | ||
err = templateInvalid.Parse() | ||
if err == nil { | ||
t.Error("Template parsing did not catch invalid template") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.