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

Commit e3086ae

Browse files
committed
Refactor functions into methods
GetRRFileContents and GetFileContents are better suited as methods of Environment and Template repectively.
1 parent bfc6eda commit e3086ae

File tree

5 files changed

+21
-18
lines changed

5 files changed

+21
-18
lines changed

openstack/orchestration/v1/stacks/environment.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (e *Environment) Validate() error {
3535
// Parse environment file to resolve the URL's of the resources. This is done by
3636
// reading from the `Resource Registry` section, which is why the function is
3737
// named GetRRFileContents.
38-
func GetRRFileContents(e *Environment, ignoreIf igFunc) error {
38+
func (e *Environment) GetRRFileContents(ignoreIf igFunc) error {
3939
// initialize environment if empty
4040
if e.Files == nil {
4141
e.Files = make(map[string]string)
@@ -71,10 +71,13 @@ func GetRRFileContents(e *Environment, ignoreIf igFunc) error {
7171
tempTemplate.baseURL = baseURL
7272
tempTemplate.client = e.client
7373

74-
if err = GetFileContents(tempTemplate, rr, ignoreIf, false); err != nil {
74+
// Fetch the contents of remote resource URL's
75+
if err = tempTemplate.GetFileContents(rr, ignoreIf, false); err != nil {
7576
return err
7677
}
77-
// check the `resources` section (if it exists) for more URLs
78+
// check the `resources` section (if it exists) for more URL's. Note that
79+
// the previous call to GetFileContents was (deliberately) not recursive
80+
// as we want more control over where to look for URL's
7881
if val, ok := rr_map["resources"]; ok {
7982
switch val.(type) {
8083
// process further only if the contents are a map
@@ -98,7 +101,7 @@ func GetRRFileContents(e *Environment, ignoreIf igFunc) error {
98101
resourceBaseURL = baseURL
99102
}
100103
tempTemplate.baseURL = resourceBaseURL
101-
if err := GetFileContents(tempTemplate, v, ignoreIf, false); err != nil {
104+
if err := tempTemplate.GetFileContents(v, ignoreIf, false); err != nil {
102105
return err
103106
}
104107
}

openstack/orchestration/v1/stacks/environment_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ service_db:
161161

162162
err = env.Parse()
163163
th.AssertNoErr(t, err)
164-
err = GetRRFileContents(env, ignoreIfEnvironment)
164+
err = env.GetRRFileContents(ignoreIfEnvironment)
165165
th.AssertNoErr(t, err)
166166
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 }"
167167
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 ]"

openstack/orchestration/v1/stacks/requests.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (opts CreateOpts) ToStackCreateMap() (map[string]interface{}, error) {
100100
return nil, err
101101
}
102102

103-
if err := GetFileContents(opts.TemplateOpts, opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
103+
if err := opts.TemplateOpts.GetFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
104104
return nil, err
105105
}
106106
opts.TemplateOpts.FixFileRefs()
@@ -118,7 +118,7 @@ func (opts CreateOpts) ToStackCreateMap() (map[string]interface{}, error) {
118118
if err := opts.EnvironmentOpts.Parse(); err != nil {
119119
return nil, err
120120
}
121-
if err := GetRRFileContents(opts.EnvironmentOpts, ignoreIfEnvironment); err != nil {
121+
if err := opts.EnvironmentOpts.GetRRFileContents(ignoreIfEnvironment); err != nil {
122122
return nil, err
123123
}
124124
opts.EnvironmentOpts.FixFileRefs()
@@ -250,7 +250,7 @@ func (opts AdoptOpts) ToStackAdoptMap() (map[string]interface{}, error) {
250250
return nil, err
251251
}
252252

253-
if err := GetFileContents(opts.TemplateOpts, opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
253+
if err := opts.TemplateOpts.GetFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
254254
return nil, err
255255
}
256256
opts.TemplateOpts.FixFileRefs()
@@ -273,7 +273,7 @@ func (opts AdoptOpts) ToStackAdoptMap() (map[string]interface{}, error) {
273273
if err := opts.EnvironmentOpts.Parse(); err != nil {
274274
return nil, err
275275
}
276-
if err := GetRRFileContents(opts.EnvironmentOpts, ignoreIfEnvironment); err != nil {
276+
if err := opts.EnvironmentOpts.GetRRFileContents(ignoreIfEnvironment); err != nil {
277277
return nil, err
278278
}
279279
opts.EnvironmentOpts.FixFileRefs()
@@ -461,7 +461,7 @@ func (opts UpdateOpts) ToStackUpdateMap() (map[string]interface{}, error) {
461461
return nil, err
462462
}
463463

464-
if err := GetFileContents(opts.TemplateOpts, opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
464+
if err := opts.TemplateOpts.GetFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
465465
return nil, err
466466
}
467467
opts.TemplateOpts.FixFileRefs()
@@ -476,7 +476,7 @@ func (opts UpdateOpts) ToStackUpdateMap() (map[string]interface{}, error) {
476476
if err := opts.EnvironmentOpts.Parse(); err != nil {
477477
return nil, err
478478
}
479-
if err := GetRRFileContents(opts.EnvironmentOpts, ignoreIfEnvironment); err != nil {
479+
if err := opts.EnvironmentOpts.GetRRFileContents(ignoreIfEnvironment); err != nil {
480480
return nil, err
481481
}
482482
opts.EnvironmentOpts.FixFileRefs()
@@ -607,7 +607,7 @@ func (opts PreviewOpts) ToStackPreviewMap() (map[string]interface{}, error) {
607607
return nil, err
608608
}
609609

610-
if err := GetFileContents(opts.TemplateOpts, opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
610+
if err := opts.TemplateOpts.GetFileContents(opts.TemplateOpts.Parsed, ignoreIfTemplate, true); err != nil {
611611
return nil, err
612612
}
613613
opts.TemplateOpts.FixFileRefs()
@@ -625,7 +625,7 @@ func (opts PreviewOpts) ToStackPreviewMap() (map[string]interface{}, error) {
625625
if err := opts.EnvironmentOpts.Parse(); err != nil {
626626
return nil, err
627627
}
628-
if err := GetRRFileContents(opts.EnvironmentOpts, ignoreIfEnvironment); err != nil {
628+
if err := opts.EnvironmentOpts.GetRRFileContents(ignoreIfEnvironment); err != nil {
629629
return nil, err
630630
}
631631
opts.EnvironmentOpts.FixFileRefs()

openstack/orchestration/v1/stacks/template.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (t *Template) Validate() error {
3838
// parameter of the template structure. This is the only way that a user can
3939
// use child templates that are located in their filesystem; urls located on the
4040
// web (e.g. on github or swift) can be fetched directly by Heat engine.
41-
func GetFileContents(t *Template, te interface{}, ignoreIf igFunc, recurse bool) error {
41+
func (t *Template) GetFileContents(te interface{}, ignoreIf igFunc, recurse bool) error {
4242
// initialize template if empty
4343
if t.Files == nil {
4444
t.Files = make(map[string]string)
@@ -57,7 +57,7 @@ func GetFileContents(t *Template, te interface{}, ignoreIf igFunc, recurse bool)
5757
value, ok := v.(string)
5858
if !ok {
5959
// if the value is not a string, recursively parse that value
60-
if err := GetFileContents(t, v, ignoreIf, recurse); err != nil {
60+
if err := t.GetFileContents(v, ignoreIf, recurse); err != nil {
6161
return err
6262
}
6363
} else if !ignoreIf(k, value) {
@@ -87,7 +87,7 @@ func GetFileContents(t *Template, te interface{}, ignoreIf igFunc, recurse bool)
8787
// required if the child template itself contains references to
8888
// other templates
8989
if recurse {
90-
if err := GetFileContents(childTemplate, childTemplate.Parsed, ignoreIf, recurse); err != nil {
90+
if err := childTemplate.GetFileContents(childTemplate.Parsed, ignoreIf, recurse); err != nil {
9191
return err
9292
}
9393
}
@@ -103,7 +103,7 @@ func GetFileContents(t *Template, te interface{}, ignoreIf igFunc, recurse bool)
103103
case []interface{}:
104104
te_slice := te.([]interface{})
105105
for i := range te_slice {
106-
if err := GetFileContents(t, te_slice[i], ignoreIf, recurse); err != nil {
106+
if err := t.GetFileContents(te_slice[i], ignoreIf, recurse); err != nil {
107107
return err
108108
}
109109
}

openstack/orchestration/v1/stacks/template_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ resources:
114114

115115
err = te.Parse()
116116
th.AssertNoErr(t, err)
117-
err = GetFileContents(te, te.Parsed, ignoreIfTemplate, true)
117+
err = te.GetFileContents(te.Parsed, ignoreIfTemplate, true)
118118
th.AssertNoErr(t, err)
119119
expected_files := map[string]string{
120120
"my_nova.yaml": `heat_template_version: 2014-10-16

0 commit comments

Comments
 (0)