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,308 @@
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 "gcloud"
17
+ require "gcloud/logging/project"
18
+
19
+ module Gcloud
20
+ ##
21
+ # Creates a new object for connecting to the Logging service.
22
+ # Each call creates a new connection.
23
+ #
24
+ # For more information on connecting to Google Cloud see the [Authentication
25
+ # Guide](https://googlecloudplatform.github.io/gcloud-ruby/#/docs/guides/authentication).
26
+ #
27
+ # @param [String] project Project identifier for the Logging service you are
28
+ # connecting to.
29
+ # @param [String, Hash] keyfile Keyfile downloaded from Google Cloud. If file
30
+ # path the file must be readable.
31
+ # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
32
+ # set of resources and operations that the connection can access. See [Using
33
+ # OAuth 2.0 to Access Google
34
+ # APIs](https://developers.google.com/identity/protocols/OAuth2).
35
+ #
36
+ # The default scope is:
37
+ #
38
+ # * `https://www.googleapis.com/auth/logging.admin`
39
+ #
40
+ # @return [Gcloud::Logging::Project]
41
+ #
42
+ # @example
43
+ # require "gcloud/logging"
44
+ #
45
+ # gcloud = Gcloud.new
46
+ # logging = gcloud.logging
47
+ # # ...
48
+ #
49
+ def self.logging project = nil, keyfile = nil, scope: nil
50
+ project ||= Gcloud::Logging::Project.default_project
51
+ if keyfile.nil?
52
+ credentials = Gcloud::Logging::Credentials.default scope: scope
53
+ else
54
+ credentials = Gcloud::Logging::Credentials.new keyfile, scope: scope
55
+ end
56
+ Gcloud::Logging::Project.new project, credentials
57
+ end
58
+
59
+ ##
60
+ # # Google Cloud Logging
61
+ #
62
+ # The Google Cloud Logging service collects and stores logs from applications
63
+ # and services on the Google Cloud Platform, giving you fine-grained,
64
+ # programmatic control over your projects' logs. You can use the Cloud Logging
65
+ # API to:
66
+ #
67
+ # * [Read and filter log entries](#listing-log-entries)
68
+ # * [Export your log entries](#exporting-log-entries) to Cloud Storage,
69
+ # BigQuery, or Cloud Pub/Sub
70
+ # * [Create logs-based metrics](#creating-logs-based-metrics) for use in Cloud
71
+ # Monitoring
72
+ # * [Write log entries](#writing-log-entries)
73
+ #
74
+ # For general information about Cloud Logging, read [What is Google Cloud
75
+ # Logging?](https://cloud.google.com/logging/docs/).
76
+ #
77
+ # Gcloud's goal is to provide an API that is familiar and comfortable to
78
+ # Rubyists. Authentication is handled by {Gcloud#logging}. You can provide the
79
+ # project and credential information to connect to the Cloud Logging service,
80
+ # or if you are running on Google Compute Engine this configuration is taken
81
+ # care of for you. You can read more about the options for connecting in the
82
+ # [Authentication
83
+ # Guide](https://googlecloudplatform.github.io/gcloud-ruby/#/docs/guides/authentication).
84
+ #
85
+ # ## Listing log entries
86
+ #
87
+ # Cloud Logging gathers log entries from many services, including Google App
88
+ # Engine and Google Compute Engine. (See the [List of Log
89
+ # Types](https://cloud.google.com/logging/docs/view/logs_index).) In addition,
90
+ # you can write your own log entries to the service.
91
+ #
92
+ # {Gcloud::Logging::Project#entries} returns the {Gcloud::Logging::Entry}
93
+ # records belonging to your project:
94
+ #
95
+ # ```ruby
96
+ # require "gcloud"
97
+ #
98
+ # gcloud = Gcloud.new
99
+ # logging = gcloud.logging
100
+ # entries = logging.entries
101
+ # entries.each do |e|
102
+ # puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
103
+ # end
104
+ # ```
105
+ #
106
+ # You can narrow the results to a single log using an [advanced logs
107
+ # filter](https://cloud.google.com/logging/docs/view/advanced_filters). A
108
+ # log is a named collection of entries. Logs can be produced by Google Cloud
109
+ # Platform services, by third-party services, or by your applications. For
110
+ # example, the log `compute.googleapis.com/activity_log` is produced by Google
111
+ # Compute Engine. Logs are simply referenced by name in Gcloud. There is no
112
+ # `Log` type in Gcloud or `Log` resource in the Cloud Logging API.
113
+ #
114
+ # ```ruby
115
+ # require "gcloud"
116
+ #
117
+ # gcloud = Gcloud.new
118
+ # logging = gcloud.logging
119
+ # entries = logging.entries filter: "log:syslog"
120
+ # entries.each do |e|
121
+ # puts "[#{e.timestamp}] #{e.payload.inspect}"
122
+ # end
123
+ # ```
124
+ #
125
+ # You can also order the log entries by `timestamp`.
126
+ #
127
+ # ```ruby
128
+ # require "gcloud"
129
+ #
130
+ # gcloud = Gcloud.new
131
+ # logging = gcloud.logging
132
+ # entries = logging.entries order: "timestamp desc"
133
+ # entries.each do |e|
134
+ # puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
135
+ # end
136
+ # ```
137
+ #
138
+ # ## Exporting log entries
139
+ #
140
+ # Cloud Logging lets you export log entries to destinations including Google
141
+ # Cloud Storage buckets (for long term log storage), Google BigQuery datasets
142
+ # (for log analysis), and Google Pub/Sub (for streaming to other
143
+ # applications).
144
+ #
145
+ # ### Creating sinks
146
+ #
147
+ # A {Gcloud::Logging::Sink} is an object that lets you to specify a set of log
148
+ # entries to export.
149
+ #
150
+ # In addition to the name of the sink and the export destination,
151
+ # {Gcloud::Logging::Project#create_sink} accepts an [advanced
152
+ # logs filter](https://cloud.google.com/logging/docs/view/advanced_filters) to
153
+ # narrow the collection.
154
+ #
155
+ # Before creating the sink, ensure that you have granted
156
+ # `cloud-logs@google.com` permission to write logs to the destination. See
157
+ # [Permissions for writing exported
158
+ # logs](https://cloud.google.com/logging/docs/export/configure_export#setting_product_name_short_permissions_for_writing_exported_logs).
159
+ #
160
+ # ```ruby
161
+ # require "gcloud"
162
+ #
163
+ # gcloud = Gcloud.new
164
+ # logging = gcloud.logging
165
+ # storage = gcloud.storage
166
+ #
167
+ # bucket = storage.create_bucket "my-logs-bucket"
168
+ #
169
+ # # Grant owner permission to Cloud Logging service
170
+ # email = "cloud-logs@google.com"
171
+ # bucket.acl.add_owner "group-#{email}"
172
+ #
173
+ # sink = logging.create_sink "my-sink", "storage.googleapis.com/#{bucket.id}"
174
+ # ```
175
+ #
176
+ # When you create a sink, only new log entries are exported. Cloud Logging
177
+ # does not send previously-ingested log entries to the sink's destination.
178
+ #
179
+ # ### Listing sinks
180
+ #
181
+ # You can also list the sinks belonging to your project with
182
+ # {Gcloud::Logging::Project#sinks}.
183
+ #
184
+ # ```ruby
185
+ # require "gcloud"
186
+ #
187
+ # gcloud = Gcloud.new
188
+ # logging = gcloud.logging
189
+ # sinks = logging.sinks
190
+ # sinks.each do |s|
191
+ # puts "#{s.name}: #{s.filter} -> #{s.destination}"
192
+ # end
193
+ # ```
194
+ #
195
+ # ## Creating logs-based metrics
196
+ #
197
+ # You can use log entries in your project as the basis for [Google Cloud
198
+ # Monitoring](https://cloud.google.com/monitoring/docs) metrics. These metrics
199
+ # can then be used to produce Cloud Monitoring reports and alerts.
200
+ #
201
+ # ### Creating metrics
202
+ #
203
+ # A metric is a measured value that can be used to assess a system. Use
204
+ # {Gcloud::Logging::Project#create_metric} to configure a
205
+ # {Gcloud::Logging::Metric} based on a collection of log entries matching an
206
+ # [advanced
207
+ # logs filter](https://cloud.google.com/logging/docs/view/advanced_filters).
208
+ #
209
+ # ```ruby
210
+ # require "gcloud"
211
+ #
212
+ # gcloud = Gcloud.new
213
+ # logging = gcloud.logging
214
+ # metric = logging.create_metric "errors", "severity>=ERROR"
215
+ # ```
216
+ #
217
+ # ### Listing metrics
218
+ #
219
+ # You can also list the metrics belonging to your project with
220
+ # {Gcloud::Logging::Project#metrics}.
221
+ #
222
+ # ```ruby
223
+ # require "gcloud"
224
+ #
225
+ # gcloud = Gcloud.new
226
+ # logging = gcloud.logging
227
+ # metrics = logging.metrics
228
+ # metrics.each do |m|
229
+ # puts "#{m.name}: #{m.filter}"
230
+ # end
231
+ # ```
232
+ #
233
+ # ## Writing log entries
234
+ #
235
+ # An {Gcloud::Logging::Entry} is composed of metadata and a payload. The
236
+ # payload is traditionally a message string, but in Cloud Logging it can
237
+ # also be a JSON or protocol buffer object. A single log can have entries with
238
+ # different payload types. In addition to the payload, your argument(s) to
239
+ # {Gcloud::Logging::Project#write_entries} must also contain a log name and a
240
+ # resource.
241
+ #
242
+ # ```ruby
243
+ # require "gcloud"
244
+ #
245
+ # gcloud = Gcloud.new
246
+ # logging = gcloud.logging
247
+ #
248
+ # entry = logging.entry
249
+ # entry.payload = "Job started."
250
+ # entry.log_name = "my_app_log"
251
+ # entry.resource.type = "gae_app"
252
+ # entry.resource.labels[:module_id] = "1"
253
+ # entry.resource.labels[:version_id] = "20150925t173233"
254
+ #
255
+ # logging.write_entries entry
256
+ # ```
257
+ #
258
+ # If you write a collection of log entries, you can provide the log name,
259
+ # resource, and/or labels hash to be used for all of the entries, and omit
260
+ # these values from the individual entries.
261
+ #
262
+ # ```ruby
263
+ # require "gcloud"
264
+ #
265
+ # gcloud = Gcloud.new
266
+ # logging = gcloud.logging
267
+ #
268
+ # entry1 = logging.entry
269
+ # entry1.payload = "Job started."
270
+ # entry2 = logging.entry
271
+ # entry2.payload = "Job completed."
272
+ # labels = { job_size: "large", job_code: "red" }
273
+ #
274
+ # resource = logging.resource "gae_app",
275
+ # "module_id" => "1",
276
+ # "version_id" => "20150925t173233"
277
+ #
278
+ # logging.write_entries [entry1, entry2],
279
+ # log_name: "my_app_log",
280
+ # resource: resource,
281
+ # labels: labels
282
+ # ```
283
+ #
284
+ # ### Creating a Ruby Logger implementation
285
+ #
286
+ # If your environment requires a logger instance that is API-compatible with
287
+ # Ruby's standard library
288
+ # [Logger](http://ruby-doc.org/stdlib/libdoc/logger/rdoc), you can use
289
+ # {Gcloud::Logging::Project#logger} to create one.
290
+ #
291
+ # ```ruby
292
+ # require "gcloud"
293
+ #
294
+ # gcloud = Gcloud.new
295
+ # logging = gcloud.logging
296
+ #
297
+ # resource = logging.resource "gae_app",
298
+ # module_id: "1",
299
+ # version_id: "20150925t173233"
300
+ #
301
+ # logger = logging.logger "my_app_log", resource, env: :production
302
+ # logger.info "Job started."
303
+ # ```
304
+ #
305
+ #
306
+ module Logging
307
+ end
308
+ end
@@ -0,0 +1,29 @@
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 "gcloud/credentials"
17
+
18
+ module Gcloud
19
+ module Logging
20
+ ##
21
+ # @private Represents the OAuth 2.0 signing logic for Logging.
22
+ class Credentials < Gcloud::Credentials
23
+ SCOPE = ["https://www.googleapis.com/auth/logging.admin"]
24
+ PATH_ENV_VARS = %w(LOGGING_KEYFILE GCLOUD_KEYFILE GOOGLE_CLOUD_KEYFILE)
25
+ JSON_ENV_VARS = %w(LOGGING_KEYFILE_JSON GCLOUD_KEYFILE_JSON
26
+ GOOGLE_CLOUD_KEYFILE_JSON)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,303 @@
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 "gcloud/logging/resource"
17
+ require "gcloud/logging/entry/http_request"
18
+ require "gcloud/logging/entry/operation"
19
+ require "gcloud/logging/entry/list"
20
+ require "gcloud/grpc_utils"
21
+
22
+ module Gcloud
23
+ module Logging
24
+ ##
25
+ # # Entry
26
+ #
27
+ # An individual entry in a log.
28
+ #
29
+ # Each log entry is composed of metadata and a payload. The metadata
30
+ # includes standard information used by Cloud Logging, such as when the
31
+ # entry was created and where it came from. The payload is the event record.
32
+ # Traditionally this is a message string, but in Cloud Logging it can also
33
+ # be a JSON or protocol buffer object. A single log can have entries with
34
+ # different payload types.
35
+ #
36
+ # A log is a named collection of entries. Logs can be produced by Google
37
+ # Cloud Platform services, by third-party services, or by your applications.
38
+ # For example, the log `compute.googleapis.com/activity_log` is produced by
39
+ # Google Compute Engine. Logs are simply referenced by name in Gcloud. There
40
+ # is no `Log` type in Gcloud or `Log` resource in the Cloud Logging API.
41
+ #
42
+ # @see https://cloud.google.com/logging/docs/view/logs_index List of Log
43
+ # Types
44
+ #
45
+ # @example
46
+ # require "gcloud"
47
+ #
48
+ # gcloud = Gcloud.new
49
+ # logging = gcloud.logging
50
+ #
51
+ # entry = logging.entry
52
+ # entry.payload = "Job started."
53
+ # entry.log_name = "my_app_log"
54
+ # entry.resource.type = "gae_app"
55
+ # entry.resource.labels[:module_id] = "1"
56
+ # entry.resource.labels[:version_id] = "20150925t173233"
57
+ #
58
+ # logging.write_entries entry
59
+ #
60
+ class Entry
61
+ ##
62
+ # Create a new Entry instance. The {#resource} attribute is
63
+ # pre-populated with a new {Gcloud::Logging::Resource} instance. See also
64
+ # {Gcloud::Logging::Project#entry}.
65
+ def initialize
66
+ @labels = {}
67
+ @resource = Resource.new
68
+ @http_request = HttpRequest.new
69
+ @operation = Operation.new
70
+ @severity = :DEFAULT
71
+ end
72
+
73
+ ##
74
+ # The resource name of the log to which this log entry belongs. The format
75
+ # of the name is `projects/<project-id>/logs/<log-id>`. e.g.
76
+ # `projects/my-projectid/logs/my_app_log` and
77
+ # `projects/1234567890/logs/library.googleapis.com%2Fbook_log`
78
+ #
79
+ # The log ID part of resource name must be less than 512 characters long
80
+ # and can only include the following characters: upper and lower case
81
+ # alphanumeric characters: `[A-Za-z0-9]`; and punctuation characters:
82
+ # forward-slash (`/`), underscore (`_`), hyphen (`-`), and period (`.`).
83
+ # Forward-slash (`/`) characters in the log ID must be URL-encoded.
84
+ attr_accessor :log_name
85
+
86
+ ##
87
+ # The monitored resource associated with this log entry. Example: a log
88
+ # entry that reports a database error would be associated with the
89
+ # monitored resource designating the particular database that reported the
90
+ # error.
91
+ # @return [Gcloud::Logging::Resource]
92
+ attr_reader :resource
93
+
94
+ ##
95
+ # The time the event described by the log entry occurred. If omitted,
96
+ # Cloud Logging will use the time the log entry is written.
97
+ attr_accessor :timestamp
98
+
99
+ ##
100
+ # The severity level of the log entry. The default value is `DEFAULT`.
101
+ attr_accessor :severity
102
+
103
+ ##
104
+ # Returns `true` if the severity level is `DEFAULT`.
105
+ def default?
106
+ severity == :DEFAULT
107
+ end
108
+
109
+ ##
110
+ # Returns `true` if the severity level is `DEBUG`.
111
+ def debug?
112
+ severity == :DEBUG
113
+ end
114
+
115
+ ##
116
+ # Returns `true` if the severity level is `INFO`.
117
+ def info?
118
+ severity == :INFO
119
+ end
120
+
121
+ ##
122
+ # Returns `true` if the severity level is `NOTICE`.
123
+ def notice?
124
+ severity == :NOTICE
125
+ end
126
+
127
+ ##
128
+ # Returns `true` if the severity level is `WARNING`.
129
+ def warning?
130
+ severity == :WARNING
131
+ end
132
+
133
+ ##
134
+ # Returns `true` if the severity level is `ERROR`.
135
+ def error?
136
+ severity == :ERROR
137
+ end
138
+
139
+ ##
140
+ # Returns `true` if the severity level is `CRITICAL`.
141
+ def critical?
142
+ severity == :CRITICAL
143
+ end
144
+
145
+ ##
146
+ # Returns `true` if the severity level is `ALERT`.
147
+ def alert?
148
+ severity == :ALERT
149
+ end
150
+
151
+ ##
152
+ # Returns `true` if the severity level is `EMERGENCY`.
153
+ def emergency?
154
+ severity == :EMERGENCY
155
+ end
156
+
157
+ ##
158
+ # A unique ID for the log entry. If you provide this field, the logging
159
+ # service considers other log entries in the same log with the same ID as
160
+ # duplicates which can be removed. If omitted, Cloud Logging will generate
161
+ # a unique ID for this log entry.
162
+ attr_accessor :insert_id
163
+
164
+ ##
165
+ # A set of user-defined data that provides additional information about
166
+ # the log entry.
167
+ # @return [Hash]
168
+ attr_accessor :labels
169
+
170
+ ##
171
+ # The log entry payload, represented as either a string, a hash (JSON), or
172
+ # a hash (protocol buffer).
173
+ # @return [String, Hash]
174
+ attr_accessor :payload
175
+
176
+ ##
177
+ # Information about the HTTP request associated with this log entry, if
178
+ # applicable.
179
+ # @return [Gcloud::Logging::Entry::HttpRequest]
180
+ attr_reader :http_request
181
+
182
+ ##
183
+ # Information about an operation associated with the log entry, if
184
+ # applicable.
185
+ # @return [Gcloud::Logging::Entry::Operation]
186
+ attr_reader :operation
187
+
188
+ ##
189
+ # @private Determines if the Entry has any data.
190
+ def empty?
191
+ log_name.nil? &&
192
+ timestamp.nil? &&
193
+ insert_id.nil? &&
194
+ (labels.nil? || labels.empty?) &&
195
+ payload.nil? &&
196
+ resource.empty? &&
197
+ http_request.empty? &&
198
+ operation.empty?
199
+ end
200
+
201
+ ##
202
+ # @private Exports the Entry to a Google::Logging::V2::LogEntry object.
203
+ def to_grpc
204
+ grpc = Google::Logging::V2::LogEntry.new(
205
+ log_name: log_name.to_s,
206
+ timestamp: timestamp_grpc,
207
+ # TODO: verify severity is the correct type?
208
+ severity: severity,
209
+ insert_id: insert_id.to_s,
210
+ labels: labels_grpc,
211
+ resource: resource.to_grpc,
212
+ http_request: http_request.to_grpc,
213
+ operation: operation.to_grpc
214
+ )
215
+ # Add payload
216
+ append_payload grpc
217
+ grpc
218
+ end
219
+
220
+ ##
221
+ # @private New Entry from a Google::Logging::V2::LogEntry object.
222
+ def self.from_grpc grpc
223
+ return new if grpc.nil?
224
+ new.tap do |e|
225
+ e.log_name = grpc.log_name
226
+ e.timestamp = extract_timestamp(grpc)
227
+ e.severity = grpc.severity
228
+ e.insert_id = grpc.insert_id
229
+ e.labels = map_to_hash(grpc.labels)
230
+ e.payload = extract_payload(grpc)
231
+ e.instance_eval do
232
+ @resource = Resource.from_grpc grpc.resource
233
+ @http_request = HttpRequest.from_grpc grpc.http_request
234
+ @operation = Operation.from_grpc grpc.operation
235
+ end
236
+ end
237
+ end
238
+
239
+ ##
240
+ # @private Convert a Google::Protobuf::Map to a Hash
241
+ def self.map_to_hash map
242
+ if map.respond_to? :to_h
243
+ map.to_h
244
+ else
245
+ # Enumerable doesn't have to_h on ruby 2.0...
246
+ Hash[map.to_a]
247
+ end
248
+ end
249
+
250
+ ##
251
+ # @private Formats the timestamp as a Google::Protobuf::Timestamp object.
252
+ def timestamp_grpc
253
+ return nil if timestamp.nil?
254
+ # TODO: ArgumentError if timestamp is not a Time object?
255
+ Google::Protobuf::Timestamp.new(
256
+ seconds: timestamp.to_i,
257
+ nanos: timestamp.nsec
258
+ )
259
+ end
260
+
261
+ ##
262
+ # @private Formats the labels so they can be saved to a
263
+ # Google::Logging::V2::LogEntry object.
264
+ def labels_grpc
265
+ # Coerce symbols to strings
266
+ Hash[labels.map do |k, v|
267
+ v = String(v) if v.is_a? Symbol
268
+ [String(k), v]
269
+ end]
270
+ end
271
+
272
+ ##
273
+ # @private Adds the payload data to a Google::Logging::V2::LogEntry
274
+ # object.
275
+ def append_payload grpc
276
+ grpc.proto_payload = nil
277
+ grpc.json_payload = nil
278
+ grpc.text_payload = nil
279
+
280
+ if payload.is_a? Google::Protobuf::Any
281
+ grpc.proto_payload = payload
282
+ elsif payload.respond_to? :to_hash
283
+ grpc.json_payload = GRPCUtils.hash_to_struct payload.to_hash
284
+ else
285
+ grpc.text_payload = payload.to_s
286
+ end
287
+ end
288
+
289
+ ##
290
+ # @private Extract payload data from Google API Client object.
291
+ def self.extract_payload grpc
292
+ grpc.proto_payload || grpc.json_payload || grpc.text_payload
293
+ end
294
+
295
+ ##
296
+ # @private Get a Time object from a Google::Protobuf::Timestamp object.
297
+ def self.extract_timestamp grpc
298
+ return nil if grpc.timestamp.nil?
299
+ Time.at grpc.timestamp.seconds, grpc.timestamp.nanos/1000.0
300
+ end
301
+ end
302
+ end
303
+ end