Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
429 additions
and
6 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
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,33 @@ | ||
package capsules | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gophercloud/gophercloud" | ||
) | ||
|
||
type ErrInvalidEnvironment struct { | ||
gophercloud.BaseError | ||
Section string | ||
} | ||
|
||
func (e ErrInvalidEnvironment) Error() string { | ||
return fmt.Sprintf("Environment has wrong section: %s", e.Section) | ||
} | ||
|
||
type ErrInvalidDataFormat struct { | ||
gophercloud.BaseError | ||
} | ||
|
||
func (e ErrInvalidDataFormat) Error() string { | ||
return fmt.Sprintf("Data in neither json nor yaml format.") | ||
} | ||
|
||
type ErrInvalidTemplateFormatVersion struct { | ||
gophercloud.BaseError | ||
Version string | ||
} | ||
|
||
func (e ErrInvalidTemplateFormatVersion) Error() string { | ||
return fmt.Sprintf("Template format version not found.") | ||
} |
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,26 @@ | ||
package capsules | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
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") | ||
} | ||
} |
Oops, something went wrong.