stratos-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From asankasanjaya <...@git.apache.org>
Subject [GitHub] stratos pull request: Google compute engine load balancing support
Date Mon, 17 Aug 2015 08:49:25 GMT
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.
---

Mime
View raw message