Add dependency and Netris API client (#4)

* Add dependency and first approach to Netris API client

* Fix authentication and create Netris API client, in progress sites listing

* Fix get sites
This commit is contained in:
Nicolas Vazquez 2024-10-10 11:03:42 -03:00 committed by GitHub
parent 9b97a2a6f6
commit c5bf82aa7b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 173 additions and 0 deletions

View File

@ -85,4 +85,7 @@ mvn install:install-file -Dfile=juniper-contrail-api-1.0-SNAPSHOT.jar -DgroupId=
# From https://github.com/radu-todirica/tungsten-api/raw/master/net/juniper/tungsten/juniper-tungsten-api/2.0/juniper-tungsten-api-2.0.jar
mvn install:install-file -Dfile=juniper-tungsten-api-2.0.jar -DgroupId=net.juniper.tungsten -DartifactId=juniper-tungsten-api -Dversion=2.0 -Dpackaging=jar
# Netris Integration
mvn install:install-file -Dfile=netris-java-sdk-1.0.0.jar -DgroupId=io.netris -DartifactId=netris-java-sdk -Dversion=1.0.0 -Dpackaging=jar
exit 0

View File

@ -30,5 +30,10 @@
<relativePath>../../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>io.netris</groupId>
<artifactId>netris-java-sdk</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,26 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.service;
import io.netris.model.GetSiteBody;
import java.util.List;
public interface NetrisApiClient {
boolean isSessionAlive();
List<GetSiteBody> listSites();
}

View File

@ -0,0 +1,96 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.service;
import com.cloud.utils.exception.CloudRuntimeException;
import io.netris.ApiClient;
import io.netris.ApiException;
import io.netris.ApiResponse;
import io.netris.api.AuthenticationApi;
import io.netris.api.SitesApi;
import io.netris.model.AuthSchema;
import io.netris.model.GetSiteBody;
import io.netris.model.SitesResponseOK;
import io.netris.model.response.AuthResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.math.BigDecimal;
import java.util.List;
public class NetrisApiClientImpl implements NetrisApiClient {
private final Logger logger = LogManager.getLogger(getClass());
private static final ApiClient apiClient = new ApiClient();
public NetrisApiClientImpl(String endpointBaseUrl, String username, String password) {
apiClient.setBasePath(endpointBaseUrl);
authenticate(username, password);
}
private void authenticate(String username, String password) {
AuthSchema authSchema = createAuthSchema(username, password);
AuthenticationApi authenticationApi = new AuthenticationApi(apiClient);
try {
ApiResponse<AuthResponse> authResponse = authenticationApi.apiAuthPost(authSchema);
if (authResponse.getStatusCode() == 200) {
String cookie = authResponse.getHeaders().get("Set-Cookie").get(0).split(";")[0];
apiClient.setApiKey(cookie);
apiClient.addDefaultHeader("Cookie", cookie);
} else {
String msg = String.format("Authentication to the Netris Controller %s failed, please check the credentials provided", apiClient.getBasePath());
logger.error(msg);
throw new CloudRuntimeException(msg);
}
} catch (ApiException e) {
String msg = String.format("Error authenticating to the Netris Controller %s: Code %s - Message: %s", apiClient.getBasePath(), e.getCode(), e.getResponseBody());
logger.error(msg, e);
throw new CloudRuntimeException(msg);
}
}
private AuthSchema createAuthSchema(String username, String password) {
AuthSchema authSchema = new AuthSchema();
authSchema.setUser(username);
authSchema.setPassword(password);
authSchema.setAuthSchemeID(new BigDecimal(1));
return authSchema;
}
@Override
public boolean isSessionAlive() {
AuthenticationApi api = new AuthenticationApi(apiClient);
try {
ApiResponse<AuthResponse> response = api.apiAuthGet();
return response.getStatusCode() == 200;
} catch (ApiException e) {
throw new CloudRuntimeException(e);
}
}
@Override
public List<GetSiteBody> listSites() {
SitesApi api = new SitesApi(apiClient);
try {
SitesResponseOK response = api.apiSitesGet();
return response.getData();
} catch (ApiException e) {
throw new CloudRuntimeException(e);
}
}
}

View File

@ -0,0 +1,43 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.service;
import io.netris.model.GetSiteBody;
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
public class NetrisApiClientImplTest {
private static final String endpointUrl = "https://shapeblue-ctl.netris.dev";
private static final String username = "netris";
private static final String password = "qHHa$CZ2oJv*@!7mwoSR";
private NetrisApiClientImpl client = new NetrisApiClientImpl(endpointUrl, username, password);
@Test
public void testNetrisAuthStatus() {
Assert.assertTrue(client.isSessionAlive());
}
@Test
public void testListSites() {
List<GetSiteBody> sites = client.listSites();
Assert.assertTrue(sites.size() > 0);
}
}