Return-Path: X-Original-To: apmail-stratos-dev-archive@minotaur.apache.org Delivered-To: apmail-stratos-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 529DC18C08 for ; Mon, 17 Aug 2015 05:12:18 +0000 (UTC) Received: (qmail 69768 invoked by uid 500); 17 Aug 2015 05:12:18 -0000 Delivered-To: apmail-stratos-dev-archive@stratos.apache.org Received: (qmail 69712 invoked by uid 500); 17 Aug 2015 05:12:18 -0000 Mailing-List: contact dev-help@stratos.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@stratos.apache.org Delivered-To: mailing list dev@stratos.apache.org Received: (qmail 69702 invoked by uid 99); 17 Aug 2015 05:12:17 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 17 Aug 2015 05:12:17 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id C79BDDFBC8; Mon, 17 Aug 2015 05:12:17 +0000 (UTC) From: swgkg To: dev@stratos.apache.org Reply-To: dev@stratos.apache.org References: In-Reply-To: Subject: [GitHub] stratos pull request: Google compute engine load balancing support Content-Type: text/plain Message-Id: <20150817051217.C79BDDFBC8@git1-us-west.apache.org> Date: Mon, 17 Aug 2015 05:12:17 +0000 (UTC) Github user swgkg commented on a diff in the pull request: https://github.com/apache/stratos/pull/433#discussion_r37158810 --- Diff: extensions/load-balancer/gce-extension/src/main/java/org/apache/stratos/gce/extension/util/GCEOperations.java --- @@ -0,0 +1,589 @@ +/* + * 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"); --- End diff -- Please use log.error(message,e) to log the exception messages. --- 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 infrastructure@apache.org or file a JIRA ticket with INFRA. ---