|
4 | 4 | ApiQueries |
5 | 5 | """ |
6 | 6 | import logging |
7 | | -from typing import List, Dict, Any |
| 7 | +from typing import List, Dict, Any, Optional |
8 | 8 | import json |
| 9 | +import urllib.parse |
9 | 10 |
|
| 11 | +from utils.execption_utils import ExceptionUtils |
| 12 | +from utils.spp_utils import SppUtils |
10 | 13 | from sppConnection.rest_client import RequestType, RestClient |
11 | 14 |
|
12 | 15 |
|
@@ -108,33 +111,67 @@ def get_all_vms(self) -> List[Dict[str, Any]]: |
108 | 111 |
|
109 | 112 | def get_vms_per_sla(self) -> List[Dict[str, Any]]: |
110 | 113 | """retrieves and calculates all vmware per SLA.""" |
111 | | - endpoint = "/api/endeavour/catalog/recovery/hypervisorvm" |
112 | | - params = { |
113 | | - "action": "aggregate", |
114 | | - "pageSize": None |
115 | | - } |
116 | | - # other options: volume, vm, tag, tagcategory |
117 | | - post_data = { |
118 | | - "op":[ |
119 | | - { |
120 | | - "operation": "count", |
121 | | - "fieldname": "protectionInfo.policyName", |
122 | | - "outputname": "vmCountBySLA" # buggy request, does actually not change anything |
123 | | - } |
124 | | - ], |
125 | | - "group": [ |
126 | | - "protectionInfo.policyName" |
127 | | - ] |
128 | | - } |
129 | 114 |
|
130 | | - return self.__rest_client.get_objects( |
| 115 | + endpoint = "/ngp/slapolicy" |
| 116 | + allow_list = ["name", "id"] |
| 117 | + array_name = "slapolicies" |
| 118 | + |
| 119 | + sla_policty_list = self.__rest_client.get_objects( |
131 | 120 | endpoint=endpoint, |
132 | | - params=params, |
133 | | - request_type=RequestType.POST, |
134 | | - post_data=post_data, |
135 | | - add_time_stamp=True, |
136 | | - array_name="results") |
| 121 | + allow_list=allow_list, |
| 122 | + array_name=array_name, |
| 123 | + add_time_stamp=False |
| 124 | + ) |
| 125 | + |
| 126 | + result_list: List[Dict[str, Any]] = [] |
| 127 | + for sla_policty in sla_policty_list: |
| 128 | + try: |
| 129 | + sla_name: str = sla_policty["name"] |
| 130 | + except KeyError as error: |
| 131 | + ExceptionUtils.exception_info(error, extra_message="skipping one sla entry due missing name.") |
| 132 | + continue |
| 133 | + sla_id: Optional[str] = sla_policty.get("id", None) |
| 134 | + |
| 135 | + result_dict: Dict[str, Any] = {} |
| 136 | + |
| 137 | + ## hotadd: |
| 138 | + sla_name = urllib.parse.quote_plus(sla_name) |
| 139 | + |
| 140 | + endpoint = "/api/hypervisor/search" |
| 141 | + params = { |
| 142 | + "resourceType": "vm", |
| 143 | + "from": "hlo", |
| 144 | + "pageSize": 1, |
| 145 | + "filter": json.dumps([ |
| 146 | + { |
| 147 | + "property": "storageProfileName", |
| 148 | + "value": sla_name, |
| 149 | + "op": "=" |
| 150 | + } |
| 151 | + ]) |
| 152 | + } |
| 153 | + # other options: volume, vm, tag, tagcategory |
| 154 | + post_data = { |
| 155 | + "name": "*", |
| 156 | + "hypervisorType": "vmware", |
| 157 | + } |
| 158 | + |
| 159 | + (response_json, _) = self.__rest_client.query_url( |
| 160 | + self.__rest_client.get_url(endpoint), |
| 161 | + params, |
| 162 | + RequestType.POST, |
| 163 | + post_data) |
| 164 | + |
| 165 | + result_dict["slaName"] = sla_name |
| 166 | + result_dict["slaId"] = sla_id |
| 167 | + result_dict["vmCountBySLA"] = response_json.get("total", None) |
| 168 | + |
| 169 | + time_key, time = SppUtils.get_capture_timestamp_sec() |
| 170 | + result_dict[time_key] = time |
| 171 | + |
| 172 | + result_list.append(result_dict) |
137 | 173 |
|
| 174 | + return result_list |
138 | 175 |
|
139 | 176 | def get_sla_dump(self) -> List[Dict[str, Any]]: |
140 | 177 | """retrieves all storage profiles.""" |
|
0 commit comments