From b88a4c9792c2d91a3bdbe611faf2c360e2f9552f Mon Sep 17 00:00:00 2001 From: Hirotaka Wakabayashi Date: Thu, 3 Oct 2024 17:48:56 +0900 Subject: [PATCH] Deprecates create_conf_resource API --- src/k2hr3client/examples/create_role.py | 51 ++++++++++++++++++------- src/k2hr3client/resource.py | 36 ++++++++++++++--- src/tests/test_resource.py | 18 ++++----- 3 files changed, 78 insertions(+), 27 deletions(-) diff --git a/src/k2hr3client/examples/create_role.py b/src/k2hr3client/examples/create_role.py index 5a0a61e..f20c8d6 100644 --- a/src/k2hr3client/examples/create_role.py +++ b/src/k2hr3client/examples/create_role.py @@ -26,8 +26,9 @@ import argparse import json import os -import sys from pathlib import Path +import re +import sys import urllib.parse import urllib.request @@ -37,12 +38,15 @@ sys.path.append(src_dir) import k2hr3client # type: ignore # pylint: disable=import-error, wrong-import-position # noqa +from k2hr3client.exception import K2hr3Exception # type: ignore # pylint: disable=import-error, wrong-import-position # noqa from k2hr3client.http import K2hr3Http # type: ignore # pylint: disable=import-error, wrong-import-position # noqa from k2hr3client.token import K2hr3Token # type: ignore # pylint: disable=import-error, wrong-import-position # noqa from k2hr3client.resource import K2hr3Resource # type: ignore # pylint: disable=import-error, wrong-import-position # noqa from k2hr3client.policy import K2hr3Policy # type: ignore # pylint: disable=import-error, wrong-import-position # noqa from k2hr3client.role import K2hr3Role # type: ignore # pylint: disable=import-error, wrong-import-position # noqa +_MAX_LINE_LENGTH = 1024 * 8 + IDENTITY_V3_PASSWORD_AUTH_JSON_DATA = """ { "auth": { @@ -125,6 +129,31 @@ def get_scoped_token_id(url, user, password, project): return scoped_token_id +def set_data(val: Path, projectname: str, clustername: str) -> str: + """Set data.""" + if val.exists() is False: + raise K2hr3Exception(f'path must exist, not {val}') + if val.is_file() is False: + raise K2hr3Exception( + f'path must be a regular file, not {val}') + data = "" + with val.open(encoding='utf-8') as f: # pylint: disable=no-member + line_len = 0 + for line in iter(f.readline, ''): + # 3. replace TROVE_K2HDKC_CLUSTER_NAME with clustername + line = re.sub('__TROVE_K2HDKC_CLUSTER_NAME__', clustername, + line) + # 4. replace TROVE_K2HDKC_TENANT_NAME with projectname + line = re.sub('__TROVE_K2HDKC_TENANT_NAME__', projectname, + line) + line_len += len(line) + if line_len > _MAX_LINE_LENGTH: + raise K2hr3Exception('data too big') + data = "".join([data, line]) # type: ignore + + return data + + if __name__ == '__main__': parser = argparse.ArgumentParser(description='k2hr3 token api example') parser.add_argument( @@ -174,16 +203,16 @@ def get_scoped_token_id(url, user, password, project): # 3. Makes a new k2hr3 resource k2hr3_resource = K2hr3Resource(k2hr3_token.token) - init_py = Path(k2hr3client.__file__) - txt_file = init_py.parent.joinpath('examples', - 'example_resource.txt') + k2hr3client_init_py = Path(k2hr3client.__file__) + val = k2hr3client_init_py.parent.joinpath('examples', + 'example_resource.txt') + data = set_data(val, projectname=args.project, clustername=args.resource) + http.POST( k2hr3_resource.create_conf_resource( name=args.resource, data_type='string', - data=Path(txt_file), - tenant=args.project, - cluster_name=args.resource, + resource_data=data, keys={ "cluster-name": args.resource, "chmpx-server-port": "8020", @@ -202,11 +231,9 @@ def get_scoped_token_id(url, user, password, project): k2hr3_resource_server = K2hr3Resource(k2hr3_token.token) http.POST( k2hr3_resource_server.create_conf_resource( - tenant=args.project, - cluster_name=args.resource, name="/".join([args.resource, "server"]), data_type='string', - data="", + resource_data="", keys={"chmpx-mode": "SERVER", "k2hr3-init-packages": "", "k2hr3-init-packagecloud-packages": "", @@ -219,11 +246,9 @@ def get_scoped_token_id(url, user, password, project): k2hr3_resource_slave = K2hr3Resource(k2hr3_token.token) http.POST( k2hr3_resource_slave.create_conf_resource( - tenant=args.project, - cluster_name=args.resource, name="/".join([args.resource, "slave"]), data_type='string', - data="", + resource_data="", keys={"chmpx-mode": "SLAVE", "k2hr3-init-packages": "", "k2hr3-init-packagecloud-packages": "", diff --git a/src/k2hr3client/resource.py b/src/k2hr3client/resource.py index a2d97bb..60ee8d4 100644 --- a/src/k2hr3client/resource.py +++ b/src/k2hr3client/resource.py @@ -62,7 +62,7 @@ from pathlib import Path import re from typing import Optional, Any - +import warnings import k2hr3client from k2hr3client.api import K2hr3Api, K2hr3HTTPMethod @@ -141,6 +141,7 @@ def __init__(self, r3token: Optional[str] = None, self.urlparams = None # attributes that are unique to this class self.name = None + self.resource_data = None self.data_type = None self.keys = None self.alias = None @@ -177,15 +178,40 @@ def __init__(self, r3token: Optional[str] = None, # data=resource data # keys=json key value object # - def create_conf_resource(self, name: str, data_type: str, data: Any, # pylint: disable=R0917 # noqa - tenant: str, cluster_name: str, - keys: Optional[dict], + def create_conf_resource(self, name: str, data_type: str, resource_data: str, # pylint: disable=R0917 # noqa + data: Optional[Any] = None, + tenant: Optional[str] = None, + cluster_name: Optional[str] = None, + keys: Optional[dict] = None, alias: Optional[list] = None): """Create the resource.""" self.api_id = 1 self.name = name # type: ignore + self.resource_data = resource_data # type: ignore self.data_type = data_type # type: ignore - self._set_data(data, tenant, cluster_name) # type: ignore + if resource_data is None and data is not None: + warnings.warn( + "The 'datae' parameter to 'create_conf_resource' " + "is deprecated and slated for removal in " + "k2hr3client-1.1.0", + DeprecationWarning, + stacklevel=1) + self.resource_data = data + if tenant is not None: + warnings.warn( + "The 'tenant' parameter to 'create_conf_resource' " + "is deprecated and slated for removal in " + "k2hr3client-1.1.0", + DeprecationWarning, + stacklevel=1) + if cluster_name is not None: + warnings.warn( + "The 'cluster_name' parameter to 'create_conf_resource' " + "is deprecated and slated for removal in " + "k2hr3client-1.1.0", + DeprecationWarning, + stacklevel=1) + self._set_data(resource_data, tenant, cluster_name) # type: ignore self.keys = keys # type: ignore self.alias = alias # type: ignore return self diff --git a/src/tests/test_resource.py b/src/tests/test_resource.py index 13687ac..f22ea3f 100644 --- a/src/tests/test_resource.py +++ b/src/tests/test_resource.py @@ -47,7 +47,7 @@ def setUp(self): self.name = "test_resource" # self.project = "test_project" self.data_type = 'string' - self.data = "testresourcedata" + self.resource_data = "testresourcedata" self.tenant = "mytenant" self.cluster_name = "mycluster" self.keys = { @@ -94,13 +94,13 @@ def test_resource_create_resource_using_put(self, self.assertEqual(myresource.r3token, "token") """ root path.""" myresource.create_conf_resource( - self.name, - self.data_type, - self.data, - self.tenant, - self.cluster_name, - self.keys, - self.alias) + name = self.name, + data_type = self.data_type, + resource_data = self.resource_data, + tenant = self.tenant, + cluster_name = self.cluster_name, + keys = self.keys, + alias = self.alias) httpreq = khttp.K2hr3Http(self.base_url) self.assertTrue(httpreq.PUT(myresource)) @@ -110,7 +110,7 @@ def test_resource_create_resource_using_put(self, s_s_urlparams = { 'name': self.name, 'type': self.data_type, - 'data': self.data, + 'data': self.resource_data, 'keys': self.keys, 'alias': self.alias }