Fix for nova aggregate mappings

- Use compute.host if defined and not linux hostname
- Do not try to create aggregate with name of AZ.
  Compute should have it in config file and it would
  register itself automatically in that AZ without
  external call. Aggregates != AZs
- Map compute host to cell if not mapped before
  adding it to aggregate
- Check that aggregate exists before adding
  This feature is disabled by default, so you will
  not miss faulty aggregate during deploy

Prod-Related: PROD-34033
Change-Id: Iefc25eb9cbc2e4602f95f59f1e366a6cde7db484
diff --git a/nova/compute.sls b/nova/compute.sls
index fb622c2..3189df3 100644
--- a/nova/compute.sls
+++ b/nova/compute.sls
@@ -354,26 +354,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' %}