From 4931fb2681717ada3de78fc8209c465fabc819f0 Mon Sep 17 00:00:00 2001
From: Robert Osowiecki <robert.osowiecki@gmail.com>
Date: Mon, 28 Sep 2020 22:05:15 +0200
Subject: [PATCH] parted: proper fix for change of partition label case (#594)

* parted: proper fix for change of partition label case
calling mkpart even when partition existed before mklabel call, fixes #522

* changelog fragment for parted fix #522

* Update changelogs/fragments/522-parted_change_label.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* typo in comment

Co-authored-by: Felix Fontein <felix@fontein.de>
---
 changelogs/fragments/522-parted_change_label.yml |  2 ++
 plugins/modules/system/parted.py                 |  5 +++--
 tests/unit/plugins/modules/system/test_parted.py | 13 +++++++++++++
 3 files changed, 18 insertions(+), 2 deletions(-)
 create mode 100644 changelogs/fragments/522-parted_change_label.yml

diff --git a/changelogs/fragments/522-parted_change_label.yml b/changelogs/fragments/522-parted_change_label.yml
new file mode 100644
index 0000000000..4d30dec1d8
--- /dev/null
+++ b/changelogs/fragments/522-parted_change_label.yml
@@ -0,0 +1,2 @@
+bugfixes:
+ - "parted - fix creating partition when label is changed (https://github.com/ansible-collections/community.general/issues/522)."
diff --git a/plugins/modules/system/parted.py b/plugins/modules/system/parted.py
index 6e35e5880e..d687ec895b 100644
--- a/plugins/modules/system/parted.py
+++ b/plugins/modules/system/parted.py
@@ -634,11 +634,12 @@ def main():
     if state == 'present':
 
         # Assign label if required
-        if current_device['generic'].get('table', None) != label:
+        mklabel_needed = current_device['generic'].get('table', None) != label
+        if mklabel_needed:
             script += "mklabel %s " % label
 
         # Create partition if required
-        if part_type and not part_exists(current_parts, 'num', number):
+        if part_type and (mklabel_needed or not part_exists(current_parts, 'num', number)):
             script += "mkpart %s %s%s %s " % (
                 part_type,
                 '%s ' % fs_type if fs_type is not None else '',
diff --git a/tests/unit/plugins/modules/system/test_parted.py b/tests/unit/plugins/modules/system/test_parted.py
index ea003d4328..3db8fae231 100644
--- a/tests/unit/plugins/modules/system/test_parted.py
+++ b/tests/unit/plugins/modules/system/test_parted.py
@@ -262,6 +262,19 @@ class TestParted(ModuleTestCase):
         with patch('ansible_collections.community.general.plugins.modules.system.parted.get_device_info', return_value=parted_dict2):
             self.execute_module(changed=True, script='unit KiB mklabel gpt mkpart primary 0% 100% unit KiB name 1 \'"lvmpartition"\' set 1 lvm on')
 
+    def test_change_label_gpt(self):
+        # When partitions already exists and label is changed, mkpart should be called even when partition already exists,
+        # because new empty label will be created anyway
+        set_module_args({
+            'device': '/dev/sdb',
+            'number': 1,
+            'state': 'present',
+            'label': 'gpt',
+            '_ansible_check_mode': True,
+        })
+        with patch('ansible_collections.community.general.plugins.modules.system.parted.get_device_info', return_value=parted_dict1):
+            self.execute_module(changed=True, script='unit KiB mklabel gpt mkpart primary 0% 100%')
+
     def test_check_mode_unchanged(self):
         # Test that get_device_info result is checked in check mode too
         # No change on partition 1