From 3160b80e7b9b60b34ea5d2d7fa56b080a79a3269 Mon Sep 17 00:00:00 2001
From: Trey Morris <trey.morris@rackspace.com>
Date: Thu, 1 Dec 2011 16:54:40 -0600
Subject: [PATCH] Adds network model and network info cache.

The next merge will prepopulate the cache, and use the model to keep the
cache up to date.
I realize "cache" is a bit of a stretch for what this is doing.

blueprint network-info-model
blueprint compute-network-info

Change-Id: I0f0f4ba3de1310e1ff89239dab6ea8e24c85f2c8
---
 nova/db/api.py            | 42 +++++++++++++++++++++++
 nova/db/sqlalchemy/api.py | 71 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 113 insertions(+)

diff --git a/nova/db/api.py b/nova/db/api.py
index 4af79b13c..bc413319e 100644
--- a/nova/db/api.py
+++ b/nova/db/api.py
@@ -649,6 +649,48 @@ def instance_get_id_to_uuid_mapping(context, ids):
 ###################
 
 
+def instance_info_cache_create(context, values):
+    """Create a new instance cache record in the table.
+
+    :param context: = request context object
+    :param values: = dict containing column values
+    """
+    return IMPL.instance_info_cache_create(context, values)
+
+
+def instance_info_cache_get(context, instance_id, session=None):
+    """Gets an instance info cache from the table.
+
+    :param instance_id: = id of the info cache's instance
+    :param session: = optional session object
+    """
+    return IMPL.instance_info_cache_get(context, instance_id, session=None)
+
+
+def instance_info_cache_update(context, instance_id, values,
+                               session=None):
+    """Update an instance info cache record in the table.
+
+    :param instance_id: = id of info cache's instance
+    :param values: = dict containing column values to update
+    """
+    return IMPL.instance_info_cache_update(context, instance_id, values,
+                                           session)
+
+
+def instance_info_cache_delete_by_instance_id(context, instance_id,
+                                              session=None):
+    """Deletes an existing instance_info_cache record
+
+    :param instance_id: = id of the instance tied to the cache record
+    """
+    return IMPL.instance_info_cache_delete_by_instance_id(context, instance_id,
+                                                          session)
+
+
+###################
+
+
 def key_pair_create(context, values):
     """Create a key_pair from the values dictionary."""
     return IMPL.key_pair_create(context, values)
diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py
index a8424686f..2619c247f 100644
--- a/nova/db/sqlalchemy/api.py
+++ b/nova/db/sqlalchemy/api.py
@@ -1132,6 +1132,8 @@ def instance_destroy(context, instance_id):
                 update({'deleted': True,
                         'deleted_at': utils.utcnow(),
                         'updated_at': literal_column('updated_at')})
+        instance_info_cache_delete_by_instance_id(context, instance_id,
+                                                  session=session)
 
 
 @require_context
@@ -1557,6 +1559,75 @@ def instance_get_id_to_uuid_mapping(context, ids):
 ###################
 
 
+@require_context
+def instance_info_cache_create(context, values):
+    """Create a new instance cache record in the table.
+
+    :param context: = request context object
+    :param values: = dict containing column values
+    """
+    info_cache = models.InstanceInfoCache()
+    info_cache['id'] = str(utils.gen_uuid())
+    info_cache.update(values)
+
+    session = get_session()
+    with session.begin():
+        info_cache.save(session=session)
+    return info_cache
+
+
+@require_context
+def instance_info_cache_get(context, instance_id, session=None):
+    """Gets an instance info cache from the table.
+
+    :param instance_id: = id of the info cache's instance
+    :param session: = optional session object
+    """
+    session = session or get_session()
+
+    info_cache = session.query(models.InstanceInfoCache).\
+                         filter_by(instance_id=instance_id).\
+                         first()
+    return info_cache
+
+
+@require_context
+def instance_info_cache_update(context, instance_id, values,
+                               session=None):
+    """Update an instance info cache record in the table.
+
+    :param instance_id: = id of info cache's instance
+    :param values: = dict containing column values to update
+    :param session: = optional session object
+    """
+    session = session or get_session()
+    info_cache = instance_info_cache_get(context, instance_id,
+                                         session=session)
+
+    values['updated_at'] = literal_column('updated_at')
+
+    if info_cache:
+        info_cache.update(values)
+        info_cache.save(session=session)
+    return info_cache
+
+
+@require_context
+def instance_info_cache_delete_by_instance_id(context, instance_id,
+                                              session=None):
+    """Deletes an existing instance_info_cache record
+
+    :param instance_id: = id of the instance tied to the cache record
+    :param session: = optional session object
+    """
+    values = {'deleted': True,
+              'deleted_at': utils.utcnow()}
+    instance_info_cache_update(context, instance_id, values, session)
+
+
+###################
+
+
 @require_context
 def key_pair_create(context, values):
     key_pair_ref = models.KeyPair()