From 6d1a386e401095c169b069ac7d30930038159de3 Mon Sep 17 00:00:00 2001
From: Theodoros Tsioutsias <theodoros.tsioutsias@cern.ch>
Date: Wed, 15 Apr 2020 14:59:07 +0000
Subject: [PATCH] Labels override

This adds a boolean flag called --merge-labels at the create CLIs
for clusters and nodegroups. The show output for clusters and
nodegroups is modified to contain also new fields called
"labels_overridden", "labels_added", "labels_skipped". The new labels
contain the differences of the object's labels with its parent's.

story: 2007515
task: 39692
Depends-On: I1054c54da96005a49e874de6f4cf60b5db57fc02
Change-Id: Ie68bb85f4b8bbacc5b71b523d8639cf30e6e1992
(cherry picked from commit 0febfc728f61b20c98b9c3c527aafe3f1199e25f)
---
 magnumclient/osc/v1/clusters.py         | 17 ++++++++++++++++-
 magnumclient/osc/v1/nodegroups.py       | 17 ++++++++++++++++-
 magnumclient/tests/osc/unit/v1/fakes.py |  6 ++++++
 magnumclient/v1/clusters.py             |  1 +
 magnumclient/v1/nodegroups.py           |  2 +-
 5 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/magnumclient/osc/v1/clusters.py b/magnumclient/osc/v1/clusters.py
index 3cf77d4..3ab06e9 100644
--- a/magnumclient/osc/v1/clusters.py
+++ b/magnumclient/osc/v1/clusters.py
@@ -34,6 +34,9 @@ CLUSTER_ATTRIBUTES = [
     'updated_at',
     'coe_version',
     'labels',
+    'labels_overridden',
+    'labels_skipped',
+    'labels_added',
     'faults',
     'keypair',
     'api_address',
@@ -47,7 +50,7 @@ CLUSTER_ATTRIBUTES = [
     'master_flavor_id',
     'flavor_id',
     'health_status_reason',
-    'project_id'
+    'project_id',
 ]
 
 
@@ -141,6 +144,13 @@ class CreateCluster(command.Command):
             action='append_const',
             const=False,
             help=_('Disables floating ip creation on the new Cluster'))
+        parser.add_argument(
+            '--merge-labels',
+            dest='merge_labels',
+            action='store_true',
+            default=False,
+            help=_('The labels provided will be merged with the labels '
+                   'configured in the specified cluster template.'))
 
         return parser
 
@@ -185,6 +195,11 @@ class CreateCluster(command.Command):
         if parsed_args.fixed_subnet is not None:
             args["fixed_subnet"] = parsed_args.fixed_subnet
 
+        if parsed_args.merge_labels:
+            # We are only sending this if it's True. This
+            # way we avoid breaking older APIs.
+            args["merge_labels"] = parsed_args.merge_labels
+
         cluster = mag_client.clusters.create(**args)
         print("Request to create cluster %s accepted"
               % cluster.uuid)
diff --git a/magnumclient/osc/v1/nodegroups.py b/magnumclient/osc/v1/nodegroups.py
index 04967bf..afb5870 100644
--- a/magnumclient/osc/v1/nodegroups.py
+++ b/magnumclient/osc/v1/nodegroups.py
@@ -27,6 +27,9 @@ NODEGROUP_ATTRIBUTES = [
     'project_id',
     'docker_volume_size',
     'labels',
+    'labels_overridden',
+    'labels_skipped',
+    'labels_added',
     'flavor_id',
     'image_id',
     'node_addresses',
@@ -37,7 +40,7 @@ NODEGROUP_ATTRIBUTES = [
     'is_default',
     'stack_id',
     'status',
-    'status_reason'
+    'status_reason',
 ]
 
 
@@ -100,6 +103,13 @@ class CreateNodeGroup(command.Command):
             metavar='<flavor>',
             help=_('The nova flavor name or UUID to use when launching the '
                    'nodes in this NodeGroup.'))
+        parser.add_argument(
+            '--merge-labels',
+            dest='merge_labels',
+            action='store_true',
+            default=False,
+            help=_('The labels provided will be merged with the labels '
+                   'configured in the specified cluster.'))
 
         return parser
 
@@ -127,6 +137,11 @@ class CreateNodeGroup(command.Command):
         if parsed_args.image is not None:
             args['image_id'] = parsed_args.image
 
+        if parsed_args.merge_labels:
+            # We are only sending this if it's True. This
+            # way we avoid breaking older APIs.
+            args["merge_labels"] = parsed_args.merge_labels
+
         cluster_id = parsed_args.cluster
         nodegroup = mag_client.nodegroups.create(cluster_id, **args)
         print("Request to create nodegroup %s accepted"
diff --git a/magnumclient/tests/osc/unit/v1/fakes.py b/magnumclient/tests/osc/unit/v1/fakes.py
index 2a25df5..4181010 100644
--- a/magnumclient/tests/osc/unit/v1/fakes.py
+++ b/magnumclient/tests/osc/unit/v1/fakes.py
@@ -237,6 +237,9 @@ class FakeCluster(object):
             'stack_id': 'c4554582-77bd-4734-8f1a-72c3c40e5fb4',
             'status_reason': None,
             'labels': {},
+            'labels_overridden': {},
+            'labels_added': {},
+            'labels_skipped': {},
             'created_at': '2017-03-16T18:40:39+00:00',
             'updated_at': '2017-03-16T18:40:45+00:00',
             'coe_version': None,
@@ -324,6 +327,9 @@ class FakeNodeGroup(object):
             'docker_volume_size': None,
             'node_addresses': [],
             'labels': {},
+            'labels_overridden': {},
+            'labels_added': {},
+            'labels_skipped': {},
             'node_count': 1,
             'name': 'fake-nodegroup',
             'flavor_id': 'm1.medium',
diff --git a/magnumclient/v1/clusters.py b/magnumclient/v1/clusters.py
index 989de85..6aabc4b 100644
--- a/magnumclient/v1/clusters.py
+++ b/magnumclient/v1/clusters.py
@@ -26,6 +26,7 @@ CREATION_ATTRIBUTES.append('flavor_id')
 CREATION_ATTRIBUTES.append('fixed_network')
 CREATION_ATTRIBUTES.append('fixed_subnet')
 CREATION_ATTRIBUTES.append('floating_ip_enabled')
+CREATION_ATTRIBUTES.append('merge_labels')
 
 
 class Cluster(baseunit.BaseTemplate):
diff --git a/magnumclient/v1/nodegroups.py b/magnumclient/v1/nodegroups.py
index cd79934..111d8fb 100644
--- a/magnumclient/v1/nodegroups.py
+++ b/magnumclient/v1/nodegroups.py
@@ -20,7 +20,7 @@ from magnumclient.v1 import baseunit
 
 CREATION_ATTRIBUTES = ['docker_volume_size', 'labels', 'flavor_id', 'image_id',
                        'project_id', 'node_count', 'name', 'role',
-                       'min_node_count', 'max_node_count']
+                       'min_node_count', 'max_node_count', 'merge_labels']
 
 
 class NodeGroup(baseunit.BaseTemplate):
-- 
GitLab