Support terminate for Azure Log Analytics artifacts (#336)

* Delete Azure Log Analytics resources in Muchos terminate
* Delete workbook & dashboard
* Resolve Ansible deprecation warning for azure_rm_virtualnetwork_facts

Co-authored-by: Shan Nawaz <shnawa@microsoft.com>
diff --git a/ansible/roles/azure/tasks/create_log_analytics_ws.yml b/ansible/roles/azure/tasks/create_log_analytics_ws.yml
index 9db83c6..126da4d 100644
--- a/ansible/roles/azure/tasks/create_log_analytics_ws.yml
+++ b/ansible/roles/azure/tasks/create_log_analytics_ws.yml
@@ -24,32 +24,7 @@
 # 
 #
 
-- name: Generate name for Workspace, Dashboard and Workbook
-  shell:  echo -n {{resource_group + vmss_name + location }}|md5sum|tr -cd "[:alnum:]"|cut -c 1-48
-  register: monitor_name
-
-- name: Set name for log analytics workspace
-  set_fact:
-     log_workspace_name: "{{ monitor_name.stdout + '-la' }}"
-
-- name: Set name for dashboard
-  set_fact:
-     dashboard_name: "{{ monitor_name.stdout + '-db' }}"
-
-- name: Set name for workbook
-  set_fact:
-    workbook_name: "{{ monitor_name.stdout + '-wb' }}"
-
-- name: Query all the resources in the resource group
-  azure_rm_resource_info:
-    resource_group: "{{ resource_group }}"
-    resource_type: resources
-  register: rgfacts
-
-- name: Retrieve workbook name
-  set_fact: 
-     workbook_exists: "{{ rgfacts.response|selectattr('type', 'equalto', 'microsoft.insights/workbooks')|map(attribute='name')|list }}"
-  when: workbook_name in rgfacts.response|map(attribute='tags')|selectattr('hidden-title','defined')|map(attribute='hidden-title')|list 
+- import_tasks: log_analytics_ws_common.yml
      
 - name: Deploy log analytics workspace and performance counters
   azure_rm_deployment:
diff --git a/ansible/roles/azure/tasks/log_analytics_ws_common.yml b/ansible/roles/azure/tasks/log_analytics_ws_common.yml
new file mode 100644
index 0000000..4d9e8fd
--- /dev/null
+++ b/ansible/roles/azure/tasks/log_analytics_ws_common.yml
@@ -0,0 +1,47 @@
+---
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+# This file is imported in create_log_analytics_ws.yml and terminate_cluster.yml
+#
+
+- name: Generate name for Workspace, Dashboard and Workbook
+  shell:  echo -n {{resource_group + vmss_name + location }}|md5sum|tr -cd "[:alnum:]"|cut -c 1-48
+  register: monitor_name
+
+- name: Set name for log analytics workspace
+  set_fact:
+     log_workspace_name: "{{ monitor_name.stdout + '-la' }}"
+
+- name: Set name for dashboard
+  set_fact:
+     dashboard_name: "{{ monitor_name.stdout + '-db' }}"
+
+- name: Set name for workbook
+  set_fact:
+    workbook_name: "{{ monitor_name.stdout + '-wb' }}"
+
+- name: Query all the resources in the resource group
+  azure_rm_resource_info:
+    resource_group:  "{{ resource_group }}"
+    resource_type: resources
+  register: rgfacts
+
+- name: Retrieve workbook name
+  set_fact:
+     workbook_exists: "{{ rgfacts.response|selectattr('type', 'equalto', 'microsoft.insights/workbooks')|map(attribute='name')|list }}"
+  when: workbook_name in rgfacts.response|map(attribute='tags')|selectattr('hidden-title','defined')|map(attribute='hidden-title')|list
diff --git a/ansible/roles/azure/tasks/terminate_cluster.yml b/ansible/roles/azure/tasks/terminate_cluster.yml
index e1e1e9e..3676cae 100644
--- a/ansible/roles/azure/tasks/terminate_cluster.yml
+++ b/ansible/roles/azure/tasks/terminate_cluster.yml
@@ -19,6 +19,38 @@
 
 - hosts: localhost
   tasks:
+    - block:
+      - import_tasks: log_analytics_ws_common.yml
+
+      - name: Delete workbook
+        azure_rm_resource:
+           resource_group: "{{ resource_group }}"
+           provider: insights
+           resource_type: workbooks
+           resource_name: "{{ workbook_exists|join('') }}"
+           api_version: '2020-02-12'
+           state: absent
+        when: workbook_exists is defined
+
+      - name: Delete dashboard
+        azure_rm_resource:
+           resource_group: "{{ resource_group }}"
+           provider: Portal
+           resource_type: dashboards
+           resource_name:  "{{ dashboard_name }}"
+           api_version: '2019-01-01-preview'
+           state: absent
+
+      - name: Delete log analytics workspace
+        azure_rm_resource:
+           resource_group: "{{ resource_group }}"
+           provider: OperationalInsights
+           resource_type: workspaces
+           resource_name: "{{ log_workspace_name }}"
+           api_version: '2015-03-20'
+           state: absent
+      when: az_oms_integration_needed
+
     - name: Delete User Assigned Identity
       azure_rm_resource:
          resource_group: "{{ resource_group }}"
@@ -62,7 +94,7 @@
       when: azure_proxy_host is defined and azure_proxy_host|length
 
     - name: Get facts for virtual network
-      azure_rm_virtualnetwork_facts:
+      azure_rm_virtualnetwork_info:
         resource_group: "{{ resource_group }}"
         name: "{{ vnet }}"
       register: vnet_facts
@@ -73,9 +105,8 @@
         name: "{{ vnet }}"
         state: absent
       when: >
-        vnet_facts['ansible_facts']['azure_virtualnetworks']|length and
-        'deployment_type' in vnet_facts['ansible_facts']['azure_virtualnetworks'][0]['tags'] and
-        vnet_facts['ansible_facts']['azure_virtualnetworks'][0]['tags']['deployment_type'] == 'muchos'
+        vnet_facts.virtualnetworks|length and
+        vnet_facts.virtualnetworks|map(attribute='tags')|map(attribute='deployment_type')|join('') == 'muchos'
 
     - name: Delete the resource group if it is empty
       azure_rm_resourcegroup: