Skip to content

Commit

Permalink
Updates docs (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiwakaba authored Oct 7, 2024
1 parent a269d0c commit 5bfc92a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 100 deletions.
106 changes: 10 additions & 96 deletions src/k2hr3client/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
.. code-block:: python
# Simple CUI application to create a full k2hdkc dbaas resource.
# Simple CUI application to create a role.
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import argparse
Expand Down Expand Up @@ -101,7 +101,7 @@ def get_scoped_token_id(url, user, password, project):
python_data['auth']['identity']['password']['user']['name'] = user
python_data['auth']['identity']['password']['user']['password'] = password
headers = {
'User-Agent': 'hiwkby-sample',
'User-Agent': 'doc_sample',
'Content-Type': 'application/json'
}
req = urllib.request.Request(url,
Expand All @@ -118,7 +118,7 @@ def get_scoped_token_id(url, user, password, project):
python_data['auth']['identity']['token']['id'] = unscoped_token_id
python_data['auth']['scope']['project']['name'] = project
headers = {
'User-Agent': 'hiwkby-sample',
'User-Agent': 'doc_sample',
'Content-Type': 'application/json'
}
req = urllib.request.Request(url,
Expand All @@ -131,31 +131,6 @@ 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 All @@ -181,19 +156,15 @@ def set_data(val: Path, projectname: str, clustername: str) -> str:
help='k2hr3 api url')
parser.add_argument('--resource',
dest='resource',
default='k2hdkccluster',
default='doc_sample',
help='resource name')
parser.add_argument('--resource_file',
dest='resource_file',
default=None,
help='resource file name')
parser.add_argument('--policy',
dest='policy',
default='k2hdkccluster',
default='doc_sample',
help='policy name')
parser.add_argument('--role',
dest='role',
default='k2hdkccluster',
default='doc_sample',
help='k2hr3 rolename')
args = parser.parse_args()
Expand All @@ -209,71 +180,30 @@ def set_data(val: Path, projectname: str, clustername: str) -> str:
# 3. Makes a new k2hr3 resource
k2hr3_resource = K2hr3Resource(k2hr3_token.token)
resource_data = ""
if args.resource_file:
val = Path(args.resource_file)
resource_data = set_data(val, projectname=args.project, clustername=args.resource)
else:
resource_data = args.resource
resource_data = args.resource
http.POST(
k2hr3_resource.create_conf_resource(
name=args.resource,
data_type='string',
resource_data=resource_data,
keys={
"cluster-name": args.resource,
"chmpx-server-port": "8020",
"chmpx-server-ctlport": "8021",
"chmpx-slave-ctlport": "8031"
"doc_sample": args.resource
},
alias=[]
)
)
# 3.1. Makes a new k2hr3 resource for server
k2hr3_resource_server = K2hr3Resource(k2hr3_token.token)
http.POST(
k2hr3_resource_server.create_conf_resource(
name="/".join([args.resource, "server"]),
data_type='string',
resource_data="",
keys={"chmpx-mode": "SERVER",
"k2hr3-init-packages": "",
"k2hr3-init-packagecloud-packages": "",
"k2hr3-init-systemd-packages": ""},
alias=[]
)
)
# 3.2. Makes a new k2hr3 resource for slave
k2hr3_resource_slave = K2hr3Resource(k2hr3_token.token)
http.POST(
k2hr3_resource_slave.create_conf_resource(
name="/".join([args.resource, "slave"]),
data_type='string',
resource_data="",
keys={"chmpx-mode": "SLAVE",
"k2hr3-init-packages": "",
"k2hr3-init-packagecloud-packages": "",
"k2hr3-init-systemd-packages": "",
"k2hdkc-dbaas-add-user": 1},
alias=[]
)
)
# 4. Makes a new k2hr3 policy for the resource
k2hr3_policy = K2hr3Policy(k2hr3_token.token)
SERVER_RESOURCE_PATH = "yrn:yahoo:::{}:resource:{}/server".format(
args.project, args.resource)
SLAVE_RESOURCE_PATH = "yrn:yahoo:::{}:resource:{}/slave".format(
MY_RESOURCE_PATH = "yrn:yahoo:::{}:resource:{}/doc_sample".format(
args.project, args.resource)
http.POST(
k2hr3_policy.create(
name=args.policy,
effect='allow',
action=['yrn:yahoo::::action:read'],
resource=[SERVER_RESOURCE_PATH, SLAVE_RESOURCE_PATH],
resource=[MY_RESOURCE_PATH],
condition=None,
alias=[]
)
Expand All @@ -289,22 +219,6 @@ def set_data(val: Path, projectname: str, clustername: str) -> str:
alias=[]
)
)
server_role = K2hr3Role(k2hr3_token.token)
http.POST(
server_role.create(
name="/".join([args.role, "server"]),
policies=[],
alias=[]
)
)
slave_role = K2hr3Role(k2hr3_token.token)
http.POST(
slave_role.create(
name="/".join([args.role, "slave"]),
policies=[],
alias=[]
)
)
print(slave_role.resp.body)
sys.exit(0)
Expand Down
8 changes: 4 additions & 4 deletions src/k2hr3client/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
.. code-block:: python
# Simple CUI application to get registerpath for k2hdkc dbaas.
# Simple CUI application to get registerpath for a k2hr3 role.
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import argparse
Expand Down Expand Up @@ -94,7 +94,7 @@ def get_scoped_token_id(url, user, password, project):
python_data['auth']['identity']['password']['user']['name'] = user
python_data['auth']['identity']['password']['user']['password'] = password # noqa
headers = {
'User-Agent': 'hiwkby-sample',
'User-Agent': 'doc_sample',
'Content-Type': 'application/json'
}
req = urllib.request.Request(url,
Expand All @@ -111,7 +111,7 @@ def get_scoped_token_id(url, user, password, project):
python_data['auth']['identity']['token']['id'] = unscoped_token_id
python_data['auth']['scope']['project']['name'] = project
headers = {
'User-Agent': 'hiwkby-sample',
'User-Agent': 'doc_sample',
'Content-Type': 'application/json'
}
req = urllib.request.Request(url,
Expand Down Expand Up @@ -149,7 +149,7 @@ def get_scoped_token_id(url, user, password, project):
help='k2hr3 api url')
parser.add_argument('--role',
dest='role',
default='k2hdkccluster',
default='doc_sample',
help='k2hr3 rolename')
args = parser.parse_args()
Expand Down

0 comments on commit 5bfc92a

Please sign in to comment.