gcloud 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (175) hide show
  1. checksums.yaml +8 -8
  2. data/AUTHENTICATION.md +13 -9
  3. data/CHANGELOG.md +8 -3
  4. data/OVERVIEW.md +46 -8
  5. data/lib/gcloud.rb +123 -117
  6. data/lib/gcloud/backoff.rb +43 -15
  7. data/lib/gcloud/bigquery.rb +211 -195
  8. data/lib/gcloud/bigquery/connection.rb +9 -9
  9. data/lib/gcloud/bigquery/copy_job.rb +15 -16
  10. data/lib/gcloud/bigquery/credentials.rb +3 -3
  11. data/lib/gcloud/bigquery/data.rb +12 -11
  12. data/lib/gcloud/bigquery/dataset.rb +162 -216
  13. data/lib/gcloud/bigquery/dataset/access.rb +59 -43
  14. data/lib/gcloud/bigquery/dataset/list.rb +3 -3
  15. data/lib/gcloud/bigquery/errors.rb +9 -5
  16. data/lib/gcloud/bigquery/extract_job.rb +18 -18
  17. data/lib/gcloud/bigquery/insert_response.rb +7 -4
  18. data/lib/gcloud/bigquery/job.rb +48 -44
  19. data/lib/gcloud/bigquery/job/list.rb +3 -3
  20. data/lib/gcloud/bigquery/load_job.rb +24 -25
  21. data/lib/gcloud/bigquery/project.rb +145 -204
  22. data/lib/gcloud/bigquery/query_data.rb +10 -9
  23. data/lib/gcloud/bigquery/query_job.rb +23 -32
  24. data/lib/gcloud/bigquery/table.rb +238 -280
  25. data/lib/gcloud/bigquery/table/list.rb +3 -3
  26. data/lib/gcloud/bigquery/table/schema.rb +79 -87
  27. data/lib/gcloud/bigquery/view.rb +69 -82
  28. data/lib/gcloud/credentials.rb +3 -9
  29. data/lib/gcloud/datastore.rb +194 -170
  30. data/lib/gcloud/datastore/connection.rb +12 -8
  31. data/lib/gcloud/datastore/credentials.rb +6 -4
  32. data/lib/gcloud/datastore/dataset.rb +74 -141
  33. data/lib/gcloud/datastore/dataset/lookup_results.rb +6 -4
  34. data/lib/gcloud/datastore/dataset/query_results.rb +6 -4
  35. data/lib/gcloud/datastore/entity.rb +81 -76
  36. data/lib/gcloud/datastore/errors.rb +10 -8
  37. data/lib/gcloud/datastore/key.rb +41 -77
  38. data/lib/gcloud/datastore/properties.rb +3 -3
  39. data/lib/gcloud/datastore/proto.rb +7 -4
  40. data/lib/gcloud/datastore/query.rb +26 -3
  41. data/lib/gcloud/datastore/transaction.rb +12 -8
  42. data/lib/gcloud/dns.rb +180 -152
  43. data/lib/gcloud/dns/change.rb +16 -16
  44. data/lib/gcloud/dns/change/list.rb +3 -3
  45. data/lib/gcloud/dns/connection.rb +9 -10
  46. data/lib/gcloud/dns/credentials.rb +3 -3
  47. data/lib/gcloud/dns/errors.rb +9 -5
  48. data/lib/gcloud/dns/importer.rb +17 -23
  49. data/lib/gcloud/dns/project.rb +42 -64
  50. data/lib/gcloud/dns/record.rb +58 -46
  51. data/lib/gcloud/dns/record/list.rb +6 -7
  52. data/lib/gcloud/dns/zone.rb +198 -289
  53. data/lib/gcloud/dns/zone/list.rb +3 -3
  54. data/lib/gcloud/dns/zone/transaction.rb +56 -72
  55. data/lib/gcloud/errors.rb +174 -3
  56. data/lib/gcloud/gce.rb +3 -4
  57. data/lib/gcloud/grpc_utils.rb +76 -0
  58. data/lib/gcloud/logging.rb +308 -0
  59. data/lib/gcloud/logging/credentials.rb +29 -0
  60. data/lib/gcloud/logging/entry.rb +303 -0
  61. data/lib/gcloud/logging/entry/http_request.rb +141 -0
  62. data/lib/gcloud/logging/entry/list.rb +111 -0
  63. data/lib/gcloud/logging/entry/operation.rb +90 -0
  64. data/lib/gcloud/logging/logger.rb +307 -0
  65. data/lib/gcloud/logging/metric.rb +175 -0
  66. data/lib/gcloud/logging/metric/list.rb +98 -0
  67. data/lib/gcloud/logging/project.rb +650 -0
  68. data/lib/gcloud/logging/resource.rb +95 -0
  69. data/lib/gcloud/logging/resource_descriptor.rb +140 -0
  70. data/lib/gcloud/logging/resource_descriptor/list.rb +78 -0
  71. data/lib/gcloud/logging/service.rb +258 -0
  72. data/lib/gcloud/logging/sink.rb +233 -0
  73. data/lib/gcloud/logging/sink/list.rb +97 -0
  74. data/lib/gcloud/pubsub.rb +241 -199
  75. data/lib/gcloud/pubsub/credentials.rb +3 -3
  76. data/lib/gcloud/pubsub/message.rb +26 -20
  77. data/lib/gcloud/pubsub/project.rb +166 -233
  78. data/lib/gcloud/pubsub/received_message.rb +28 -38
  79. data/lib/gcloud/pubsub/service.rb +323 -0
  80. data/lib/gcloud/pubsub/subscription.rb +172 -242
  81. data/lib/gcloud/pubsub/subscription/list.rb +11 -9
  82. data/lib/gcloud/pubsub/topic.rb +152 -271
  83. data/lib/gcloud/pubsub/topic/batch.rb +66 -0
  84. data/lib/gcloud/pubsub/topic/list.rb +9 -7
  85. data/lib/gcloud/resource_manager.rb +158 -138
  86. data/lib/gcloud/resource_manager/connection.rb +6 -5
  87. data/lib/gcloud/resource_manager/credentials.rb +3 -3
  88. data/lib/gcloud/resource_manager/errors.rb +9 -5
  89. data/lib/gcloud/resource_manager/manager.rb +54 -86
  90. data/lib/gcloud/resource_manager/project.rb +69 -88
  91. data/lib/gcloud/resource_manager/project/list.rb +4 -5
  92. data/lib/gcloud/resource_manager/project/updater.rb +12 -14
  93. data/lib/gcloud/search.rb +158 -135
  94. data/lib/gcloud/search/api_client.rb +7 -7
  95. data/lib/gcloud/search/connection.rb +8 -8
  96. data/lib/gcloud/search/credentials.rb +3 -3
  97. data/lib/gcloud/search/document.rb +64 -87
  98. data/lib/gcloud/search/document/list.rb +5 -5
  99. data/lib/gcloud/search/errors.rb +9 -5
  100. data/lib/gcloud/search/field_value.rb +32 -38
  101. data/lib/gcloud/search/field_values.rb +50 -80
  102. data/lib/gcloud/search/fields.rb +44 -65
  103. data/lib/gcloud/search/index.rb +163 -204
  104. data/lib/gcloud/search/index/list.rb +5 -5
  105. data/lib/gcloud/search/project.rb +31 -47
  106. data/lib/gcloud/search/result.rb +27 -31
  107. data/lib/gcloud/search/result/list.rb +6 -6
  108. data/lib/gcloud/storage.rb +224 -190
  109. data/lib/gcloud/storage/bucket.rb +202 -227
  110. data/lib/gcloud/storage/bucket/acl.rb +83 -170
  111. data/lib/gcloud/storage/bucket/cors.rb +31 -34
  112. data/lib/gcloud/storage/bucket/list.rb +3 -3
  113. data/lib/gcloud/storage/connection.rb +11 -7
  114. data/lib/gcloud/storage/credentials.rb +3 -3
  115. data/lib/gcloud/storage/errors.rb +11 -8
  116. data/lib/gcloud/storage/file.rb +129 -171
  117. data/lib/gcloud/storage/file/acl.rb +51 -99
  118. data/lib/gcloud/storage/file/list.rb +3 -3
  119. data/lib/gcloud/storage/file/verifier.rb +3 -2
  120. data/lib/gcloud/storage/project.rb +111 -132
  121. data/lib/gcloud/upload.rb +4 -7
  122. data/lib/gcloud/version.rb +2 -4
  123. data/lib/google/api/annotations.rb +14 -0
  124. data/lib/google/api/http.rb +30 -0
  125. data/lib/google/api/label.rb +24 -0
  126. data/lib/google/api/monitored_resource.rb +25 -0
  127. data/lib/google/datastore/v1beta3/datastore.rb +115 -0
  128. data/lib/google/datastore/v1beta3/datastore_services.rb +33 -0
  129. data/lib/google/datastore/v1beta3/entity.rb +63 -0
  130. data/lib/google/datastore/v1beta3/query.rb +128 -0
  131. data/lib/google/devtools/cloudtrace/v1/trace.rb +78 -0
  132. data/lib/google/devtools/cloudtrace/v1/trace_services.rb +32 -0
  133. data/lib/google/example/library/v1/library.rb +91 -0
  134. data/lib/google/example/library/v1/library_services.rb +40 -0
  135. data/lib/google/iam/v1/iam_policy.rb +33 -0
  136. data/lib/google/iam/v1/iam_policy_services.rb +30 -0
  137. data/lib/google/iam/v1/policy.rb +25 -0
  138. data/lib/google/logging/type/http_request.rb +28 -0
  139. data/lib/google/logging/type/log_severity.rb +27 -0
  140. data/lib/google/logging/v2/log_entry.rb +44 -0
  141. data/lib/google/logging/v2/logging.rb +56 -0
  142. data/lib/google/logging/v2/logging_config.rb +59 -0
  143. data/lib/google/logging/v2/logging_config_services.rb +32 -0
  144. data/lib/google/logging/v2/logging_metrics.rb +51 -0
  145. data/lib/google/logging/v2/logging_metrics_services.rb +32 -0
  146. data/lib/google/logging/v2/logging_services.rb +31 -0
  147. data/lib/google/longrunning/operations.rb +50 -0
  148. data/lib/google/longrunning/operations_services.rb +29 -0
  149. data/lib/google/protobuf/any.rb +17 -0
  150. data/lib/google/protobuf/api.rb +31 -0
  151. data/lib/google/protobuf/descriptor.rb +0 -0
  152. data/lib/google/protobuf/duration.rb +17 -0
  153. data/lib/google/protobuf/empty.rb +15 -0
  154. data/lib/google/protobuf/field_mask.rb +16 -0
  155. data/lib/google/protobuf/source_context.rb +16 -0
  156. data/lib/google/protobuf/struct.rb +35 -0
  157. data/lib/google/protobuf/timestamp.rb +17 -0
  158. data/lib/google/protobuf/type.rb +79 -0
  159. data/lib/google/protobuf/wrappers.rb +48 -0
  160. data/lib/google/pubsub/v1/pubsub.rb +129 -0
  161. data/lib/google/pubsub/v1/pubsub_services.rb +56 -0
  162. data/lib/google/pubsub/v1beta2/pubsub.rb +126 -0
  163. data/lib/google/pubsub/v1beta2/pubsub_services.rb +56 -0
  164. data/lib/google/rpc/code.rb +32 -0
  165. data/lib/google/rpc/error_details.rb +61 -0
  166. data/lib/google/rpc/status.rb +19 -0
  167. data/lib/google/type/color.rb +20 -0
  168. data/lib/google/type/date.rb +18 -0
  169. data/lib/google/type/dayofweek.rb +23 -0
  170. data/lib/google/type/latlng.rb +17 -0
  171. data/lib/google/type/money.rb +18 -0
  172. data/lib/google/type/timeofday.rb +19 -0
  173. metadata +101 -4
  174. data/lib/gcloud/pubsub/connection.rb +0 -295
  175. data/lib/gcloud/pubsub/errors.rb +0 -93
@@ -0,0 +1,141 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ module Gcloud
17
+ module Logging
18
+ class Entry
19
+ ##
20
+ # # Http Request
21
+ #
22
+ # HTTP request data associated with a log entry.
23
+ #
24
+ # See also {Gcloud::Logging::Entry#http_request}.
25
+ #
26
+ class HttpRequest
27
+ ##
28
+ # @private Create an empty HttpRequest object.
29
+ def initialize
30
+ end
31
+
32
+ ##
33
+ # The request method. Examples: `"GET"`, `"HEAD"`, `"PUT"`, `"POST"`.
34
+ # (String)
35
+ attr_accessor :method
36
+
37
+ ##
38
+ # The URL. The scheme (http, https), the host name, the path and the
39
+ # query portion of the URL that was requested. Example:
40
+ # `"http://example.com/some/info?color=red"`. (String)
41
+ attr_accessor :url
42
+
43
+ ##
44
+ # The size of the HTTP request message in bytes, including the request
45
+ # headers and the request body. (Integer)
46
+ attr_accessor :size
47
+
48
+ ##
49
+ # The response code indicating the status of response. Examples: `200`,
50
+ # `404`. (Integer)
51
+ attr_accessor :status
52
+
53
+ ##
54
+ # The size of the HTTP response message sent back to the client, in
55
+ # bytes, including the response headers and the response body. (Integer)
56
+ attr_accessor :response_size
57
+
58
+ ##
59
+ # The user agent sent by the client. Example: `"Mozilla/4.0 (compatible;
60
+ # MSIE 6.0; Windows 98; Q312461; .NET CLR 1.0.3705)"`. (String)
61
+ attr_accessor :user_agent
62
+
63
+ ##
64
+ # The IP address (IPv4 or IPv6) of the client that issued the HTTP
65
+ # request. Examples: `"192.168.1.1"`, `"FE80::0202:B3FF:FE1E:8329"`.
66
+ # (String)
67
+ attr_accessor :remote_ip
68
+
69
+ ##
70
+ # The referer URL of the request, as defined in [HTTP/1.1 Header Field
71
+ # Definitions](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html).
72
+ # (String)
73
+ attr_accessor :referer
74
+
75
+ ##
76
+ # Whether an entity was served from cache (with or without validation).
77
+ # (Boolean)
78
+ attr_accessor :cache_hit
79
+
80
+ ##
81
+ # Whether the response was validated with the origin server before being
82
+ # served from cache. This field is only meaningful if `cache_hit` is
83
+ # `true`. (Boolean)
84
+ attr_accessor :validated
85
+
86
+ ##
87
+ # @private Determines if the HttpRequest has any data.
88
+ def empty?
89
+ method.nil? &&
90
+ url.nil? &&
91
+ size.nil? &&
92
+ status.nil? &&
93
+ response_size.nil? &&
94
+ user_agent.nil? &&
95
+ remote_ip.nil? &&
96
+ referer.nil? &&
97
+ cache_hit.nil? &&
98
+ validated.nil?
99
+ end
100
+
101
+ ##
102
+ # @private Exports the HttpRequest to a
103
+ # Google::Logging::Type::HttpRequest object.
104
+ def to_grpc
105
+ return nil if empty?
106
+ Google::Logging::Type::HttpRequest.new(
107
+ request_method: method.to_s,
108
+ request_url: url.to_s,
109
+ request_size: size.to_i,
110
+ status: status.to_i,
111
+ response_size: response_size.to_i,
112
+ user_agent: user_agent.to_s,
113
+ remote_ip: remote_ip.to_s,
114
+ referer: referer.to_s,
115
+ cache_hit: !(!cache_hit),
116
+ validated_with_origin_server: !(!validated)
117
+ )
118
+ end
119
+
120
+ ##
121
+ # @private New HttpRequest from a Google::Logging::Type::HttpRequest
122
+ # object.
123
+ def self.from_grpc grpc
124
+ return new if grpc.nil?
125
+ new.tap do |h|
126
+ h.method = grpc.request_method
127
+ h.url = grpc.request_url
128
+ h.size = grpc.request_size
129
+ h.status = grpc.status
130
+ h.response_size = grpc.response_size
131
+ h.user_agent = grpc.user_agent
132
+ h.remote_ip = grpc.remote_ip
133
+ h.referer = grpc.referer
134
+ h.cache_hit = grpc.cache_hit
135
+ h.validated = grpc.validated_with_origin_server
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,111 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "delegate"
17
+
18
+ module Gcloud
19
+ module Logging
20
+ class Entry
21
+ ##
22
+ # Entry::List is a special case Array with additional values.
23
+ class List < DelegateClass(::Array)
24
+ ##
25
+ # If not empty, indicates that there are more records that match
26
+ # the request and this value should be passed to continue.
27
+ attr_accessor :token
28
+
29
+ ##
30
+ # @private Create a new Entry::List with an array of Entry instances.
31
+ def initialize arr = []
32
+ super arr
33
+ end
34
+
35
+ ##
36
+ # Whether there is a next page of entries.
37
+ def next?
38
+ !token.nil?
39
+ end
40
+
41
+ ##
42
+ # Retrieve the next page of entries.
43
+ def next
44
+ return nil unless next?
45
+ ensure_service!
46
+ grpc = @service.list_entries token: token, projects: @projects,
47
+ filter: @filter, order: @order, max: @max
48
+ self.class.from_grpc grpc, @service
49
+ rescue GRPC::BadStatus => e
50
+ raise Gcloud::Error.from_error(e)
51
+ end
52
+
53
+ ##
54
+ # Retrieves all log entries by repeatedly loading {#next} until
55
+ # {#next?} returns `false`. Returns the list instance for method
56
+ # chaining.
57
+ #
58
+ # This method may make several API calls until all log entries are
59
+ # retrieved. Be sure to use as narrow a search criteria as possible.
60
+ # Please use with caution.
61
+ #
62
+ # @example
63
+ # require "gcloud"
64
+ #
65
+ # gcloud = Gcloud.new
66
+ # logging = gcloud.logging
67
+ # hour_ago = (Time.now - 60*60).utc.strftime('%FT%TZ')
68
+ # recent_errors = "timestamp >= \"#{hour_ago}\" severity >= ERROR"
69
+ # entries = logging.entries(filter: recent_errors).all
70
+ #
71
+ def all
72
+ while next?
73
+ next_records = self.next
74
+ push(*next_records)
75
+ self.token = next_records.token
76
+ end
77
+ self
78
+ end
79
+
80
+ ##
81
+ # @private New Entry::List from a
82
+ # Google::Logging::V2::ListLogEntryResponse object.
83
+ def self.from_grpc grpc_list, service, projects: nil, filter: nil,
84
+ order: nil, max: nil
85
+ entries = new(Array(grpc_list.entries).map do |grpc_entry|
86
+ Entry.from_grpc grpc_entry
87
+ end)
88
+ entries.instance_eval do
89
+ @token = grpc_list.next_page_token
90
+ @token = nil if @token == ""
91
+ @service = service
92
+ @projects = projects
93
+ @filter = filter
94
+ @order = order
95
+ @max = max
96
+ end
97
+ entries
98
+ end
99
+
100
+ protected
101
+
102
+ ##
103
+ # @private Raise an error unless an active connection to the service is
104
+ # available.
105
+ def ensure_service!
106
+ fail "Must have active connection to service" unless @service
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,90 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ module Gcloud
17
+ module Logging
18
+ class Entry
19
+ ##
20
+ # # Operation
21
+ #
22
+ # Additional information about a potentially long-running operation with
23
+ # which a log entry is associated.
24
+ #
25
+ # See also {Gcloud::Logging::Entry#operation}.
26
+ #
27
+ class Operation
28
+ ##
29
+ # @private Create an empty Operation object.
30
+ def initialize
31
+ end
32
+
33
+ ##
34
+ # An arbitrary operation identifier. Log entries with the same
35
+ # identifier are assumed to be part of the same operation.
36
+ attr_accessor :id
37
+
38
+ ##
39
+ # An arbitrary producer identifier. The combination of `id` and
40
+ # `producer` must be globally unique. Examples for `producer`:
41
+ # `"MyDivision.MyBigCompany.com"`,
42
+ # `"github.com/MyProject/MyApplication"`.
43
+ attr_accessor :producer
44
+
45
+ ##
46
+ # Set this to `true` if this is the first log entry in the operation.
47
+ attr_accessor :first
48
+
49
+ ##
50
+ # Set this to `true` if this is the last log entry in the operation.
51
+ attr_accessor :last
52
+
53
+ ##
54
+ # @private Determines if the Operation has any data.
55
+ def empty?
56
+ id.nil? &&
57
+ producer.nil? &&
58
+ first.nil? &&
59
+ last.nil?
60
+ end
61
+
62
+ ##
63
+ # @private Exports the Operation to a
64
+ # Google::Logging::V2::LogEntryOperation object.
65
+ def to_grpc
66
+ return nil if empty?
67
+ Google::Logging::V2::LogEntryOperation.new(
68
+ id: id.to_s,
69
+ producer: producer.to_s,
70
+ first: !(!first),
71
+ last: !(!last)
72
+ )
73
+ end
74
+
75
+ ##
76
+ # @private New HttpRequest from a Google::Logging::V2::LogEntryOperation
77
+ # object.
78
+ def self.from_grpc grpc
79
+ return new if grpc.nil?
80
+ new.tap do |o|
81
+ o.id = grpc.id
82
+ o.producer = grpc.producer
83
+ o.first = grpc.first
84
+ o.last = grpc.last
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,307 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ module Gcloud
17
+ module Logging
18
+ ##
19
+ # # Logger
20
+ #
21
+ # A (mostly) API-compatible logger for ruby's Logger.
22
+ #
23
+ # @example
24
+ # require "gcloud"
25
+ #
26
+ # gcloud = Gcloud.new
27
+ # logging = gcloud.logging
28
+ #
29
+ # resource = logging.resource "gae_app",
30
+ # module_id: "1",
31
+ # version_id: "20150925t173233"
32
+ #
33
+ # logger = logging.logger "my_app_log", resource, env: :production
34
+ # logger.info "Job started."
35
+ #
36
+ class Logger
37
+ ##
38
+ # @private The logging object.
39
+ attr_accessor :logging
40
+
41
+ ##
42
+ # @private The Google Cloud log_name to write the log entry with.
43
+ attr_reader :log_name
44
+
45
+ ##
46
+ # @private The Google Cloud resource to write the log entry with.
47
+ attr_reader :resource
48
+
49
+ ##
50
+ # @private The Google Cloud labels to write the log entry with.
51
+ attr_reader :labels
52
+
53
+ ##
54
+ # @private Creates a new Logger instance.
55
+ def initialize logging, log_name, resource, labels = nil
56
+ @logging = logging
57
+ @log_name = log_name
58
+ @resource = resource
59
+ @labels = labels
60
+ @level = 0 # DEBUG is the default behavior
61
+ end
62
+
63
+ ##
64
+ # Log a `DEBUG` entry.
65
+ #
66
+ # @param [String, Hash] message The log entry payload, represented as
67
+ # either a string, a hash (JSON), or a hash (protocol buffer).
68
+ # @yield Evaluates to the message to log. This is not evaluated unless the
69
+ # logger's level is sufficient to log the message. This allows you to
70
+ # create potentially expensive logging messages that are only called
71
+ # when the logger is configured to show them.
72
+ #
73
+ def debug message = nil, &block
74
+ if block_given?
75
+ add 0, nil, message, &block
76
+ else
77
+ add 0, message, nil, &block
78
+ end
79
+ end
80
+
81
+ ##
82
+ # Log an `INFO` entry.
83
+ #
84
+ # @param [String, Hash] message The log entry payload, represented as
85
+ # either a string, a hash (JSON), or a hash (protocol buffer).
86
+ # @yield Evaluates to the message to log. This is not evaluated unless the
87
+ # logger's level is sufficient to log the message. This allows you to
88
+ # create potentially expensive logging messages that are only called
89
+ # when the logger is configured to show them.
90
+ #
91
+ def info message = nil, &block
92
+ if block_given?
93
+ add 1, nil, message, &block
94
+ else
95
+ add 1, message, nil, &block
96
+ end
97
+ end
98
+
99
+ ##
100
+ # Log a `WARN` entry.
101
+ #
102
+ # @param [String, Hash] message The log entry payload, represented as
103
+ # either a string, a hash (JSON), or a hash (protocol buffer).
104
+ # @yield Evaluates to the message to log. This is not evaluated unless the
105
+ # logger's level is sufficient to log the message. This allows you to
106
+ # create potentially expensive logging messages that are only called
107
+ # when the logger is configured to show them.
108
+ #
109
+ def warn message = nil, &block
110
+ if block_given?
111
+ add 2, nil, message, &block
112
+ else
113
+ add 2, message, nil, &block
114
+ end
115
+ end
116
+
117
+ ##
118
+ # Log an `ERROR` entry.
119
+ #
120
+ # @param [String, Hash] message The log entry payload, represented as
121
+ # either a string, a hash (JSON), or a hash (protocol buffer).
122
+ # @yield Evaluates to the message to log. This is not evaluated unless the
123
+ # logger's level is sufficient to log the message. This allows you to
124
+ # create potentially expensive logging messages that are only called
125
+ # when the logger is configured to show them.
126
+ #
127
+ def error message = nil, &block
128
+ if block_given?
129
+ add 3, nil, message, &block
130
+ else
131
+ add 3, message, nil, &block
132
+ end
133
+ end
134
+
135
+ ##
136
+ # Log a `FATAL` entry.
137
+ #
138
+ # @param [String, Hash] message The log entry payload, represented as
139
+ # either a string, a hash (JSON), or a hash (protocol buffer).
140
+ # @yield Evaluates to the message to log. This is not evaluated unless the
141
+ # logger's level is sufficient to log the message. This allows you to
142
+ # create potentially expensive logging messages that are only called
143
+ # when the logger is configured to show them.
144
+ #
145
+ def fatal message = nil, &block
146
+ if block_given?
147
+ add 4, nil, message, &block
148
+ else
149
+ add 4, message, nil, &block
150
+ end
151
+ end
152
+
153
+ ##
154
+ # Log an `UNKNOWN` entry. This will be printed no matter what the
155
+ # logger's current severity level is.
156
+ #
157
+ # @param [String, Hash] message The log entry payload, represented as
158
+ # either a string, a hash (JSON), or a hash (protocol buffer).
159
+ # @yield Evaluates to the message to log. This is not evaluated unless the
160
+ # logger's level is sufficient to log the message. This allows you to
161
+ # create potentially expensive logging messages that are only called
162
+ # when the logger is configured to show them.
163
+ #
164
+ def unknown message = nil, &block
165
+ if block_given?
166
+ add 5, nil, message, &block
167
+ else
168
+ add 5, message, nil, &block
169
+ end
170
+ end
171
+
172
+ ##
173
+ # Log a message if the given severity is high enough. This is the generic
174
+ # logging method. Users will be more inclined to use {#debug}, {#info},
175
+ # {#warn}, {#error}, and {#fatal}.
176
+ #
177
+ # @param [Integer, String, Symbol] severity the integer code for or the
178
+ # name of the severity level
179
+ # @param [String, Hash] message The log entry payload, represented as
180
+ # either a string, a hash (JSON), or a hash (protocol buffer).
181
+ # @yield Evaluates to the message to log. This is not evaluated unless the
182
+ # logger's level is sufficient to log the message. This allows you to
183
+ # create potentially expensive logging messages that are only called
184
+ # when the logger is configured to show them.
185
+ #
186
+ def add severity, message = nil, progname = nil
187
+ severity = derive_severity(severity) || 5 # 5 is UNKNOWN/DEFAULT
188
+ return true if severity < @level
189
+
190
+ if message.nil?
191
+ if block_given?
192
+ message = yield
193
+ else
194
+ message = progname
195
+ # progname = nil # TODO: Figure out what to do with the progname
196
+ end
197
+ end
198
+
199
+ write_entry severity, message
200
+ end
201
+ alias_method :log, :add
202
+
203
+ ##
204
+ # Returns `true` if the current severity level allows for sending `DEBUG`
205
+ # messages.
206
+ def debug?
207
+ @level <= 0
208
+ end
209
+
210
+ ##
211
+ # Returns `true` if the current severity level allows for sending `INFO`
212
+ # messages.
213
+ def info?
214
+ @level <= 1
215
+ end
216
+
217
+ ##
218
+ # Returns `true` if the current severity level allows for sending `WARN`
219
+ # messages.
220
+ def warn?
221
+ @level <= 2
222
+ end
223
+
224
+ ##
225
+ # Returns `true` if the current severity level allows for sending `ERROR`
226
+ # messages.
227
+ def error?
228
+ @level <= 3
229
+ end
230
+
231
+ ##
232
+ # Returns `true` if the current severity level allows for sending `FATAL`
233
+ # messages.
234
+ def fatal?
235
+ @level <= 4
236
+ end
237
+
238
+ ##
239
+ # Sets the logging severity level.
240
+ #
241
+ # @param [Integer, String, Symbol] severity the integer code for or the
242
+ # name of the severity level
243
+ #
244
+ # @example
245
+ # require "gcloud"
246
+ #
247
+ # gcloud = Gcloud.new
248
+ # logging = gcloud.logging
249
+ #
250
+ # resource = logging.resource "gae_app",
251
+ # module_id: "1",
252
+ # version_id: "20150925t173233"
253
+ #
254
+ # logger = logging.logger "my_app_log", resource, env: :production
255
+ #
256
+ # logger.level = "INFO"
257
+ # logger.debug "Job started." # No log entry written
258
+ #
259
+ def level= severity
260
+ new_level = derive_severity severity
261
+ fail ArgumentError, "invalid log level: #{severity}" if new_level.nil?
262
+ @level = new_level
263
+ end
264
+ alias_method :sev_threshold=, :level=
265
+
266
+ protected
267
+
268
+ ##
269
+ # @private Write a log entry to Google Cloud Logging service.
270
+ def write_entry severity, message
271
+ entry = logging.entry.tap do |e|
272
+ e.severity = gcloud_severity(severity)
273
+ e.payload = message
274
+ end
275
+
276
+ logging.write_entries entry, log_name: log_name,
277
+ resource: resource,
278
+ labels: labels
279
+ end
280
+
281
+ ##
282
+ # @private Get the logger level number from severity value object.
283
+ def derive_severity severity
284
+ return severity if severity.is_a? Integer
285
+
286
+ downcase_severity = severity.to_s.downcase
287
+ case downcase_severity
288
+ when "debug".freeze then 0
289
+ when "info".freeze then 1
290
+ when "warn".freeze then 2
291
+ when "error".freeze then 3
292
+ when "fatal".freeze then 4
293
+ when "unknown".freeze then 5
294
+ else nil
295
+ end
296
+ end
297
+
298
+ ##
299
+ # @private Get Google Cloud deverity from logger level number.
300
+ def gcloud_severity severity_int
301
+ %i(DEBUG INFO WARNING ERROR CRITICAL DEFAULT)[severity_int]
302
+ rescue
303
+ :DEFAULT
304
+ end
305
+ end
306
+ end
307
+ end