You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stratos.apache.org by asankasanjaya <gi...@git.apache.org> on 2015/08/14 09:48:32 UTC

[GitHub] stratos pull request: Google compute engine load balancing support

GitHub user asankasanjaya opened a pull request:

    https://github.com/apache/stratos/pull/433

    Google compute engine load balancing support

    

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/asankasanjaya/stratos master

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/stratos/pull/433.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #433
    
----
commit c8ea6faf30ebc0708670a86e422c0541ed063dfe
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-04T09:12:59Z

    adding gce-extension files

commit 14676ec1fb0eb47ac27b505112e9bc01493103b2
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-04T09:57:34Z

    adding gce-extension files

commit e2fa0567bced027ca7fb7eff1603a9ee7f769020
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-04T10:00:15Z

    adding gce-extension files

commit 11f114a2bc16ef3623fae81d0710a68bd716c0e6
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T04:49:14Z

    Merge branch 'master' of https://github.com/apache/stratos

commit da86337582debfa74a90bcaa089093df07b54065
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T04:49:59Z

    adding a test method

commit b536600da14f1c962bca2065349ab2872b88b6ce
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T06:00:02Z

    fixing zone name error

commit a109bc39081f3bcb077c421e48399174edf072f6
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T06:22:36Z

    adding create target pool method

commit 410927b8ed77890c9311008c9831da4b23313873
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T07:00:56Z

    adding create target pool method

commit 3553eae7744ace49c31cc8b9b604865de3779a68
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T07:05:49Z

    adding create target pool method

commit 1b1d3133d9b43ef8b0a47af771f9f264aef07e09
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T07:46:25Z

    adding create target pool method

commit 4a296f5d8e23ca18f465e508cab1336aec860018
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T07:49:47Z

    adding create target pool method

commit 59713f6b040240b0c49be4296c42a49fb86a0aa4
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T07:53:03Z

    adding create target pool method

commit 6f036e0346c8b266d722fb44e07df57a3739e710
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T08:21:55Z

    adding create target pool method

commit df21f52f9d57e927d8241e15f804dd7a2ddb7932
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T08:29:22Z

    adding create target pool method

commit 658aaf1267ee367d2c568a0adbb0324976208027
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T08:34:34Z

    adding create target pool method

commit 41574cd4f1557365fd63a67cd763fcca5e0bbe76
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T08:43:03Z

    adding create target pool method

commit 5ce548c134ed89c13b9e4052bfaf669a54b2b2a6
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T08:47:36Z

    adding create target pool method

commit 30ce224228cd8ca40ac07fa411dae1c6aac9e7d9
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T08:49:13Z

    adding create target pool method

commit b987c4545c4b9382883ce271afa8e7c6138454fe
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T08:51:35Z

    adding create target pool method

commit 4f0b334676b7f598014a904d02f6fae8e6b46939
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T08:53:33Z

    adding create target pool method

commit c3dc11bd69dfd89eb1d09fd5a8dc2beb6463c480
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T09:04:13Z

    adding create target pool method

commit 88df9299b26ec6bfb9d6dea4a79e39680ce752da
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T09:07:06Z

    adding create target pool method

commit 9cee960e8e3e221e227515247c8e628c1169ec3c
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T09:21:47Z

    adding create target pool method

commit e12036e460c31fd18d3b384006d382f40df05f00
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T09:23:24Z

    adding create target pool method

commit 6457e31ffed3c24e65a4b4eb31dd21725a84bb07
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T09:41:29Z

    changes in gce operations class

commit 8e248793642af0e3d09bdaed819131b7e55575d7
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T09:42:50Z

    changes in gce operations class

commit 8f11bf008a0301f4adf882aa8b5f670b7e68e67b
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T09:49:37Z

    changes in gce operations class

commit 7a4033da2eddb838b9f0b6e74725ffba232b62e1
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T09:54:02Z

    changes in gce operations class

commit 0d345e7b686891cc6872dc9ff607715e25ea921f
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T10:09:54Z

    changes in gce operations class

commit 1dd8cd15af554ce56adcd80ae153396357736468
Author: asanka sanjaya <an...@gmail.com>
Date:   2015-06-06T10:14:41Z

    changes in gce operations class

----


---
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.
---

[GitHub] stratos pull request: Google compute engine load balancing support

Posted by asankasanjaya <gi...@git.apache.org>.
Github user asankasanjaya commented on the pull request:

    https://github.com/apache/stratos/pull/433#issuecomment-147193032
  
    I have changed the logic for health check creation. So health checks are created using the port map configuration related to the application instead of using the user provided port.


---
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.
---

[GitHub] stratos pull request: Google compute engine load balancing support

Posted by swgkg <gi...@git.apache.org>.
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.
---

[GitHub] stratos pull request: Google compute engine load balancing support

Posted by swgkg <gi...@git.apache.org>.
Github user swgkg commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/433#discussion_r37158573
  
    --- 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");
    +            throw new IOException(e);
    +        }
    +    }
    +
    +    /**
    +     * Get list of instances in given project and zone which matches the given filter
    +     *
    +     * @param zoneName - name of the zone
    +     * @param filter - a condition to filter the instances
    +     * @return instanceList - list of instances(members in Stratos side)
    +     */
    +    public static InstanceList getInstanceList(String zoneName, String filter) {
    +        Compute.Instances.List instances;
    +        try {
    +            instances = compute.instances().
    +                    list(PROJECT_ID, zoneName).setFilter(filter);
    +            InstanceList instanceList = instances.execute();
    +            if (instanceList.getItems() == null) {
    +                log.warn("No instances found for filter " + filter + " and zone " + zoneName);
    +                return null;
    +            } else {
    +                return instanceList;
    +            }
    +        } catch (IOException e) {
    +            log.error("Could not get instance list from GCE ", e);
    +            return null;
    +        }
    +    }
    +
    +
    +    /**
    +     * Get instance resource URL from given instance name
    +     *
    +     * @param instanceId - Id of the instance provided by IaaS
    +     * @return - Resource URL of the instance in IaaS
    +     */
    +    public static String getInstanceURLFromId(String instanceId) {
    +
    +        String instanceURL;
    +        String zoneName = getZoneNameFromInstanceId(instanceId);
    +        String filter = "name eq " + getInstanceNameFromInstanceId(instanceId);
    +        //check whether the given instance is available
    +        InstanceList instanceList = getInstanceList(zoneName, filter);
    +        if (instanceList == null) {
    +            log.warn("No matching instance found for filter " + filter);
    +            return null;
    +        }
    +        for (Instance instance : instanceList.getItems()) {
    +            String instanceIdInIaaS = zoneName + "/" + instance.getName();
    +            if (instanceIdInIaaS.equals(instanceId)) {
    +                //instance is available
    +                //getInstance URL
    +                instanceURL = instance.getSelfLink();
    +                return instanceURL;
    +            }
    +        }
    +        log.warn("No matching instance found for filter " + filter);
    +        return null;
    +    }
    +
    +    /**
    +     * Get a given health check resource URL
    +     *
    +     * @param healthCheckName - name of the health check in IaaS
    +     * @return - Resource URL of health check in IaaS side
    +     */
    +    public static String getHealthCheckURLFromName(String healthCheckName) {
    +
    +        String healthCheckURL;
    +
    +        //check whether the given instance is available
    +        HttpHealthCheckList healthCheckList;
    +        healthCheckList = getHealthCheckList();
    +        if (healthCheckList == null) {
    +            log.warn("Could not found health check " + healthCheckName + " because the health check list is null");
    +            return null;
    +        }
    +        for (HttpHealthCheck httpHealthCheck : healthCheckList.getItems()) {
    +            if (httpHealthCheck.getName().equals(healthCheckName)) {
    +                //instance is available
    +                //getInstance URL
    +                healthCheckURL = httpHealthCheck.getSelfLink();
    +                return healthCheckURL;
    +            }
    +        }
    +        log.warn("Could not found the health check " + healthCheckName);
    +        return null;
    +    }
    +
    +    /**
    +     * Get list of health checks
    +     *
    +     * @return - list of health checks
    +     */
    +    private static HttpHealthCheckList getHealthCheckList() {
    +
    +        Compute.HttpHealthChecks.List healthChecks;
    +        try {
    +            healthChecks = compute.httpHealthChecks().list(PROJECT_ID);
    +            HttpHealthCheckList healthCheckList = healthChecks.execute();
    +            if (healthCheckList.getItems() == null) {
    +                log.info("No health check found for specified project");
    +                return null;
    +            } else {
    +                return healthCheckList;
    +            }
    +        } catch (IOException e) {
    +            log.error("Could not get health check list from GCE", e);
    +            return null;
    +        }
    +    }
    +
    +    /**
    +     * Manually create the self link for an instance. This method is useful when the instance is deleted from IaaS
    +     *
    +     * @return - the self link of the instance
    +     */
    +    private static String createInstanceSelfLink(String instanceId) {
    +        return "https://www.googleapis.com/compute/v1/projects/" + PROJECT_ID +
    +                "/zones/" + getZoneNameFromInstanceId(instanceId) + "/instances/" + getInstanceNameFromInstanceId(instanceId);
    +    }
    +
    --- End diff --
    
    Shall we read the google api url from the configuration?


---
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.
---

[GitHub] stratos pull request: Google compute engine load balancing support

Posted by asankasanjaya <gi...@git.apache.org>.
Github user asankasanjaya commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/433#discussion_r37168335
  
    --- 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 --
    
    fixed it


---
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.
---

[GitHub] stratos pull request: Google compute engine load balancing support

Posted by asankasanjaya <gi...@git.apache.org>.
Github user asankasanjaya commented on a diff in the pull request:

    https://github.com/apache/stratos/pull/433#discussion_r37168330
  
    --- 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");
    +            throw new IOException(e);
    +        }
    +    }
    +
    +    /**
    +     * Get list of instances in given project and zone which matches the given filter
    +     *
    +     * @param zoneName - name of the zone
    +     * @param filter - a condition to filter the instances
    +     * @return instanceList - list of instances(members in Stratos side)
    +     */
    +    public static InstanceList getInstanceList(String zoneName, String filter) {
    +        Compute.Instances.List instances;
    +        try {
    +            instances = compute.instances().
    +                    list(PROJECT_ID, zoneName).setFilter(filter);
    +            InstanceList instanceList = instances.execute();
    +            if (instanceList.getItems() == null) {
    +                log.warn("No instances found for filter " + filter + " and zone " + zoneName);
    +                return null;
    +            } else {
    +                return instanceList;
    +            }
    +        } catch (IOException e) {
    +            log.error("Could not get instance list from GCE ", e);
    +            return null;
    +        }
    +    }
    +
    +
    +    /**
    +     * Get instance resource URL from given instance name
    +     *
    +     * @param instanceId - Id of the instance provided by IaaS
    +     * @return - Resource URL of the instance in IaaS
    +     */
    +    public static String getInstanceURLFromId(String instanceId) {
    +
    +        String instanceURL;
    +        String zoneName = getZoneNameFromInstanceId(instanceId);
    +        String filter = "name eq " + getInstanceNameFromInstanceId(instanceId);
    +        //check whether the given instance is available
    +        InstanceList instanceList = getInstanceList(zoneName, filter);
    +        if (instanceList == null) {
    +            log.warn("No matching instance found for filter " + filter);
    +            return null;
    +        }
    +        for (Instance instance : instanceList.getItems()) {
    +            String instanceIdInIaaS = zoneName + "/" + instance.getName();
    +            if (instanceIdInIaaS.equals(instanceId)) {
    +                //instance is available
    +                //getInstance URL
    +                instanceURL = instance.getSelfLink();
    +                return instanceURL;
    +            }
    +        }
    +        log.warn("No matching instance found for filter " + filter);
    +        return null;
    +    }
    +
    +    /**
    +     * Get a given health check resource URL
    +     *
    +     * @param healthCheckName - name of the health check in IaaS
    +     * @return - Resource URL of health check in IaaS side
    +     */
    +    public static String getHealthCheckURLFromName(String healthCheckName) {
    +
    +        String healthCheckURL;
    +
    +        //check whether the given instance is available
    +        HttpHealthCheckList healthCheckList;
    +        healthCheckList = getHealthCheckList();
    +        if (healthCheckList == null) {
    +            log.warn("Could not found health check " + healthCheckName + " because the health check list is null");
    +            return null;
    +        }
    +        for (HttpHealthCheck httpHealthCheck : healthCheckList.getItems()) {
    +            if (httpHealthCheck.getName().equals(healthCheckName)) {
    +                //instance is available
    +                //getInstance URL
    +                healthCheckURL = httpHealthCheck.getSelfLink();
    +                return healthCheckURL;
    +            }
    +        }
    +        log.warn("Could not found the health check " + healthCheckName);
    +        return null;
    +    }
    +
    +    /**
    +     * Get list of health checks
    +     *
    +     * @return - list of health checks
    +     */
    +    private static HttpHealthCheckList getHealthCheckList() {
    +
    +        Compute.HttpHealthChecks.List healthChecks;
    +        try {
    +            healthChecks = compute.httpHealthChecks().list(PROJECT_ID);
    +            HttpHealthCheckList healthCheckList = healthChecks.execute();
    +            if (healthCheckList.getItems() == null) {
    +                log.info("No health check found for specified project");
    +                return null;
    +            } else {
    +                return healthCheckList;
    +            }
    +        } catch (IOException e) {
    +            log.error("Could not get health check list from GCE", e);
    +            return null;
    +        }
    +    }
    +
    +    /**
    +     * Manually create the self link for an instance. This method is useful when the instance is deleted from IaaS
    +     *
    +     * @return - the self link of the instance
    +     */
    +    private static String createInstanceSelfLink(String instanceId) {
    +        return "https://www.googleapis.com/compute/v1/projects/" + PROJECT_ID +
    +                "/zones/" + getZoneNameFromInstanceId(instanceId) + "/instances/" + getInstanceNameFromInstanceId(instanceId);
    +    }
    +
    --- End diff --
    
    fixed it


---
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.
---