Permalink
Browse files

Linting and comments fixes

  • Loading branch information...
1 parent a979f5b commit 3de347fbaecca85221e732c76b93abdc0f4a01d3 @pratikmallya pratikmallya committed Sep 22, 2015
@@ -255,7 +255,7 @@ func HandleMetadataSuccessfully(t *testing.T, output string) {
}
// ListTypesExpected represents the expected object from a ListTypes request.
-var ListTypesExpected = resourceTypes{
+var ListTypesExpected = ResourceTypes{
"OS::Nova::Server",
"OS::Heat::RandomString",
"OS::Swift::Container",
@@ -267,7 +267,7 @@ var ListTypesExpected = resourceTypes{
}
// same as above, but sorted
-var SortedListTypesExpected = resourceTypes{
+var SortedListTypesExpected = ResourceTypes{
"OS::Cinder::VolumeAttachment",
"OS::Heat::RandomString",
"OS::Nova::FloatingIP",
@@ -206,28 +206,28 @@ func (r ResourceTypePage) IsEmpty() (bool, error) {
return len(rts) == 0, nil
}
-// resourceTypes represents the type that holds the result of ExtractResourceTypes.
+// ResourceTypes represents the type that holds the result of ExtractResourceTypes.
// We define methods on this type to sort it before output
-type resourceTypes []string
+type ResourceTypes []string
-func (r resourceTypes) Len() int {
+func (r ResourceTypes) Len() int {
return len(r)
}
-func (r resourceTypes) Swap(i, j int) {
+func (r ResourceTypes) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
-func (r resourceTypes) Less(i, j int) bool {
+func (r ResourceTypes) Less(i, j int) bool {
return r[i] < r[j]
}
// ExtractResourceTypes extracts and returns resource types.
-func ExtractResourceTypes(page pagination.Page) (resourceTypes, error) {
+func ExtractResourceTypes(page pagination.Page) (ResourceTypes, error) {
casted := page.(ResourceTypePage).Body
var response struct {
- ResourceTypes resourceTypes `mapstructure:"resource_types"`
+ ResourceTypes ResourceTypes `mapstructure:"resource_types"`
}
if err := mapstructure.Decode(casted, &response); err != nil {
@@ -1,32 +1,32 @@
package stacks
import (
- "errors"
"fmt"
"strings"
)
-// an interface to represent stack environments
+// Environment is a structure that represents stack environments
type Environment struct {
TE
}
-// allowed sections in a stack environment file
+// EnvironmentSections is a map containing allowed sections in a stack environment file
var EnvironmentSections = map[string]bool{
"parameters": true,
"parameter_defaults": true,
"resource_registry": true,
}
+// Validate validates the contents of the Environment
func (e *Environment) Validate() error {
if e.Parsed == nil {
if err := e.Parse(); err != nil {
return err
}
}
- for key, _ := range e.Parsed {
+ for key := range e.Parsed {
if _, ok := EnvironmentSections[key]; !ok {
- return errors.New(fmt.Sprintf("Environment has wrong section: %s", key))
+ return fmt.Errorf("Environment has wrong section: %s", key)
}
}
return nil
@@ -51,14 +51,14 @@ func (e *Environment) getRRFileContents(ignoreIf igFunc) error {
switch rr.(type) {
// process further only if the resource registry is a map
case map[string]interface{}, map[interface{}]interface{}:
- rr_map, err := toStringKeys(rr)
+ rrMap, err := toStringKeys(rr)
if err != nil {
return err
}
// the resource registry might contain a base URL for the resource. If
// such a field is present, use it. Otherwise, use the default base URL.
var baseURL string
- if val, ok := rr_map["base_url"]; ok {
+ if val, ok := rrMap["base_url"]; ok {
baseURL = val.(string)
} else {
baseURL = e.baseURL
@@ -78,24 +78,24 @@ func (e *Environment) getRRFileContents(ignoreIf igFunc) error {
// check the `resources` section (if it exists) for more URL's. Note that
// the previous call to GetFileContents was (deliberately) not recursive
// as we want more control over where to look for URL's
- if val, ok := rr_map["resources"]; ok {
+ if val, ok := rrMap["resources"]; ok {
switch val.(type) {
// process further only if the contents are a map
case map[string]interface{}, map[interface{}]interface{}:
- resources_map, err := toStringKeys(val)
+ resourcesMap, err := toStringKeys(val)
if err != nil {
return err
}
- for _, v := range resources_map {
+ for _, v := range resourcesMap {
switch v.(type) {
case map[string]interface{}, map[interface{}]interface{}:
- resource_map, err := toStringKeys(v)
+ resourceMap, err := toStringKeys(v)
if err != nil {
return err
}
var resourceBaseURL string
// if base_url for the resource type is defined, use it
- if val, ok := resource_map["base_url"]; ok {
+ if val, ok := resourceMap["base_url"]; ok {
resourceBaseURL = val.(string)
} else {
resourceBaseURL = baseURL
@@ -76,7 +76,7 @@ func TestIgnoreIfEnvironment(t *testing.T) {
func TestGetRRFileContents(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- environment_content := `
+ environmentContent := `
heat_template_version: 2013-05-23
description:
@@ -100,7 +100,7 @@ resources:
flavor: { get_param: instance_type }
key_name: { get_param: key_name }`
- db_content := `
+ dbContent := `
heat_template_version: 2014-10-16
description:
@@ -139,7 +139,7 @@ service_db:
th.TestMethod(t, r, "GET")
w.Header().Set("Content-Type", "application/jason")
w.WriteHeader(http.StatusOK)
- fmt.Fprintf(w, environment_content)
+ fmt.Fprintf(w, environmentContent)
})
fakeDBURL := strings.Join([]string{baseurl, "my_db.yaml"}, "/")
@@ -151,7 +151,7 @@ service_db:
th.TestMethod(t, r, "GET")
w.Header().Set("Content-Type", "application/jason")
w.WriteHeader(http.StatusOK)
- fmt.Fprintf(w, db_content)
+ fmt.Fprintf(w, dbContent)
})
client := fakeClient{BaseClient: getHTTPClient()}
@@ -163,14 +163,14 @@ service_db:
th.AssertNoErr(t, err)
err = env.getRRFileContents(ignoreIfEnvironment)
th.AssertNoErr(t, err)
- expected_env_files_content := "\nheat_template_version: 2013-05-23\n\ndescription:\n Heat WordPress template to support F18, using only Heat OpenStack-native\n resource types, and without the requirement for heat-cfntools in the image.\n WordPress is web software you can use to create a beautiful website or blog.\n This template installs a single-instance WordPress deployment using a local\n MySQL database to store the data.\n\nparameters:\n\n key_name:\n type: string\n description : Name of a KeyPair to enable SSH access to the instance\n\nresources:\n wordpress_instance:\n type: OS::Nova::Server\n properties:\n image: { get_param: image_id }\n flavor: { get_param: instance_type }\n key_name: { get_param: key_name }"
- expected_db_files_content := "\nheat_template_version: 2014-10-16\n\ndescription:\n Test template for Trove resource capabilities\n\nparameters:\n db_pass:\n type: string\n hidden: true\n description: Database access password\n default: secrete\n\nresources:\n\nservice_db:\n type: OS::Trove::Instance\n properties:\n name: trove_test_db\n datastore_type: mariadb\n flavor: 1GB Instance\n size: 10\n databases:\n - name: test_data\n users:\n - name: kitchen_sink\n password: { get_param: db_pass }\n databases: [ test_data ]"
+ expectedEnvFilesContent := "\nheat_template_version: 2013-05-23\n\ndescription:\n Heat WordPress template to support F18, using only Heat OpenStack-native\n resource types, and without the requirement for heat-cfntools in the image.\n WordPress is web software you can use to create a beautiful website or blog.\n This template installs a single-instance WordPress deployment using a local\n MySQL database to store the data.\n\nparameters:\n\n key_name:\n type: string\n description : Name of a KeyPair to enable SSH access to the instance\n\nresources:\n wordpress_instance:\n type: OS::Nova::Server\n properties:\n image: { get_param: image_id }\n flavor: { get_param: instance_type }\n key_name: { get_param: key_name }"
+ expectedDBFilesContent := "\nheat_template_version: 2014-10-16\n\ndescription:\n Test template for Trove resource capabilities\n\nparameters:\n db_pass:\n type: string\n hidden: true\n description: Database access password\n default: secrete\n\nresources:\n\nservice_db:\n type: OS::Trove::Instance\n properties:\n name: trove_test_db\n datastore_type: mariadb\n flavor: 1GB Instance\n size: 10\n databases:\n - name: test_data\n users:\n - name: kitchen_sink\n password: { get_param: db_pass }\n databases: [ test_data ]"
- th.AssertEquals(t, expected_env_files_content, env.Files[fakeEnvURL])
- th.AssertEquals(t, expected_db_files_content, env.Files[fakeDBURL])
+ th.AssertEquals(t, expectedEnvFilesContent, env.Files[fakeEnvURL])
+ th.AssertEquals(t, expectedDBFilesContent, env.Files[fakeDBURL])
env.fixFileRefs()
- expected_parsed := map[string]interface{}{
+ expectedParsed := map[string]interface{}{
"resource_registry": "2015-04-30",
"My::WP::Server": fakeEnvURL,
"resources": map[string]interface{}{
@@ -180,5 +180,5 @@ service_db:
},
}
env.Parse()
- th.AssertDeepEquals(t, expected_parsed, env.Parsed)
+ th.AssertDeepEquals(t, expectedParsed, env.Parsed)
}
@@ -405,6 +405,7 @@ func HandleAbandonSuccessfully(t *testing.T, output string) {
})
}
+// ValidJSONTemplate is a valid OpenStack Heat template in JSON format
const ValidJSONTemplate = `
{
"heat_template_version": "2014-10-16",
@@ -429,6 +430,7 @@ const ValidJSONTemplate = `
}
`
+// ValidJSONTemplateParsed is the expected parsed version of ValidJSONTemplate
var ValidJSONTemplateParsed = map[string]interface{}{
"heat_template_version": "2014-10-16",
"parameters": map[string]interface{}{
@@ -451,6 +453,7 @@ var ValidJSONTemplateParsed = map[string]interface{}{
},
}
+// ValidYAMLTemplate is a valid OpenStack Heat template in YAML format
const ValidYAMLTemplate = `
heat_template_version: 2014-10-16
parameters:
@@ -468,6 +471,7 @@ resources:
image: Debian 7 (Wheezy) (PVHVM)
`
+// InvalidTemplateNoVersion is an invalid template as it has no `version` section
const InvalidTemplateNoVersion = `
parameters:
flavor:
@@ -484,6 +488,7 @@ resources:
image: Debian 7 (Wheezy) (PVHVM)
`
+// ValidJSONEnvironment is a valid environment for a stack in JSON format
const ValidJSONEnvironment = `
{
"parameters": {
@@ -519,6 +524,7 @@ const ValidJSONEnvironment = `
}
`
+// ValidJSONEnvironmentParsed is the expected parsed version of ValidJSONEnvironment
var ValidJSONEnvironmentParsed = map[string]interface{}{
"parameters": map[string]interface{}{
"user_key": "userkey",
@@ -552,6 +558,7 @@ var ValidJSONEnvironmentParsed = map[string]interface{}{
},
}
+// ValidYAMLEnvironment is a valid environment for a stack in YAML format
const ValidYAMLEnvironment = `
parameters:
user_key: userkey
@@ -577,6 +584,7 @@ resource_registry:
hooks: [pre-create, pre-update]
`
+// InvalidEnvironment is an invalid environment as it has an extra section called `resources`
const InvalidEnvironment = `
parameters:
flavor:
@@ -1,35 +1,38 @@
package stacks
import (
- "errors"
"fmt"
"github.com/rackspace/gophercloud"
"reflect"
"strings"
)
+// Template is a structure that represents OpenStack Heat templates
type Template struct {
TE
}
+// TemplateFormatVersions is a map containing allowed variations of the template format version
+// Note that this contains the permitted variations of the _keys_ not the values.
var TemplateFormatVersions = map[string]bool{
"HeatTemplateFormatVersion": true,
"heat_template_version": true,
"AWSTemplateFormatVersion": true,
}
+// Validate validates the contents of the Template
func (t *Template) Validate() error {
if t.Parsed == nil {
if err := t.Parse(); err != nil {
return err
}
}
- for key, _ := range t.Parsed {
+ for key := range t.Parsed {
if _, ok := TemplateFormatVersions[key]; ok {
return nil
}
}
- return errors.New(fmt.Sprintf("Template format version not found."))
+ return fmt.Errorf("Template format version not found.")
}
// GetFileContents recursively parses a template to search for urls. These urls
@@ -49,11 +52,11 @@ func (t *Template) getFileContents(te interface{}, ignoreIf igFunc, recurse bool
switch te.(type) {
// if te is a map
case map[string]interface{}, map[interface{}]interface{}:
- te_map, err := toStringKeys(te)
+ teMap, err := toStringKeys(te)
if err != nil {
return err
}
- for k, v := range te_map {
+ for k, v := range teMap {
value, ok := v.(string)
if !ok {
// if the value is not a string, recursively parse that value
@@ -101,17 +104,17 @@ func (t *Template) getFileContents(te interface{}, ignoreIf igFunc, recurse bool
return nil
// if te is a slice, call the function on each element of the slice.
case []interface{}:
- te_slice := te.([]interface{})
- for i := range te_slice {
- if err := t.getFileContents(te_slice[i], ignoreIf, recurse); err != nil {
+ teSlice := te.([]interface{})
+ for i := range teSlice {
+ if err := t.getFileContents(teSlice[i], ignoreIf, recurse); err != nil {
return err
}
}
// if te is anything else, return
case string, bool, float64, nil, int:
return nil
default:
- return errors.New(fmt.Sprintf("%v: Unrecognized type", reflect.TypeOf(te)))
+ return fmt.Errorf("%v: Unrecognized type", reflect.TypeOf(te))
}
return nil
@@ -81,7 +81,7 @@ func TestGetFileContents(t *testing.T) {
fakeURL := strings.Join([]string{baseurl, "my_nova.yaml"}, "/")
urlparsed, err := url.Parse(fakeURL)
th.AssertNoErr(t, err)
- my_nova_content := `heat_template_version: 2014-10-16
+ myNovaContent := `heat_template_version: 2014-10-16
parameters:
flavor:
type: string
@@ -101,7 +101,7 @@ resources:
th.TestMethod(t, r, "GET")
w.Header().Set("Content-Type", "application/jason")
w.WriteHeader(http.StatusOK)
- fmt.Fprintf(w, my_nova_content)
+ fmt.Fprintf(w, myNovaContent)
})
client := fakeClient{BaseClient: getHTTPClient()}
@@ -116,7 +116,7 @@ resources:
th.AssertNoErr(t, err)
err = te.getFileContents(te.Parsed, ignoreIfTemplate, true)
th.AssertNoErr(t, err)
- expected_files := map[string]string{
+ expectedFiles := map[string]string{
"my_nova.yaml": `heat_template_version: 2014-10-16
parameters:
flavor:
@@ -133,9 +133,9 @@ resources:
image: Debian 7 (Wheezy) (PVHVM)
networks:
- {uuid: 11111111-1111-1111-1111-111111111111}`}
- th.AssertEquals(t, expected_files["my_nova.yaml"], te.Files[fakeURL])
+ th.AssertEquals(t, expectedFiles["my_nova.yaml"], te.Files[fakeURL])
te.fixFileRefs()
- expected_parsed := map[string]interface{}{
+ expectedParsed := map[string]interface{}{
"heat_template_version": "2015-04-30",
"resources": map[string]interface{}{
"my_server": map[string]interface{}{
@@ -144,5 +144,5 @@ resources:
},
}
te.Parse()
- th.AssertDeepEquals(t, expected_parsed, te.Parsed)
+ th.AssertDeepEquals(t, expectedParsed, te.Parsed)
}
Oops, something went wrong.

0 comments on commit 3de347f

Please sign in to comment.