4
4
from cloudtower .exceptions import ApiException
5
5
from cloudtower .api .task_api import TaskApi
6
6
from cloudtower .api .user_api import UserApi
7
+ import json
7
8
8
9
9
10
def wait_task (id , api_client , interval = 5 , timeout = 300 ):
@@ -28,7 +29,7 @@ def wait_task(id, api_client, interval=5, timeout=300):
28
29
)
29
30
)
30
31
start = time .time ()
31
- while (True ):
32
+ while (True ):
32
33
now = time .time ()
33
34
if (now - start ) > timeout :
34
35
raise ApiException (
@@ -105,11 +106,33 @@ def login(api_client: ApiClient, username, password, source=UserSource.LOCAL):
105
106
:type password: UserSource
106
107
"""
107
108
user_api = UserApi (api_client )
108
- login_res = user_api . login ( {
109
+ login_params = {
109
110
"username" : username ,
110
111
"password" : password ,
111
112
"source" : source
112
- })
113
+ }
114
+ if source == UserSource .LDAP :
115
+ host = api_client .configuration .host
116
+ if host .endswith ("/" ): # remove trailing slash
117
+ host = host [:- 1 ]
118
+ if host .endswith ("/v2/api" ):
119
+ # replace v2/api with api
120
+ host = host [:- 7 ] + "/api"
121
+ try :
122
+ resp = api_client .request ("POST" , host , body = {
123
+ "query" : "{authnStrategies{id type}}" ,
124
+ "variables" : {}
125
+ })
126
+ configs = json .loads (resp .data )['data' ]['authnStrategies' ]
127
+ for config in configs :
128
+ if config ['type' ] == 'LDAP' :
129
+ login_params ["auth_config_id" ] = config ['id' ]
130
+ login_params ["source" ] = UserSource .AUTHN
131
+ break
132
+ except :
133
+ # ignore error for backward compatibility, old version of tower has not authn config query
134
+ pass
135
+ login_res = user_api .login (login_params )
113
136
api_client .configuration .api_key ["Authorization" ] = login_res .data .token
114
137
return
115
138
0 commit comments