Skip to content

Commit 5081c84

Browse files
authored
Apply suggestions from code review
1 parent 98b71fa commit 5081c84

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

minio/minioadmin.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import os
2525
from datetime import timedelta
2626
from enum import Enum, unique
27-
from pathlib import Path
2827
from typing import Any, TextIO, Tuple, cast
2928
from urllib.parse import urlunsplit
3029

@@ -458,14 +457,12 @@ def policy_add(self,
458457
policy_file: str | os.PathLike | None = None,
459458
policy: dict | None = None) -> str:
460459
"""Add new policy."""
460+
if not (policy_file is not None) ^ (policy is not None):
461+
raise ValueError("either policy_file or policy must be provided")
462+
body = policy
461463
if policy_file:
462-
with Path(policy_file).open(encoding='utf-8') as file:
464+
with open(policy_file, encoding='utf-8') as file:
463465
body = file.read().encode()
464-
elif policy:
465-
body = json.dumps(policy).encode()
466-
else:
467-
raise ValueError("either policy or policy_file must be specified")
468-
469466
response = self._url_open(
470467
"PUT",
471468
_COMMAND.ADD_CANNED_POLICY,
@@ -775,8 +772,8 @@ def add_service_account(self,
775772
raise ValueError("both access key and secret key must be provided")
776773
if access_key == "" or secret_key == "":
777774
raise ValueError("access key or secret key must not be empty")
778-
if policy_file and policy:
779-
raise ValueError("specify either policy_file or policy, not both")
775+
if policy_file is not None and policy is not None:
776+
raise ValueError("either policy_file or policy must be provided")
780777
data: dict[str, Any] = {
781778
"status": "enabled",
782779
"accessKey": access_key,
@@ -787,7 +784,7 @@ def add_service_account(self,
787784
if description:
788785
data["description"] = description
789786
if policy_file:
790-
with Path(policy_file).open(encoding="utf-8") as file:
787+
with open(policy_file, encoding="utf-8") as file:
791788
data["policy"] = json.load(file)
792789
if policy:
793790
data["policy"] = policy
@@ -824,8 +821,8 @@ def update_service_account(self,
824821
raise ValueError("at least one of secret_key, name, description, "
825822
"policy_file, policy, expiration or status must "
826823
"be specified")
827-
if policy_file and policy:
828-
raise ValueError("specify either policy_file or policy, not both")
824+
if policy_file is not None and policy is not None:
825+
raise ValueError("either policy_file or policy must be provided")
829826
data: dict[str, Any] = {}
830827
if secret_key:
831828
data["newSecretKey"] = secret_key
@@ -834,7 +831,7 @@ def update_service_account(self,
834831
if description:
835832
data["newDescription"] = description
836833
if policy_file:
837-
with Path(policy_file).open(encoding="utf-8") as file:
834+
with open(policy_file, encoding="utf-8") as file:
838835
data["newPolicy"] = json.load(file)
839836
if policy:
840837
data["newPolicy"] = policy

0 commit comments

Comments
 (0)