fog-google 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/README.md +7 -3
  4. data/examples/pubsub/subscriptions.rb +54 -0
  5. data/examples/pubsub/topics.rb +33 -0
  6. data/fog-google.gemspec +2 -2
  7. data/lib/fog/compute/google.rb +4 -884
  8. data/lib/fog/compute/google/mock.rb +871 -0
  9. data/lib/fog/compute/google/real.rb +22 -0
  10. data/lib/fog/dns/google.rb +3 -42
  11. data/lib/fog/dns/google/mock.rb +33 -0
  12. data/lib/fog/dns/google/real.rb +19 -0
  13. data/lib/fog/google.rb +14 -208
  14. data/lib/fog/google/mock.rb +14 -0
  15. data/lib/fog/google/models/pubsub/received_message.rb +40 -0
  16. data/lib/fog/google/models/pubsub/subscription.rb +86 -0
  17. data/lib/fog/google/models/pubsub/subscriptions.rb +32 -0
  18. data/lib/fog/google/models/pubsub/topic.rb +72 -0
  19. data/lib/fog/google/models/pubsub/topics.rb +31 -0
  20. data/lib/fog/google/monitoring.rb +3 -45
  21. data/lib/fog/google/monitoring/mock.rb +35 -0
  22. data/lib/fog/google/monitoring/real.rb +20 -0
  23. data/lib/fog/google/pubsub.rb +59 -0
  24. data/lib/fog/google/pubsub/mock.rb +34 -0
  25. data/lib/fog/google/pubsub/real.rb +20 -0
  26. data/lib/fog/google/requests/pubsub/acknowledge_subscription.rb +46 -0
  27. data/lib/fog/google/requests/pubsub/create_subscription.rb +57 -0
  28. data/lib/fog/google/requests/pubsub/create_topic.rb +36 -0
  29. data/lib/fog/google/requests/pubsub/delete_subscription.rb +28 -0
  30. data/lib/fog/google/requests/pubsub/delete_topic.rb +29 -0
  31. data/lib/fog/google/requests/pubsub/get_subscription.rb +44 -0
  32. data/lib/fog/google/requests/pubsub/get_topic.rb +41 -0
  33. data/lib/fog/google/requests/pubsub/list_subscriptions.rb +39 -0
  34. data/lib/fog/google/requests/pubsub/list_topics.rb +33 -0
  35. data/lib/fog/google/requests/pubsub/publish_topic.rb +61 -0
  36. data/lib/fog/google/requests/pubsub/pull_subscription.rb +77 -0
  37. data/lib/fog/google/shared.rb +191 -0
  38. data/lib/fog/google/sql.rb +3 -50
  39. data/lib/fog/google/sql/mock.rb +40 -0
  40. data/lib/fog/google/sql/real.rb +20 -0
  41. data/lib/fog/google/version.rb +1 -1
  42. data/lib/fog/storage/google_json.rb +4 -99
  43. data/lib/fog/storage/google_json/mock.rb +18 -0
  44. data/lib/fog/storage/google_json/real.rb +64 -0
  45. data/lib/fog/storage/google_json/utils.rb +32 -0
  46. data/lib/fog/storage/google_xml.rb +4 -260
  47. data/lib/fog/storage/google_xml/mock.rb +102 -0
  48. data/lib/fog/storage/google_xml/models/file.rb +14 -4
  49. data/lib/fog/storage/google_xml/real.rb +106 -0
  50. data/lib/fog/storage/google_xml/utils.rb +66 -0
  51. data/tests/models/pubsub/received_message_tests.rb +18 -0
  52. data/tests/models/pubsub/subscription_tests.rb +26 -0
  53. data/tests/models/pubsub/subscriptions_tests.rb +33 -0
  54. data/tests/models/pubsub/topic_tests.rb +18 -0
  55. data/tests/models/pubsub/topics_tests.rb +27 -0
  56. metadata +50 -14
@@ -0,0 +1,32 @@
1
+ require "fog/core/collection"
2
+ require "fog/google/models/pubsub/subscription"
3
+
4
+ module Fog
5
+ module Google
6
+ class Pubsub
7
+ class Subscriptions < Fog::Collection
8
+ model Fog::Google::Pubsub::Subscription
9
+
10
+ # Lists all subscriptions that exist on the project.
11
+ #
12
+ # @return [Array<Fog::Google::Pubsub::Subscription>] list of
13
+ # subscriptions
14
+ def all
15
+ data = service.list_subscriptions.body["subscriptions"] || []
16
+ load(data)
17
+ end
18
+
19
+ # Retrieves a subscription by name
20
+ #
21
+ # @param subscription_name [String] name of subscription to retrieve
22
+ # @return [Fog::Google::Pubsub::Topic] topic found, or nil if not found
23
+ def get(subscription_name)
24
+ subscription = service.get_subscription(subscription_name).body
25
+ new(subscription)
26
+ rescue Fog::Errors::NotFound
27
+ nil
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,72 @@
1
+ require "fog/core/model"
2
+
3
+ module Fog
4
+ module Google
5
+ class Pubsub
6
+ # Represents a Pubsub topic resource
7
+ #
8
+ # @see https://cloud.google.com/pubsub/reference/rest/v1/projects.topics#Topic
9
+ class Topic < Fog::Model
10
+ identity :name
11
+
12
+ # Creates this topic resource on the service.
13
+ #
14
+ # @return [Fog::Google::Pubsub::Topic] this instance
15
+ def create
16
+ requires :name
17
+
18
+ service.create_topic(name)
19
+ self
20
+ end
21
+
22
+ # Deletes this topic resource on the service.
23
+ #
24
+ # @return [Fog::Google::Pubsub::Topic] this instance
25
+ def destroy
26
+ requires :name
27
+
28
+ service.delete_topic(name)
29
+ self
30
+ end
31
+
32
+ # Publish a message to this topic. This method will automatically
33
+ # encode the message via base64 encoding.
34
+ #
35
+ # @param messages [Array<Hash{String => String}, #to_s>] list of messages
36
+ # to send; if it's a hash, then the value of key "data" will be sent
37
+ # as the message. Additionally, if the hash contains a value for key
38
+ # "attributes", then they will be sent as well as attributes on the
39
+ # message.
40
+ # @return [Array<String>] list of message ids
41
+ def publish(messages)
42
+ requires :name
43
+
44
+ # Ensure our messages are base64 encoded
45
+ encoded_messages = []
46
+
47
+ messages.each do |message|
48
+ encoded_message = {}
49
+ if message.is_a?(Hash)
50
+ encoded_message["attributes"] = message["attributes"]
51
+ if message.key?("data")
52
+ encoded_message["data"] = Base64.strict_encode64(message["data"])
53
+ end
54
+ else
55
+ encoded_message["data"] = Base64.strict_encode64(message.to_s)
56
+ end
57
+
58
+ encoded_messages << encoded_message
59
+ end
60
+
61
+ service.publish_topic(name, encoded_messages).body["messageIds"]
62
+ end
63
+
64
+ # Save the instance (does the same thing as #create)
65
+ # @return [Fog::Google::Pubsub::Topic] this instance
66
+ def save
67
+ create
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,31 @@
1
+ require "fog/core/collection"
2
+ require "fog/google/models/pubsub/topic"
3
+
4
+ module Fog
5
+ module Google
6
+ class Pubsub
7
+ class Topics < Fog::Collection
8
+ model Fog::Google::Pubsub::Topic
9
+
10
+ # Lists all topics that exist on the project.
11
+ #
12
+ # @return [Array<Fog::Google::Pubsub::Topic>] list of topics
13
+ def all
14
+ data = service.list_topics.body["topics"] || []
15
+ load(data)
16
+ end
17
+
18
+ # Retrieves a topic by name
19
+ #
20
+ # @param topic_name [String] name of topic to retrieve
21
+ # @return [Fog::Google::Pubsub::Topic] topic found, or nil if not found
22
+ def get(topic_name)
23
+ topic = service.get_topic(topic_name).body
24
+ new(topic)
25
+ rescue Fog::Errors::NotFound
26
+ nil
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,6 +1,9 @@
1
1
  module Fog
2
2
  module Google
3
3
  class Monitoring < Fog::Service
4
+ autoload :Mock, File.expand_path("../monitoring/mock", __FILE__)
5
+ autoload :Real, File.expand_path("../monitoring/real", __FILE__)
6
+
4
7
  requires :google_project
5
8
  recognizes :google_client_email, :google_key_location, :google_key_string, :google_client,
6
9
  :app_name, :app_version, :google_json_key_location, :google_json_key_string
@@ -37,51 +40,6 @@ module Fog
37
40
 
38
41
  # MetricDescriptors
39
42
  request :list_metric_descriptors
40
-
41
- class Mock
42
- include Fog::Google::Shared
43
-
44
- def initialize(options)
45
- shared_initialize(options[:google_project], GOOGLE_MONITORING_API_VERSION, GOOGLE_MONITORING_BASE_URL)
46
- end
47
-
48
- def self.data
49
- @data ||= Hash.new do |hash, key|
50
- hash[key] = {
51
- :timeseries => {},
52
- :timeseries_descriptors => {},
53
- :metric_descriptors => {}
54
- }
55
- end
56
- end
57
-
58
- def self.reset
59
- @data = nil
60
- end
61
-
62
- def data
63
- self.class.data[project]
64
- end
65
-
66
- def reset_data
67
- self.class.data.delete(project)
68
- end
69
- end
70
-
71
- class Real
72
- include Fog::Google::Shared
73
-
74
- attr_accessor :client
75
- attr_reader :monitoring
76
-
77
- def initialize(options)
78
- shared_initialize(options[:google_project], GOOGLE_MONITORING_API_VERSION, GOOGLE_MONITORING_BASE_URL)
79
- options.merge!(:google_api_scope_url => GOOGLE_MONITORING_API_SCOPE_URLS.join(" "))
80
-
81
- @client = initialize_google_client(options)
82
- @monitoring = @client.discovered_api("cloudmonitoring", api_version)
83
- end
84
- end
85
43
  end
86
44
  end
87
45
  end
@@ -0,0 +1,35 @@
1
+ module Fog
2
+ module Google
3
+ class Monitoring
4
+ class Mock
5
+ include Fog::Google::Shared
6
+
7
+ def initialize(options)
8
+ shared_initialize(options[:google_project], GOOGLE_MONITORING_API_VERSION, GOOGLE_MONITORING_BASE_URL)
9
+ end
10
+
11
+ def self.data
12
+ @data ||= Hash.new do |hash, key|
13
+ hash[key] = {
14
+ :timeseries => {},
15
+ :timeseries_descriptors => {},
16
+ :metric_descriptors => {}
17
+ }
18
+ end
19
+ end
20
+
21
+ def self.reset
22
+ @data = nil
23
+ end
24
+
25
+ def data
26
+ self.class.data[project]
27
+ end
28
+
29
+ def reset_data
30
+ self.class.data.delete(project)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ module Fog
2
+ module Google
3
+ class Monitoring
4
+ class Real
5
+ include Fog::Google::Shared
6
+
7
+ attr_accessor :client
8
+ attr_reader :monitoring
9
+
10
+ def initialize(options)
11
+ shared_initialize(options[:google_project], GOOGLE_MONITORING_API_VERSION, GOOGLE_MONITORING_BASE_URL)
12
+ options[:google_api_scope_url] = GOOGLE_MONITORING_API_SCOPE_URLS.join(" ")
13
+
14
+ @client = initialize_google_client(options)
15
+ @monitoring = @client.discovered_api("cloudmonitoring", api_version)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,59 @@
1
+ module Fog
2
+ module Google
3
+ class Pubsub < Fog::Service
4
+ autoload :Mock, File.expand_path("../pubsub/mock", __FILE__)
5
+ autoload :Real, File.expand_path("../pubsub/real", __FILE__)
6
+
7
+ requires :google_project
8
+ recognizes :google_client_email, :google_key_location, :google_key_string, :google_client,
9
+ :app_name, :app_version, :google_json_key_location, :google_json_key_string
10
+
11
+ GOOGLE_PUBSUB_API_VERSION = "v1".freeze
12
+ GOOGLE_PUBSUB_BASE_URL = "https://www.googleapis.com/pubsub".freeze
13
+ GOOGLE_PUBSUB_API_SCOPE_URLS = %w(https://www.googleapis.com/auth/pubsub).freeze
14
+
15
+ ##
16
+ # MODELS
17
+ model_path "fog/google/models/pubsub"
18
+
19
+ # Topic
20
+ model :topic
21
+ collection :topics
22
+
23
+ # Subscription
24
+ model :subscription
25
+ collection :subscriptions
26
+
27
+ # ReceivedMessage
28
+ model :received_message
29
+
30
+ ##
31
+ # REQUESTS
32
+ request_path "fog/google/requests/pubsub"
33
+
34
+ # Topic
35
+ request :list_topics
36
+ request :get_topic
37
+ request :create_topic
38
+ request :delete_topic
39
+ request :publish_topic
40
+
41
+ # Subscription
42
+ request :list_subscriptions
43
+ request :get_subscription
44
+ request :create_subscription
45
+ request :delete_subscription
46
+ request :pull_subscription
47
+ request :acknowledge_subscription
48
+
49
+ # Helper class for getting a subscription name
50
+ #
51
+ # @param subscription [Subscription, #to_s] subscription instance or name
52
+ # of subscription
53
+ # @return [String] name of subscription
54
+ def self.subscription_name(subscription)
55
+ subscription.is_a?(Subscription) ? subscription.name : subscription.to_s
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,34 @@
1
+ module Fog
2
+ module Google
3
+ class Pubsub
4
+ class Mock
5
+ include Fog::Google::Shared
6
+
7
+ def initialize(options)
8
+ shared_initialize(options[:google_project], GOOGLE_PUBSUB_API_VERSION, GOOGLE_PUBSUB_BASE_URL)
9
+ end
10
+
11
+ def self.data
12
+ @data ||= Hash.new do |hash, key|
13
+ hash[key] = {
14
+ :topics => {},
15
+ :subscriptions => {}
16
+ }
17
+ end
18
+ end
19
+
20
+ def self.reset
21
+ @data = nil
22
+ end
23
+
24
+ def data
25
+ self.class.data[project]
26
+ end
27
+
28
+ def reset_data
29
+ self.class.data.delete(project)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ module Fog
2
+ module Google
3
+ class Pubsub
4
+ class Real
5
+ include Fog::Google::Shared
6
+
7
+ attr_accessor :client
8
+ attr_reader :pubsub
9
+
10
+ def initialize(options)
11
+ shared_initialize(options[:google_project], GOOGLE_PUBSUB_API_VERSION, GOOGLE_PUBSUB_BASE_URL)
12
+ options[:google_api_scope_url] = GOOGLE_PUBSUB_API_SCOPE_URLS.join(" ")
13
+
14
+ @client = initialize_google_client(options)
15
+ @pubsub = @client.discovered_api("pubsub", api_version)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,46 @@
1
+ module Fog
2
+ module Google
3
+ class Pubsub
4
+ class Real
5
+ # Acknowledges a message received from a subscription.
6
+ #
7
+ # @see https://cloud.google.com/pubsub/reference/rest/v1/projects.subscriptions/acknowledge
8
+ def acknowledge_subscription(subscription, ack_ids)
9
+ api_method = @pubsub.projects.subscriptions.acknowledge
10
+
11
+ parameters = {
12
+ "subscription" => subscription.to_s
13
+ }
14
+
15
+ body = {
16
+ "ackIds" => ack_ids
17
+ }
18
+
19
+ request(api_method, parameters, body)
20
+ end
21
+ end
22
+
23
+ class Mock
24
+ def acknowledge_subscription(subscription, ack_ids)
25
+ unless data[:subscriptions].key?(subscription)
26
+ subscription_resource = subscription.to_s.split("/")[-1]
27
+ body = {
28
+ "error" => {
29
+ "code" => 404,
30
+ "message" => "Resource not found (resource=#{subscription_resource}).",
31
+ "status" => "NOT_FOUND"
32
+ }
33
+ }
34
+ status = 404
35
+ return build_excon_response(body, status)
36
+ end
37
+
38
+ sub = data[:subscriptions][subscription]
39
+ sub[:messages].delete_if { |msg| ack_ids.member?(msg["messageId"]) }
40
+
41
+ build_excon_response(nil, 200)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,57 @@
1
+ module Fog
2
+ module Google
3
+ class Pubsub
4
+ class Real
5
+ # Create a subscription resource on a topic.
6
+ #
7
+ # @param subscription_name [#to_s] name of the subscription to create.
8
+ # Note that it must follow the restrictions of subscription names;
9
+ # specifically it must be named within a project (e.g.
10
+ # "projects/my-project/subscriptions/my-subscripton")
11
+ # @param topic [Topic, #to_s] topic instance or name of topic to create
12
+ # subscription on
13
+ # @param push_config [Hash] configuration for a push config (if empty
14
+ # hash, then no push_config is created)
15
+ # @param ack_deadline_seconds [Number] how long the service waits for
16
+ # an acknowledgement before redelivering the message; if nil then
17
+ # service default of 10 is used
18
+ # @see https://cloud.google.com/pubsub/reference/rest/v1/projects.subscriptions/create
19
+ def create_subscription(subscription_name, topic, push_config = {}, ack_deadline_seconds = nil)
20
+ api_method = @pubsub.projects.subscriptions.create
21
+
22
+ parameters = {}
23
+ parameters["name"] = subscription_name.to_s unless subscription_name.nil?
24
+
25
+ body = {
26
+ "topic" => (topic.is_a?(Topic) ? topic.name : topic.to_s)
27
+ }
28
+
29
+ if !push_config.nil? && push_config.key?("push_endpoint")
30
+ body["pushConfig"] = push_config["push_endpoint"].clone
31
+ body["pushConfig"]["attributes"] = push_config["attributes"] if push_config.key?("attributes")
32
+ end
33
+
34
+ body["ackDeadlineSeconds"] = ack_deadline_seconds unless ack_deadline_seconds.nil?
35
+
36
+ request(api_method, parameters, body)
37
+ end
38
+ end
39
+
40
+ class Mock
41
+ def create_subscription(subscription_name, topic, push_config = {}, ack_deadline_seconds = nil)
42
+ subscription = {
43
+ "name" => subscription_name,
44
+ "topic" => topic,
45
+ "pushConfig" => push_config,
46
+ "ackDeadlineSeconds" => ack_deadline_seconds
47
+ }
48
+
49
+ # We also track pending messages
50
+ data[:subscriptions][subscription_name] = subscription.merge(:messages => [])
51
+
52
+ build_excon_response(subscription, 200)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end