class GoogleDirectory::Connection

The GoogleDirectory, makes it easy to work with Google Directory. @since 0.1.0

@note Its important to have your oauth setup and its client_secret.json file downloaded in the root directory @note You can also use environment variables to override google defaults as wanted.

Constants

CLIENT_SECRETS_PATH

Get info the Google Cloud Admin console.cloud.google.com/apis/ or build using: developers.google.com/api-client-library/ruby/guide/aaa_client_secrets

CREDENTIALS_PATH
OOB_URI

default settings from google for all users

SCOPE

Scope options - www.googleapis.com/auth/admin.directory.user developers.google.com/admin-sdk/directory/v1/guides/authorizing

Attributes

service[R]

# answer = GoogleDirectory.(command: :user_get, attributes: {primary_email: “btihen@las.ch”}) def self.call(service: Google::Apis::AdminDirectoryV1::DirectoryService,

            app_name: nil,
            command:, attributes: {} )
new(service: service, app_name: app_name).
  run(command: command, attributes: attributes)

end

Public Class Methods

new( service: Google::Apis::AdminDirectoryV1::DirectoryService ) click to toggle source

@note make connection to google directory services @param service [Class] the default is: Google::Apis::AdminDirectoryV1::DirectoryService

# File lib/google_directory/connection.rb, line 41
def initialize( service: Google::Apis::AdminDirectoryV1::DirectoryService )
  app_name ||= ENV['APPLICATION_NAME'] || 'google_cloud_app_name'
  @service   = service.new
  @service.client_options.application_name = app_name
  @service.authorization = authorize
end

Public Instance Methods

execute( command:, attributes: {} )
Alias for: run
run( command:, attributes: {} ) click to toggle source

@note Run a command against Google Directory

@param command [Symbol] choose command to perform these include: :user_get, :user_exists? (t/f), :user_create, :user_delete, :user_update & convience commands :user_suspend, :user_reactivate, :user_change_password @param attributes [Hash] attributes needed to perform command @return [Hash] formatted as: `{success: {command: :command, attributes: {primary_email: “user@domain”}, response: GoogleAnswer} }`

# File lib/google_directory/connection.rb, line 57
def run( command:, attributes: {} )
  response  = {}
  begin
    response           = send( command, attributes: attributes )
    response[:status]  = 'success'
  rescue Google::Apis::ClientError => error
    response = {status: 'error', response: error,
                attributes: attributes, command: command,
               }
  end
  response
end
Also aliased as: execute
version() click to toggle source
# File lib/google_directory/connection.rb, line 48
def version
  VERSION
end

Private Instance Methods

authorize() click to toggle source

FROM: www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/AdminDirectoryV1/DirectoryService Ensure valid credentials, either by restoring from the saved credentials files or intitiating an OAuth2 authorization. If authorization is required, the user's default browser will be launched to approve the request. @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials

# File lib/google_directory/connection.rb, line 88
def authorize
  FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH))

  client_id   = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
  token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
  authorizer  = Google::Auth::UserAuthorizer.new( client_id, SCOPE, token_store )
  user_id = 'default'
  credentials = authorizer.get_credentials(user_id)
  if credentials.nil?
    url = authorizer.get_authorization_url(
      base_url: OOB_URI)
    puts "Open the following URL in the browser and enter the " +
         "resulting code after authorization"
    puts url
    code = gets
    credentials = authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: code, base_url: OOB_URI)
  end
  credentials
end