Merge remote-tracking branch 'origin/geni-v3' into geni-v3
[sfa.git] / sfa / openstack / nova_driver.py
index efbc258..af9ce4d 100644 (file)
@@ -2,13 +2,12 @@ import time
 import datetime
 
 from sfa.util.faults import MissingSfaInfo, UnknownSfaType, \
-    RecordNotFound, SfaNotImplemented, SliverDoesNotExist, \
-    SfaInvalidArgument
+    RecordNotFound, SfaNotImplemented, SfaInvalidArgument
 
 from sfa.util.sfalogging import logger
 from sfa.util.defaultdict import defaultdict
 from sfa.util.sfatime import utcparse, datetime_to_string, datetime_to_epoch
-from sfa.util.xrn import Xrn, hrn_to_urn, get_leaf, urn_to_sliver_id
+from sfa.util.xrn import Xrn, hrn_to_urn, get_leaf 
 from sfa.openstack.osxrn import OSXrn, hrn_to_os_slicename, hrn_to_os_tenant_name
 from sfa.util.cache import Cache
 from sfa.trust.credential import Credential
@@ -43,7 +42,7 @@ class NovaDriver(Driver):
 
     def __init__ (self, config):
         Driver.__init__(self, config)
-        self.shell = Shell(config)
+        self.shell = Shell(config=config)
         self.cache=None
         if config.SFA_AGGREGATE_CACHING:
             if NovaDriver.cache is None:
@@ -90,7 +89,9 @@ class NovaDriver(Driver):
         for researcher in researchers:
             name = Xrn(researcher).get_leaf()
             user = self.shell.auth_manager.users.find(name=name)
+            self.shell.auth_manager.roles.add_user_role(user, 'Member', tenant)
             self.shell.auth_manager.roles.add_user_role(user, 'user', tenant)
+            
 
         pis = sfa_record.get('pis', [])
         for pi in pis:
@@ -327,61 +328,24 @@ class NovaDriver(Driver):
         return instance_urns
         
     # first 2 args are None in case of resource discovery
-    def list_resources (self, creds, options):
+    def list_resources (self, creds, version, options):
         aggregate = OSAggregate(self)
-        rspec =  aggregate.get_rspec(version=rspec_version, options=options)
+        rspec =  aggregate.list_resources(version=version, options=options)
         return rspec
 
-    def describe(self, creds, urns, options):
-        return {}
+    def describe(self, creds, urns, version, options):
+        aggregate = OSAggregate(self)
+        return aggregate.describe(urns, version=version, options=options)
     
-    def sliver_status (self, slice_urn, slice_hrn):
-        # find out where this slice is currently running
-        project_name = hrn_to_os_slicename(slice_hrn)
-        project = self.shell.auth_manager.get_project(project_name)
-        instances = self.shell.db.instance_get_all_by_project(project_name)
-        if len(instances) == 0:
-            raise SliverDoesNotExist("You have not allocated any slivers here") 
-        
-        result = {}
-        result['geni_urn'] = slice_urn
-        result['plos_login'] = 'root'
-        # do we need real dates here? 
-        result['plos_expires'] = None
-        result['geni_expires'] = None
-        
-        resources = []
-        for instance in instances:
-            res = {}
-            # instances are accessed by ip, not hostname. We need to report the ip
-            # somewhere so users know where to ssh to.     
-            res['geni_expires'] = None
-            res['plos_hostname'] = instance.hostname
-            res['plos_created_at'] = datetime_to_string(utcparse(instance.created_at))    
-            res['plos_boot_state'] = instance.vm_state
-            res['plos_sliver_type'] = instance.instance_type.name 
-            sliver_id =  Xrn(slice_urn).get_sliver_id(instance.project_id, \
-                                                      instance.hostname, instance.id)
-            res['geni_urn'] = sliver_id
-
-            if instance.vm_state == 'running':
-                res['boot_state'] = 'ready'
-                res['geni_status'] = 'ready'
-            else:
-                res['boot_state'] = 'unknown'  
-                res['geni_status'] = 'unknown'
-            res['geni_allocation_status'] = 'geni_provisioned'
-            resources.append(res)
-            
-        result['geni_resources'] = resources
-        return result
+    def status (self, urns, options={}):
+        aggregate = OSAggregate(self)
+        desc =  aggregate.describe(urns)
+        return desc['geni_slivers']
 
     def create_sliver (self, slice_urn, slice_hrn, creds, rspec_string, users, options):
 
         aggregate = OSAggregate(self)
-        rspec = RSpec(rspec_string)
-        instance_name = hrn_to_os_slicename(slice_hrn)
-       
+
         # assume first user is the caller and use their context
         # for the ec2/euca api connection. Also, use the first users
         # key as the project key.
@@ -394,17 +358,22 @@ class NovaDriver(Driver):
         for user in users:
             pubkeys.extend(user['keys'])
            
-        aggregate.run_instances(instance_name, rspec_string, key_name, pubkeys)    
+        rspec = RSpec(rspec_string)
+        instance_name = hrn_to_os_slicename(slice_hrn)
+        tenant_name = OSXrn(xrn=slice_hrn, type='slice').get_tenant_name()
+        aggregate.run_instances(instance_name, tenant_name, rspec_string, key_name, pubkeys)    
    
-        return aggregate.get_rspec(slice_xrn=slice_urn, version=rspec.version)
+        return aggregate.describe(slice_xrn=slice_urn, version=rspec.version)
 
     def delete_sliver (self, slice_urn, slice_hrn, creds, options):
         aggregate = OSAggregate(self)
+        tenant_name = OSXrn(xrn=slice_hrn, type='slice').get_tenant_name()
         project_name = hrn_to_os_slicename(slice_hrn)
-        return aggregate.delete_instances(project_name)   
+        return aggregate.delete_instances(project_name, tenant_name)   
 
     def update_sliver(self, slice_urn, slice_hrn, rspec, creds, options):
         name = hrn_to_os_slicename(slice_hrn)
+        tenant_name = OSXrn(xrn=slice_hrn, type='slice').get_tenant_name()
         aggregate = OSAggregate(self)
         return aggregate.update_instances(name)
     
@@ -415,9 +384,10 @@ class NovaDriver(Driver):
         return 1
 
     def stop_slice (self, slice_urn, slice_hrn, creds):
+        tenant_name = OSXrn(xrn=slice_hrn, type='slice').get_tenant_name()
         name = OSXrn(xrn=slice_urn).name
         aggregate = OSAggregate(self)
-        return aggregate.stop_instances(name) 
+        return aggregate.stop_instances(name, tenant_name
 
     def reset_slice (self, slice_urn, slice_hrn, creds):
         raise SfaNotImplemented ("reset_slice not available at this interface")