From e499c1c39dbea505858874ee47436641df3d93d4 Mon Sep 17 00:00:00 2001 From: Shinya Maeda Date: Sun, 1 Oct 2017 00:54:22 +0900 Subject: Replace reactive_cache by multipel sidekiq workers --- lib/google_api/auth.rb | 53 +++++++++++++++++++++++++++++++++ lib/google_api/authentication.rb | 53 --------------------------------- lib/google_api/cloud_platform/client.rb | 28 +++++++---------- 3 files changed, 63 insertions(+), 71 deletions(-) create mode 100644 lib/google_api/auth.rb delete mode 100644 lib/google_api/authentication.rb (limited to 'lib/google_api') diff --git a/lib/google_api/auth.rb b/lib/google_api/auth.rb new file mode 100644 index 00000000000..92787b87ac6 --- /dev/null +++ b/lib/google_api/auth.rb @@ -0,0 +1,53 @@ +module GoogleApi + class Auth + attr_reader :access_token, :redirect_uri, :state + + ConfigMissingError = Class.new(StandardError) + + def initialize(access_token, redirect_uri, state: nil) + @access_token = access_token + @redirect_uri = redirect_uri + @state = state + end + + def authorize_url + client.auth_code.authorize_url( + redirect_uri: redirect_uri, + scope: scope, + state: state # This is used for arbitary redirection + ) + end + + def get_token(code) + client.auth_code.get_token(code, redirect_uri: redirect_uri).token + end + + protected + + def scope + raise NotImplementedError + end + + private + + def config + Gitlab.config.omniauth.providers.find { |provider| provider.name == "google_oauth2" } + end + + def client + return @client if defined?(@client) + + unless config + raise ConfigMissingError + end + + @client = ::OAuth2::Client.new( + config.app_id, + config.app_secret, + site: 'https://accounts.google.com', + token_url: '/o/oauth2/token', + authorize_url: '/o/oauth2/auth' + ) + end + end +end diff --git a/lib/google_api/authentication.rb b/lib/google_api/authentication.rb deleted file mode 100644 index 4c9016e1085..00000000000 --- a/lib/google_api/authentication.rb +++ /dev/null @@ -1,53 +0,0 @@ -module GoogleApi - class Authentication - attr_reader :access_token, :redirect_uri, :state - - ConfigMissingError = Class.new(StandardError) - - def initialize(access_token, redirect_uri, state: nil) - @access_token = access_token - @redirect_uri = redirect_uri - @state = state - end - - def authorize_url - client.auth_code.authorize_url( - redirect_uri: redirect_uri, - scope: scope, - state: state # This is used for arbitary redirection - ) - end - - def get_token(code) - client.auth_code.get_token(code, redirect_uri: redirect_uri).token - end - - protected - - def scope - raise NotImplementedError - end - - private - - def config - Gitlab.config.omniauth.providers.find { |provider| provider.name == "google_oauth2" } - end - - def client - return @client if defined?(@client) - - unless config - raise ConfigMissingError - end - - @client = ::OAuth2::Client.new( - config.app_id, - config.app_secret, - site: 'https://accounts.google.com', - token_url: '/o/oauth2/token', - authorize_url: '/o/oauth2/auth' - ) - end - end -end diff --git a/lib/google_api/cloud_platform/client.rb b/lib/google_api/cloud_platform/client.rb index 0bc306a24e6..74ae5e16ab2 100644 --- a/lib/google_api/cloud_platform/client.rb +++ b/lib/google_api/cloud_platform/client.rb @@ -2,9 +2,9 @@ require 'google/apis/container_v1' module GoogleApi module CloudPlatform - class Client < GoogleApi::Authentication + class Client < GoogleApi::Auth class << self - def token_in_session + def session_key_for_token :cloud_platform_access_token end end @@ -13,20 +13,14 @@ module GoogleApi 'https://www.googleapis.com/auth/cloud-platform' end - ## - # Exception - # Google::Apis::ClientError: - # Google::Apis::AuthorizationError: - ## - def projects_zones_clusters_get(project_id, zone, cluster_id) service = Google::Apis::ContainerV1::ContainerService.new service.authorization = access_token begin cluster = service.get_zone_cluster(project_id, zone, cluster_id) - rescue Google::Apis::ClientError, Google::Apis::AuthorizationError => e - return nil + rescue Google::Apis::ServerError, Google::Apis::ClientError, Google::Apis::AuthorizationError => e + return e end puts "#{self.class.name} - #{__callee__}: cluster: #{cluster.inspect}" @@ -35,7 +29,7 @@ module GoogleApi # Responce exmaple # TODO: machine_type : Defailt 3.75 GB - def projects_zones_clusters_create(project_id, zone, cluster_name, cluster_size:, machine_type:) + def projects_zones_clusters_create(project_id, zone, cluster_name, cluster_size, machine_type:) service = Google::Apis::ContainerV1::ContainerService.new service.authorization = access_token @@ -50,8 +44,8 @@ module GoogleApi begin operation = service.create_cluster(project_id, zone, request_body) - rescue Google::Apis::ClientError, Google::Apis::AuthorizationError => e - return nil + rescue Google::Apis::ServerError, Google::Apis::ClientError, Google::Apis::AuthorizationError => e + return e end puts "#{self.class.name} - #{__callee__}: operation: #{operation.inspect}" @@ -65,17 +59,15 @@ module GoogleApi begin operation = service.get_zone_operation(project_id, zone, operation_id) rescue Google::Apis::ClientError, Google::Apis::AuthorizationError => e - return nil + return e end puts "#{self.class.name} - #{__callee__}: operation: #{operation.inspect}" operation end - def parse_self_link(self_link) - ret = self_link.match(/projects\/(.*)\/zones\/(.*)\/operations\/(.*)/) - - return ret[1], ret[2], ret[3] # project_id, zone, operation_id + def parse_operation_id(self_link) + self_link.match(/projects\/.*\/zones\/.*\/operations\/(.*)/)[1] end end end -- cgit v1.2.1