From 22d647b3d0a3f2ba3463001c0630ae65bdb06255 Mon Sep 17 00:00:00 2001
From: parmentelat <thierry.parmentelat@inria.fr>
Date: Fri, 16 Mar 2018 09:50:09 +0100
Subject: [PATCH] code for exposing country & city for a site this applies only
 to sites that have a latitude and longitude in this case, the 'country' and
 'city' site tags are used; they are set to 'unknown' otherwise

---
 sfa/planetlab/plaggregate.py    | 17 +++++++++++++++--
 sfa/planetlab/plshell.py        |  2 +-
 sfa/rspecs/elements/location.py |  1 +
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/sfa/planetlab/plaggregate.py b/sfa/planetlab/plaggregate.py
index 8095291d..a6b76da2 100644
--- a/sfa/planetlab/plaggregate.py
+++ b/sfa/planetlab/plaggregate.py
@@ -272,8 +272,21 @@ class PlAggregate:
         # add site/interface info to nodes.
         # assumes that sites, interfaces and tags have already been prepared.
         if site['longitude'] and site['latitude']:
-            location = Location({'longitude': site['longitude'], 'latitude': site[
-                                'latitude'], 'country': 'unknown'})
+            location_dict = {
+                'longitude': site['longitude'],
+                'latitude': site['latitude'],
+            }
+            for extra in ('country', 'city'):
+                try:
+                    tts = self.driver.shell.GetSiteTags({
+                        'site_id' : site['site_id'],
+                        'tagname' : extra,
+                    })
+                    location_dict[extra] = tts[0]['value']
+                except:
+                    logger.log_exc('extra = {}'.format(extra))
+                    location_dict[extra] = 'unknown'
+            location = Location(location_dict)
             rspec_node['location'] = location
         # Granularity
         granularity = Granularity({'grain': grain})
diff --git a/sfa/planetlab/plshell.py b/sfa/planetlab/plshell.py
index 8877f410..94b66eac 100644
--- a/sfa/planetlab/plshell.py
+++ b/sfa/planetlab/plshell.py
@@ -23,7 +23,7 @@ class PlShell:
                     'UnBindObjectFromPeer', 'UpdateNode', 'UpdatePerson', 'UpdateSite',
                     'UpdateSlice', 'UpdateSliceTag',
                     # also used as-is in importer
-                    'GetSites', 'GetNodes',
+                    'GetSites', 'GetNodes', 'GetSiteTags',
                     # Lease management methods
                     'GetLeases', 'GetLeaseGranularity', 'DeleteLeases', 'UpdateLeases',
                     'AddLeases',
diff --git a/sfa/rspecs/elements/location.py b/sfa/rspecs/elements/location.py
index f99c5432..e42e1600 100644
--- a/sfa/rspecs/elements/location.py
+++ b/sfa/rspecs/elements/location.py
@@ -7,4 +7,5 @@ class Location(Element):
         'country',
         'longitude',
         'latitude',
+        'city',
     ]
-- 
2.47.0