From 13d0b5301ddc27bf1a7994a86e1553ff12467b76 Mon Sep 17 00:00:00 2001 From: vicky liu Date: Thu, 28 Mar 2019 17:34:41 +0800 Subject: [PATCH] Add create API to inventory client Also fix query string from "cluster-id" to "cluster_id" Change-Id: I2e894cd30d27ff332aa729b04c3706450ebd61ed --- vmware_nsxlib/tests/unit/v3/test_resources.py | 17 ++++++++++++++++- vmware_nsxlib/v3/resources.py | 10 +++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/vmware_nsxlib/tests/unit/v3/test_resources.py b/vmware_nsxlib/tests/unit/v3/test_resources.py index 76c6625f..92e671d8 100644 --- a/vmware_nsxlib/tests/unit/v3/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/test_resources.py @@ -2166,7 +2166,7 @@ class InventoryTestCase(BaseTestResource): self.CONTAINER_CLUSTER, 'ContainerApplication') base_url = 'https://1.2.3.4/api/v1/fabric/container-applications' - surfix = '?cluster-id=%s' % self.CONTAINER_CLUSTER + surfix = '?cluster_id=%s' % self.CONTAINER_CLUSTER test_client.assert_json_call( 'get', mocked_resource, base_url + surfix, @@ -2182,6 +2182,21 @@ class InventoryTestCase(BaseTestResource): base_url + surfix, headers=self.default_headers()) + def test_create_resource(self): + mocked_resource = self.get_mocked_resource() + body = {'resource_type': 'ContainerCluster', + 'name': 'k8s-1', + 'external_id': 'id-1', + 'cluster_type': 'Kubernetes', + 'infrastructure': {'infra_type': 'vSphere'}} + mocked_resource.create('ContainerCluster', body) + base_url = 'https://1.2.3.4/api/v1/fabric/container-clusters' + test_client.assert_json_call( + 'post', mocked_resource, + base_url, + data=jsonutils.dumps(body, sort_keys=True), + headers=self.default_headers()) + def test_update(self): mocked_resource = self.get_mocked_resource() body = {} diff --git a/vmware_nsxlib/v3/resources.py b/vmware_nsxlib/v3/resources.py index cd5a62bb..c161a925 100644 --- a/vmware_nsxlib/v3/resources.py +++ b/vmware_nsxlib/v3/resources.py @@ -757,7 +757,7 @@ class Inventory(utils.NsxLibApiBase): if not resource_type: msg = "null resource type is not supported" raise exceptions.ResourceNotFound(details=msg) - request_url = "%s?cluster-id=%s" % ( + request_url = "%s?cluster_id=%s" % ( self.get_path(self._get_path_for_resource(resource_type)), cluster_id) return self.client.url_get(request_url) @@ -771,6 +771,14 @@ class Inventory(utils.NsxLibApiBase): resource_id) return self.client.url_delete(request_url) + def create(self, resource_type, resource): + if not resource_type: + msg = "null resource type is not supported" + raise exceptions.ResourceNotFound(details=msg) + request_url = self.get_path( + self._get_path_for_resource(resource_type)) + return self.client.url_post(request_url, resource) + def _get_path_for_resource(self, resource_type): path = self.RESOURCES_PATH.get(resource_type) if not path: