github.com/coreos/mantle@v0.13.0/platform/api/gcloud/api.go (about)

     1  // Copyright 2015 CoreOS, Inc.
     2  // Copyright 2015 The Go Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package gcloud
    17  
    18  import (
    19  	"fmt"
    20  	"io/ioutil"
    21  	"net/http"
    22  	"strings"
    23  	"time"
    24  
    25  	"github.com/coreos/pkg/capnslog"
    26  	"google.golang.org/api/compute/v1"
    27  
    28  	"github.com/coreos/mantle/auth"
    29  	"github.com/coreos/mantle/platform"
    30  )
    31  
    32  var (
    33  	plog = capnslog.NewPackageLogger("github.com/coreos/mantle", "platform/api/gcloud")
    34  )
    35  
    36  type Options struct {
    37  	Image       string
    38  	Project     string
    39  	Zone        string
    40  	MachineType string
    41  	DiskType    string
    42  	Network     string
    43  	JSONKeyFile string
    44  	ServiceAuth bool
    45  	*platform.Options
    46  }
    47  
    48  type API struct {
    49  	client  *http.Client
    50  	compute *compute.Service
    51  	options *Options
    52  }
    53  
    54  func New(opts *Options) (*API, error) {
    55  	const endpointPrefix = "https://www.googleapis.com/compute/v1/"
    56  
    57  	// If the image name isn't a full api endpoint accept a name beginning
    58  	// with "projects/" to specify a different project from the instance.
    59  	// Also accept a short name and use instance project.
    60  	if strings.HasPrefix(opts.Image, "projects/") {
    61  		opts.Image = endpointPrefix + opts.Image
    62  	} else if !strings.Contains(opts.Image, "/") {
    63  		opts.Image = fmt.Sprintf("%sprojects/%s/global/images/%s", endpointPrefix, opts.Project, opts.Image)
    64  	} else if !strings.HasPrefix(opts.Image, endpointPrefix) {
    65  		return nil, fmt.Errorf("GCE Image argument must be the full api endpoint, begin with 'projects/', or use the short name")
    66  	}
    67  
    68  	var (
    69  		client *http.Client
    70  		err    error
    71  	)
    72  
    73  	if opts.ServiceAuth {
    74  		client = auth.GoogleServiceClient()
    75  	} else if opts.JSONKeyFile != "" {
    76  		b, err := ioutil.ReadFile(opts.JSONKeyFile)
    77  		if err != nil {
    78  			plog.Fatal(err)
    79  		}
    80  		client, err = auth.GoogleClientFromJSONKey(b)
    81  	} else {
    82  		client, err = auth.GoogleClient()
    83  	}
    84  
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	capi, err := compute.New(client)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  
    94  	api := &API{
    95  		client:  client,
    96  		compute: capi,
    97  		options: opts,
    98  	}
    99  
   100  	return api, nil
   101  }
   102  
   103  func (a *API) Client() *http.Client {
   104  	return a.client
   105  }
   106  
   107  func (a *API) GC(gracePeriod time.Duration) error {
   108  	return a.gcInstances(gracePeriod)
   109  }