fixing issue from mitaka to newton and then to ocata upgrade

Change-Id: I571009d3bf4d0d6db1c29ae7e5fe2d3703957f73
diff --git a/_states/novang.py b/_states/novang.py
index cf52512..224757c 100644
--- a/_states/novang.py
+++ b/_states/novang.py
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 '''
-Nova state that ensures that defined flavor is present
+Custom Nova state
 '''
 import logging
 import collections
@@ -12,7 +12,7 @@
     '''
     Only load if the nova module is in __salt__
     '''
-    return 'novang' if 'novang.flavor_list' in __salt__ else False
+    return 'novang'
 
 
 def flavor_present(name, flavor_id=0, ram=0, disk=0, vcpus=1, profile=None):
@@ -35,6 +35,28 @@
     return ret
 
 
+def map_instances(name='cell1'):
+    '''
+    Ensures that the nova instances are mapped to cell
+    '''
+    ret = {'name': name,
+           'changes': {},
+           'result': False,
+           'comment': 'Cell "{0}" does not exists'.format(name)}
+    cell_uuid = __salt__['cmd.shell']('nova-manage cell_v2 list_cells 2>&- | grep ' + name + ' | tr -d \"\n\" | awk \'{print $4}\'')
+    if not cell_uuid:
+        try:
+            __salt__['cmd.shell']('nova-manage cell_v2 map_instances --cell_uuid ' + cell_uuid)
+            ret['result'] = True
+            ret['comment'] = 'Instances were mapped to cell named {0}'.format(name)
+            ret['changes']['Instances'] = 'Mapped to cell named {0}'.format(name)
+        except:
+            ret['result'] = False
+            ret['comment'] = 'Error while mapping instances to cell named {0}'.format(name)
+            ret['changes']['Instances'] = 'Failed to map to cell named {0}'.format(name)
+    return ret
+
+
 def quota_present(tenant_name, profile, name=None, **kwargs):
     '''
     Ensures that the nova quota exists
diff --git a/nova/controller.sls b/nova/controller.sls
index 006a541..cacd272 100644
--- a/nova/controller.sls
+++ b/nova/controller.sls
@@ -93,47 +93,16 @@
 
 {%- endfor %}
 
-{%- if controller.version in ["mitaka", "newton", "ocata"] %}
-nova_controller_sync_apidb:
-  cmd.run:
-  - name: nova-manage api_db sync
-  {%- if grains.get('noservices') %}
-  - onlyif: /bin/false
-  {%- endif %}
-  - require:
-    - file: /etc/nova/nova.conf
-
-{%- if controller.version in ["newton", "ocata"] %}
-nova_controller_online_data_migrations:
-  cmd.run:
-  - name: nova-manage db online_data_migrations
-{%- endif %}
-
-nova_controller_syncdb:
-  cmd.run:
-  - names:
-    - nova-manage db sync
-  - require:
-    - file: /etc/nova/nova.conf
-
 {%- if controller.version in ["ocata"] %}
 
 nova_controller_map_cell0:
   cmd.run:
   - name: nova-manage cell_v2 map_cell0
-  - require:
-    - cmd: nova_controller_sync_apidb
-  - require_in:
-    - cmd: nova_controller_syncdb
 
 nova_cell1_create:
   cmd.run:
   - name: nova-manage cell_v2 create_cell --name=cell1
   - unless: 'nova-manage cell_v2 list_cells | grep cell1'
-  - require:
-    - cmd: nova_controller_sync_apidb
-  - require_in:
-    - cmd: nova_controller_syncdb
 
 nova_placement_service_mask:
   file.symlink:
@@ -159,6 +128,53 @@
      - name: /etc/apache2/sites-enabled/nova-placement-api.conf
      - target: /etc/apache2/sites-available/nova-placement-api.conf
 
+nova_controller_discover_hosts:
+  cmd.run:
+  - name: nova-manage cell_v2 discover_hosts
+  - require:
+    - cmd: nova_controller_map_cell0
+    - cmd: nova_cell1_create
+
+nova_controller_map_instances:
+  novang.map_instances:
+  - name: 'cell1'
+  - require:
+    - cmd: nova_controller_discover_hosts
+    - pkg: nova_controller_packages
+
+{%- endif %}
+
+{%- if controller.version in ["mitaka", "newton", "ocata"] %}
+nova_controller_sync_apidb:
+  cmd.run:
+  - name: nova-manage api_db sync
+  {%- if grains.get('noservices') %}
+  - onlyif: /bin/false
+  {%- endif %}
+  - require:
+    - file: /etc/nova/nova.conf
+
+{%- endif %}
+
+nova_controller_syncdb:
+  cmd.run:
+  - names:
+    - nova-manage db sync
+  - require:
+    - file: /etc/nova/nova.conf
+
+{%- if controller.version in ["mitaka", "newton", "ocata"] %}
+
+nova_controller_online_data_migrations:
+  cmd.run:
+  - name: nova-manage db online_data_migrations
+  - require:
+    - cmd: nova_controller_syncdb
+
+{%- endif %}
+
+{%- if controller.version in ["ocata"] %}
+
 nova_apache_restart:
   service.running:
   - enable: true
@@ -182,8 +198,6 @@
     - file: /etc/nova/nova.conf
     - file: /etc/nova/api-paste.ini
 
-{%- endif %}
-
 {%- if grains.get('virtual_subtype', None) == "Docker" %}
 
 nova_entrypoint: