Merge "Adding ability to set default_availability_zone for nova controller." into release/2019.2.0
diff --git a/nova/compute.sls b/nova/compute.sls
index 67d19c8..a8e60d8 100644
--- a/nova/compute.sls
+++ b/nova/compute.sls
@@ -355,26 +355,41 @@
 
 {%- set identity_params = " --os-username="+ident.user+" --os-password="+ident.password+" --os-project-name="+ident.tenant+" --os-auth-url="+protocol+"://"+ident.host+":"+ident.port|string+"/"+version %}
 
-{%- if compute.availability_zone != None %}
-
-Add_compute_to_availability_zone_{{ compute.availability_zone }}:
-  cmd.run:
-  - name: "nova {{ identity_params }} aggregate-add-host {{ compute.availability_zone }} {{ pillar.linux.system.name }}"
-  - unless: "nova {{ identity_params }} service-list | grep {{ compute.availability_zone }} | grep {{ pillar.linux.system.name }}"
-
+{%- if compute.host is defined %}
+{%-   set compute_name = compute.host %}
+{%- else %}
+{%-   if pillar.get('linux',{}).get('system',{}).name is defined %}
+{%-     set compute_name = pillar.linux.system.name %}
+{%-   else %}
+{%-     set compute_name = grains.get('nodename') %}
+{%-   endif %}
 {%- endif %}
 
-{%- for aggregate in compute.aggregates %}
+{%- if compute.aggregates|length > 0 %}
+{%-   if compute.version not in ['juno','kilo','liberty','mitaka'] %}
+map_compute_hosts_to_cell:
+  cmd.run:
+  - name: nova-manage cell_v2 map_cell_and_hosts
+  - unless: nova-manage cell_v2 list_hosts | grep -w {{ compute_name }}
+{%-   endif %}
+{%-   for aggregate in compute.aggregates %}
+
 Add_compute_to_aggregate_{{ aggregate }}:
   cmd.run:
-  - name: "nova {{ identity_params }} aggregate-add-host {{ aggregate }} {{ pillar.linux.system.name }}"
+  - name: "nova {{ identity_params }} aggregate-add-host {{ aggregate }} {{ compute_name }}"
   {%- if compute.version in ['juno','kilo','liberty','mitaka'] %}
-  - unless: "nova {{ identity_params }} aggregate-details {{ aggregate }} | grep {{ pillar.linux.system.name }}"
+  - unless: "nova {{ identity_params }} aggregate-details {{ aggregate }} | grep -w {{ compute_name }}"
   {%- else %}
-  - unless: "nova {{ identity_params }} aggregate-show {{ aggregate }} | grep {{ pillar.linux.system.name }}"
+  - unless: "nova {{ identity_params }} aggregate-show {{ aggregate }} | grep -w {{ compute_name }}"
+  {%- if compute.get('check_aggregates', False) %}
+  - onlyif: "nova {{ identity_params }} aggregate-list | grep -w {{ aggregate }}"
+  {%- endif %}
+  - require:
+    - cmd: map_compute_hosts_to_cell
   {%- endif %}
 
-{%- endfor %}
+{%-   endfor %}
+{%- endif %}
 
 {%- if compute.get('compute_driver', 'libvirt.LibvirtDriver') == 'libvirt.LibvirtDriver' %}