Skip to content

Commit

Permalink
container: migrate the API to use the Client pattern
Browse files Browse the repository at this point in the history
This is a backwards-incompatible change. This change removes the last
non-deprecated use cases for the client.WithContext and
client.NewContext functions.

Change-Id: Ib64e643fcdde50b96226d8fe219fbf40cc4d14a6
Reviewed-on: https://code-review.googlesource.com/5421
Reviewed-by: Jonathan Amsterdam <[email protected]>
  • Loading branch information
okdave committed Aug 2, 2016
1 parent 75a69ea commit f5498c3
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 31 deletions.
88 changes: 57 additions & 31 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,25 @@ package container // import "google.golang.org/cloud/container"

import (
"errors"
"net/http"
"fmt"
"time"

"golang.org/x/net/context"
raw "google.golang.org/api/container/v1"
"google.golang.org/cloud/internal"
"google.golang.org/api/option"
"google.golang.org/api/transport"
)

type Type string

var (
TypeCreate Type = Type("createCluster")
TypeDelete Type = Type("deleteCluster")
const (
TypeCreate = Type("createCluster")
TypeDelete = Type("deleteCluster")
)

type Status string

var (
const (
Done = Status("done")
Pending = Status("pending")
Running = Status("running")
Expand All @@ -46,6 +47,43 @@ var (
Stopping = Status("stopping")
)

const prodAddr = "https://container.googleapis.com/"
const userAgent = "gcloud-golang-container/20151008"

// Client is a Google Container Engine client, which may be used to manage
// clusters with a project. It must be constructed via NewClient.
type Client struct {
projectID string
svc *raw.Service
}

// NewClient creates a new Google Container Engine client.
func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*Client, error) {
o := []option.ClientOption{
option.WithEndpoint(prodAddr),
option.WithScopes(raw.CloudPlatformScope),
option.WithUserAgent(userAgent),
}
o = append(o, opts...)
httpClient, endpoint, err := transport.NewHTTPClient(ctx, o...)
if err != nil {
return nil, fmt.Errorf("dialing: %v", err)
}

svc, err := raw.New(httpClient)
if err != nil {
return nil, fmt.Errorf("constructing container client: %v", err)
}
svc.BasePath = endpoint

c := &Client{
projectID: projectID,
svc: svc,
}

return c, nil
}

// Resource is a Google Container Engine cluster resource.
type Resource struct {
// Name is the name of this cluster. The name must be unique
Expand Down Expand Up @@ -172,26 +210,24 @@ func opsFromRaw(o []*raw.Operation) []*Op {

// Clusters returns a list of cluster resources from the specified zone.
// If no zone is specified, it returns all clusters under the user project.
func Clusters(ctx context.Context, zone string) ([]*Resource, error) {
s := rawService(ctx)
func (c *Client) Clusters(ctx context.Context, zone string) ([]*Resource, error) {
if zone == "" {
resp, err := s.Projects.Zones.Clusters.List(internal.ProjID(ctx), "-").Do()
resp, err := c.svc.Projects.Zones.Clusters.List(c.projectID, "-").Do()
if err != nil {
return nil, err
}
return resourcesFromRaw(resp.Clusters), nil
}
resp, err := s.Projects.Zones.Clusters.List(internal.ProjID(ctx), zone).Do()
resp, err := c.svc.Projects.Zones.Clusters.List(c.projectID, zone).Do()
if err != nil {
return nil, err
}
return resourcesFromRaw(resp.Clusters), nil
}

// Cluster returns metadata about the specified cluster.
func Cluster(ctx context.Context, zone, name string) (*Resource, error) {
s := rawService(ctx)
resp, err := s.Projects.Zones.Clusters.Get(internal.ProjID(ctx), zone, name).Do()
func (c *Client) Cluster(ctx context.Context, zone, name string) (*Resource, error) {
resp, err := c.svc.Projects.Zones.Clusters.Get(c.projectID, zone, name).Do()
if err != nil {
return nil, err
}
Expand All @@ -200,40 +236,37 @@ func Cluster(ctx context.Context, zone, name string) (*Resource, error) {

// CreateCluster creates a new cluster with the provided metadata
// in the specified zone.
func CreateCluster(ctx context.Context, zone string, resource *Resource) (*Resource, error) {
func (c *Client) CreateCluster(ctx context.Context, zone string, resource *Resource) (*Resource, error) {
panic("not implemented")
}

// DeleteCluster deletes a cluster.
func DeleteCluster(ctx context.Context, zone, name string) error {
s := rawService(ctx)
_, err := s.Projects.Zones.Clusters.Delete(internal.ProjID(ctx), zone, name).Do()
func (c *Client) DeleteCluster(ctx context.Context, zone, name string) error {
_, err := c.svc.Projects.Zones.Clusters.Delete(c.projectID, zone, name).Do()
return err
}

// Operations returns a list of operations from the specified zone.
// If no zone is specified, it looks up for all of the operations
// that are running under the user's project.
func Operations(ctx context.Context, zone string) ([]*Op, error) {
s := rawService(ctx)
func (c *Client) Operations(ctx context.Context, zone string) ([]*Op, error) {
if zone == "" {
resp, err := s.Projects.Zones.Operations.List(internal.ProjID(ctx), "-").Do()
resp, err := c.svc.Projects.Zones.Operations.List(c.projectID, "-").Do()
if err != nil {
return nil, err
}
return opsFromRaw(resp.Operations), nil
}
resp, err := s.Projects.Zones.Operations.List(internal.ProjID(ctx), zone).Do()
resp, err := c.svc.Projects.Zones.Operations.List(c.projectID, zone).Do()
if err != nil {
return nil, err
}
return opsFromRaw(resp.Operations), nil
}

// Operation returns an operation.
func Operation(ctx context.Context, zone, name string) (*Op, error) {
s := rawService(ctx)
resp, err := s.Projects.Zones.Operations.Get(internal.ProjID(ctx), zone, name).Do()
func (c *Client) Operation(ctx context.Context, zone, name string) (*Op, error) {
resp, err := c.svc.Projects.Zones.Operations.Get(c.projectID, zone, name).Do()
if err != nil {
return nil, err
}
Expand All @@ -242,10 +275,3 @@ func Operation(ctx context.Context, zone, name string) (*Op, error) {
}
return opFromRaw(resp), nil
}

func rawService(ctx context.Context) *raw.Service {
return internal.Service(ctx, "container", func(hc *http.Client) interface{} {
svc, _ := raw.New(hc)
return svc
}).(*raw.Service)
}
96 changes: 96 additions & 0 deletions container/legacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2016 Google Inc. All Rights Reserved.
//
// 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 container

import (
"errors"
"net/http"

"golang.org/x/net/context"
raw "google.golang.org/api/container/v1"
"google.golang.org/cloud/internal"
)

// Cluster returns metadata about the specified cluster.
//
// Deprecated: please use Client.Cluster instead.
func Cluster(ctx context.Context, zone, name string) (*Resource, error) {
s := rawService(ctx)
resp, err := s.Projects.Zones.Clusters.Get(internal.ProjID(ctx), zone, name).Do()
if err != nil {
return nil, err
}
return resourceFromRaw(resp), nil
}

// CreateCluster creates a new cluster with the provided metadata
// in the specified zone.
//
// Deprecated: please use Client.CreateCluster instead.
func CreateCluster(ctx context.Context, zone string, resource *Resource) (*Resource, error) {
panic("not implemented")
}

// DeleteCluster deletes a cluster.
//
// Deprecated: please use Client.DeleteCluster instead.
func DeleteCluster(ctx context.Context, zone, name string) error {
s := rawService(ctx)
_, err := s.Projects.Zones.Clusters.Delete(internal.ProjID(ctx), zone, name).Do()
return err
}

// Operations returns a list of operations from the specified zone.
// If no zone is specified, it looks up for all of the operations
// that are running under the user's project.
//
// Deprecated: please use Client.Operations instead.
func Operations(ctx context.Context, zone string) ([]*Op, error) {
s := rawService(ctx)
if zone == "" {
resp, err := s.Projects.Zones.Operations.List(internal.ProjID(ctx), "-").Do()
if err != nil {
return nil, err
}
return opsFromRaw(resp.Operations), nil
}
resp, err := s.Projects.Zones.Operations.List(internal.ProjID(ctx), zone).Do()
if err != nil {
return nil, err
}
return opsFromRaw(resp.Operations), nil
}

// Operation returns an operation.
//
// Deprecated: please use Client.Operation instead.
func Operation(ctx context.Context, zone, name string) (*Op, error) {
s := rawService(ctx)
resp, err := s.Projects.Zones.Operations.Get(internal.ProjID(ctx), zone, name).Do()
if err != nil {
return nil, err
}
if resp.StatusMessage != "" {
return nil, errors.New(resp.StatusMessage)
}
return opFromRaw(resp), nil
}

func rawService(ctx context.Context) *raw.Service {
return internal.Service(ctx, "container", func(hc *http.Client) interface{} {
svc, _ := raw.New(hc)
return svc
}).(*raw.Service)
}

0 comments on commit f5498c3

Please sign in to comment.