Merge pull request #11 from damjanek/master

Create aggregates + add hosts to aggregates
diff --git a/README.rst b/README.rst
index bb8f2de..d6d5dd8 100644
--- a/README.rst
+++ b/README.rst
@@ -131,6 +131,9 @@
         enabled: true
         virtualization: kvm
         availability_zone: availability_zone_01
+        aggregates:
+        - hosts_with_fc
+        - hosts_with_ssd
         security_group: true
         resume_guests_state_on_host_boot: False
         bind:
@@ -281,6 +284,21 @@
             - availability_zone_01
             - availability_zone_02
 
+
+
+Aggregates
+
+.. code-block:: yaml
+
+    nova:
+      client:
+        enabled: true
+        server:
+          identity:
+            aggregates:
+            - aggregate1
+            - aggregate2
+
 SR-IOV
 ------
 
diff --git a/_modules/novang.py b/_modules/novang.py
index d2009ae..dcac2fc 100644
--- a/_modules/novang.py
+++ b/_modules/novang.py
@@ -243,3 +243,44 @@
         'Availability Zone': item.__getattr__('availability_zone'),
     }
     return ret
+
+def aggregate_list(profile=None):
+    '''
+    list existing aggregates
+    '''
+    connection_args = get_connection_args(profile)
+    conn = _auth(profile)
+    nt_ks = conn.compute_conn
+    ret = nt_ks.aggregates.list()
+    return ret
+
+
+def aggregate_get(name, profile=None):
+    '''
+    list existing aggregates
+    '''
+    connection_args = get_connection_args(profile)
+    conn = _auth(profile)
+    nt_ks = conn.compute_conn
+    aggregate_exists=False
+    items = aggregate_list(profile)
+    for p in items:
+        item = nt_ks.aggregates.get(p).__getattr__('name')
+        if item == name:
+            aggregate_exists = True
+    return aggregate_exists
+
+
+def aggregate_create(name, aggregate, profile=None):
+    '''
+    create aggregate
+    '''
+    connection_args = get_connection_args(profile)
+    conn = _auth(profile)
+    nt_ks = conn.compute_conn
+    item = nt_ks.aggregates.create(name, aggregate)
+    ret = {
+        'Id': item.__getattr__('id'),
+        'Aggregate Name': item.__getattr__('name'),
+    }
+    return ret
diff --git a/_states/novang.py b/_states/novang.py
index 46fae9c..17ba41c 100644
--- a/_states/novang.py
+++ b/_states/novang.py
@@ -67,6 +67,20 @@
         return _already_exists(availability_zone, 'availabilty zone')
     return existing_availability_zones
 
+def aggregate_present(name=None, aggregate=None, profile=None):
+    '''
+    Ensures that the nova aggregate exists
+    '''
+    name = aggregate
+    aggregate_exists = __salt__['novang.aggregate_get'](name, profile)
+    if aggregate_exists == False:
+        item_created = __salt__['novang.aggregate_create'](name, aggregate, profile)
+        if bool(item_created):
+            return _created(aggregate, 'aggregate', item_created)
+    else:
+        return _already_exists(aggregate, 'aggregate')
+    return existing_aggregate
+
 
 def instance_present(name, flavor, image, networks, security_groups=None, profile=None, tenant_name=None):
     ret = {'name': name,
@@ -166,4 +180,4 @@
     else:
         changes_dict['comment'] = \
             '{0} {1} is in correct state'.format(resource, name)
-    return changes_dict
\ No newline at end of file
+    return changes_dict
diff --git a/nova/client.sls b/nova/client.sls
index 2d20a8f..57d62e5 100644
--- a/nova/client.sls
+++ b/nova/client.sls
@@ -44,6 +44,17 @@
 
 {%- endif %}
 
+{%- if identity.aggregates is defined %}
+
+{%- for aggregate_name in identity.aggregates %}
+nova_aggregate_{{ aggregate_name }}:
+  novang.aggregate_present:
+    - aggregate: {{ aggregate_name }}
+    - profile: {{ identity_name }}
+{%- endfor %}
+
+{%- endif %}
+
 {%- endfor %}
 
 {%- endif %}
diff --git a/nova/compute.sls b/nova/compute.sls
index a599fcf..efc8b0a 100644
--- a/nova/compute.sls
+++ b/nova/compute.sls
@@ -120,8 +120,6 @@
   - watch:
     - file: /etc/nova/nova.conf
 
-{%- if compute.availability_zone != None %}
-
 {%- set ident = compute.identity %}
 
 {%- if ident.get('api_version', '2') == '3' %}
@@ -138,6 +136,8 @@
 
 {%- 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 }}"
@@ -145,6 +145,14 @@
 
 {%- 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 }}"
+  - unless: "nova {{ identity_params }} aggregate-details {{ aggregate }} | grep {{ pillar.linux.system.name }}"
+
+{%- endfor %}
+
 {%- if compute.virtualization == 'kvm' %}
 
 {% if compute.ceph is defined %}
diff --git a/nova/map.jinja b/nova/map.jinja
index 10577d5..1842234 100644
--- a/nova/map.jinja
+++ b/nova/map.jinja
@@ -47,6 +47,7 @@
         'debug': false,
         'notification': false,
         'availability_zone': None,
+        'aggregates': [],
         'identity': {
             'region': 'RegionOne'
         },