7
7
from spaceone .core .connector import BaseConnector
8
8
from spaceone .core .error import *
9
9
10
- __all__ = [' SpaceONEConnector' ]
10
+ __all__ = [" SpaceONEConnector" ]
11
11
12
12
_LOGGER = logging .getLogger (__name__ )
13
13
@@ -22,96 +22,89 @@ def __init__(self, *args, **kwargs):
22
22
self .endpoint = None
23
23
24
24
def init_client (self , options : dict , secret_data : dict , schema : str = None ) -> None :
25
- self ._check_secret_data (secret_data )
26
- spaceone_endpoint = secret_data ['spaceone_endpoint' ]
27
- self .token = secret_data ['spaceone_client_secret' ]
28
-
29
- if spaceone_endpoint .startswith ('http' ) or spaceone_endpoint .startswith ('https' ):
30
- self .protocol = 'http'
25
+ task_type = options .get ("task_type" , "identity" )
26
+ self ._check_secret_data (secret_data , task_type )
27
+ spaceone_endpoint = secret_data ["spaceone_endpoint" ]
28
+ self .token = secret_data ["spaceone_client_secret" ]
29
+
30
+ if spaceone_endpoint .startswith ("http" ) or spaceone_endpoint .startswith (
31
+ "https"
32
+ ):
33
+ self .protocol = "http"
31
34
self .endpoint = spaceone_endpoint
32
- elif spaceone_endpoint .startswith ('grpc' ) or spaceone_endpoint .startswith ('grpc+ssl' ):
33
- self .protocol = 'grpc'
34
- self .grpc_client : SpaceConnector = SpaceConnector (endpoint = spaceone_endpoint , token = self .token )
35
+ elif spaceone_endpoint .startswith ("grpc" ) or spaceone_endpoint .startswith (
36
+ "grpc+ssl"
37
+ ):
38
+ self .protocol = "grpc"
39
+ self .grpc_client : SpaceConnector = SpaceConnector (
40
+ endpoint = spaceone_endpoint , token = self .token
41
+ )
35
42
36
43
def verify_plugin (self , domain_id : str ) -> None :
37
- method = ' Project.list'
44
+ method = " Project.list"
38
45
params = {
39
- "query" : {
40
- "filter" : [
41
- {"k" : "tags.domain_id" , "v" : domain_id , "o" : "eq" }
42
- ]
43
- }
46
+ "query" : {"filter" : [{"k" : "tags.domain_id" , "v" : domain_id , "o" : "eq" }]}
44
47
}
45
48
self .dispatch (method , params )
46
49
47
50
def list_projects (self , domain_id : str ):
48
51
params = {
49
- 'query' : {
50
- 'filter' : [
51
- {'k' : 'tags.domain_id' , 'v' : domain_id , 'o' : 'eq' }
52
- ]
53
- }
52
+ "query" : {"filter" : [{"k" : "tags.domain_id" , "v" : domain_id , "o" : "eq" }]}
54
53
}
55
54
56
- return self .dispatch (' Project.list' , params )
55
+ return self .dispatch (" Project.list" , params )
57
56
58
57
def get_service_account (self , service_account_id ):
59
- params = {
60
- 'service_account_id' : service_account_id
61
- }
58
+ params = {"service_account_id" : service_account_id }
62
59
63
- return self .dispatch (' ServiceAccount.get' , params )
60
+ return self .dispatch (" ServiceAccount.get" , params )
64
61
65
62
def update_service_account (self , service_account_id , tags ):
66
- params = {
67
- 'service_account_id' : service_account_id ,
68
- 'tags' : tags
69
- }
63
+ params = {"service_account_id" : service_account_id , "tags" : tags }
70
64
71
- return self .dispatch (' ServiceAccount.update' , params )
65
+ return self .dispatch (" ServiceAccount.update" , params )
72
66
73
67
def list_service_accounts (self , project_id : str ):
74
- params = {
75
- 'provider' : 'aws' ,
76
- 'project_id' : project_id
77
- }
68
+ params = {"provider" : "aws" , "project_id" : project_id }
78
69
79
- return self .dispatch (' ServiceAccount.list' , params )
70
+ return self .dispatch (" ServiceAccount.list" , params )
80
71
81
72
def _get_metadata (self ):
82
- return (' token' , self .token ),
73
+ return (( " token" , self .token ),)
83
74
84
75
def dispatch (self , method : str = None , params : dict = None , ** kwargs ):
85
- if self .protocol == ' grpc' :
76
+ if self .protocol == " grpc" :
86
77
return self .grpc_client .dispatch (method , params , ** kwargs )
87
78
else :
88
79
return self .request (method , params , ** kwargs )
89
80
90
81
def request (self , method , params , ** kwargs ):
91
82
method = self ._convert_method_to_snake_case (method )
92
- url = f' { self .endpoint } /{ method } '
83
+ url = f" { self .endpoint } /{ method } "
93
84
94
85
headers = self ._make_request_header (self .token , ** kwargs )
95
86
response = requests .post (url , json = params , headers = headers )
96
87
97
88
if response .status_code >= 400 :
98
- raise requests .HTTPError (f'HTTP { response .status_code } Error: { response .json ()["detail" ]} ' )
89
+ raise requests .HTTPError (
90
+ f'HTTP { response .status_code } Error: { response .json ()["detail" ]} '
91
+ )
99
92
100
93
response = response .json ()
101
94
return response
102
95
103
96
@staticmethod
104
97
def _convert_method_to_snake_case (method ):
105
- method = re .sub (r' (?<!^)(?=[A-Z])' , '_' , method )
106
- method = method .replace ('.' , '/' ).replace ('_' , '-' ).lower ()
98
+ method = re .sub (r" (?<!^)(?=[A-Z])" , "_" , method )
99
+ method = method .replace ("." , "/" ).replace ("_" , "-" ).lower ()
107
100
return method
108
101
109
102
@staticmethod
110
103
def _make_request_header (token , ** kwargs ):
111
104
access_token = token
112
105
headers = {
113
- ' Authorization' : f' Bearer { access_token } ' ,
114
- ' Content-Type' : ' application/json' ,
106
+ " Authorization" : f" Bearer { access_token } " ,
107
+ " Content-Type" : " application/json" ,
115
108
}
116
109
return headers
117
110
@@ -120,9 +113,9 @@ def _change_message(message):
120
113
return MessageToDict (message , preserving_proto_field_name = True )
121
114
122
115
@staticmethod
123
- def _check_secret_data (secret_data : dict ):
124
- if ' spaceone_endpoint' not in secret_data :
125
- raise ERROR_REQUIRED_PARAMETER (key = ' secret_data.spaceone_endpoint' )
116
+ def _check_secret_data (secret_data : dict , task_type : str ):
117
+ if " spaceone_endpoint" not in secret_data :
118
+ raise ERROR_REQUIRED_PARAMETER (key = " secret_data.spaceone_endpoint" )
126
119
127
- if ' spaceone_client_secret' not in secret_data :
128
- raise ERROR_REQUIRED_PARAMETER (key = ' secret_data.spaceone_client_secret' )
120
+ if " spaceone_client_secret" not in secret_data :
121
+ raise ERROR_REQUIRED_PARAMETER (key = " secret_data.spaceone_client_secret" )
0 commit comments