Make LaunchConfiguration resources mergeable
authorRoman Podolyaka <rpodolyaka@mirantis.com>
Fri, 11 Oct 2013 14:54:09 +0000 (17:54 +0300)
committerRoman Podolyaka <rpodolyaka@mirantis.com>
Mon, 14 Oct 2013 12:30:24 +0000 (15:30 +0300)
This can be very useful for merging of Metadata to be used
by optional services (e. g. to add services like tuskar to
undercloud on demand).

Change-Id: Ifc4016d6e994064c0772c12e668e98bf055fada9

examples/launchconfig1.yaml [new file with mode: 0644]
examples/launchconfig2.yaml [new file with mode: 0644]
examples/launchconfig_result.yaml [new file with mode: 0644]
merge.py
test_merge.bash

diff --git a/examples/launchconfig1.yaml b/examples/launchconfig1.yaml
new file mode 100644 (file)
index 0000000..9127eab
--- /dev/null
@@ -0,0 +1,23 @@
+Parameters:
+  A:
+    Type: String
+    Default: test1
+  B:
+    Type: String
+    Default: test2
+  resource1Image:
+    Type: String
+    Default: resource1
+Resources:
+  notcomputeConfigBase:
+    Type: AWS::AutoScaling::LaunchConfiguration
+    Metadata:
+      OpenStack::Role: notcomputeConfig
+      a: {Ref: A}
+      b: {Ref: B}
+  resource1:
+    Type: OS::Nova::Server
+    Properties:
+      flavor: test_flavor
+      image: {Ref: resource1Image}
+      key_name: test_key
diff --git a/examples/launchconfig2.yaml b/examples/launchconfig2.yaml
new file mode 100644 (file)
index 0000000..1681637
--- /dev/null
@@ -0,0 +1,19 @@
+Parameters:
+  C:
+    Type: String
+    Default: test3
+  resource2Image:
+    Type: String
+    Default: resource2
+Resources:
+  notcomputeConfigMixin:
+    Type: AWS::AutoScaling::LaunchConfiguration
+    Metadata:
+      OpenStack::Role: notcomputeConfig
+      c: {Ref: C}
+  resource2:
+    Type: OS::Nova::Server
+    Properties:
+      flavor: test_flavor
+      image: {Ref: resource2Image}
+      key_name: test_key
diff --git a/examples/launchconfig_result.yaml b/examples/launchconfig_result.yaml
new file mode 100644 (file)
index 0000000..76c12b8
--- /dev/null
@@ -0,0 +1,43 @@
+Description: examples/launchconfig1.yaml,examples/launchconfig2.yaml
+HeatTemplateFormatVersion: '2012-12-12'
+Parameters:
+  A:
+    Default: test1
+    Type: String
+  B:
+    Default: test2
+    Type: String
+  C:
+    Default: test3
+    Type: String
+  resource1Image:
+    Default: resource1
+    Type: String
+  resource2Image:
+    Default: resource2
+    Type: String
+Resources:
+  notcomputeConfig:
+    Metadata:
+      OpenStack::Role: notcomputeConfig
+      a:
+        Ref: A
+      b:
+        Ref: B
+      c:
+        Ref: C
+    Type: AWS::AutoScaling::LaunchConfiguration
+  resource1:
+    Properties:
+      flavor: test_flavor
+      image:
+        Ref: resource1Image
+      key_name: test_key
+    Type: OS::Nova::Server
+  resource2:
+    Properties:
+      flavor: test_flavor
+      image:
+        Ref: resource2Image
+      key_name: test_key
+    Type: OS::Nova::Server
index 3b7d2dd..fe42b8d 100644 (file)
--- a/merge.py
+++ b/merge.py
@@ -48,6 +48,8 @@ MERGABLE_TYPES = {'OS::Nova::Server':
                   {'image': 'image'},
                   'AWS::EC2::Instance':
                   {'image': 'ImageId'},
+                  'AWS::AutoScaling::LaunchConfiguration':
+                  {},
                  }
 
 
@@ -119,10 +121,11 @@ for template_path in templates:
     new_resources = template.get('Resources', {})
     for r, rbody in sorted(new_resources.items()):
         if rbody['Type'] in MERGABLE_TYPES:
-            image_key = MERGABLE_TYPES[rbody['Type']]['image']
-            # XXX Assuming ImageId is always a Ref
-            ikey_val = end_template['Parameters'][rbody['Properties'][image_key]['Ref']]
-            del end_template['Parameters'][rbody['Properties'][image_key]['Ref']]
+            if 'image' in MERGABLE_TYPES[rbody['Type']]:
+                image_key = MERGABLE_TYPES[rbody['Type']]['image']
+                # XXX Assuming ImageId is always a Ref
+                ikey_val = end_template['Parameters'][rbody['Properties'][image_key]['Ref']]
+                del end_template['Parameters'][rbody['Properties'][image_key]['Ref']]
             role = rbody.get('Metadata', {}).get('OpenStack::Role', r)
             role = translate_role(role)
             if role != r:
@@ -143,9 +146,10 @@ for template_path in templates:
             if 'Resources' not in end_template:
                 end_template['Resources'] = {}
             end_template['Resources'][role] = rbody
-            ikey = '%sImage' % (role)
-            end_template['Resources'][role]['Properties'][image_key] = {'Ref': ikey}
-            end_template['Parameters'][ikey] = ikey_val
+            if 'image' in MERGABLE_TYPES[rbody['Type']]:
+                ikey = '%sImage' % (role)
+                end_template['Resources'][role]['Properties'][image_key] = {'Ref': ikey}
+                end_template['Parameters'][ikey] = ikey_val
         elif rbody['Type'] == 'FileInclude':
             with open(rbody['Path']) as rfile:
                 include_content = yaml.safe_load(rfile.read())
@@ -196,7 +200,7 @@ def fix_ref(item, old, new):
 
 for change in resource_changes:
     fix_ref(end_template, change[0], change[1])
+
 if errors:
     for e in errors:
         sys.stderr.write("ERROR: %s\n" % e)
index 0eeb226..e6bfb19 100755 (executable)
@@ -26,6 +26,7 @@ echo
 run_test "python merge.py examples/source.yaml" examples/source_lib_result.yaml
 run_test "python merge.py examples/source2.yaml" examples/source2_lib_result.yaml
 run_test "python merge.py examples/source_include_subkey.yaml" examples/source_include_subkey_result.yaml
+run_test "python merge.py examples/launchconfig1.yaml examples/launchconfig2.yaml" examples/launchconfig_result.yaml
 echo
 trap - EXIT
 exit $fail