<commit_before>// Copyright 2014 <NAME>, <NAME>, <NAME>, <NAME>, <NAME>
//
// Licensed 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 gce

import (
    "sync"
    "net/http"
    "io/ioutil"

    "github.com/lighthouse/beacon/structs"

    "code.google.com/p/gameboy1092-goauth2/compute/serviceaccount"
    compute "code.google.com/p/google-api-go-client/compute/v1"
)

var Driver = &structs.Driver {
    Name: "gce",
    IsApplicable: IsApplicable,
    GetVMs: GetProjectVMs,
}

func IsApplicable() bool {
    request, _ := http.NewRequest("GET", "http://metadata.google.internal/", nil)
    request.Header.Add("Metadata-Flavor", "Google")

    client := http.Client{}
    resp, err := client.Do(request)

    return err == nil && resp.StatusCode == 200
}

func GetCurrentProjectID() (string, error) {
    request, _ := http.NewRequest(
        "GET", "http://metadata.google.internal/computeMetadata/v1/project/project-id", nil)

    request.Header.Add("Metadata-Flavor", "Google")

    client := http.Client{}
    response, err := client.Do(request)
    if err != nil {
        return "", err
    }

    projectID, err := ioutil.ReadAll(response.Body)
    if err != nil {
        return "", err
    }
    response.Body.Close()

    return string(projectID), nil
}

func GetProjectVMs() []*structs.VM {
    client, _ := serviceaccount.NewClient(&serviceaccount.Options{})
    computeClient, _ := compute.New(client)

    projectID, _ := GetCurrentProjectID()

    zones, _ := computeClient.Instances.AggregatedList(projectID).Do()

    var discoveredVMs []*structs.VM

    for _, zone := range zones.Items {
        for _, instance := range zone.Instances {
            // For future reference we need figure out which network interface
            // to use instead of deafulting to the first one.
            network := instance.NetworkInterfaces[0]

            discoveredVMs = append(discoveredVMs, &structs.VM{
                Name: instance.Name,
                Address: network.NetworkIP,
                Port: "2375",
                Version: "v1",
                CanAccessDocker: false,
            })
        }
    }<commit_msg>Update gce driver with new golang gce api<commit_after>// Copyright 2014 <NAME>, <NAME>, <NAME>, <NAME>, <NAME>
//
// Licensed 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 gce

import (
    "sync"
    "net/http"
    "io/ioutil"

    "github.com/lighthouse/beacon/structs"

    "golang.org/x/oauth2"
    "golang.org/x/oauth2/google"
    "google.golang.org/cloud/compute/metadata"
    compute "google.golang.org/api/compute/v1"
)


var Driver = &structs.Driver {
    Name: "gce",
    IsApplicable: IsApplicable,
    GetVMs: GetProjectVMs,
}

func IsApplicable() bool {
    return metadata.OnGCE()
}

func GetCurrentProjectID() (string, error) {
    request, _ := http.NewRequest(
        "GET", "http://metadata.google.internal/computeMetadata/v1/project/project-id", nil)

    request.Header.Add("Metadata-Flavor", "Google")

    client := http.Client{}
    response, err := client.Do(request)
    if err != nil {
        return "", err
    }

    projectID, err := ioutil.ReadAll(response.Body)
    if err != nil {
        return "", err
    }
    response.Body.Close()

    return string(projectID), nil
}

func GetProjectVMs() []*structs.VM {
    client := &http.Client{
        Transport: &oauth2.Transport{
            Source: google.ComputeTokenSource(""),
        },
    }
    computeClient, _ := compute.New(client)

    projectID, _ := GetCurrentProjectID()

    zones, _ := computeClient.Instances.AggregatedList(projectID).Do()

    var discoveredVMs []*structs.VM

    for _, zone := range zones.Items {
        for _, instance := range zone.Instances {
            // For future reference we need figure out which network interface
            // to use instead of deafulting to the first one.
            network := instance.NetworkInterfaces[0]

            discoveredVMs = append(discoveredVMs, &structs.VM{
                Name: instance.Name,
                Address: network.NetworkIP,
                Port: "2375",
                Version: "v1",
                CanAccessDocker: false,
            })
        }
    }
{'fraction_non_alphanumeric': 0.12239784117193524, 'fraction_numerical': 0.008288357748650733, 'mean_word_length': 2.5984743411927878, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '187417', 'n_tokens_mistral': 1569, 'n_tokens_neox': 1509, 'n_words': 425}