24
24
import os
25
25
from datetime import timedelta
26
26
from enum import Enum , unique
27
- from pathlib import Path
28
27
from typing import Any , TextIO , Tuple , cast
29
28
from urllib .parse import urlunsplit
30
29
@@ -458,14 +457,12 @@ def policy_add(self,
458
457
policy_file : str | os .PathLike | None = None ,
459
458
policy : dict | None = None ) -> str :
460
459
"""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
461
463
if policy_file :
462
- with Path ( policy_file ). open (encoding = 'utf-8' ) as file :
464
+ with open (policy_file , encoding = 'utf-8' ) as file :
463
465
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
-
469
466
response = self ._url_open (
470
467
"PUT" ,
471
468
_COMMAND .ADD_CANNED_POLICY ,
@@ -775,8 +772,8 @@ def add_service_account(self,
775
772
raise ValueError ("both access key and secret key must be provided" )
776
773
if access_key == "" or secret_key == "" :
777
774
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 " )
780
777
data : dict [str , Any ] = {
781
778
"status" : "enabled" ,
782
779
"accessKey" : access_key ,
@@ -787,7 +784,7 @@ def add_service_account(self,
787
784
if description :
788
785
data ["description" ] = description
789
786
if policy_file :
790
- with Path ( policy_file ). open (encoding = "utf-8" ) as file :
787
+ with open (policy_file , encoding = "utf-8" ) as file :
791
788
data ["policy" ] = json .load (file )
792
789
if policy :
793
790
data ["policy" ] = policy
@@ -824,8 +821,8 @@ def update_service_account(self,
824
821
raise ValueError ("at least one of secret_key, name, description, "
825
822
"policy_file, policy, expiration or status must "
826
823
"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 " )
829
826
data : dict [str , Any ] = {}
830
827
if secret_key :
831
828
data ["newSecretKey" ] = secret_key
@@ -834,7 +831,7 @@ def update_service_account(self,
834
831
if description :
835
832
data ["newDescription" ] = description
836
833
if policy_file :
837
- with Path ( policy_file ). open (encoding = "utf-8" ) as file :
834
+ with open (policy_file , encoding = "utf-8" ) as file :
838
835
data ["newPolicy" ] = json .load (file )
839
836
if policy :
840
837
data ["newPolicy" ] = policy
0 commit comments