Skip to content

Commit

Permalink
Release 1.0.3 (#2)
Browse files Browse the repository at this point in the history
* Removes the test because the API always v1 returns
* Returns v1 always
* Deprecates create_conf_resource API
* Updates History
  • Loading branch information
hiwakaba authored Oct 3, 2024
1 parent 1d8daa6 commit 2c8af41
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 59 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
-------------------

Expand Down
28 changes: 20 additions & 8 deletions docs/locale/ja/LC_MESSAGES/history.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language: ja\n"
Expand All @@ -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をサポート"

2 changes: 1 addition & 1 deletion src/k2hr3client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"""K2HR3 Python Client of Token API."""

__author__ = 'Hirotaka Wakabayashi <[email protected]>'
__version__ = '1.0.2'
__version__ = '1.0.3'

import sys

Expand Down
4 changes: 2 additions & 2 deletions src/k2hr3client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
51 changes: 38 additions & 13 deletions src/k2hr3client/examples/create_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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": {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand All @@ -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": "",
Expand All @@ -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": "",
Expand Down
36 changes: 31 additions & 5 deletions src/k2hr3client/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions src/tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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))

Expand All @@ -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
}
Expand Down
21 changes: 0 additions & 21 deletions src/tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,6 @@ def test_k2hr3version_repr(self):
# Note: The order of _error and _code is unknown!
self.assertRegex(repr(myversion), '<K2hr3Version .*>')

@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."""
Expand Down

0 comments on commit 2c8af41

Please sign in to comment.