diff --git a/HISTORY.rst b/HISTORY.rst index 9d9cfc6..83d8b42 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,12 @@ History ======= +1.0.3 (2024-10-03) +------------------- + +* Fixes the K2hr3 API version +* Deprecates K2hr3Resource.create_conf_resource params + 1.0.2 (2024-09-26) ------------------- diff --git a/docs/locale/ja/LC_MESSAGES/history.po b/docs/locale/ja/LC_MESSAGES/history.po index d8f666e..45e337f 100644 --- a/docs/locale/ja/LC_MESSAGES/history.po +++ b/docs/locale/ja/LC_MESSAGES/history.po @@ -10,7 +10,7 @@ msgid "" msgstr "" "Project-Id-Version: k2hr3client \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-09-26 10:42+0900\n" +"POT-Creation-Date: 2024-10-03 18:04+0900\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language: ja\n" @@ -26,34 +26,46 @@ msgid "History" msgstr "歴史" #: ../../../HISTORY.rst:6 -msgid "1.0.2 (2024-09-26)" +msgid "1.0.3 (2024-10-03)" msgstr "" #: ../../../HISTORY.rst:8 +msgid "Fixes the K2hr3 API version" +msgstr "K2hr3 APIのバージョンを固定化" + +#: ../../../HISTORY.rst:9 +msgid "Deprecates K2hr3Resource.create_conf_resource params" +msgstr "K2hr3Resource.create_conf_resourceのパラメータを一部非推奨化" + +#: ../../../HISTORY.rst:12 +msgid "1.0.2 (2024-09-26)" +msgstr "" + +#: ../../../HISTORY.rst:14 msgid "Fixes lint errors" msgstr "静的解析ツールのエラーを修正" -#: ../../../HISTORY.rst:11 +#: ../../../HISTORY.rst:17 msgid "1.0.1 (2024-09-25)" msgstr "" -#: ../../../HISTORY.rst:13 +#: ../../../HISTORY.rst:19 msgid "Fixes resource API bugs" msgstr "Resource APIの不具合を修正" -#: ../../../HISTORY.rst:16 +#: ../../../HISTORY.rst:22 msgid "1.0.0 (2024-08-28)" msgstr "" -#: ../../../HISTORY.rst:18 +#: ../../../HISTORY.rst:24 msgid "Supports the other APIs" msgstr "他のAPIをサポート" -#: ../../../HISTORY.rst:21 +#: ../../../HISTORY.rst:27 msgid "0.0.1 (2020-08-28)" msgstr "" -#: ../../../HISTORY.rst:23 +#: ../../../HISTORY.rst:29 msgid "Supports Token, Resource, Policy and Role API" msgstr "トークン、リソース、ポリシーおよびロールAPIをサポート" diff --git a/src/k2hr3client/__init__.py b/src/k2hr3client/__init__.py index ac401b5..e444602 100644 --- a/src/k2hr3client/__init__.py +++ b/src/k2hr3client/__init__.py @@ -22,7 +22,7 @@ """K2HR3 Python Client of Token API.""" __author__ = 'Hirotaka Wakabayashi ' -__version__ = '1.0.2' +__version__ = '1.0.3' import sys diff --git a/src/k2hr3client/api.py b/src/k2hr3client/api.py index 385b264..33f88c5 100644 --- a/src/k2hr3client/api.py +++ b/src/k2hr3client/api.py @@ -157,7 +157,7 @@ class K2hr3Api(abc.ABC): # pylint: disable=too-many-instance-attributes def __init__(self, basepath: str, params: Optional[str] = None, # pylint: disable=R0917 # noqa hdrs: Optional[dict] = None, body: Optional[str] = None, - version: str = DEFAULT_VERSION) -> None: # noqa + version: str = DEFAULT_VERSION) -> None: # pylint: disable=W0613 # noqa """Init the K2hr3 API members. :raise K2hr3Exception: if the val is invalid. @@ -167,7 +167,7 @@ def __init__(self, basepath: str, params: Optional[str] = None, # pylint: disab self.urlparams = params self.headers = hdrs self.body = body - self.version = version + self.version = K2hr3Api.DEFAULT_VERSION # following attrs are dynamically set later. self.resp = None # type: ignore 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 } diff --git a/src/tests/test_version.py b/src/tests/test_version.py index fd40fa4..dc1db35 100644 --- a/src/tests/test_version.py +++ b/src/tests/test_version.py @@ -58,27 +58,6 @@ def test_k2hr3version_repr(self): # Note: The order of _error and _code is unknown! self.assertRegex(repr(myversion), '') - @patch('k2hr3client.http.K2hr3Http._HTTP_REQUEST_METHOD') - def test_k2hr3version_root_get_ok(self, mock_HTTP_REQUEST_METHOD): - """Get root path.""" - myversion = kversion.K2hr3Version() - self.assertEqual(myversion.name, "") - myversion.get() - httpreq = khttp.K2hr3Http(self.base_url) - self.assertTrue(httpreq.GET(myversion)) - - # 1. assert URL - self.assertEqual(httpreq.url, f"{self.base_url}/") - # 2. assert URL params - self.assertEqual(myversion.urlparams, None) - # 3. assert Request header - headers = { - 'Content-Type': 'application/json', - } - self.assertEqual(myversion.headers, headers) - # 4. assert Request body - self.assertEqual(myversion.body, None) - @patch('k2hr3client.http.K2hr3Http._HTTP_REQUEST_METHOD') def test_k2hr3version_root_get_v1_ok(self, mock_HTTP_REQUEST_METHOD): """Get root path."""