Linting and comments fixes
diff --git a/openstack/orchestration/v1/stackresources/fixtures.go b/openstack/orchestration/v1/stackresources/fixtures.go
index 2273ba7..952dc54 100644
--- a/openstack/orchestration/v1/stackresources/fixtures.go
+++ b/openstack/orchestration/v1/stackresources/fixtures.go
@@ -255,7 +255,7 @@
}
// 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 @@
}
// same as above, but sorted
-var SortedListTypesExpected = resourceTypes{
+var SortedListTypesExpected = ResourceTypes{
"OS::Cinder::VolumeAttachment",
"OS::Heat::RandomString",
"OS::Nova::FloatingIP",
diff --git a/openstack/orchestration/v1/stackresources/results.go b/openstack/orchestration/v1/stackresources/results.go
index 51c3c0c..6ddc766 100644
--- a/openstack/orchestration/v1/stackresources/results.go
+++ b/openstack/orchestration/v1/stackresources/results.go
@@ -206,28 +206,28 @@
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 {
diff --git a/openstack/orchestration/v1/stacks/environment.go b/openstack/orchestration/v1/stacks/environment.go
index 4d4779c..abaff20 100644
--- a/openstack/orchestration/v1/stacks/environment.go
+++ b/openstack/orchestration/v1/stacks/environment.go
@@ -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 @@
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 @@
// 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
diff --git a/openstack/orchestration/v1/stacks/environment_test.go b/openstack/orchestration/v1/stacks/environment_test.go
index bbef283..3a3c2b9 100644
--- a/openstack/orchestration/v1/stacks/environment_test.go
+++ b/openstack/orchestration/v1/stacks/environment_test.go
@@ -76,7 +76,7 @@
func TestGetRRFileContents(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
- environment_content := `
+ environmentContent := `
heat_template_version: 2013-05-23
description:
@@ -100,7 +100,7 @@
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 @@
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 @@
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 @@
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 @@
},
}
env.Parse()
- th.AssertDeepEquals(t, expected_parsed, env.Parsed)
+ th.AssertDeepEquals(t, expectedParsed, env.Parsed)
}
diff --git a/openstack/orchestration/v1/stacks/fixtures.go b/openstack/orchestration/v1/stacks/fixtures.go
index 0cb38c2..83f5dec 100644
--- a/openstack/orchestration/v1/stacks/fixtures.go
+++ b/openstack/orchestration/v1/stacks/fixtures.go
@@ -405,6 +405,7 @@
})
}
+// ValidJSONTemplate is a valid OpenStack Heat template in JSON format
const ValidJSONTemplate = `
{
"heat_template_version": "2014-10-16",
@@ -429,6 +430,7 @@
}
`
+// 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 @@
},
}
+// ValidYAMLTemplate is a valid OpenStack Heat template in YAML format
const ValidYAMLTemplate = `
heat_template_version: 2014-10-16
parameters:
@@ -468,6 +471,7 @@
image: Debian 7 (Wheezy) (PVHVM)
`
+// InvalidTemplateNoVersion is an invalid template as it has no `version` section
const InvalidTemplateNoVersion = `
parameters:
flavor:
@@ -484,6 +488,7 @@
image: Debian 7 (Wheezy) (PVHVM)
`
+// ValidJSONEnvironment is a valid environment for a stack in JSON format
const ValidJSONEnvironment = `
{
"parameters": {
@@ -519,6 +524,7 @@
}
`
+// ValidJSONEnvironmentParsed is the expected parsed version of ValidJSONEnvironment
var ValidJSONEnvironmentParsed = map[string]interface{}{
"parameters": map[string]interface{}{
"user_key": "userkey",
@@ -552,6 +558,7 @@
},
}
+// ValidYAMLEnvironment is a valid environment for a stack in YAML format
const ValidYAMLEnvironment = `
parameters:
user_key: userkey
@@ -577,6 +584,7 @@
hooks: [pre-create, pre-update]
`
+// InvalidEnvironment is an invalid environment as it has an extra section called `resources`
const InvalidEnvironment = `
parameters:
flavor:
diff --git a/openstack/orchestration/v1/stacks/template.go b/openstack/orchestration/v1/stacks/template.go
index 32fe95b..234ce49 100644
--- a/openstack/orchestration/v1/stacks/template.go
+++ b/openstack/orchestration/v1/stacks/template.go
@@ -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 @@
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,9 +104,9 @@
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
}
}
@@ -111,7 +114,7 @@
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
diff --git a/openstack/orchestration/v1/stacks/template_test.go b/openstack/orchestration/v1/stacks/template_test.go
index a297f77..6884db8 100644
--- a/openstack/orchestration/v1/stacks/template_test.go
+++ b/openstack/orchestration/v1/stacks/template_test.go
@@ -81,7 +81,7 @@
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 @@
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 @@
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 @@
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 @@
},
}
te.Parse()
- th.AssertDeepEquals(t, expected_parsed, te.Parsed)
+ th.AssertDeepEquals(t, expectedParsed, te.Parsed)
}
diff --git a/openstack/orchestration/v1/stacks/utils.go b/openstack/orchestration/v1/stacks/utils.go
index 420a7fa..7b476a9 100644
--- a/openstack/orchestration/v1/stacks/utils.go
+++ b/openstack/orchestration/v1/stacks/utils.go
@@ -2,7 +2,6 @@
import (
"encoding/json"
- "errors"
"fmt"
"io/ioutil"
"net/http"
@@ -21,7 +20,7 @@
Get(string) (*http.Response, error)
}
-// Base structure for both Template and Environment
+// TE is a base structure for both Template and Environment
type TE struct {
// Bin stores the contents of the template or environment.
Bin []byte
@@ -59,7 +58,7 @@
return nil
}
- // get a fqdn from the URL using the baseURL of the template. For local files,
+ // get a fqdn from the URL using the baseURL of the TE. For local files,
// the URL's will have the `file` scheme.
u, err := gophercloud.NormalizePathURL(t.baseURL, t.URL)
if err != nil {
@@ -72,7 +71,7 @@
t.client = getHTTPClient()
}
- // use the client to fetch the contents of the template
+ // use the client to fetch the contents of the TE
resp, err := t.client.Get(t.URL)
if err != nil {
return err
@@ -86,7 +85,7 @@
return nil
}
-// get the basepath of the template.
+// get the basepath of the TE
func getBasePath() (string, error) {
basePath, err := filepath.Abs(".")
if err != nil {
@@ -100,7 +99,7 @@
}
// get a an HTTP client to retrieve URL's. This client allows the use of `file`
-// scheme since we may need to fetch templates from users filesystem
+// scheme since we may need to fetch files from users filesystem
func getHTTPClient() Client {
transport := &http.Transport{}
transport.RegisterProtocol("file", http.NewFileTransport(http.Dir("/")))
@@ -114,49 +113,49 @@
}
if jerr := json.Unmarshal(t.Bin, &t.Parsed); jerr != nil {
if yerr := yaml.Unmarshal(t.Bin, &t.Parsed); yerr != nil {
- return errors.New(fmt.Sprintf("Data in neither json nor yaml format."))
+ return fmt.Errorf("Data in neither json nor yaml format.")
}
}
return t.Validate()
}
-// base Validate method, always returns nil
+// Validate validates the contents of TE
func (t *TE) Validate() error {
return nil
}
// igfunc is a parameter used by GetFileContents and GetRRFileContents to check
-// for valid template URL's.
+// for valid URL's.
type igFunc func(string, interface{}) bool
// convert map[interface{}]interface{} to map[string]interface{}
func toStringKeys(m interface{}) (map[string]interface{}, error) {
switch m.(type) {
case map[string]interface{}, map[interface{}]interface{}:
- typed_map := make(map[string]interface{})
+ typedMap := make(map[string]interface{})
if _, ok := m.(map[interface{}]interface{}); ok {
for k, v := range m.(map[interface{}]interface{}) {
- typed_map[k.(string)] = v
+ typedMap[k.(string)] = v
}
} else {
- typed_map = m.(map[string]interface{})
+ typedMap = m.(map[string]interface{})
}
- return typed_map, nil
+ return typedMap, nil
default:
- return nil, errors.New(fmt.Sprintf("Expected a map of type map[string]interface{} or map[interface{}]interface{}, actual type: %v", reflect.TypeOf(m)))
+ return nil, fmt.Errorf("Expected a map of type map[string]interface{} or map[interface{}]interface{}, actual type: %v", reflect.TypeOf(m))
}
}
-// fix the template reference to files by replacing relative URL's by absolute
+// fix the reference to files by replacing relative URL's by absolute
// URL's
func (t *TE) fixFileRefs() {
- t_str := string(t.Bin)
+ tStr := string(t.Bin)
if t.fileMaps == nil {
return
}
for k, v := range t.fileMaps {
- t_str = strings.Replace(t_str, k, v, -1)
+ tStr = strings.Replace(tStr, k, v, -1)
}
- t.Bin = []byte(t_str)
+ t.Bin = []byte(tStr)
}
diff --git a/openstack/orchestration/v1/stacks/utils_test.go b/openstack/orchestration/v1/stacks/utils_test.go
index 6a3c5c0..2536e03 100644
--- a/openstack/orchestration/v1/stacks/utils_test.go
+++ b/openstack/orchestration/v1/stacks/utils_test.go
@@ -22,18 +22,18 @@
}
func TesttoStringKeys(t *testing.T) {
- var test_1 interface{} = map[interface{}]interface{}{
+ var test1 interface{} = map[interface{}]interface{}{
"Adam": "Smith",
"Isaac": "Newton",
}
- result_1, err := toStringKeys(test_1)
+ result1, err := toStringKeys(test1)
th.AssertNoErr(t, err)
expected := map[string]interface{}{
"Adam": "Smith",
"Isaac": "Newton",
}
- th.AssertDeepEquals(t, result_1, expected)
+ th.AssertDeepEquals(t, result1, expected)
}
func TestGetBasePath(t *testing.T) {
diff --git a/util.go b/util.go
index 655436e..3d6a4e4 100644
--- a/util.go
+++ b/util.go
@@ -45,7 +45,7 @@
return url
}
-// NormalizeFilePathURL is used to convert rawPath to a fqdn, using basePath as
+// NormalizePathURL is used to convert rawPath to a fqdn, using basePath as
// a reference in the filesystem, if necessary. basePath is assumed to contain
// either '.' when first used, or the file:// type fqdn of the parent resource.
// e.g. myFavScript.yaml => file://opt/lib/myFavScript.yaml
@@ -69,13 +69,14 @@
absPathSys = filepath.Join(basePathSys, rawPath)
bu.Path = filepath.ToSlash(absPathSys)
return bu.String(), nil
- } else {
- absPathSys = filepath.Join(basePath, rawPath)
- u.Path = filepath.ToSlash(absPathSys)
- if err != nil {
- return "", err
- }
- u.Scheme = "file"
- return u.String(), nil
}
+
+ absPathSys = filepath.Join(basePath, rawPath)
+ u.Path = filepath.ToSlash(absPathSys)
+ if err != nil {
+ return "", err
+ }
+ u.Scheme = "file"
+ return u.String(), nil
+
}