Skip to content

Commit

Permalink
feat: add quota project and misc to REGAPIC templates
Browse files Browse the repository at this point in the history
  • Loading branch information
viacheslav-rostovtsev committed Nov 17, 2022
1 parent 8498c78 commit edda3a3
Show file tree
Hide file tree
Showing 15 changed files with 479 additions and 206 deletions.
17 changes: 15 additions & 2 deletions gapic-generator/templates/default/service/rest/client/_client.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class <%= service.rest.client_name %>
#
# See {<%= service.rest.client_name_full %>::Configuration}
# for a description of the configuration fields.
<%- if service.is_deprecated? -%>
#
# @deprecated This service is deprecated and may be removed in the next major version update.
<%- end -%>
#
# @example
#
Expand Down Expand Up @@ -94,16 +98,25 @@ class <%= service.rest.client_name %>
# Create credentials
credentials = @config.credentials
<%- unless service.generic_endpoint? -%>
credentials ||= Credentials.default scope: @config.scope
# Use self-signed JWT if the endpoint is unchanged from default,
# but only if the default endpoint does not have a region prefix.
enable_self_signed_jwt = @config.endpoint == <%= service.client_name %>.configure.endpoint &&
!@config.endpoint.split(".").first.include?("-")
credentials ||= Credentials.default scope: @config.scope,
enable_self_signed_jwt: enable_self_signed_jwt
if credentials.is_a?(::String) || credentials.is_a?(::Hash)
credentials = Credentials.new credentials, scope: @config.scope
end
<%- end -%>


@quota_project_id = @config.quota_project
@quota_project_id ||= credentials.quota_project_id if credentials.respond_to? :quota_project_id

<%- if service.rest.subclients? -%>
<%- service.rest.subclients.each do |subclient| -%>
@<%= subclient.client_var_name %> = <%= subclient.client_class_docname %>.new do |config|
config.credentials = credentials
config.quota_project = @quota_project_id
config.endpoint = @config.endpoint
end

Expand Down
30 changes: 18 additions & 12 deletions gapic-generator/templates/default/service/rest/client/_config.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@
# on construction.
#
<%- unless method_service.rest.methods.empty? -%>
# # Examples
# @example
#
# To modify the global config, setting the timeout for all calls to 10 seconds:
# # Modify the global config, setting the timeout for
# # <%= method_service.methods.first.name %> to 20 seconds,
# # and all remaining timeouts to 10 seconds.
# <%= service.client_name_full %>.configure do |config|
# config.timeout = 10.0
# config.rpcs.<%= method_service.methods.first.name %>.timeout = 20.0
# end
#
# <%= service.client_name_full %>.configure do |config|
# config.timeout = 10.0
# end
#
# To apply the above configuration only to a new client:
#
# client = <%= service.client_name_full %>.new do |config|
# config.timeout = 10.0
# end
# # Apply the above configuration only to a new client.
# client = <%= service.client_name_full %>.new do |config|
# config.timeout = 10.0
# config.rpcs.<%= method_service.methods.first.name %>.timeout = 20.0
# end
#
<%- end -%>
# @!attribute [rw] endpoint
Expand Down Expand Up @@ -52,8 +54,11 @@
# The call timeout in seconds.
# @return [::Numeric]
# @!attribute [rw] metadata
# Additional REST headers to be sent with the call.
# Additional headers to be sent with the call.
# @return [::Hash{::Symbol=>::String}]
# @!attribute [rw] quota_project
# A separate project against which to charge quota.
# @return [::String]
#
class Configuration
extend ::Gapic::Config
Expand All @@ -68,6 +73,7 @@ class Configuration
config_attr :lib_version, nil, ::String, nil
config_attr :timeout, nil, ::Numeric, nil
config_attr :metadata, nil, ::Hash, nil
config_attr :quota_project, nil, ::String, nil

# @private
def initialize parent_config = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class <%= service.operations_name %>
credentials = Credentials.new credentials, scope: @config.scope
end

@quota_project_id = @config.quota_project
@quota_project_id ||= credentials.quota_project_id if credentials.respond_to? :quota_project_id

@<%= service.lro_service.stub_name %> = <%= service.operations_stub_name %>.new(
endpoint: @config.endpoint,
credentials: credentials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ options = ::Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h
# Customize the options with defaults
call_metadata = @config.rpcs.<%= method.name %>.metadata.to_h

# Set x-goog-api-client header
# Set x-goog-api-client and x-goog-user-project headers
call_metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
lib_name: @config.lib_name, lib_version: @config.lib_version,
gapic_version: ::<%= method.service.gem.version_name_full %>,
transports_version_send: [:rest]

call_metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id

options.apply_defaults timeout: @config.rpcs.<%= method.name %>.timeout,
metadata: call_metadata

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,22 @@ def initialize

# Create credentials
credentials = @config.credentials
credentials ||= Credentials.default scope: @config.scope
# Use self-signed JWT if the endpoint is unchanged from default,
# but only if the default endpoint does not have a region prefix.
enable_self_signed_jwt = @config.endpoint == Client.configure.endpoint &&
!@config.endpoint.split(".").first.include?("-")
credentials ||= Credentials.default scope: @config.scope,
enable_self_signed_jwt: enable_self_signed_jwt
if credentials.is_a?(::String) || credentials.is_a?(::Hash)
credentials = Credentials.new credentials, scope: @config.scope
end

@quota_project_id = @config.quota_project
@quota_project_id ||= credentials.quota_project_id if credentials.respond_to? :quota_project_id

@region_operations = ::Google::Cloud::Compute::V1::RegionOperations::Rest::Client.new do |config|
config.credentials = credentials
config.quota_project = @quota_project_id
config.endpoint = @config.endpoint
end

Expand Down Expand Up @@ -200,12 +209,14 @@ def aggregated_list request, options = nil
# Customize the options with defaults
call_metadata = @config.rpcs.aggregated_list.metadata.to_h

# Set x-goog-api-client header
# Set x-goog-api-client and x-goog-user-project headers
call_metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
lib_name: @config.lib_name, lib_version: @config.lib_version,
gapic_version: ::Google::Cloud::Compute::V1::VERSION,
transports_version_send: [:rest]

call_metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id

options.apply_defaults timeout: @config.rpcs.aggregated_list.timeout,
metadata: call_metadata

Expand Down Expand Up @@ -275,12 +286,14 @@ def delete request, options = nil
# Customize the options with defaults
call_metadata = @config.rpcs.delete.metadata.to_h

# Set x-goog-api-client header
# Set x-goog-api-client and x-goog-user-project headers
call_metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
lib_name: @config.lib_name, lib_version: @config.lib_version,
gapic_version: ::Google::Cloud::Compute::V1::VERSION,
transports_version_send: [:rest]

call_metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id

options.apply_defaults timeout: @config.rpcs.delete.timeout,
metadata: call_metadata

Expand Down Expand Up @@ -352,12 +365,14 @@ def get request, options = nil
# Customize the options with defaults
call_metadata = @config.rpcs.get.metadata.to_h

# Set x-goog-api-client header
# Set x-goog-api-client and x-goog-user-project headers
call_metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
lib_name: @config.lib_name, lib_version: @config.lib_version,
gapic_version: ::Google::Cloud::Compute::V1::VERSION,
transports_version_send: [:rest]

call_metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id

options.apply_defaults timeout: @config.rpcs.get.timeout,
metadata: call_metadata

Expand Down Expand Up @@ -426,12 +441,14 @@ def insert request, options = nil
# Customize the options with defaults
call_metadata = @config.rpcs.insert.metadata.to_h

# Set x-goog-api-client header
# Set x-goog-api-client and x-goog-user-project headers
call_metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
lib_name: @config.lib_name, lib_version: @config.lib_version,
gapic_version: ::Google::Cloud::Compute::V1::VERSION,
transports_version_send: [:rest]

call_metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id

options.apply_defaults timeout: @config.rpcs.insert.timeout,
metadata: call_metadata

Expand Down Expand Up @@ -521,12 +538,14 @@ def list request, options = nil
# Customize the options with defaults
call_metadata = @config.rpcs.list.metadata.to_h

# Set x-goog-api-client header
# Set x-goog-api-client and x-goog-user-project headers
call_metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
lib_name: @config.lib_name, lib_version: @config.lib_version,
gapic_version: ::Google::Cloud::Compute::V1::VERSION,
transports_version_send: [:rest]

call_metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id

options.apply_defaults timeout: @config.rpcs.list.timeout,
metadata: call_metadata

Expand Down Expand Up @@ -555,19 +574,21 @@ def list request, options = nil
# Configuration can be applied globally to all clients, or to a single client
# on construction.
#
# # Examples
#
# To modify the global config, setting the timeout for all calls to 10 seconds:
#
# ::Google::Cloud::Compute::V1::Addresses::Client.configure do |config|
# config.timeout = 10.0
# end
# @example
#
# To apply the above configuration only to a new client:
# # Modify the global config, setting the timeout for
# # aggregated_list to 20 seconds,
# # and all remaining timeouts to 10 seconds.
# ::Google::Cloud::Compute::V1::Addresses::Client.configure do |config|
# config.timeout = 10.0
# config.rpcs.aggregated_list.timeout = 20.0
# end
#
# client = ::Google::Cloud::Compute::V1::Addresses::Client.new do |config|
# config.timeout = 10.0
# end
# # Apply the above configuration only to a new client.
# client = ::Google::Cloud::Compute::V1::Addresses::Client.new do |config|
# config.timeout = 10.0
# config.rpcs.aggregated_list.timeout = 20.0
# end
#
# @!attribute [rw] endpoint
# The hostname or hostname:port of the service endpoint.
Expand Down Expand Up @@ -596,8 +617,11 @@ def list request, options = nil
# The call timeout in seconds.
# @return [::Numeric]
# @!attribute [rw] metadata
# Additional REST headers to be sent with the call.
# Additional headers to be sent with the call.
# @return [::Hash{::Symbol=>::String}]
# @!attribute [rw] quota_project
# A separate project against which to charge quota.
# @return [::String]
#
class Configuration
extend ::Gapic::Config
Expand All @@ -612,6 +636,7 @@ class Configuration
config_attr :lib_version, nil, ::String, nil
config_attr :timeout, nil, ::Numeric, nil
config_attr :metadata, nil, ::Hash, nil
config_attr :quota_project, nil, ::String, nil

# @private
def initialize parent_config = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,19 @@ def initialize

# Create credentials
credentials = @config.credentials
credentials ||= Credentials.default scope: @config.scope
# Use self-signed JWT if the endpoint is unchanged from default,
# but only if the default endpoint does not have a region prefix.
enable_self_signed_jwt = @config.endpoint == Client.configure.endpoint &&
!@config.endpoint.split(".").first.include?("-")
credentials ||= Credentials.default scope: @config.scope,
enable_self_signed_jwt: enable_self_signed_jwt
if credentials.is_a?(::String) || credentials.is_a?(::Hash)
credentials = Credentials.new credentials, scope: @config.scope
end

@quota_project_id = @config.quota_project
@quota_project_id ||= credentials.quota_project_id if credentials.respond_to? :quota_project_id

@global_operations_stub = ::Google::Cloud::Compute::V1::GlobalOperations::Rest::ServiceStub.new endpoint: @config.endpoint, credentials: credentials
end

Expand Down Expand Up @@ -167,12 +175,14 @@ def delete request, options = nil
# Customize the options with defaults
call_metadata = @config.rpcs.delete.metadata.to_h

# Set x-goog-api-client header
# Set x-goog-api-client and x-goog-user-project headers
call_metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
lib_name: @config.lib_name, lib_version: @config.lib_version,
gapic_version: ::Google::Cloud::Compute::V1::VERSION,
transports_version_send: [:rest]

call_metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id

options.apply_defaults timeout: @config.rpcs.delete.timeout,
metadata: call_metadata

Expand Down Expand Up @@ -233,12 +243,14 @@ def get request, options = nil
# Customize the options with defaults
call_metadata = @config.rpcs.get.metadata.to_h

# Set x-goog-api-client header
# Set x-goog-api-client and x-goog-user-project headers
call_metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
lib_name: @config.lib_name, lib_version: @config.lib_version,
gapic_version: ::Google::Cloud::Compute::V1::VERSION,
transports_version_send: [:rest]

call_metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id

options.apply_defaults timeout: @config.rpcs.get.timeout,
metadata: call_metadata

Expand Down Expand Up @@ -266,19 +278,21 @@ def get request, options = nil
# Configuration can be applied globally to all clients, or to a single client
# on construction.
#
# # Examples
#
# To modify the global config, setting the timeout for all calls to 10 seconds:
#
# ::Google::Cloud::Compute::V1::GlobalOperations::Client.configure do |config|
# config.timeout = 10.0
# end
# @example
#
# To apply the above configuration only to a new client:
# # Modify the global config, setting the timeout for
# # delete to 20 seconds,
# # and all remaining timeouts to 10 seconds.
# ::Google::Cloud::Compute::V1::GlobalOperations::Client.configure do |config|
# config.timeout = 10.0
# config.rpcs.delete.timeout = 20.0
# end
#
# client = ::Google::Cloud::Compute::V1::GlobalOperations::Client.new do |config|
# config.timeout = 10.0
# end
# # Apply the above configuration only to a new client.
# client = ::Google::Cloud::Compute::V1::GlobalOperations::Client.new do |config|
# config.timeout = 10.0
# config.rpcs.delete.timeout = 20.0
# end
#
# @!attribute [rw] endpoint
# The hostname or hostname:port of the service endpoint.
Expand Down Expand Up @@ -307,8 +321,11 @@ def get request, options = nil
# The call timeout in seconds.
# @return [::Numeric]
# @!attribute [rw] metadata
# Additional REST headers to be sent with the call.
# Additional headers to be sent with the call.
# @return [::Hash{::Symbol=>::String}]
# @!attribute [rw] quota_project
# A separate project against which to charge quota.
# @return [::String]
#
class Configuration
extend ::Gapic::Config
Expand All @@ -323,6 +340,7 @@ class Configuration
config_attr :lib_version, nil, ::String, nil
config_attr :timeout, nil, ::Numeric, nil
config_attr :metadata, nil, ::Hash, nil
config_attr :quota_project, nil, ::String, nil

# @private
def initialize parent_config = nil
Expand Down
Loading

0 comments on commit edda3a3

Please sign in to comment.