24
24
import os
25
25
from datetime import timedelta
26
26
from enum import Enum , unique
27
+ from pathlib import Path
27
28
from typing import Any , TextIO , Tuple , cast
28
29
from urllib .parse import urlunsplit
29
30
@@ -452,16 +453,26 @@ def group_list(self) -> str:
452
453
response = self ._url_open ("GET" , _COMMAND .LIST_GROUPS )
453
454
return response .data .decode ()
454
455
455
- def policy_add (self , policy_name : str , policy_file : str ) -> str :
456
+ def policy_add (self ,
457
+ policy_name : str ,
458
+ policy_file : str | os .PathLike | None = None ,
459
+ policy : dict | None = None ) -> str :
456
460
"""Add new policy."""
457
- with open (policy_file , encoding = 'utf-8' ) as file :
458
- response = self ._url_open (
459
- "PUT" ,
460
- _COMMAND .ADD_CANNED_POLICY ,
461
- query_params = {"name" : policy_name },
462
- body = file .read ().encode (),
463
- )
464
- return response .data .decode ()
461
+ if policy_file :
462
+ with Path (policy_file ).open (encoding = 'utf-8' ) as file :
463
+ 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
+ response = self ._url_open (
470
+ "PUT" ,
471
+ _COMMAND .ADD_CANNED_POLICY ,
472
+ query_params = {"name" : policy_name },
473
+ body = body ,
474
+ )
475
+ return response .data .decode ()
465
476
466
477
def policy_remove (self , policy_name : str ) -> str :
467
478
"""Remove policy."""
@@ -753,7 +764,8 @@ def add_service_account(self,
753
764
secret_key : str | None = None ,
754
765
name : str | None = None ,
755
766
description : str | None = None ,
756
- policy_file : str | None = None ,
767
+ policy : dict | None = None ,
768
+ policy_file : str | os .PathLike | None = None ,
757
769
expiration : str | None = None ,
758
770
status : str | None = None ) -> str :
759
771
"""
@@ -763,7 +775,9 @@ def add_service_account(self,
763
775
raise ValueError ("both access key and secret key must be provided" )
764
776
if access_key == "" or secret_key == "" :
765
777
raise ValueError ("access key or secret key must not be empty" )
766
- data = {
778
+ if policy_file and policy :
779
+ raise ValueError ("specify either policy_file or policy, not both" )
780
+ data : dict [str , Any ] = {
767
781
"status" : "enabled" ,
768
782
"accessKey" : access_key ,
769
783
"secretKey" : secret_key ,
@@ -773,8 +787,10 @@ def add_service_account(self,
773
787
if description :
774
788
data ["description" ] = description
775
789
if policy_file :
776
- with open (policy_file , encoding = "utf-8" ) as file :
790
+ with Path (policy_file ). open ( encoding = "utf-8" ) as file :
777
791
data ["policy" ] = json .load (file )
792
+ if policy :
793
+ data ["policy" ] = policy
778
794
if expiration :
779
795
data ["expiration" ] = expiration
780
796
if status :
@@ -797,25 +813,31 @@ def update_service_account(self,
797
813
secret_key : str | None = None ,
798
814
name : str | None = None ,
799
815
description : str | None = None ,
800
- policy_file : str | None = None ,
816
+ policy_file : str | os .PathLike | None = None ,
817
+ policy : dict | None = None ,
801
818
expiration : str | None = None ,
802
819
status : str | None = None ) -> str :
803
820
"""Update an existing service account"""
804
- args = [secret_key , name , description , policy_file , expiration , status ]
821
+ args = [secret_key , name , description ,
822
+ policy_file , policy , expiration , status ]
805
823
if not any (arg for arg in args ):
806
824
raise ValueError ("at least one of secret_key, name, description, "
807
- "policy_file, expiration or status must be "
808
- "specified" )
809
- data = {}
825
+ "policy_file, policy, expiration or status must "
826
+ "be specified" )
827
+ if policy_file and policy :
828
+ raise ValueError ("specify either policy_file or policy, not both" )
829
+ data : dict [str , Any ] = {}
810
830
if secret_key :
811
831
data ["newSecretKey" ] = secret_key
812
832
if name :
813
833
data ["newName" ] = name
814
834
if description :
815
835
data ["newDescription" ] = description
816
836
if policy_file :
817
- with open (policy_file , encoding = "utf-8" ) as file :
837
+ with Path (policy_file ). open ( encoding = "utf-8" ) as file :
818
838
data ["newPolicy" ] = json .load (file )
839
+ if policy :
840
+ data ["newPolicy" ] = policy
819
841
if expiration :
820
842
data ["newExpiration" ] = expiration
821
843
if status :
0 commit comments