Browse Source

Add --labels for cluster-create

Add a labels override parameter on cluster-create to override
the value present on the cluster template.

Partial-Bug: #1697651
Depends-On: I8990c78433dcbbca5bc4aa121678b02636346802

Change-Id: I39f3423f6889ce93021248223600e342f4217cec
tags/2.8.0
Ricardo Rocha 1 year ago
parent
commit
57dc025007

+ 11
- 0
magnumclient/tests/v1/test_clusters.py View File

@@ -274,6 +274,17 @@ class ClusterManagerTest(testtools.TestCase):
274 274
         self.assertEqual(expect, self.api.calls)
275 275
         self.assertTrue(cluster)
276 276
 
277
+    def test_cluster_create_with_labels(self):
278
+        cluster_with_labels = dict()
279
+        cluster_with_labels.update(CREATE_CLUSTER)
280
+        cluster_with_labels['labels'] = "key=val"
281
+        cluster = self.mgr.create(**cluster_with_labels)
282
+        expect = [
283
+            ('POST', '/v1/clusters', {}, cluster_with_labels),
284
+        ]
285
+        self.assertEqual(expect, self.api.calls)
286
+        self.assertTrue(cluster)
287
+
277 288
     def test_cluster_create_with_discovery_url(self):
278 289
         cluster_with_discovery = dict()
279 290
         cluster_with_discovery.update(CREATE_CLUSTER)

+ 14
- 0
magnumclient/tests/v1/test_clusters_shell.py View File

@@ -27,6 +27,7 @@ class FakeCluster(Cluster):
27 27
         self.uuid = kwargs.get('uuid', 'x')
28 28
         self.keypair = kwargs.get('keypair', 'x')
29 29
         self.docker_volume_size = kwargs.get('docker_volume_size', 3)
30
+        self.labels = kwargs.get('labels', 'key=val')
30 31
         self.name = kwargs.get('name', 'x')
31 32
         self.cluster_template_id = kwargs.get('cluster_template_id', 'x')
32 33
         self.stack_id = kwargs.get('stack_id', 'x')
@@ -58,6 +59,7 @@ class ShellTest(shell_test_base.TestCommandLineArgument):
58 59
                                   master_count=1, node_count=1,
59 60
                                   create_timeout=60, keypair=None,
60 61
                                   docker_volume_size=None,
62
+                                  labels={},
61 63
                                   discovery_url=None):
62 64
         expected_args = {}
63 65
         expected_args['name'] = name
@@ -69,6 +71,8 @@ class ShellTest(shell_test_base.TestCommandLineArgument):
69 71
         expected_args['keypair'] = keypair
70 72
         if docker_volume_size is not None:
71 73
             expected_args['docker_volume_size'] = docker_volume_size
74
+        if labels is not None:
75
+            expected_args['labels'] = labels
72 76
 
73 77
         return expected_args
74 78
 
@@ -154,6 +158,11 @@ class ShellTest(shell_test_base.TestCommandLineArgument):
154 158
                                '--docker-volume-size 20')
155 159
         expected_args = self._get_expected_args_create('xxx',
156 160
                                                        docker_volume_size=20)
161
+
162
+        self._test_arg_success('cluster-create --cluster-template xxx '
163
+                               '--labels key=val')
164
+        expected_args = self._get_expected_args_create('xxx',
165
+                                                       labels={'key': 'val'})
157 166
         mock_create.assert_called_with(**expected_args)
158 167
 
159 168
         self._test_arg_success('cluster-create test '
@@ -281,6 +290,11 @@ class ShellTest(shell_test_base.TestCommandLineArgument):
281 290
                                self._mandatory_arg_error)
282 291
         mock_create.assert_not_called()
283 292
 
293
+    @mock.patch('magnumclient.v1.clusters.ClusterManager.create')
294
+    def test_cluster_create_failure_only_labels(self, mock_create):
295
+        self._test_arg_failure('cluster-create --labels key=val',
296
+                               self._mandatory_arg_error)
297
+
284 298
     @mock.patch('magnumclient.v1.clusters.ClusterManager.create')
285 299
     def test_cluster_create_failure_only_node_count(self, mock_create):
286 300
         self._test_arg_failure('cluster-create --node-count 1',

+ 1
- 0
magnumclient/v1/clusters.py View File

@@ -20,6 +20,7 @@ CREATION_ATTRIBUTES.append('cluster_template_id')
20 20
 CREATION_ATTRIBUTES.append('create_timeout')
21 21
 CREATION_ATTRIBUTES.append('keypair')
22 22
 CREATION_ATTRIBUTES.append('docker_volume_size')
23
+CREATION_ATTRIBUTES.append('labels')
23 24
 
24 25
 
25 26
 class Cluster(baseunit.BaseTemplate):

+ 7
- 0
magnumclient/v1/clusters_shell.py View File

@@ -105,6 +105,11 @@ def do_cluster_list(cs, args):
105 105
            metavar='<docker-volume-size>',
106 106
            type=int,
107 107
            help=_('The size in GB for the docker volume to use'))
108
+@utils.arg('--labels', metavar='<KEY1=VALUE1,KEY2=VALUE2;KEY3=VALUE3...>',
109
+           action='append', default=[],
110
+           help=_('Arbitrary labels in the form of key=value pairs '
111
+                  'to associate with a cluster. '
112
+                  'May be used multiple times.'))
108 113
 @utils.arg('--node-count',
109 114
            metavar='<node-count>',
110 115
            type=int,
@@ -137,6 +142,8 @@ def do_cluster_create(cs, args):
137 142
     opts['keypair'] = args.keypair
138 143
     if args.docker_volume_size is not None:
139 144
         opts['docker_volume_size'] = args.docker_volume_size
145
+    if args.labels is not None:
146
+        opts['labels'] = magnum_utils.handle_labels(args.labels)
140 147
     opts['node_count'] = args.node_count
141 148
     opts['master_count'] = args.master_count
142 149
     opts['discovery_url'] = args.discovery_url

Loading…
Cancel
Save