Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added force deletion capability to log analytics module and updated requirements.txt #273

Merged
merged 6 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions plugins/modules/azure_rm_loganalyticsworkspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
- Disable one pack by setting it to C(false). For example "Backup:false".
- Other intelligence packs not list in this property will not be changed.
type: dict
force:
description:
- Deletes the workspace without the recovery option. A workspace that was deleted with this flag cannot be recovered.
default: false
type: bool
Fred-sun marked this conversation as resolved.
Show resolved Hide resolved
extends_documentation_fragment:
- azure.azcollection.azure
- azure.azcollection.azure_tags
Expand Down Expand Up @@ -173,7 +178,8 @@ def __init__(self):
location=dict(type='str'),
sku=dict(type='str', default='per_gb2018', choices=['free', 'standard', 'premium', 'unlimited', 'per_node', 'per_gb2018', 'standalone']),
retention_in_days=dict(type='int'),
intelligence_packs=dict(type='dict')
intelligence_packs=dict(type='dict'),
force=dict(type='bool', default=False)
)

self.results = dict(
Expand All @@ -188,6 +194,7 @@ def __init__(self):
self.sku = None
self.retention_in_days = None
self.intelligence_packs = None
self.force = None

super(AzureRMLogAnalyticsWorkspace, self).__init__(self.module_arg_spec, supports_check_mode=True)

Expand All @@ -210,7 +217,7 @@ def exec_module(self, **kwargs):
workspace = self.get_workspace()
if not workspace and self.state == 'present':
changed = True
workspace = self.log_analytics_models.Workspace(sku=self.log_analytics_models.Sku(name=self.sku),
workspace = self.log_analytics_models.Workspace(sku=self.log_analytics_models.WorkspaceSku(name=self.sku),
retention_in_days=self.retention_in_days,
location=self.location)
if not self.check_mode:
Expand Down Expand Up @@ -257,7 +264,7 @@ def get_workspace(self):

def delete_workspace(self):
try:
self.log_analytics_client.workspaces.delete(self.resource_group, self.name)
self.log_analytics_client.workspaces.delete(self.resource_group, self.name, force=self.force)
except CloudError as exc:
self.fail('Error when deleting workspace {0} - {1}'.format(self.name, exc.message or str(exc)))

Expand All @@ -268,24 +275,24 @@ def to_dict(self, workspace):

def list_intelligence_packs(self):
try:
response = self.log_analytics_client.workspaces.list_intelligence_packs(self.resource_group, self.name)
response = self.log_analytics_client.intelligence_packs.list(self.resource_group, self.name)
return [x.as_dict() for x in response]
except CloudError as exc:
self.fail('Error when listing intelligence packs {0}'.format(exc.message or str(exc)))

def change_intelligence(self, key, value):
try:
if value:
self.log_analytics_client.workspaces.enable_intelligence_pack(self.resource_group, self.name, key)
self.log_analytics_client.intelligence_packs.enable(self.resource_group, self.name, key)
else:
self.log_analytics_client.workspaces.disable_intelligence_pack(self.resource_group, self.name, key)
self.log_analytics_client.intelligence_packs.disable(self.resource_group, self.name, key)
except CloudError as exc:
self.fail('Error when changing intelligence pack {0} - {1}'.format(key, exc.message or str(exc)))

def list_management_groups(self):
result = []
try:
response = self.log_analytics_client.workspaces.list_management_groups(self.resource_group, self.name)
response = self.log_analytics_client.management_groups.list(self.resource_group, self.name)
while True:
result.append(response.next().as_dict())
except StopIteration:
Expand All @@ -297,7 +304,7 @@ def list_management_groups(self):
def list_usages(self):
result = []
try:
response = self.log_analytics_client.workspaces.list_usages(self.resource_group, self.name)
response = self.log_analytics_client.usages.list(self.resource_group, self.name)
while True:
result.append(response.next().as_dict())
except StopIteration:
Expand All @@ -308,7 +315,7 @@ def list_usages(self):

def get_shared_keys(self):
try:
return self.log_analytics_client.workspaces.get_shared_keys(self.resource_group, self.name).as_dict()
return self.log_analytics_client.shared_keys.get_shared_keys(self.resource_group, self.name).as_dict()
except CloudError as exc:
self.fail('Error when getting shared key {0}'.format(exc.message or str(exc)))

Expand Down
10 changes: 5 additions & 5 deletions plugins/modules/azure_rm_loganalyticsworkspace_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,22 +205,22 @@ def get_workspace(self):

def list_by_resource_group(self):
try:
return self.log_analytics_client.workspaces.list_by_resource_group(self.resource_group)
return self.log_analytics_client.resource_group.list(self.resource_group)
except CloudError:
pass
return []

def list_intelligence_packs(self):
try:
response = self.log_analytics_client.workspaces.list_intelligence_packs(self.resource_group, self.name)
response = self.log_analytics_client.intelligence_packs.list(self.resource_group, self.name)
return [x.as_dict() for x in response]
except CloudError as exc:
self.fail('Error when listing intelligence packs {0}'.format(exc.message or str(exc)))

def list_management_groups(self):
result = []
try:
response = self.log_analytics_client.workspaces.list_management_groups(self.resource_group, self.name)
response = self.log_analytics_client.management_groups.list(self.resource_group, self.name)
while True:
result.append(response.next().as_dict())
except StopIteration:
Expand All @@ -232,7 +232,7 @@ def list_management_groups(self):
def list_usages(self):
result = []
try:
response = self.log_analytics_client.workspaces.list_usages(self.resource_group, self.name)
response = self.log_analytics_client.usages.list(self.resource_group, self.name)
while True:
result.append(response.next().as_dict())
except StopIteration:
Expand All @@ -243,7 +243,7 @@ def list_usages(self):

def get_shared_keys(self):
try:
return self.log_analytics_client.workspaces.get_shared_keys(self.resource_group, self.name).as_dict()
return self.log_analytics_client.shared_keys.get_shared_keys(self.resource_group, self.name).as_dict()
except CloudError as exc:
self.fail('Error when getting shared key {0}'.format(exc.message or str(exc)))

Expand Down
1 change: 1 addition & 0 deletions pr-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ parameters:
- "azure_rm_keyvaultkey"
- "azure_rm_keyvaultsecret"
- "azure_rm_loadbalancer"
- "azure_rm_loganalyticsworkspace"
- "azure_rm_manageddisk"
- "azure_rm_mariadbserver"
- "azure_rm_monitorlogprofile"
Expand Down
2 changes: 1 addition & 1 deletion requirements-azure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ azure-graphrbac==0.61.1
azure-mgmt-cosmosdb==0.5.2
azure-mgmt-hdinsight==0.1.0
azure-mgmt-devtestlabs==3.0.0
azure-mgmt-loganalytics==0.2.0
azure-mgmt-loganalytics==1.0.0
azure-mgmt-automation==0.1.1
azure-mgmt-iothub==0.7.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cloud/azure
shippable/azure/group4
destructive
azure_rm_loganalyticsworkspace
azure_rm_loganalyticsworkspace_info
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dependencies:
- setup_azure
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
- name: Prepare random number
set_fact:
name: "loganalytics{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
retention_days: 30

- name: Create Log Analytics Workspace (Check Mode On)
azure_rm_loganalyticsworkspace:
name: "{{ name }}"
intelligence_packs:
Backup: true
Containers: true
retention_in_days: "{{ retention_days }}"
resource_group: "{{ resource_group }}"
check_mode: yes
register: output

- assert:
that:
- output.changed

- name: Get Log Analytics workspace information
azure_rm_loganalyticsworkspace_info:
name: "{{ name }}"
resource_group: "{{ resource_group }}"
show_intelligence_packs: true
show_management_groups: true
show_shared_keys: true
show_usages: true
register: facts

- assert:
that:
- facts.workspaces | length == 0

- name: Create Log Analytics Workspace
azure_rm_loganalyticsworkspace:
name: "{{ name }}"
intelligence_packs:
Backup: true
Containers: true
retention_in_days: "{{ retention_days }}"
resource_group: "{{ resource_group }}"
register: output

- assert:
that:
- output.changed
- output.retention_in_days == retention_days
# - output.intelligence_packs | json_query('[?name == `Backup`].enabled') | first == true
# - output.intelligence_packs | json_query('[?name == `Containers`].enabled') | first == true
- output.sku == 'pergb2018'

- name: Get Log Analytics workspace information (Show full information)
azure_rm_loganalyticsworkspace_info:
name: "{{ name }}"
resource_group: "{{ resource_group }}"
show_intelligence_packs: true
show_management_groups: true
show_shared_keys: true
show_usages: true
register: facts

- assert:
that:
- not facts.changed
- facts.workspaces | length > 0
- facts.workspaces[0].retention_in_days == retention_days
- facts.workspaces[0].intelligence_packs | length > 0
# - facts.workspaces[0].intelligence_packs | json_query('[?name == `Backup`].enabled') | first == true
# - facts.workspaces[0].intelligence_packs | json_query('[?name == `Containers`].enabled') | first == true
- facts.workspaces[0].shared_keys is defined
- facts.workspaces[0].shared_keys.primary_shared_key is defined
- facts.workspaces[0].shared_keys.secondary_shared_key is defined
- facts.workspaces[0].usages is defined
- facts.workspaces[0].usages | length > 0
- facts.workspaces[0].management_groups is defined
- facts.workspaces[0].sku == 'pergb2018'

- name: Get Log Analytics workspace information (Show default information)
azure_rm_loganalyticsworkspace_info:
name: "{{ name }}"
resource_group: "{{ resource_group }}"
register: facts

- assert:
that:
- not facts.changed
- facts.workspaces | length > 0
- facts.workspaces[0].retention_in_days == retention_days
- facts.workspaces[0].intelligence_packs is not defined
- facts.workspaces[0].shared_keys is not defined
- facts.workspaces[0].usages is not defined
- facts.workspaces[0].management_groups is not defined
- facts.workspaces[0].sku == 'pergb2018'

- name: Create Log Analytics workspace (Test Idempotence)
azure_rm_loganalyticsworkspace:
name: "{{ name }}"
resource_group: "{{ resource_group }}"
retention_in_days: "{{ retention_days }}"
register: output

- assert:
that:
- not output.changed

- name: Remove Log Analytics workspace (Check Mode On)
azure_rm_loganalyticsworkspace:
name: "{{ name }}"
resource_group: "{{ resource_group }}"
state: absent
check_mode: yes
register: output

- assert:
that:
- output.changed

- name: Get Log Analytics workspace information(Check still exists after remove Check Mode On)
azure_rm_loganalyticsworkspace_info:
name: "{{ name }}"
resource_group: "{{ resource_group }}"
register: facts

- assert:
that:
- facts.workspaces | length == 1

- name: Remove Log Analytics workspace
azure_rm_loganalyticsworkspace:
name: "{{ name }}"
resource_group: "{{ resource_group }}"
state: absent
force: true
register: output

- assert:
that:
- output.changed

- name: Get Log Analytics workspace information
azure_rm_loganalyticsworkspace_info:
name: "{{ name }}"
resource_group: "{{ resource_group }}"
register: facts

- assert:
that:
- facts.workspaces | length == 0

- name: Remove Log Analytics workspace (Test Idempotence)
azure_rm_loganalyticsworkspace:
name: "{{ name }}"
resource_group: "{{ resource_group }}"
state: absent
force: true
register: output

- assert:
that:
- not output.changed