Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 3de347f

Browse files
committed
Linting and comments fixes
1 parent a979f5b commit 3de347f

File tree

10 files changed

+87
-76
lines changed

10 files changed

+87
-76
lines changed

openstack/orchestration/v1/stackresources/fixtures.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func HandleMetadataSuccessfully(t *testing.T, output string) {
255255
}
256256

257257
// ListTypesExpected represents the expected object from a ListTypes request.
258-
var ListTypesExpected = resourceTypes{
258+
var ListTypesExpected = ResourceTypes{
259259
"OS::Nova::Server",
260260
"OS::Heat::RandomString",
261261
"OS::Swift::Container",
@@ -267,7 +267,7 @@ var ListTypesExpected = resourceTypes{
267267
}
268268

269269
// same as above, but sorted
270-
var SortedListTypesExpected = resourceTypes{
270+
var SortedListTypesExpected = ResourceTypes{
271271
"OS::Cinder::VolumeAttachment",
272272
"OS::Heat::RandomString",
273273
"OS::Nova::FloatingIP",

openstack/orchestration/v1/stackresources/results.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,28 +206,28 @@ func (r ResourceTypePage) IsEmpty() (bool, error) {
206206
return len(rts) == 0, nil
207207
}
208208

209-
// resourceTypes represents the type that holds the result of ExtractResourceTypes.
209+
// ResourceTypes represents the type that holds the result of ExtractResourceTypes.
210210
// We define methods on this type to sort it before output
211-
type resourceTypes []string
211+
type ResourceTypes []string
212212

213-
func (r resourceTypes) Len() int {
213+
func (r ResourceTypes) Len() int {
214214
return len(r)
215215
}
216216

217-
func (r resourceTypes) Swap(i, j int) {
217+
func (r ResourceTypes) Swap(i, j int) {
218218
r[i], r[j] = r[j], r[i]
219219
}
220220

221-
func (r resourceTypes) Less(i, j int) bool {
221+
func (r ResourceTypes) Less(i, j int) bool {
222222
return r[i] < r[j]
223223
}
224224

225225
// ExtractResourceTypes extracts and returns resource types.
226-
func ExtractResourceTypes(page pagination.Page) (resourceTypes, error) {
226+
func ExtractResourceTypes(page pagination.Page) (ResourceTypes, error) {
227227
casted := page.(ResourceTypePage).Body
228228

229229
var response struct {
230-
ResourceTypes resourceTypes `mapstructure:"resource_types"`
230+
ResourceTypes ResourceTypes `mapstructure:"resource_types"`
231231
}
232232

233233
if err := mapstructure.Decode(casted, &response); err != nil {

openstack/orchestration/v1/stacks/environment.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
package stacks
22

33
import (
4-
"errors"
54
"fmt"
65
"strings"
76
)
87

9-
// an interface to represent stack environments
8+
// Environment is a structure that represents stack environments
109
type Environment struct {
1110
TE
1211
}
1312

14-
// allowed sections in a stack environment file
13+
// EnvironmentSections is a map containing allowed sections in a stack environment file
1514
var EnvironmentSections = map[string]bool{
1615
"parameters": true,
1716
"parameter_defaults": true,
1817
"resource_registry": true,
1918
}
2019

20+
// Validate validates the contents of the Environment
2121
func (e *Environment) Validate() error {
2222
if e.Parsed == nil {
2323
if err := e.Parse(); err != nil {
2424
return err
2525
}
2626
}
27-
for key, _ := range e.Parsed {
27+
for key := range e.Parsed {
2828
if _, ok := EnvironmentSections[key]; !ok {
29-
return errors.New(fmt.Sprintf("Environment has wrong section: %s", key))
29+
return fmt.Errorf("Environment has wrong section: %s", key)
3030
}
3131
}
3232
return nil
@@ -51,14 +51,14 @@ func (e *Environment) getRRFileContents(ignoreIf igFunc) error {
5151
switch rr.(type) {
5252
// process further only if the resource registry is a map
5353
case map[string]interface{}, map[interface{}]interface{}:
54-
rr_map, err := toStringKeys(rr)
54+
rrMap, err := toStringKeys(rr)
5555
if err != nil {
5656
return err
5757
}
5858
// the resource registry might contain a base URL for the resource. If
5959
// such a field is present, use it. Otherwise, use the default base URL.
6060
var baseURL string
61-
if val, ok := rr_map["base_url"]; ok {
61+
if val, ok := rrMap["base_url"]; ok {
6262
baseURL = val.(string)
6363
} else {
6464
baseURL = e.baseURL
@@ -78,24 +78,24 @@ func (e *Environment) getRRFileContents(ignoreIf igFunc) error {
7878
// check the `resources` section (if it exists) for more URL's. Note that
7979
// the previous call to GetFileContents was (deliberately) not recursive
8080
// as we want more control over where to look for URL's
81-
if val, ok := rr_map["resources"]; ok {
81+
if val, ok := rrMap["resources"]; ok {
8282
switch val.(type) {
8383
// process further only if the contents are a map
8484
case map[string]interface{}, map[interface{}]interface{}:
85-
resources_map, err := toStringKeys(val)
85+
resourcesMap, err := toStringKeys(val)
8686
if err != nil {
8787
return err
8888
}
89-
for _, v := range resources_map {
89+
for _, v := range resourcesMap {
9090
switch v.(type) {
9191
case map[string]interface{}, map[interface{}]interface{}:
92-
resource_map, err := toStringKeys(v)
92+
resourceMap, err := toStringKeys(v)
9393
if err != nil {
9494
return err
9595
}
9696
var resourceBaseURL string
9797
// if base_url for the resource type is defined, use it
98-
if val, ok := resource_map["base_url"]; ok {
98+
if val, ok := resourceMap["base_url"]; ok {
9999
resourceBaseURL = val.(string)
100100
} else {
101101
resourceBaseURL = baseURL

openstack/orchestration/v1/stacks/environment_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func TestIgnoreIfEnvironment(t *testing.T) {
7676
func TestGetRRFileContents(t *testing.T) {
7777
th.SetupHTTP()
7878
defer th.TeardownHTTP()
79-
environment_content := `
79+
environmentContent := `
8080
heat_template_version: 2013-05-23
8181
8282
description:
@@ -100,7 +100,7 @@ resources:
100100
flavor: { get_param: instance_type }
101101
key_name: { get_param: key_name }`
102102

103-
db_content := `
103+
dbContent := `
104104
heat_template_version: 2014-10-16
105105
106106
description:
@@ -139,7 +139,7 @@ service_db:
139139
th.TestMethod(t, r, "GET")
140140
w.Header().Set("Content-Type", "application/jason")
141141
w.WriteHeader(http.StatusOK)
142-
fmt.Fprintf(w, environment_content)
142+
fmt.Fprintf(w, environmentContent)
143143
})
144144

145145
fakeDBURL := strings.Join([]string{baseurl, "my_db.yaml"}, "/")
@@ -151,7 +151,7 @@ service_db:
151151
th.TestMethod(t, r, "GET")
152152
w.Header().Set("Content-Type", "application/jason")
153153
w.WriteHeader(http.StatusOK)
154-
fmt.Fprintf(w, db_content)
154+
fmt.Fprintf(w, dbContent)
155155
})
156156

157157
client := fakeClient{BaseClient: getHTTPClient()}
@@ -163,14 +163,14 @@ service_db:
163163
th.AssertNoErr(t, err)
164164
err = env.getRRFileContents(ignoreIfEnvironment)
165165
th.AssertNoErr(t, err)
166-
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 }"
167-
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 ]"
166+
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 }"
167+
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 ]"
168168

169-
th.AssertEquals(t, expected_env_files_content, env.Files[fakeEnvURL])
170-
th.AssertEquals(t, expected_db_files_content, env.Files[fakeDBURL])
169+
th.AssertEquals(t, expectedEnvFilesContent, env.Files[fakeEnvURL])
170+
th.AssertEquals(t, expectedDBFilesContent, env.Files[fakeDBURL])
171171

172172
env.fixFileRefs()
173-
expected_parsed := map[string]interface{}{
173+
expectedParsed := map[string]interface{}{
174174
"resource_registry": "2015-04-30",
175175
"My::WP::Server": fakeEnvURL,
176176
"resources": map[string]interface{}{
@@ -180,5 +180,5 @@ service_db:
180180
},
181181
}
182182
env.Parse()
183-
th.AssertDeepEquals(t, expected_parsed, env.Parsed)
183+
th.AssertDeepEquals(t, expectedParsed, env.Parsed)
184184
}

openstack/orchestration/v1/stacks/fixtures.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ func HandleAbandonSuccessfully(t *testing.T, output string) {
405405
})
406406
}
407407

408+
// ValidJSONTemplate is a valid OpenStack Heat template in JSON format
408409
const ValidJSONTemplate = `
409410
{
410411
"heat_template_version": "2014-10-16",
@@ -429,6 +430,7 @@ const ValidJSONTemplate = `
429430
}
430431
`
431432

433+
// ValidJSONTemplateParsed is the expected parsed version of ValidJSONTemplate
432434
var ValidJSONTemplateParsed = map[string]interface{}{
433435
"heat_template_version": "2014-10-16",
434436
"parameters": map[string]interface{}{
@@ -451,6 +453,7 @@ var ValidJSONTemplateParsed = map[string]interface{}{
451453
},
452454
}
453455

456+
// ValidYAMLTemplate is a valid OpenStack Heat template in YAML format
454457
const ValidYAMLTemplate = `
455458
heat_template_version: 2014-10-16
456459
parameters:
@@ -468,6 +471,7 @@ resources:
468471
image: Debian 7 (Wheezy) (PVHVM)
469472
`
470473

474+
// InvalidTemplateNoVersion is an invalid template as it has no `version` section
471475
const InvalidTemplateNoVersion = `
472476
parameters:
473477
flavor:
@@ -484,6 +488,7 @@ resources:
484488
image: Debian 7 (Wheezy) (PVHVM)
485489
`
486490

491+
// ValidJSONEnvironment is a valid environment for a stack in JSON format
487492
const ValidJSONEnvironment = `
488493
{
489494
"parameters": {
@@ -519,6 +524,7 @@ const ValidJSONEnvironment = `
519524
}
520525
`
521526

527+
// ValidJSONEnvironmentParsed is the expected parsed version of ValidJSONEnvironment
522528
var ValidJSONEnvironmentParsed = map[string]interface{}{
523529
"parameters": map[string]interface{}{
524530
"user_key": "userkey",
@@ -552,6 +558,7 @@ var ValidJSONEnvironmentParsed = map[string]interface{}{
552558
},
553559
}
554560

561+
// ValidYAMLEnvironment is a valid environment for a stack in YAML format
555562
const ValidYAMLEnvironment = `
556563
parameters:
557564
user_key: userkey
@@ -577,6 +584,7 @@ resource_registry:
577584
hooks: [pre-create, pre-update]
578585
`
579586

587+
// InvalidEnvironment is an invalid environment as it has an extra section called `resources`
580588
const InvalidEnvironment = `
581589
parameters:
582590
flavor:

openstack/orchestration/v1/stacks/template.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
11
package stacks
22

33
import (
4-
"errors"
54
"fmt"
65
"github.com/rackspace/gophercloud"
76
"reflect"
87
"strings"
98
)
109

10+
// Template is a structure that represents OpenStack Heat templates
1111
type Template struct {
1212
TE
1313
}
1414

15+
// TemplateFormatVersions is a map containing allowed variations of the template format version
16+
// Note that this contains the permitted variations of the _keys_ not the values.
1517
var TemplateFormatVersions = map[string]bool{
1618
"HeatTemplateFormatVersion": true,
1719
"heat_template_version": true,
1820
"AWSTemplateFormatVersion": true,
1921
}
2022

23+
// Validate validates the contents of the Template
2124
func (t *Template) Validate() error {
2225
if t.Parsed == nil {
2326
if err := t.Parse(); err != nil {
2427
return err
2528
}
2629
}
27-
for key, _ := range t.Parsed {
30+
for key := range t.Parsed {
2831
if _, ok := TemplateFormatVersions[key]; ok {
2932
return nil
3033
}
3134
}
32-
return errors.New(fmt.Sprintf("Template format version not found."))
35+
return fmt.Errorf("Template format version not found.")
3336
}
3437

3538
// 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
4952
switch te.(type) {
5053
// if te is a map
5154
case map[string]interface{}, map[interface{}]interface{}:
52-
te_map, err := toStringKeys(te)
55+
teMap, err := toStringKeys(te)
5356
if err != nil {
5457
return err
5558
}
56-
for k, v := range te_map {
59+
for k, v := range teMap {
5760
value, ok := v.(string)
5861
if !ok {
5962
// 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
101104
return nil
102105
// if te is a slice, call the function on each element of the slice.
103106
case []interface{}:
104-
te_slice := te.([]interface{})
105-
for i := range te_slice {
106-
if err := t.getFileContents(te_slice[i], ignoreIf, recurse); err != nil {
107+
teSlice := te.([]interface{})
108+
for i := range teSlice {
109+
if err := t.getFileContents(teSlice[i], ignoreIf, recurse); err != nil {
107110
return err
108111
}
109112
}
110113
// if te is anything else, return
111114
case string, bool, float64, nil, int:
112115
return nil
113116
default:
114-
return errors.New(fmt.Sprintf("%v: Unrecognized type", reflect.TypeOf(te)))
117+
return fmt.Errorf("%v: Unrecognized type", reflect.TypeOf(te))
115118

116119
}
117120
return nil

openstack/orchestration/v1/stacks/template_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestGetFileContents(t *testing.T) {
8181
fakeURL := strings.Join([]string{baseurl, "my_nova.yaml"}, "/")
8282
urlparsed, err := url.Parse(fakeURL)
8383
th.AssertNoErr(t, err)
84-
my_nova_content := `heat_template_version: 2014-10-16
84+
myNovaContent := `heat_template_version: 2014-10-16
8585
parameters:
8686
flavor:
8787
type: string
@@ -101,7 +101,7 @@ resources:
101101
th.TestMethod(t, r, "GET")
102102
w.Header().Set("Content-Type", "application/jason")
103103
w.WriteHeader(http.StatusOK)
104-
fmt.Fprintf(w, my_nova_content)
104+
fmt.Fprintf(w, myNovaContent)
105105
})
106106

107107
client := fakeClient{BaseClient: getHTTPClient()}
@@ -116,7 +116,7 @@ resources:
116116
th.AssertNoErr(t, err)
117117
err = te.getFileContents(te.Parsed, ignoreIfTemplate, true)
118118
th.AssertNoErr(t, err)
119-
expected_files := map[string]string{
119+
expectedFiles := map[string]string{
120120
"my_nova.yaml": `heat_template_version: 2014-10-16
121121
parameters:
122122
flavor:
@@ -133,9 +133,9 @@ resources:
133133
image: Debian 7 (Wheezy) (PVHVM)
134134
networks:
135135
- {uuid: 11111111-1111-1111-1111-111111111111}`}
136-
th.AssertEquals(t, expected_files["my_nova.yaml"], te.Files[fakeURL])
136+
th.AssertEquals(t, expectedFiles["my_nova.yaml"], te.Files[fakeURL])
137137
te.fixFileRefs()
138-
expected_parsed := map[string]interface{}{
138+
expectedParsed := map[string]interface{}{
139139
"heat_template_version": "2015-04-30",
140140
"resources": map[string]interface{}{
141141
"my_server": map[string]interface{}{
@@ -144,5 +144,5 @@ resources:
144144
},
145145
}
146146
te.Parse()
147-
th.AssertDeepEquals(t, expected_parsed, te.Parsed)
147+
th.AssertDeepEquals(t, expectedParsed, te.Parsed)
148148
}

0 commit comments

Comments
 (0)