|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.service; |
| 18 | + |
| 19 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 20 | +import io.netris.ApiClient; |
| 21 | +import io.netris.ApiException; |
| 22 | +import io.netris.ApiResponse; |
| 23 | +import io.netris.api.AuthenticationApi; |
| 24 | +import io.netris.api.SitesApi; |
| 25 | +import io.netris.model.AuthSchema; |
| 26 | +import io.netris.model.GetSiteBody; |
| 27 | +import io.netris.model.SitesResponseOK; |
| 28 | +import io.netris.model.response.AuthResponse; |
| 29 | +import org.apache.logging.log4j.LogManager; |
| 30 | +import org.apache.logging.log4j.Logger; |
| 31 | + |
| 32 | +import java.math.BigDecimal; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +public class NetrisApiClientImpl implements NetrisApiClient { |
| 36 | + |
| 37 | + private final Logger logger = LogManager.getLogger(getClass()); |
| 38 | + |
| 39 | + private static final ApiClient apiClient = new ApiClient(); |
| 40 | + |
| 41 | + public NetrisApiClientImpl(String endpointBaseUrl, String username, String password) { |
| 42 | + apiClient.setBasePath(endpointBaseUrl); |
| 43 | + authenticate(username, password); |
| 44 | + } |
| 45 | + |
| 46 | + private void authenticate(String username, String password) { |
| 47 | + AuthSchema authSchema = createAuthSchema(username, password); |
| 48 | + AuthenticationApi authenticationApi = new AuthenticationApi(apiClient); |
| 49 | + try { |
| 50 | + ApiResponse<AuthResponse> authResponse = authenticationApi.apiAuthPost(authSchema); |
| 51 | + if (authResponse.getStatusCode() == 200) { |
| 52 | + String cookie = authResponse.getHeaders().get("Set-Cookie").get(0).split(";")[0]; |
| 53 | + apiClient.setApiKey(cookie); |
| 54 | + apiClient.addDefaultHeader("Cookie", cookie); |
| 55 | + } else { |
| 56 | + String msg = String.format("Authentication to the Netris Controller %s failed, please check the credentials provided", apiClient.getBasePath()); |
| 57 | + logger.error(msg); |
| 58 | + throw new CloudRuntimeException(msg); |
| 59 | + } |
| 60 | + } catch (ApiException e) { |
| 61 | + String msg = String.format("Error authenticating to the Netris Controller %s: Code %s - Message: %s", apiClient.getBasePath(), e.getCode(), e.getResponseBody()); |
| 62 | + logger.error(msg, e); |
| 63 | + throw new CloudRuntimeException(msg); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private AuthSchema createAuthSchema(String username, String password) { |
| 68 | + AuthSchema authSchema = new AuthSchema(); |
| 69 | + authSchema.setUser(username); |
| 70 | + authSchema.setPassword(password); |
| 71 | + authSchema.setAuthSchemeID(new BigDecimal(1)); |
| 72 | + return authSchema; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean isSessionAlive() { |
| 77 | + AuthenticationApi api = new AuthenticationApi(apiClient); |
| 78 | + try { |
| 79 | + ApiResponse<AuthResponse> response = api.apiAuthGet(); |
| 80 | + return response.getStatusCode() == 200; |
| 81 | + } catch (ApiException e) { |
| 82 | + throw new CloudRuntimeException(e); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public List<GetSiteBody> listSites() { |
| 88 | + SitesApi api = new SitesApi(apiClient); |
| 89 | + try { |
| 90 | + SitesResponseOK response = api.apiSitesGet(); |
| 91 | + return response.getData(); |
| 92 | + } catch (ApiException e) { |
| 93 | + throw new CloudRuntimeException(e); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments