Added Manila shares/types/nets cleanup

Related-PROD: PROD-37187
Change-Id: I4db93efff7728d1495af61bd565f5d90dd1f0737
diff --git a/cleanup.py b/cleanup.py
index f641a95..403e549 100644
--- a/cleanup.py
+++ b/cleanup.py
@@ -14,6 +14,7 @@
 openstack.enable_logging(debug=False, path='openstack.log', stream=sys.stdout)
 
 volume_api_version = "3.43"
+manila_api_version = "2.75"
 
 # Connect to cloud
 TEST_CLOUD = os.getenv('OS_TEST_CLOUD', 'os-cloud')
@@ -38,6 +39,12 @@
 if object_store_present:
     object_store = cloud.object_store
 
+# Check if Manila Shared File System is present on the cloud, else skip
+manila_present = any(service.type == 'sharev2' for service
+                           in list(identity.services()))
+if manila_present:
+    shared_file_system = cloud.shared_file_system
+
 mask = "cvp|s_rally|rally_|tempest-|tempest_|spt|fio"
 full_mask = f"^(?!.*(manual|-static-)).*({mask}).*$"
 mask_pattern = re.compile(full_mask, re.IGNORECASE)
@@ -93,6 +100,45 @@
         yield group
 
 
+def _get_shares(all_tenants='true'):
+    ep = shared_file_system.get_endpoint()
+    uri = f"{ep}/shares/detail"
+    headers = {'X-Auth-Token': cloud.session.get_token(),
+               'Accept': 'application/json',
+               'X-Openstack-Manila-Api-Version': f'{manila_api_version}'}
+    params = {'all_tenants': all_tenants}
+    response = cloud.session.request(url=uri, method='GET',
+                                     headers=headers, params=params).json()
+    for share in response['shares']:
+        yield share
+
+
+def _get_share_types(all_tenants='true'):
+    ep = shared_file_system.get_endpoint()
+    uri = f"{ep}/types"
+    headers = {'X-Auth-Token': cloud.session.get_token(),
+               'Accept': 'application/json',
+               'X-Openstack-Manila-Api-Version': f'{manila_api_version}'}
+    params = {'all_tenants': all_tenants}
+    response = cloud.session.request(url=uri, method='GET',
+                                     headers=headers, params=params).json()
+    for share_type in response['volume_types']:
+        yield share_type
+
+
+def _get_share_networks(all_tenants='true'):
+    ep = shared_file_system.get_endpoint()
+    uri = f"{ep}/share-networks/detail"
+    headers = {'X-Auth-Token': cloud.session.get_token(),
+               'Accept': 'application/json',
+               'X-Openstack-Manila-Api-Version': f'{manila_api_version}'}
+    params = {'all_tenants': all_tenants}
+    response = cloud.session.request(url=uri, method='GET',
+                                     headers=headers, params=params).json()
+    for share_network in response['share_networks']:
+        yield share_network
+
+
 def _delete_volume_group(uuid, delete_volumes='false'):
     ep = volume.get_endpoint()
     uri = f"{ep}/groups/{uuid}/action"
@@ -105,6 +151,40 @@
         url=uri, method='POST', headers=headers, json=body)
 
 
+def _delete_share_type(uuid):
+    ep = shared_file_system.get_endpoint()
+    uri = f"{ep}/types/{uuid}"
+    headers = {'X-Auth-Token': cloud.session.get_token(),
+               'X-Openstack-Manila-Api-Version': f'{manila_api_version}',
+               'Content-Type': 'application/json',
+               'Accept': 'application/json'}
+    cloud.session.request(
+        url=uri, method='DELETE', headers=headers)
+
+
+def _delete_share(uuid, force_delete=True):
+    ep = shared_file_system.get_endpoint()
+    uri = f"{ep}/shares/{uuid}/action"
+    headers = {'X-Auth-Token': cloud.session.get_token(),
+               'X-Openstack-Manila-Api-Version': f'{manila_api_version}',
+               'Content-Type': 'application/json',
+               'Accept': 'application/json'}
+    body = {"force_delete": force_delete}
+    cloud.session.request(
+        url=uri, method='POST', headers=headers, json=body)
+
+
+def _delete_share_network(uuid):
+    ep = shared_file_system.get_endpoint()
+    uri = f"{ep}/share-networks/{uuid}"
+    headers = {'X-Auth-Token': cloud.session.get_token(),
+               'X-Openstack-Manila-Api-Version': f'{manila_api_version}',
+               'Content-Type': 'application/json',
+               'Accept': 'application/json'}
+    cloud.session.request(
+        url=uri, method='DELETE', headers=headers)
+
+
 def _reset_volume_status(uuid, status='available', attach_status='detached',
                          migration_status='None'):
     ep = volume.get_endpoint()
@@ -248,6 +328,45 @@
         compute.wait_for_delete(srv_obj)
 
 
+def cleanup_shares():
+    shares_in_response = _get_shares()
+    shares = items_to_object([g for g in shares_in_response])
+    shares_to_delete = _filter_test_resources(shares, 'name')
+    _log_resources_count(len(shares_to_delete), 'share(s)')
+    if args.dry_run:
+        return
+    for id_ in shares_to_delete:
+        _log_resource_delete(id_, shares_to_delete[id_], 'share')
+        # TODO: uncomment
+        _delete_share(id_)
+        shr_obj = shared_file_system.get_share(id_)
+        shared_file_system.wait_for_delete(shr_obj)
+
+
+def cleanup_share_types():
+    share_types_in_response = _get_share_types()
+    share_types = items_to_object([g for g in share_types_in_response])
+    share_types_to_delete = _filter_test_resources(share_types, 'name')
+    _log_resources_count(len(share_types_to_delete), 'share type(s)')
+    if args.dry_run:
+        return
+    for id_ in share_types_to_delete:
+        _log_resource_delete(id_, share_types_to_delete[id_], 'type')
+        _delete_share_type(id_)
+
+
+def cleanup_share_networks():
+    share_networks_in_response = _get_share_networks()
+    share_networks = items_to_object([g for g in share_networks_in_response])
+    share_networks_to_delete = _filter_test_resources(share_networks, 'name')
+    _log_resources_count(len(share_networks_to_delete), 'share network(s)')
+    if args.dry_run:
+        return
+    for id_ in share_networks_to_delete:
+        _log_resource_delete(id_, share_networks_to_delete[id_], 'type')
+        _delete_share_network(id_)
+
+
 def cleanup_snapshots():
     snapshots = volume.snapshots(all_projects=True)
     snapshots_to_delete = _filter_test_resources(snapshots, 'name')
@@ -457,6 +576,12 @@
 
     cleanup_stacks(stacks_alt=args.stacks_alt)
     cleanup_load_balancers()
+
+    if manila_present:
+        cleanup_shares()
+        cleanup_share_types()
+        cleanup_share_networks()
+
     cleanup_servers()
     cleanup_flavors()
     try:  # Skip if cinder-backup service is not enabled
diff --git a/k8s/yamls/heat_cinder_basic_template.yaml b/k8s/yamls/heat_cinder_basic_template.yaml
new file mode 100644
index 0000000..1f509bc
--- /dev/null
+++ b/k8s/yamls/heat_cinder_basic_template.yaml
@@ -0,0 +1,33 @@
+heat_template_version: 2013-05-23
+
+parameters:
+  volume_size:
+    type: number
+    default: 1
+
+resources:
+    volume:
+        type: OS::Cinder::Volume
+        properties:
+            size: { get_param: volume_size }
+            description: mirantis cvp test
+            name: cvp.e2e.heat.stack.test
+
+outputs:
+  status:
+    description: status
+    value: { get_attr: ['volume', 'status'] }
+
+  size:
+    description: size
+    value: { get_attr: ['volume', 'size'] }
+
+  display_description:
+    description: display_description
+    value: { get_attr: ['volume', 'display_description'] }
+
+  display_name:
+    value: { get_attr: ['volume', 'display_name'] }
+
+  volume_id:
+    value: { get_resource: volume }
\ No newline at end of file