|
24 | 24 | import io.netris.api.v1.SitesApi; |
25 | 25 | import io.netris.api.v1.TenantsApi; |
26 | 26 | import io.netris.api.v2.VpcApi; |
27 | | -import io.netris.model.AuthSchema; |
28 | 27 | import io.netris.model.GetSiteBody; |
29 | 28 | import io.netris.model.SitesResponseOK; |
30 | 29 | import io.netris.model.VPCListing; |
|
35 | 34 | import org.apache.logging.log4j.LogManager; |
36 | 35 | import org.apache.logging.log4j.Logger; |
37 | 36 |
|
38 | | -import java.math.BigDecimal; |
39 | 37 | import java.util.List; |
40 | 38 |
|
41 | 39 | public class NetrisApiClientImpl implements NetrisApiClient { |
42 | 40 |
|
43 | 41 | private final Logger logger = LogManager.getLogger(getClass()); |
44 | 42 |
|
45 | | - private static final ApiClient apiClient = new ApiClient(); |
| 43 | + private static ApiClient apiClient; |
46 | 44 |
|
47 | 45 | public NetrisApiClientImpl(String endpointBaseUrl, String username, String password) { |
48 | | - apiClient.setBasePath(endpointBaseUrl); |
49 | | - authenticate(username, password); |
50 | | - } |
51 | | - |
52 | | - private void authenticate(String username, String password) { |
53 | | - AuthSchema authSchema = createAuthSchema(username, password); |
54 | | - AuthenticationApi authenticationApi = new AuthenticationApi(apiClient); |
55 | 46 | try { |
56 | | - ApiResponse<AuthResponse> authResponse = authenticationApi.apiAuthPost(authSchema); |
57 | | - if (authResponse.getStatusCode() == 200) { |
58 | | - String cookie = authResponse.getHeaders().get("Set-Cookie").get(0).split(";")[0]; |
59 | | - apiClient.addDefaultHeader("Cookie", cookie); |
60 | | - } else { |
61 | | - String msg = String.format("Authentication to the Netris Controller %s failed, please check the credentials provided", apiClient.getBasePath()); |
62 | | - logger.error(msg); |
63 | | - throw new CloudRuntimeException(msg); |
64 | | - } |
| 47 | + apiClient = new ApiClient(endpointBaseUrl, username, password, 1L); |
65 | 48 | } catch (ApiException e) { |
66 | | - String msg = String.format("Error authenticating to the Netris Controller %s: Code %s - Message: %s", apiClient.getBasePath(), e.getCode(), e.getResponseBody()); |
67 | | - logger.error(msg, e); |
68 | | - throw new CloudRuntimeException(msg); |
| 49 | + logAndThrowException(String.format("Error creating the Netris API Client for %s", endpointBaseUrl), e); |
69 | 50 | } |
70 | 51 | } |
71 | 52 |
|
72 | | - private AuthSchema createAuthSchema(String username, String password) { |
73 | | - AuthSchema authSchema = new AuthSchema(); |
74 | | - authSchema.setUser(username); |
75 | | - authSchema.setPassword(password); |
76 | | - authSchema.setAuthSchemeID(new BigDecimal(1)); |
77 | | - return authSchema; |
| 53 | + protected void logAndThrowException(String prefix, ApiException e) throws CloudRuntimeException { |
| 54 | + String msg = String.format("%s: (%s, %s, %s)", prefix, e.getCode(), e.getMessage(), e.getResponseBody()); |
| 55 | + logger.error(msg); |
| 56 | + throw new CloudRuntimeException(msg); |
78 | 57 | } |
79 | 58 |
|
80 | 59 | @Override |
81 | 60 | public boolean isSessionAlive() { |
82 | | - AuthenticationApi api = new AuthenticationApi(apiClient); |
| 61 | + ApiResponse<AuthResponse> response = null; |
83 | 62 | try { |
84 | | - ApiResponse<AuthResponse> response = api.apiAuthGet(); |
85 | | - return response.getStatusCode() == 200; |
| 63 | + AuthenticationApi api = apiClient.getApiStubForMethod(AuthenticationApi.class); |
| 64 | + response = api.apiAuthGet(); |
86 | 65 | } catch (ApiException e) { |
87 | | - throw new CloudRuntimeException(e); |
| 66 | + logAndThrowException("Error checking the Netris API session is alive", e); |
88 | 67 | } |
| 68 | + return response != null && response.getStatusCode() == 200; |
89 | 69 | } |
90 | 70 |
|
91 | 71 | @Override |
92 | 72 | public List<GetSiteBody> listSites() { |
93 | | - SitesApi api = new SitesApi(apiClient); |
| 73 | + SitesResponseOK response = null; |
94 | 74 | try { |
95 | | - SitesResponseOK response = api.apiSitesGet(); |
96 | | - return response.getData(); |
| 75 | + SitesApi api = apiClient.getApiStubForMethod(SitesApi.class); |
| 76 | + response = api.apiSitesGet(); |
97 | 77 | } catch (ApiException e) { |
98 | | - throw new CloudRuntimeException(e); |
| 78 | + logAndThrowException("Error listing Netris Sites", e); |
99 | 79 | } |
| 80 | + return response != null ? response.getData() : null; |
100 | 81 | } |
101 | 82 |
|
102 | 83 | @Override |
103 | 84 | public List<VPCListing> listVPCs() { |
104 | | - VpcApi api = new VpcApi(apiClient); |
| 85 | + VPCResponseOK response = null; |
105 | 86 | try { |
106 | | - VPCResponseOK response = api.apiV2VpcGet(); |
107 | | - return response.getData(); |
| 87 | + VpcApi api = apiClient.getApiStubForMethod(VpcApi.class); |
| 88 | + response = api.apiV2VpcGet(); |
108 | 89 | } catch (ApiException e) { |
109 | | - throw new CloudRuntimeException(e); |
| 90 | + logAndThrowException("Error listing Netris VPCs", e); |
110 | 91 | } |
| 92 | + return response != null ? response.getData() : null; |
111 | 93 | } |
112 | 94 |
|
113 | 95 | @Override |
114 | | - public List<TenantResponse> listTenants() throws ApiException { |
115 | | - TenantsApi api = new TenantsApi(apiClient); |
116 | | - ApiResponse<TenantsResponse> response = api.apiTenantsGet(); |
117 | | - return response.getData().getData(); |
| 96 | + public List<TenantResponse> listTenants() { |
| 97 | + ApiResponse<TenantsResponse> response = null; |
| 98 | + try { |
| 99 | + TenantsApi api = apiClient.getApiStubForMethod(TenantsApi.class); |
| 100 | + response = api.apiTenantsGet(); |
| 101 | + } catch (ApiException e) { |
| 102 | + logAndThrowException("Error listing Netris Tenants", e); |
| 103 | + } |
| 104 | + return (response != null && response.getData() != null) ? response.getData().getData() : null; |
118 | 105 | } |
119 | 106 | } |
0 commit comments