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

fix(#68): add all qualities and custom formats to profiles #74

Merged
merged 1 commit into from
Oct 11, 2023
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
46 changes: 44 additions & 2 deletions plugins/modules/sonarr_quality_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
type: int
default: 0
quality_groups:
description: Quality groups
description: Quality groups ordered list. Define only the allowed groups.
type: list
elements: dict
default: []
Expand Down Expand Up @@ -66,7 +66,7 @@
description: Quality ID.
type: int
formats:
description: Format items list.
description: Format items list. Define only the used custom formats.
type: list
elements: dict
default: []
Expand Down Expand Up @@ -232,6 +232,7 @@ def run_module():

# Populate quality groups.
quality_groups = []
allowed_qualities = []
for item in module.params['quality_groups']:
if len(item['qualities']) == 1:
quality_groups.append(sonarr.QualityProfileQualityItemResource(**{
Expand All @@ -244,6 +245,7 @@ def run_module():
'items': [],
'allowed': True,
}))
allowed_qualities.append(item['qualities'][0]['id'])
else:
qualities = []
for quality in item['qualities']:
Expand All @@ -257,6 +259,7 @@ def run_module():
'allowed': True,
'items': []
}))
allowed_qualities.append(quality['id'])

quality_groups.append(sonarr.QualityProfileQualityItemResource(**{
'allowed': True,
Expand All @@ -265,14 +268,53 @@ def run_module():
'items': qualities,
}))

# Add disallowed qualities
temp_client = sonarr.QualityDefinitionApi(module.api)
# GET resources.
try:
all_qualities = temp_client.list_quality_definition()
except Exception as e:
module.fail_json('Error listing qualities: %s' % to_native(e.reason), **result)

for q in all_qualities[::-1]:
if q['quality']['id'] not in allowed_qualities:
quality_groups.insert(0, sonarr.QualityProfileQualityItemResource(**{
'quality': sonarr.Quality(**{
'id': q['quality']['id'],
'name': q['quality']['name'],
'source': q['quality']['source'],
'resolution': q['quality']['resolution'],
}),
'items': [],
'allowed': False,
}))

# Populate formats.
formats = []
used_formats = []
for item in module.params['formats']:
formats.append(sonarr.ProfileFormatItemResource(**{
'name': item['name'],
'format': item['id'],
'score': item['score'],
}))
used_formats.append(item['id'])

# Add unused formats
temp_client = sonarr.CustomFormatApi(module.api)
# GET resources.
try:
all_formats = temp_client.list_custom_format()
except Exception as e:
module.fail_json('Error listing formats: %s' % to_native(e.reason), **result)

for f in all_formats:
if f['id'] not in used_formats:
formats.append(sonarr.ProfileFormatItemResource(**{
'name': f['name'],
'format': f['id'],
'score': 0,
}))

want = sonarr.QualityProfileResource(**{
'name': module.params['name'],
Expand Down
33 changes: 30 additions & 3 deletions tests/integration/targets/sonarr_quality_profile/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
---
- name: create example custom format
devopsarr.sonarr.sonarr_custom_format:
include_custom_format_when_renaming: false
name: "Arab"
specifications:
- name: "arabic"
implementation: "LanguageSpecification"
negate: false
required: true
fields:
- name: "value"
value: 26
sonarr_api_key: "{{ sonarr_api_key }}"
sonarr_url: "{{ sonarr_url }}"
register: format
- name: create quality profile
devopsarr.sonarr.sonarr_quality_profile:
name: "Example"
Expand Down Expand Up @@ -56,7 +71,10 @@
name: "WEBDL-720p"
source: "web"
resolution: 720
formats: []
formats:
- name: "{{ format['name'] }}"
id: "{{ format['id'] }}"
score: 100
sonarr_api_key: "{{ sonarr_api_key }}"
sonarr_url: "{{ sonarr_url }}"
register: result
Expand Down Expand Up @@ -90,7 +108,10 @@
name: "WEBDL-720p"
source: "web"
resolution: 720
formats: []
formats:
- name: "{{ format['name'] }}"
id: "{{ format['id'] }}"
score: 100
sonarr_api_key: "{{ sonarr_api_key }}"
sonarr_url: "{{ sonarr_url }}"
register: result
Expand All @@ -110,4 +131,10 @@
- name: assert quality profile deletion
assert:
that:
- result['id'] == 0
- result['id'] == 0
- name: clean up custom format
devopsarr.sonarr.sonarr_custom_format:
name: "Arab"
sonarr_api_key: "{{ sonarr_api_key }}"
sonarr_url: "{{ sonarr_url }}"
state: 'absent'