1// Copyright 2015 Google Inc. All Rights Reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package cloud 16 17import ( 18 "net/http" 19 20 "golang.org/x/oauth2" 21 "google.golang.org/cloud/internal/opts" 22 "google.golang.org/grpc" 23) 24 25// ClientOption is used when construct clients for each cloud service. 26type ClientOption interface { 27 // Resolve configures the given DialOpts for this option. 28 Resolve(*opts.DialOpt) 29} 30 31// WithTokenSource returns a ClientOption that specifies an OAuth2 token 32// source to be used as the basis for authentication. 33func WithTokenSource(s oauth2.TokenSource) ClientOption { 34 return withTokenSource{s} 35} 36 37type withTokenSource struct{ ts oauth2.TokenSource } 38 39func (w withTokenSource) Resolve(o *opts.DialOpt) { 40 o.TokenSource = w.ts 41} 42 43// WithEndpoint returns a ClientOption that overrides the default endpoint 44// to be used for a service. 45func WithEndpoint(url string) ClientOption { 46 return withEndpoint(url) 47} 48 49type withEndpoint string 50 51func (w withEndpoint) Resolve(o *opts.DialOpt) { 52 o.Endpoint = string(w) 53} 54 55// WithScopes returns a ClientOption that overrides the default OAuth2 scopes 56// to be used for a service. 57func WithScopes(scope ...string) ClientOption { 58 return withScopes(scope) 59} 60 61type withScopes []string 62 63func (w withScopes) Resolve(o *opts.DialOpt) { 64 o.Scopes = []string(w) 65} 66 67// WithUserAgent returns a ClientOption that sets the User-Agent. 68func WithUserAgent(ua string) ClientOption { 69 return withUA(ua) 70} 71 72type withUA string 73 74func (w withUA) Resolve(o *opts.DialOpt) { o.UserAgent = string(w) } 75 76// WithBaseHTTP returns a ClientOption that specifies the HTTP client to 77// use as the basis of communications. This option may only be used with 78// services that support HTTP as their communication transport. 79func WithBaseHTTP(client *http.Client) ClientOption { 80 return withBaseHTTP{client} 81} 82 83type withBaseHTTP struct{ client *http.Client } 84 85func (w withBaseHTTP) Resolve(o *opts.DialOpt) { 86 o.HTTPClient = w.client 87} 88 89// WithBaseGRPC returns a ClientOption that specifies the GRPC client 90// connection to use as the basis of communications. This option many only be 91// used with services that support HRPC as their communication transport. 92func WithBaseGRPC(client *grpc.ClientConn) ClientOption { 93 return withBaseGRPC{client} 94} 95 96type withBaseGRPC struct{ client *grpc.ClientConn } 97 98func (w withBaseGRPC) Resolve(o *opts.DialOpt) { 99 o.GRPCClient = w.client 100} 101