@@ -2433,6 +2433,7 @@ def __init__(self, api_client):
2433
2433
logger = logging .getLogger ('ntnx_api.prism.Hosts.__init__' )
2434
2434
self .api_client = api_client
2435
2435
self .hosts = {}
2436
+ self .hosts_metadata = {}
2436
2437
2437
2438
def get (self , clusteruuid = None ):
2438
2439
"""Retrieve data for each host in a specific cluster
@@ -2457,10 +2458,35 @@ def get(self, clusteruuid=None):
2457
2458
if clusteruuid :
2458
2459
params ['proxyClusterUuid' ] = clusteruuid
2459
2460
2460
- self .hosts [clusteruuid ] = self .api_client .request (uri = uri , api_version = 'v2.0' , payload = payload , params = params ).get (
2461
- 'entities' )
2461
+ self .hosts [clusteruuid ] = self .api_client .request (uri = uri , api_version = 'v2.0' , payload = payload , params = params ).get ('entities' )
2462
2462
return self .hosts [clusteruuid ]
2463
2463
2464
+ def get_metadata (self , refresh = False ):
2465
+ """Retrieve metadata for each host from the connected PC instance
2466
+
2467
+ :returns: A list of dictionaries describing each vm from the specified cluster.
2468
+ :rtype: ResponseList
2469
+ """
2470
+ params = {}
2471
+ payload = {
2472
+ "kind" : "host" ,
2473
+ "offset" : 0 ,
2474
+ "length" : 2147483647
2475
+ }
2476
+ uri = '/hosts/list'
2477
+
2478
+ if self .api_client .connection_type == "pc" :
2479
+ # Remove existing data for this cluster if it exists
2480
+ if self .hosts_metadata or refresh :
2481
+ self .hosts_metadata = {}
2482
+ logger .info ('removing existing data from class dict hosts_metadata' )
2483
+
2484
+ hosts = self .api_client .request (uri = uri , api_version = 'v3' , payload = payload , params = params ).get ('entities' )
2485
+ for host in hosts :
2486
+ self .hosts_metadata [host .get ('metadata' ).get ('uuid' )] = host .get ('metadata' )
2487
+
2488
+ return self .hosts_metadata
2489
+
2464
2490
def search_uuid (self , uuid , clusteruuid = None , refresh = False ):
2465
2491
"""Retrieve data for a specific host, in a specific cluster by host uuid
2466
2492
@@ -2537,12 +2563,9 @@ def search_ip(self, ip_address, clusteruuid=None, refresh=False):
2537
2563
2538
2564
return found
2539
2565
2540
- def get_project (self , uuid , clusteruuid = None , refresh = False ):
2566
+ def get_project (self , uuid , refresh = False ):
2541
2567
"""Retrieve the project assigned to the specified host if connected to a prism central
2542
2568
2543
- :param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
2544
- `connection_type` is set to `pc`.
2545
- :type clusteruuid: str, optional
2546
2569
:param uuid: The UUID of a host.
2547
2570
:type uuid: str
2548
2571
:param refresh: Whether to refresh the class dataset (default=False).
@@ -2553,33 +2576,34 @@ def get_project(self, uuid, clusteruuid=None, refresh=False):
2553
2576
"""
2554
2577
logger = logging .getLogger ('ntnx_api.prism.Hosts.get_project' )
2555
2578
project = ''
2556
- if self .api_client .connection_type == 'pc' :
2557
- metadata = self .search_uuid (uuid = uuid , clusteruuid = clusteruuid , refresh = refresh ).get ('vm_metadata' )
2558
- if metadata .get ('project_reference' ).get ('kind' ) == 'project' :
2559
- project = metadata .get ('project_reference' ).get ('name' )
2579
+ if self .api_client .connection_type == "pc" :
2580
+ metadata = self .get_metadata (refresh = refresh )
2581
+ vm_metadata = next ((item for item in metadata if item ['uuid' ] == uuid ), None )
2582
+ if vm_metadata :
2583
+ if vm_metadata .get ('project_reference' ).get ('kind' ) == 'project' :
2584
+ project = vm_metadata .get ('project_reference' ).get ('name' )
2560
2585
return project
2561
2586
2562
- def get_categories (self , uuid , clusteruuid = None , refresh = False ):
2587
+ def get_categories (self , uuid , refresh = False ):
2563
2588
"""Retrieve the categories assigned to the specified host if connected to a prism central
2564
2589
2565
- :param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
2566
- `connection_type` is set to `pc`.
2567
- :type clusteruuid: str, optional
2568
2590
:param uuid: The UUID of a host.
2569
2591
:type uuid: str
2570
2592
:param refresh: Whether to refresh the class dataset (default=False).
2571
2593
:type refresh: bool, optional
2572
2594
2573
- :returns: A dictionary with all .
2595
+ :returns: A dictionary with all categories for the specified host .
2574
2596
:rtype: ResponseDict
2575
2597
"""
2576
2598
logger = logging .getLogger ('ntnx_api.prism.Hosts.get_categories' )
2577
- categories = []
2578
- if self .api_client .connection_type == 'pc' :
2579
- metadata = self .search_uuid (uuid = uuid , clusteruuid = clusteruuid , refresh = refresh ).get ('categories' )
2580
- if metadata .get ('project_reference' ).get ('kind' ) == 'project' :
2581
- for key , value in metadata .get ('categories' ).items ():
2582
- categories [key ] = value
2599
+ categories = {}
2600
+ if self .api_client .connection_type == "pc" :
2601
+ metadata = self .get_metadata (refresh = refresh )
2602
+ vm_metadata = next ((item for item in metadata if item ['uuid' ] == uuid ), None )
2603
+ if vm_metadata :
2604
+ if 'categories' in vm_metadata :
2605
+ for key , value in vm_metadata .get ('categories' ).items ():
2606
+ categories [key ] = value
2583
2607
return categories
2584
2608
2585
2609
@@ -2594,6 +2618,7 @@ def __init__(self, api_client):
2594
2618
logger = logging .getLogger ('ntnx_api.prism.Vms.__init__' )
2595
2619
self .api_client = api_client
2596
2620
self .vms = {}
2621
+ self .vms_metadata = {}
2597
2622
2598
2623
def get (self , clusteruuid = None , include_disks = True , include_nics = True ):
2599
2624
"""Retrieve host data for each virtual machine in a specific cluster
@@ -2636,6 +2661,32 @@ def get(self, clusteruuid=None, include_disks=True, include_nics=True):
2636
2661
self .vms [clusteruuid ] = self .api_client .request (uri = uri , api_version = 'v2.0' , payload = payload , params = params ).get ('entities' )
2637
2662
return self .vms [clusteruuid ]
2638
2663
2664
+ def get_metadata (self , refresh = False ):
2665
+ """Retrieve metadata for each virtual machine from the connected PC instance
2666
+
2667
+ :returns: A list of dictionaries describing each vm from the specified cluster.
2668
+ :rtype: ResponseList
2669
+ """
2670
+ params = {}
2671
+ payload = {
2672
+ "kind" : "vm" ,
2673
+ "offset" : 0 ,
2674
+ "length" : 2147483647
2675
+ }
2676
+ uri = '/vms/list'
2677
+
2678
+ if self .api_client .connection_type == "pc" :
2679
+ # Remove existing data for this cluster if it exists
2680
+ if self .vms_metadata or refresh :
2681
+ self .vms_metadata = {}
2682
+ logger .info ('removing existing data from class dict vms_metadata' )
2683
+
2684
+ vms = self .api_client .request (uri = uri , api_version = 'v3' , payload = payload , params = params ).get ('entities' )
2685
+ for vm in vms :
2686
+ self .vms_metadata [vm .get ('metadata' ).get ('uuid' )] = vm .get ('metadata' )
2687
+
2688
+ return self .vms_metadata
2689
+
2639
2690
def search_uuid (self , uuid , clusteruuid = None , refresh = False ):
2640
2691
"""Retrieve data for a specific vm, in a specific cluster by vm uuid
2641
2692
@@ -2688,49 +2739,47 @@ def search_name(self, name, clusteruuid=None, refresh=False):
2688
2739
2689
2740
return found
2690
2741
2691
- def get_project (self , uuid , clusteruuid = None , refresh = False ):
2742
+ def get_project (self , uuid , refresh = False ):
2692
2743
"""Retrieve the project assigned to the specified VM if connected to a prism central
2693
2744
2694
- :param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
2695
- `connection_type` is set to `pc`.
2696
- :type clusteruuid: str, optional
2697
2745
:param uuid: The UUID of a VM.
2698
2746
:type uuid: str
2699
- :param refresh: Whether to refresh the class VM dataset (default=False).
2747
+ :param refresh: Whether to refresh the class VM Metadata dataset (default=False).
2700
2748
:type refresh: bool, optional
2701
2749
2702
2750
:returns: A string containing the project name.
2703
2751
:rtype: str
2704
2752
"""
2705
2753
logger = logging .getLogger ('ntnx_api.prism.Vms.get_project' )
2706
2754
project = ''
2707
- if self .api_client .connection_type == 'pc' :
2708
- metadata = self .search_uuid (uuid = uuid , clusteruuid = clusteruuid , refresh = refresh ).get ('vm_metadata' )
2709
- if metadata .get ('project_reference' ).get ('kind' ) == 'project' :
2710
- project = metadata .get ('project_reference' ).get ('name' )
2755
+ if self .api_client .connection_type == "pc" :
2756
+ metadata = self .get_metadata (refresh = refresh )
2757
+ vm_metadata = next ((item for item in metadata if item ['uuid' ] == uuid ), None )
2758
+ if vm_metadata :
2759
+ if vm_metadata .get ('project_reference' ).get ('kind' ) == 'project' :
2760
+ project = vm_metadata .get ('project_reference' ).get ('name' )
2711
2761
return project
2712
2762
2713
- def get_categories (self , uuid , clusteruuid = None , refresh = False ):
2763
+ def get_categories (self , uuid , refresh = False ):
2714
2764
"""Retrieve the categories assigned to the specified VM if connected to a prism central
2715
2765
2716
- :param clusteruuid: A cluster UUID to define the specific cluster to query. Only required to be used when the :class:`ntnx.client.ApiClient`
2717
- `connection_type` is set to `pc`.
2718
- :type clusteruuid: str, optional
2719
2766
:param uuid: The UUID of a VM.
2720
2767
:type uuid: str
2721
- :param refresh: Whether to refresh the class VM dataset (default=False).
2768
+ :param refresh: Whether to refresh the class VM Metadata dataset (default=False).
2722
2769
:type refresh: bool, optional
2723
2770
2724
2771
:returns: A dictionary with all .
2725
2772
:rtype: ResponseDict
2726
2773
"""
2727
2774
logger = logging .getLogger ('ntnx_api.prism.Vms.get_categories' )
2728
- categories = []
2729
- if self .api_client .connection_type == 'pc' :
2730
- metadata = self .search_uuid (uuid = uuid , clusteruuid = clusteruuid , refresh = refresh ).get ('categories' )
2731
- if metadata .get ('project_reference' ).get ('kind' ) == 'project' :
2732
- for key , value in metadata .get ('categories' ).items ():
2733
- categories [key ] = value
2775
+ categories = {}
2776
+ if self .api_client .connection_type == "pc" :
2777
+ metadata = self .get_metadata (refresh = refresh )
2778
+ vm_metadata = next ((item for item in metadata if item ['uuid' ] == uuid ), None )
2779
+ if vm_metadata :
2780
+ if 'categories' in vm_metadata :
2781
+ for key , value in vm_metadata .get ('categories' ).items ():
2782
+ categories [key ] = value
2734
2783
return categories
2735
2784
2736
2785
0 commit comments