mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[google|sql] Add support for Operations
- Add models, requests and tests for Operations
This commit is contained in:
parent
716f474545
commit
3a9a63bf14
8 changed files with 341 additions and 0 deletions
61
lib/fog/google/models/sql/operation.rb
Normal file
61
lib/fog/google/models/sql/operation.rb
Normal file
|
@ -0,0 +1,61 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Google
|
||||
class SQL
|
||||
##
|
||||
# An Operation resource contains information about database instance operations
|
||||
# such as create, delete, and restart
|
||||
#
|
||||
# @see https://developers.google.com/cloud-sql/docs/admin-api/v1beta3/operations
|
||||
class Operation < Fog::Model
|
||||
identity :operation
|
||||
|
||||
attribute :end_time, :aliases => 'endTime'
|
||||
attribute :enqueued_time, :aliases => 'enqueuedTime'
|
||||
attribute :error
|
||||
attribute :export_context, :aliases => 'exportContext'
|
||||
attribute :import_context, :aliases => 'importContext'
|
||||
attribute :instance
|
||||
attribute :kind
|
||||
attribute :operation_type, :aliases => 'operationType'
|
||||
attribute :start_time, :aliases => 'startTime'
|
||||
attribute :state
|
||||
attribute :user_email_address, :aliases => 'userEmailAddress'
|
||||
|
||||
DONE_STATE = 'DONE'
|
||||
PENDING_STATE = 'PENDING'
|
||||
RUNNING_STATE = 'RUNNING'
|
||||
UNKNOWN_STATE = 'UNKNOWN'
|
||||
|
||||
##
|
||||
# Checks if the instance operation is pending
|
||||
#
|
||||
# @return [Boolean] True if the operation is pending; False otherwise
|
||||
def pending?
|
||||
self.state == PENDING_STATE
|
||||
end
|
||||
|
||||
##
|
||||
# Checks if the instance operation is done
|
||||
#
|
||||
# @return [Boolean] True if the operation is done; False otherwise
|
||||
def ready?
|
||||
self.state == DONE_STATE
|
||||
end
|
||||
|
||||
##
|
||||
# Reloads an instance operation
|
||||
#
|
||||
# @return [Fog::Google::SQL::Operation] Instance operation resource
|
||||
def reload
|
||||
requires :identity
|
||||
|
||||
data = collection.get(self.instance, self.identity)
|
||||
merge_attributes(data.attributes)
|
||||
self
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
49
lib/fog/google/models/sql/operations.rb
Normal file
49
lib/fog/google/models/sql/operations.rb
Normal file
|
@ -0,0 +1,49 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/google/models/sql/operation'
|
||||
|
||||
module Fog
|
||||
module Google
|
||||
class SQL
|
||||
class Operations < Fog::Collection
|
||||
model Fog::Google::SQL::Operation
|
||||
|
||||
##
|
||||
# Lists all instance operations that have been performed on the given instance
|
||||
#
|
||||
# @param [String] instance_id Instance ID
|
||||
# @return [Array<Fog::Google::SQL::Operation>] List of instance operation resources
|
||||
def all(instance_id)
|
||||
data = []
|
||||
begin
|
||||
data = service.list_operations(instance_id).body['items'] || []
|
||||
rescue Fog::Errors::Error => e
|
||||
# Google SQL returns a 403 if we try to access a non-existing resource
|
||||
# The default behaviour in Fog is to return an empty Array
|
||||
raise e unless e.message == 'The client is not authorized to make this request.'
|
||||
end
|
||||
|
||||
load(data)
|
||||
end
|
||||
|
||||
##
|
||||
# Retrieves an instance operation that has been performed on an instance
|
||||
#
|
||||
# @param [String] instance_id Instance ID
|
||||
# @param [String] operation_id Instance operation ID
|
||||
# @return [Fog::Google::SQL::Operation] Instance operation resource
|
||||
def get(instance_id, operation_id)
|
||||
if operation = service.get_operation(instance_id, operation_id).body
|
||||
new(operation)
|
||||
end
|
||||
rescue Fog::Errors::NotFound
|
||||
nil
|
||||
rescue Fog::Errors::Error => e
|
||||
# Google SQL returns a 403 if we try to access a non-existing resource
|
||||
# The default behaviour in Fog is to return a nil
|
||||
return nil if e.message == 'The client is not authorized to make this request.'
|
||||
raise e
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
66
lib/fog/google/requests/sql/get_operation.rb
Normal file
66
lib/fog/google/requests/sql/get_operation.rb
Normal file
|
@ -0,0 +1,66 @@
|
|||
module Fog
|
||||
module Google
|
||||
class SQL
|
||||
##
|
||||
# Retrieves an instance operation that has been performed on an instance
|
||||
#
|
||||
# @see https://developers.google.com/cloud-sql/docs/admin-api/v1beta3/operations/get
|
||||
|
||||
class Real
|
||||
def get_operation(instance_id, operation_id)
|
||||
api_method = @sql.operations.get
|
||||
parameters = {
|
||||
'project' => @project,
|
||||
'instance' => instance_id,
|
||||
'operation' => operation_id,
|
||||
}
|
||||
|
||||
request(api_method, parameters)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def get_operation(instance_id, operation_id)
|
||||
if self.data[:operations].has_key?(instance_id)
|
||||
if self.data[:operations][instance_id].has_key?(operation_id)
|
||||
body = self.data[:operations][instance_id][operation_id]
|
||||
status = 200
|
||||
else
|
||||
body = {
|
||||
'error' => {
|
||||
'errors' => [
|
||||
{
|
||||
'domain' => 'global',
|
||||
'reason' => 'operationDoesNotExist',
|
||||
'message' => 'The Cloud SQL instance operation does not exist.',
|
||||
}
|
||||
],
|
||||
'code' => 404,
|
||||
'message' => 'The Cloud SQL instance operation does not exist.',
|
||||
}
|
||||
}
|
||||
status = 404
|
||||
end
|
||||
else
|
||||
body = {
|
||||
'error' => {
|
||||
'errors' => [
|
||||
{
|
||||
'domain' => 'global',
|
||||
'reason' => 'notAuthorized',
|
||||
'message' => 'The client is not authorized to make this request.',
|
||||
}
|
||||
],
|
||||
'code' => 403,
|
||||
'message' => 'The client is not authorized to make this request.',
|
||||
}
|
||||
}
|
||||
status = 403
|
||||
end
|
||||
|
||||
build_excon_response(body, status)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
52
lib/fog/google/requests/sql/list_operations.rb
Normal file
52
lib/fog/google/requests/sql/list_operations.rb
Normal file
|
@ -0,0 +1,52 @@
|
|||
module Fog
|
||||
module Google
|
||||
class SQL
|
||||
##
|
||||
# Lists all instance operations that have been performed on the given Cloud SQL instance
|
||||
# in the reverse chronological order of the start time
|
||||
#
|
||||
# @see https://developers.google.com/cloud-sql/docs/admin-api/v1beta3/operations/list
|
||||
|
||||
class Real
|
||||
def list_operations(instance_id)
|
||||
api_method = @sql.operations.list
|
||||
parameters = {
|
||||
'project' => @project,
|
||||
'instance' => instance_id,
|
||||
}
|
||||
|
||||
request(api_method, parameters)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def list_operations(instance_id)
|
||||
if self.data[:operations].has_key?(instance_id)
|
||||
body = {
|
||||
'kind' => 'sql#operationsList',
|
||||
'items' => self.data[:operations][instance_id].values,
|
||||
}
|
||||
status = 200
|
||||
else
|
||||
body = {
|
||||
'error' => {
|
||||
'errors' => [
|
||||
{
|
||||
'domain' => 'global',
|
||||
'reason' => 'notAuthorized',
|
||||
'message' => 'The client is not authorized to make this request.',
|
||||
}
|
||||
],
|
||||
'code' => 403,
|
||||
'message' => 'The client is not authorized to make this request.',
|
||||
}
|
||||
}
|
||||
status = 403
|
||||
end
|
||||
|
||||
build_excon_response(body, status)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -20,6 +20,10 @@ module Fog
|
|||
model :flag
|
||||
collection :flags
|
||||
|
||||
# Operation
|
||||
model :operation
|
||||
collection :operations
|
||||
|
||||
# Tier
|
||||
model :tier
|
||||
collection :tiers
|
||||
|
@ -31,6 +35,10 @@ module Fog
|
|||
# Flag
|
||||
request :list_flags
|
||||
|
||||
# Operation
|
||||
request :get_operation
|
||||
request :list_operations
|
||||
|
||||
# Tier
|
||||
request :list_tiers
|
||||
|
||||
|
|
25
tests/google/models/sql/operation_tests.rb
Normal file
25
tests/google/models/sql/operation_tests.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
Shindo.tests('Fog::Google[:sql] | operation model', ['google']) do
|
||||
@instance = Fog::Google[:sql].instances.create(:instance => Fog::Mock.random_letters(16), :tier => 'D1')
|
||||
@instance.wait_for { ready? }
|
||||
@operations = Fog::Google[:sql].operations
|
||||
@operation = @operations.all(@instance.instance).first
|
||||
|
||||
tests('success') do
|
||||
|
||||
tests('#pending?').succeeds do
|
||||
@operation.pending? == false
|
||||
end
|
||||
|
||||
tests('#ready?').succeeds do
|
||||
@operation.ready? == true
|
||||
end
|
||||
|
||||
tests('#reload').succeeds do
|
||||
@operation.reload
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@instance.destroy
|
||||
|
||||
end
|
38
tests/google/models/sql/operations_tests.rb
Normal file
38
tests/google/models/sql/operations_tests.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
Shindo.tests('Fog::Google[:sql] | operations model', ['google']) do
|
||||
@instance = Fog::Google[:sql].instances.create(:instance => Fog::Mock.random_letters(16), :tier => 'D1')
|
||||
@instance.wait_for { ready? }
|
||||
@operations = Fog::Google[:sql].operations
|
||||
|
||||
tests('success') do
|
||||
|
||||
tests('#all').succeeds do
|
||||
@operations.all(@instance.instance)
|
||||
end
|
||||
|
||||
tests('#get').succeeds do
|
||||
@operations.get(@instance.instance, @operations.all(@instance.instance).first.operation)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
tests('failure') do
|
||||
|
||||
tests('#all').returns([]) do
|
||||
@operations.all(Fog::Mock.random_letters_and_numbers(16))
|
||||
end
|
||||
|
||||
tests('#get').returns(nil) do
|
||||
pending unless Fog.mocking? # Real test fails on google-api-client (mismatch between catalog and real response)
|
||||
@operations.get(@instance.instance, Fog::Mock.random_letters_and_numbers(16))
|
||||
end
|
||||
|
||||
tests('#get').returns(nil) do
|
||||
pending unless Fog.mocking? # Real test fails on google-api-client (mismatch between catalog and real response)
|
||||
@operations.get(Fog::Mock.random_letters_and_numbers(16), Fog::Mock.random_letters_and_numbers(16))
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@instance.destroy
|
||||
|
||||
end
|
42
tests/google/requests/sql/operation_tests.rb
Normal file
42
tests/google/requests/sql/operation_tests.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
Shindo.tests('Fog::Google[:sql] | operation requests', ['google']) do
|
||||
@sql = Fog::Google[:sql]
|
||||
@instance_id = Fog::Mock.random_letters(16)
|
||||
@instance = @sql.instances.create(:instance => @instance_id, :tier => 'D1')
|
||||
@instance.wait_for { ready? }
|
||||
|
||||
@get_operation_format = {
|
||||
'operation' => String,
|
||||
'endTime' => Fog::Nullable::String,
|
||||
'enqueuedTime' => String,
|
||||
'error' => Fog::Nullable::Array,
|
||||
'exportContext' => Fog::Nullable::Array,
|
||||
'importContext' => Fog::Nullable::Array,
|
||||
'instance' => String,
|
||||
'kind' => String,
|
||||
'operationType' => String,
|
||||
'startTime' => Fog::Nullable::String,
|
||||
'state' => String,
|
||||
'userEmailAddress' => String,
|
||||
}
|
||||
|
||||
@list_operations_format = {
|
||||
'kind' => String,
|
||||
'items' => [@get_operation_format],
|
||||
}
|
||||
|
||||
tests('success') do
|
||||
|
||||
tests('#list_operations').formats(@list_operations_format) do
|
||||
@sql.list_operations(@instance_id).body
|
||||
end
|
||||
|
||||
tests('#get_operation').formats(@get_operation_format) do
|
||||
operation_id = @sql.operations.all(@instance_id).first.operation
|
||||
@sql.get_operation(@instance_id, operation_id).body
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@instance.destroy
|
||||
|
||||
end
|
Loading…
Reference in a new issue