Skip to content

Commit 9f76e37

Browse files
authored
Delegate API classes creation to the SDK and simply invoke the desired API class through CloudStack (#7)
* Delegate API classes creation to the SDK and simply invoke the desired API class through CloudStack * Pass default auth scheme for now
1 parent b3c89c6 commit 9f76e37

File tree

1 file changed

+31
-44
lines changed

1 file changed

+31
-44
lines changed

plugins/network-elements/netris/src/main/java/org/apache/cloudstack/service/NetrisApiClientImpl.java

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import io.netris.api.v1.SitesApi;
2525
import io.netris.api.v1.TenantsApi;
2626
import io.netris.api.v2.VpcApi;
27-
import io.netris.model.AuthSchema;
2827
import io.netris.model.GetSiteBody;
2928
import io.netris.model.SitesResponseOK;
3029
import io.netris.model.VPCListing;
@@ -35,85 +34,73 @@
3534
import org.apache.logging.log4j.LogManager;
3635
import org.apache.logging.log4j.Logger;
3736

38-
import java.math.BigDecimal;
3937
import java.util.List;
4038

4139
public class NetrisApiClientImpl implements NetrisApiClient {
4240

4341
private final Logger logger = LogManager.getLogger(getClass());
4442

45-
private static final ApiClient apiClient = new ApiClient();
43+
private static ApiClient apiClient;
4644

4745
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);
5546
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);
6548
} 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);
6950
}
7051
}
7152

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);
7857
}
7958

8059
@Override
8160
public boolean isSessionAlive() {
82-
AuthenticationApi api = new AuthenticationApi(apiClient);
61+
ApiResponse<AuthResponse> response = null;
8362
try {
84-
ApiResponse<AuthResponse> response = api.apiAuthGet();
85-
return response.getStatusCode() == 200;
63+
AuthenticationApi api = apiClient.getApiStubForMethod(AuthenticationApi.class);
64+
response = api.apiAuthGet();
8665
} catch (ApiException e) {
87-
throw new CloudRuntimeException(e);
66+
logAndThrowException("Error checking the Netris API session is alive", e);
8867
}
68+
return response != null && response.getStatusCode() == 200;
8969
}
9070

9171
@Override
9272
public List<GetSiteBody> listSites() {
93-
SitesApi api = new SitesApi(apiClient);
73+
SitesResponseOK response = null;
9474
try {
95-
SitesResponseOK response = api.apiSitesGet();
96-
return response.getData();
75+
SitesApi api = apiClient.getApiStubForMethod(SitesApi.class);
76+
response = api.apiSitesGet();
9777
} catch (ApiException e) {
98-
throw new CloudRuntimeException(e);
78+
logAndThrowException("Error listing Netris Sites", e);
9979
}
80+
return response != null ? response.getData() : null;
10081
}
10182

10283
@Override
10384
public List<VPCListing> listVPCs() {
104-
VpcApi api = new VpcApi(apiClient);
85+
VPCResponseOK response = null;
10586
try {
106-
VPCResponseOK response = api.apiV2VpcGet();
107-
return response.getData();
87+
VpcApi api = apiClient.getApiStubForMethod(VpcApi.class);
88+
response = api.apiV2VpcGet();
10889
} catch (ApiException e) {
109-
throw new CloudRuntimeException(e);
90+
logAndThrowException("Error listing Netris VPCs", e);
11091
}
92+
return response != null ? response.getData() : null;
11193
}
11294

11395
@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;
118105
}
119106
}

0 commit comments

Comments
 (0)