Github user imesh commented on a diff in the pull request: https://github.com/apache/stratos/pull/419#discussion_r36939331 --- Diff: extensions/load-balancer/gce-extension/src/main/java/org/apache/stratos/gce/extension/util/GCEOperations.java --- @@ -0,0 +1,582 @@ +/* + * 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.stratos.gce.extension.util; + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.compute.Compute; +import com.google.api.services.compute.ComputeScopes; +import com.google.api.services.compute.model.*; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.stratos.gce.extension.config.GCEContext; +import org.apache.stratos.load.balancer.extension.api.exception.LoadBalancerExtensionException; + +import java.io.File; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * All the GCE API calls will be done using this class + */ +public class GCEOperations { + private static final Log log = LogFactory.getLog(GCEOperations.class); + + //project related + private static final String PROJECT_NAME = GCEContext.getInstance().getProjectName(); + private static final String PROJECT_ID = GCEContext.getInstance().getProjectID(); + private static final String REGION_NAME = GCEContext.getInstance().getRegionName(); + + //auth + private static final String KEY_FILE_PATH = GCEContext.getInstance().getKeyFilePath(); + private static final String ACCOUNT_ID = GCEContext.getInstance().getGceAccountID(); + + //health check + private static final String HEALTH_CHECK_REQUEST_PATH = GCEContext.getInstance().getHealthCheckRequestPath(); + private static final String HEALTH_CHECK_PORT = GCEContext.getInstance().getHealthCheckPort(); + private static final String HEALTH_CHECK_TIME_OUT_SEC = GCEContext.getInstance().getHealthCheckTimeOutSec(); + private static final String HEALTH_CHECK_INTERVAL_SEC = GCEContext.getInstance().getHealthCheckIntervalSec(); + private static final String HEALTH_CHECK_UNHEALTHY_THRESHOLD = GCEContext.getInstance().getHealthCheckUnhealthyThreshold(); + private static final String HEALTH_CHECK_HEALTHY_THRESHOLD = GCEContext.getInstance().getHealthCheckHealthyThreshold(); + + //a timeout for operation completion + private static final int OPERATION_TIMEOUT = Integer.parseInt(GCEContext.getInstance().getOperationTimeout()); + static Compute compute; + + /** + * Constructor for GCE Operations Class + */ + public GCEOperations() throws IOException, GeneralSecurityException { + buildComputeEngineObject(); + } + + /** + * Authorize and build compute engine object + */ + private void buildComputeEngineObject() throws IOException, GeneralSecurityException { + + try { + + if (log.isDebugEnabled()) { + log.debug("Authorizing and building the compute engine object"); + } + + HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); + + GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport) + .setJsonFactory(jsonFactory) + .setServiceAccountId(ACCOUNT_ID) + .setServiceAccountScopes(Collections.singleton(ComputeScopes.COMPUTE)) + .setServiceAccountPrivateKeyFromP12File(new File(KEY_FILE_PATH)) + .build(); + + // Create compute engine object + compute = new Compute.Builder( + httpTransport, jsonFactory, null).setApplicationName(PROJECT_NAME) + .setHttpRequestInitializer(credential).build(); + if (log.isDebugEnabled()) { + log.debug("Successfully built the compute engine object"); + } + } catch (GeneralSecurityException e) { + //Security exception occurred. Cant proceed further + log.error("Could not authenticate and build compute object"); + throw new GeneralSecurityException(e); + } catch (IOException e) { + //IO exception occurred. Cant proceed further + log.error("Could not authenticate and build compute object"); + throw new IOException(e); + } + } + + /** + * Get list of running instances in given project and zone.(This method can be used + * to check whether a given instance is available or not in a given project + * and zone) + * + * @return instanceList - list of instances(members in Stratos side) + */ + public static InstanceList getInstanceList(String zoneName, String filter) { --- End diff -- Method comment is not up to date, parameters are missing.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---