<commit_before>require 'fog/google'
require 'fog/compute'
require 'google/api_client'

module Fog
  module Compute
    class Google < Fog::Service

      requires :google_project
      requires :google_client_email
      requires :google_key_location

      request_path 'fog/google/requests/compute'
      request :list_servers
      request :list_disks
      request :list_firewalls
      request :list_images
      request :list_kernels
      request :list_machine_types
      request :list_networks
      request :list_operations
      request :list_zones

      request :get_server
      request :get_disk
      request :get_firewall
      request :get_image
      request :get_kernel
      request :get_machine_type
      request :get_network
      request :get_operation
      request :get_zone

      request :delete_disk
      request :delete_firewall
      request :delete_image
      request :delete_network
      request :delete_operation
      request :delete_server

      request :insert_disk
      request :insert_firewall
      request :insert_image
      request :insert_network
      request :insert_server

      model_path 'fog/google/models/compute'
      model :server
      collection :servers

      model :image
      collection :images

      model :flavor
      collection :flavors

      class Mock
        include Collections

        def initialize(options)
          Fog::Mock::not_implemented
        end

      end

      class Real
        include Collections

        attr_reader :project

        def initialize(options)
          base_url = 'https://www.googleapis.com/compute/'
          # TODO(icco): v1beta13 will probably be deprecated in April.
          api_version = 'v1beta13'
          api_scope_url = 'https://www.googleapis.com/auth/compute'

          @project = options[:google_project]
          google_client_email = options[:google_client_email]
          @api_url = base_url + api_version + '/projects/'
          key = ::Google::APIClient::KeyUtils.load_from_pkcs12(File.expand_path(options[:google_key_location]), 'notasecret')

          @client = ::Google::APIClient.new(
            :application_name => "fog",
            :application_version => Fog::VERSION
          )
          @client.authorization = Signet::OAuth2::Client.new(
            :audience => 'https://accounts.google.com/o/oauth2/token',
            :auth_provider_x509_cert_url => "https://www.googleapis.com/oauth2/v1/certs",
            :client_x509_cert_url => "https://www.googleapis.com/robot/v1/metadata/x509/#{google_client_email}",
            :issuer => google_client_email,
            :scope => api_scope_url,
            :signing_key => key,
            :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
          )
          @client.authorization.fetch_access_token!

          @compute = @client.discovered_api('compute', api_version)
          @default_network = 'default'
        end

        def build_result(api_method, parameters, body_object=nil)
          if body_object
            result = @client.execute(
              :api_method => api_method,
              :parameters => parameters,
              :body_object => body_object
            )
          else
            result = @client.execute(
              :api_method => api_method,
              :parameters => parameters
            )
          end<commit_msg>still trying to figure out 1.8.7 support<commit_after>require 'fog/google'
require 'fog/compute'
require 'google/api_client'

module Fog
  module Compute
    class Google < Fog::Service

      requires :google_project
      requires :google_client_email
      requires :google_key_location

      request_path 'fog/google/requests/compute'
      request :list_servers
      request :list_disks
      request :list_firewalls
      request :list_images
      request :list_kernels
      request :list_machine_types
      request :list_networks
      request :list_operations
      request :list_zones

      request :get_server
      request :get_disk
      request :get_firewall
      request :get_image
      request :get_kernel
      request :get_machine_type
      request :get_network
      request :get_operation
      request :get_zone

      request :delete_disk
      request :delete_firewall
      request :delete_image
      request :delete_network
      request :delete_operation
      request :delete_server

      request :insert_disk
      request :insert_firewall
      request :insert_image
      request :insert_network
      request :insert_server

      model_path 'fog/google/models/compute'
      model :server
      collection :servers

      model :image
      collection :images

      model :flavor
      collection :flavors

      class Mock
        include Collections

        def initialize(options)
          Fog::Mock::not_implemented
        end

      end

      class Real
        include Collections

        attr_reader :project

        def initialize(options)
          base_url = 'https://www.googleapis.com/compute/'
          # TODO(icco): v1beta13 will probably be deprecated in April.
          api_version = 'v1beta13'
          api_scope_url = 'https://www.googleapis.com/auth/compute'

          @project = options[:google_project]
          google_client_email = options[:google_client_email]
          @api_url = base_url + api_version + '/projects/'
          key = ::Google::APIClient::KeyUtils.load_from_pkcs12(File.expand_path(options[:google_key_location]), 'notasecret')

          @client = ::Google::APIClient.new({
            :application_name => "fog",
            :application_version => Fog::VERSION,
          })
          @client.authorization = Signet::OAuth2::Client.new(
            :audience => 'https://accounts.google.com/o/oauth2/token',
            :auth_provider_x509_cert_url => "https://www.googleapis.com/oauth2/v1/certs",
            :client_x509_cert_url => "https://www.googleapis.com/robot/v1/metadata/x509/#{google_client_email}",
            :issuer => google_client_email,
            :scope => api_scope_url,
            :signing_key => key,
            :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
          )
          @client.authorization.fetch_access_token!

          @compute = @client.discovered_api('compute', api_version)
          @default_network = 'default'
        end

        def build_result(api_method, parameters, body_object=nil)
          if body_object
            result = @client.execute(
              :api_method => api_method,
              :parameters => parameters,
              :body_object => body_object
            )
          else
            result = @client.execute(
              :api_method => api_method,
              :parameters => parameters
            )
          end
{'fraction_non_alphanumeric': 0.08560939794419971, 'fraction_numerical': 0.007195301027900147, 'mean_word_length': 2.238706609605326, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 12, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5799214', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1871, 'n_words': 377}