Skip to content

Commit

Permalink
Deprecates create_conf_resource API
Browse files Browse the repository at this point in the history
  • Loading branch information
hiwakaba committed Oct 3, 2024
1 parent f1f6846 commit b88a4c9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 27 deletions.
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

0 comments on commit b88a4c9

Please sign in to comment.