internaloption.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2020 Google LLC.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package internaloption contains options used internally by Google client code.
  5. package internaloption
  6. import (
  7. "golang.org/x/oauth2/google"
  8. "google.golang.org/api/internal"
  9. "google.golang.org/api/option"
  10. )
  11. type defaultEndpointOption string
  12. func (o defaultEndpointOption) Apply(settings *internal.DialSettings) {
  13. settings.DefaultEndpoint = string(o)
  14. }
  15. // WithDefaultEndpoint is an option that indicates the default endpoint.
  16. //
  17. // It should only be used internally by generated clients.
  18. //
  19. // This is similar to WithEndpoint, but allows us to determine whether the user has overridden the default endpoint.
  20. //
  21. // Deprecated: WithDefaultEndpoint does not support setting the universe domain.
  22. // Use WithDefaultEndpointTemplate and WithDefaultUniverseDomain to compose the
  23. // default endpoint instead.
  24. func WithDefaultEndpoint(url string) option.ClientOption {
  25. return defaultEndpointOption(url)
  26. }
  27. type defaultEndpointTemplateOption string
  28. func (o defaultEndpointTemplateOption) Apply(settings *internal.DialSettings) {
  29. settings.DefaultEndpointTemplate = string(o)
  30. }
  31. // WithDefaultEndpointTemplate provides a template for creating the endpoint
  32. // using a universe domain. See also WithDefaultUniverseDomain and
  33. // option.WithUniverseDomain. The placeholder UNIVERSE_DOMAIN should be used
  34. // instead of a concrete universe domain such as "googleapis.com".
  35. //
  36. // Example: WithDefaultEndpointTemplate("https://logging.UNIVERSE_DOMAIN/")
  37. //
  38. // It should only be used internally by generated clients.
  39. func WithDefaultEndpointTemplate(url string) option.ClientOption {
  40. return defaultEndpointTemplateOption(url)
  41. }
  42. type defaultMTLSEndpointOption string
  43. func (o defaultMTLSEndpointOption) Apply(settings *internal.DialSettings) {
  44. settings.DefaultMTLSEndpoint = string(o)
  45. }
  46. // WithDefaultMTLSEndpoint is an option that indicates the default mTLS endpoint.
  47. //
  48. // It should only be used internally by generated clients.
  49. func WithDefaultMTLSEndpoint(url string) option.ClientOption {
  50. return defaultMTLSEndpointOption(url)
  51. }
  52. // SkipDialSettingsValidation bypasses validation on ClientOptions.
  53. //
  54. // It should only be used internally.
  55. func SkipDialSettingsValidation() option.ClientOption {
  56. return skipDialSettingsValidation{}
  57. }
  58. type skipDialSettingsValidation struct{}
  59. func (s skipDialSettingsValidation) Apply(settings *internal.DialSettings) {
  60. settings.SkipValidation = true
  61. }
  62. // EnableDirectPath returns a ClientOption that overrides the default
  63. // attempt to use DirectPath.
  64. //
  65. // It should only be used internally by generated clients.
  66. // This is an EXPERIMENTAL API and may be changed or removed in the future.
  67. func EnableDirectPath(dp bool) option.ClientOption {
  68. return enableDirectPath(dp)
  69. }
  70. type enableDirectPath bool
  71. func (e enableDirectPath) Apply(o *internal.DialSettings) {
  72. o.EnableDirectPath = bool(e)
  73. }
  74. // EnableDirectPathXds returns a ClientOption that overrides the default
  75. // DirectPath type. It is only valid when DirectPath is enabled.
  76. //
  77. // It should only be used internally by generated clients.
  78. // This is an EXPERIMENTAL API and may be changed or removed in the future.
  79. func EnableDirectPathXds() option.ClientOption {
  80. return enableDirectPathXds(true)
  81. }
  82. type enableDirectPathXds bool
  83. func (x enableDirectPathXds) Apply(o *internal.DialSettings) {
  84. o.EnableDirectPathXds = bool(x)
  85. }
  86. // AllowNonDefaultServiceAccount returns a ClientOption that overrides the default
  87. // requirement for using the default service account for DirectPath.
  88. //
  89. // It should only be used internally by generated clients.
  90. // This is an EXPERIMENTAL API and may be changed or removed in the future.
  91. func AllowNonDefaultServiceAccount(nd bool) option.ClientOption {
  92. return allowNonDefaultServiceAccount(nd)
  93. }
  94. type allowNonDefaultServiceAccount bool
  95. func (a allowNonDefaultServiceAccount) Apply(o *internal.DialSettings) {
  96. o.AllowNonDefaultServiceAccount = bool(a)
  97. }
  98. // WithDefaultAudience returns a ClientOption that specifies a default audience
  99. // to be used as the audience field ("aud") for the JWT token authentication.
  100. //
  101. // It should only be used internally by generated clients.
  102. func WithDefaultAudience(audience string) option.ClientOption {
  103. return withDefaultAudience(audience)
  104. }
  105. type withDefaultAudience string
  106. func (w withDefaultAudience) Apply(o *internal.DialSettings) {
  107. o.DefaultAudience = string(w)
  108. }
  109. // WithDefaultScopes returns a ClientOption that overrides the default OAuth2
  110. // scopes to be used for a service.
  111. //
  112. // It should only be used internally by generated clients.
  113. func WithDefaultScopes(scope ...string) option.ClientOption {
  114. return withDefaultScopes(scope)
  115. }
  116. type withDefaultScopes []string
  117. func (w withDefaultScopes) Apply(o *internal.DialSettings) {
  118. o.DefaultScopes = make([]string, len(w))
  119. copy(o.DefaultScopes, w)
  120. }
  121. // WithDefaultUniverseDomain returns a ClientOption that sets the default universe domain.
  122. //
  123. // It should only be used internally by generated clients.
  124. //
  125. // This is similar to the public WithUniverse, but allows us to determine whether the user has
  126. // overridden the default universe.
  127. func WithDefaultUniverseDomain(ud string) option.ClientOption {
  128. return withDefaultUniverseDomain(ud)
  129. }
  130. type withDefaultUniverseDomain string
  131. func (w withDefaultUniverseDomain) Apply(o *internal.DialSettings) {
  132. o.DefaultUniverseDomain = string(w)
  133. }
  134. // EnableJwtWithScope returns a ClientOption that specifies if scope can be used
  135. // with self-signed JWT.
  136. //
  137. // EnableJwtWithScope is ignored when option.WithUniverseDomain is set
  138. // to a value other than the Google Default Universe (GDU) of "googleapis.com".
  139. // For non-GDU domains, token exchange is impossible and services must
  140. // support self-signed JWTs with scopes.
  141. func EnableJwtWithScope() option.ClientOption {
  142. return enableJwtWithScope(true)
  143. }
  144. type enableJwtWithScope bool
  145. func (w enableJwtWithScope) Apply(o *internal.DialSettings) {
  146. o.EnableJwtWithScope = bool(w)
  147. }
  148. // WithCredentials returns a client option to specify credentials which will be used to authenticate API calls.
  149. // This credential takes precedence over all other credential options.
  150. func WithCredentials(creds *google.Credentials) option.ClientOption {
  151. return (*withCreds)(creds)
  152. }
  153. type withCreds google.Credentials
  154. func (w *withCreds) Apply(o *internal.DialSettings) {
  155. o.InternalCredentials = (*google.Credentials)(w)
  156. }
  157. // EnableNewAuthLibrary returns a ClientOption that specifies if libraries in this
  158. // module to delegate auth to our new library. This option will be removed in
  159. // the future once all clients have been moved to the new auth layer.
  160. func EnableNewAuthLibrary() option.ClientOption {
  161. return enableNewAuthLibrary(true)
  162. }
  163. type enableNewAuthLibrary bool
  164. func (w enableNewAuthLibrary) Apply(o *internal.DialSettings) {
  165. o.EnableNewAuthLibrary = bool(w)
  166. }
  167. // EmbeddableAdapter is a no-op option.ClientOption that allow libraries to
  168. // create their own client options by embedding this type into their own
  169. // client-specific option wrapper. See example for usage.
  170. type EmbeddableAdapter struct{}
  171. func (*EmbeddableAdapter) Apply(_ *internal.DialSettings) {}