gcloud 0.6.3 → 0.7.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 (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,175 @@
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/metric/list"
17
+
18
+ module Gcloud
19
+ module Logging
20
+ ##
21
+ # # Metric
22
+ #
23
+ # A logs-based [Google Cloud
24
+ # Monitoring](https://cloud.google.com/monitoring/docs) metric. A metric is
25
+ # a measured value that can be used to assess a system. The basis of a
26
+ # logs-based metric is the collection of log entries that match a logs
27
+ # filter.
28
+ #
29
+ # @see https://cloud.google.com/logging/docs/view/logs_based_metrics
30
+ # Logs-based Metrics
31
+ # @see https://cloud.google.com/monitoring/docs Google Cloud Monitoring
32
+ #
33
+ # @example
34
+ # require "gcloud"
35
+ #
36
+ # gcloud = Gcloud.new
37
+ # logging = gcloud.logging
38
+ # metric = logging.create_metric "errors", "severity>=ERROR"
39
+ #
40
+ class Metric
41
+ ##
42
+ # @private The gRPC Service object.
43
+ attr_accessor :service
44
+
45
+ ##
46
+ # @private The gRPC Google::Logging::V2::LogMetric object.
47
+ attr_accessor :grpc
48
+
49
+ ##
50
+ # @private Create an empty Metric object.
51
+ def initialize
52
+ @service = nil
53
+ @grpc = Google::Logging::V2::LogMetric.new
54
+ end
55
+
56
+ ##
57
+ # The client-assigned metric identifier. Metric identifiers are limited to
58
+ # 1000 characters and can include only the following characters: `A-Z`,
59
+ # `a-z`, `0-9`, and the special characters `_-.,+!*',()%/\`. The
60
+ # forward-slash character (`/`) denotes a hierarchy of name pieces, and it
61
+ # cannot be the first character of the name.
62
+ def name
63
+ grpc.name
64
+ end
65
+
66
+ ##
67
+ # The description of this metric, which is used in documentation.
68
+ def description
69
+ grpc.description
70
+ end
71
+
72
+ ##
73
+ # Updates the description of this metric, which is used in documentation.
74
+ def description= description
75
+ grpc.description = description
76
+ end
77
+
78
+ ##
79
+ # An [advanced logs
80
+ # filter](https://cloud.google.com/logging/docs/view/advanced_filters).
81
+ def filter
82
+ grpc.filter
83
+ end
84
+
85
+ ##
86
+ # Updates the [advanced logs
87
+ # filter](https://cloud.google.com/logging/docs/view/advanced_filters).
88
+ def filter= filter
89
+ grpc.filter = filter
90
+ end
91
+
92
+ ##
93
+ # Updates the logs-based metric.
94
+ #
95
+ # @example
96
+ # require "gcloud"
97
+ #
98
+ # gcloud = Gcloud.new
99
+ # logging = gcloud.logging
100
+ # metric = logging.metric "severe_errors"
101
+ # metric.filter = "logName:syslog AND severity>=ERROR"
102
+ # metric.save
103
+ #
104
+ def save
105
+ ensure_service!
106
+ @grpc = service.update_metric name, description, filter
107
+ return true
108
+ rescue GRPC::BadStatus => e
109
+ raise Gcloud::Error.from_error(e)
110
+ end
111
+
112
+ ##
113
+ # Reloads the logs-based metric with current data from the Logging
114
+ # service.
115
+ #
116
+ # @example
117
+ # require "gcloud"
118
+ #
119
+ # gcloud = Gcloud.new
120
+ # logging = gcloud.logging
121
+ # metric = logging.metric "severe_errors"
122
+ # metric.filter = "Unwanted value"
123
+ # metric.reload!
124
+ # metric.filter #=> "logName:syslog"
125
+ #
126
+ def reload!
127
+ ensure_service!
128
+ @grpc = service.get_metric name
129
+ return true
130
+ rescue GRPC::BadStatus => e
131
+ raise Gcloud::Error.from_error(e)
132
+ end
133
+ alias_method :refresh!, :reload!
134
+
135
+ ##
136
+ # Permanently deletes the logs-based metric.
137
+ #
138
+ # @return [Boolean] Returns `true` if the metric was deleted.
139
+ #
140
+ # @example
141
+ # require "gcloud"
142
+ #
143
+ # gcloud = Gcloud.new
144
+ # logging = gcloud.logging
145
+ # metric = logging.metric "severe_errors"
146
+ # metric.delete
147
+ #
148
+ def delete
149
+ ensure_service!
150
+ service.delete_metric name
151
+ return true
152
+ rescue GRPC::BadStatus => e
153
+ raise Gcloud::Error.from_error(e)
154
+ end
155
+
156
+ ##
157
+ # @private New Metric from a gRPC object.
158
+ def self.from_grpc grpc, service
159
+ new.tap do |m|
160
+ m.grpc = grpc
161
+ m.service = service
162
+ end
163
+ end
164
+
165
+ protected
166
+
167
+ ##
168
+ # @private Raise an error unless an active connection to the service is
169
+ # available.
170
+ def ensure_service!
171
+ fail "Must have active connection to service" unless service
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,98 @@
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 Metric
21
+ ##
22
+ # Metric::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 Metric::List with an array of Metric instances.
31
+ def initialize arr = []
32
+ super arr
33
+ end
34
+
35
+ ##
36
+ # Whether there is a next page of metrics.
37
+ def next?
38
+ !token.nil?
39
+ end
40
+
41
+ ##
42
+ # Retrieve the next page of metrics.
43
+ def next
44
+ return nil unless next?
45
+ ensure_service!
46
+ grpc = @service.list_metrics token: token
47
+ self.class.from_grpc grpc, @service
48
+ rescue GRPC::BadStatus => e
49
+ raise Gcloud::Error.from_error(e)
50
+ end
51
+
52
+ ##
53
+ # Retrieves all metrics by repeatedly loading {#next?} until {#next?}
54
+ # returns `false`. Returns the list instance for method chaining.
55
+ #
56
+ # @example
57
+ # require "gcloud"
58
+ #
59
+ # gcloud = Gcloud.new
60
+ # logging = gcloud.logging
61
+ # all_metrics = logging.metrics.all # Load all pages of metrics
62
+ #
63
+ def all
64
+ while next?
65
+ next_records = self.next
66
+ push(*next_records)
67
+ self.token = next_records.token
68
+ end
69
+ self
70
+ end
71
+
72
+ ##
73
+ # @private New Metric::List from a
74
+ # Google::Logging::V2::ListLogMetricsResponse object.
75
+ def self.from_grpc grpc_list, service
76
+ metrics = new(Array(grpc_list.metrics).map do |grpc_metric|
77
+ Metric.from_grpc grpc_metric, service
78
+ end)
79
+ metrics.instance_eval do
80
+ @token = grpc_list.next_page_token
81
+ @token = nil if @token == ""
82
+ @service = service
83
+ end
84
+ metrics
85
+ end
86
+
87
+ protected
88
+
89
+ ##
90
+ # @private Raise an error unless an active connection to the service is
91
+ # available.
92
+ def ensure_service!
93
+ fail "Must have active connection to service" unless @service
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,650 @@
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/errors"
17
+ require "gcloud/gce"
18
+ require "gcloud/logging/service"
19
+ require "gcloud/logging/credentials"
20
+ require "gcloud/logging/entry"
21
+ require "gcloud/logging/resource_descriptor"
22
+ require "gcloud/logging/sink"
23
+ require "gcloud/logging/metric"
24
+ require "gcloud/logging/logger"
25
+
26
+ module Gcloud
27
+ module Logging
28
+ ##
29
+ # # Project
30
+ #
31
+ # Projects are top-level containers in Google Cloud Platform. They store
32
+ # information about billing and authorized users, and they control access to
33
+ # Google Cloud Logging resources. Each project has a friendly name and a
34
+ # unique ID. Projects can be created only in the [Google Developers
35
+ # Console](https://console.developers.google.com). See {Gcloud#logging}.
36
+ #
37
+ # @example
38
+ # require "gcloud"
39
+ #
40
+ # gcloud = Gcloud.new
41
+ # logging = gcloud.logging
42
+ # entries = logging.entries
43
+ #
44
+ # See Gcloud#logging
45
+ class Project
46
+ ##
47
+ # @private The gRPC Service object.
48
+ attr_accessor :service
49
+
50
+ ##
51
+ # @private Creates a new Connection instance.
52
+ def initialize project, credentials
53
+ project = project.to_s # Always cast to a string
54
+ fail ArgumentError, "project is missing" if project.empty?
55
+ @service = Service.new project, credentials
56
+ end
57
+
58
+ ##
59
+ # The ID of the current project.
60
+ #
61
+ # @return [String] the Google Cloud project ID
62
+ #
63
+ # @example
64
+ # require "gcloud"
65
+ #
66
+ # gcloud = Gcloud.new "my-project", "/path/to/keyfile.json"
67
+ # logging = gcloud.logging
68
+ #
69
+ # logging.project #=> "my-project"
70
+ #
71
+ def project
72
+ service.project
73
+ end
74
+
75
+ ##
76
+ # @private Default project.
77
+ def self.default_project
78
+ ENV["LOGGING_PROJECT"] ||
79
+ ENV["GCLOUD_PROJECT"] ||
80
+ ENV["GOOGLE_CLOUD_PROJECT"] ||
81
+ Gcloud::GCE.project_id
82
+ end
83
+
84
+ ##
85
+ # Lists log entries. Use this method to retrieve log entries from Cloud
86
+ # Logging.
87
+ #
88
+ # @param [String, Array] projects One or more project IDs or project
89
+ # numbers from which to retrieve log entries. If `nil`, the ID of the
90
+ # receiving project instance will be used.
91
+ # @param [String] filter An [advanced logs
92
+ # filter](https://cloud.google.com/logging/docs/view/advanced_filters).
93
+ # The filter is compared against all log entries in the projects
94
+ # specified by `projects`. Only entries that match the filter are
95
+ # retrieved. An empty filter matches all log entries.
96
+ # @param [String] order How the results should be sorted. Presently, the
97
+ # only permitted values are "timestamp" (default) and "timestamp desc".
98
+ # @param [String] token A previously-returned page token representing part
99
+ # of the larger set of results to view.
100
+ # @param [Integer] max Maximum number of entries to return.
101
+ #
102
+ # @return [Array<Gcloud::Logging::Entry>] (See
103
+ # {Gcloud::Logging::Entry::List})
104
+ #
105
+ # @example
106
+ # require "gcloud"
107
+ #
108
+ # gcloud = Gcloud.new
109
+ # logging = gcloud.logging
110
+ # entries = logging.entries
111
+ # entries.each do |e|
112
+ # puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
113
+ # end
114
+ #
115
+ # @example You can use a filter to narrow results to a single log.
116
+ # require "gcloud"
117
+ #
118
+ # gcloud = Gcloud.new
119
+ # logging = gcloud.logging
120
+ # entries = logging.entries filter: "log:syslog"
121
+ # entries.each do |e|
122
+ # puts "[#{e.timestamp}] #{e.payload.inspect}"
123
+ # end
124
+ #
125
+ # @example You can also order the results by timestamp.
126
+ # require "gcloud"
127
+ #
128
+ # gcloud = Gcloud.new
129
+ # logging = gcloud.logging
130
+ # entries = logging.entries order: "timestamp desc"
131
+ # entries.each do |e|
132
+ # puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
133
+ # end
134
+ #
135
+ # @example With pagination: (See {Gcloud::Logging::Entry::List})
136
+ # require "gcloud"
137
+ #
138
+ # gcloud = Gcloud.new
139
+ # logging = gcloud.logging
140
+ # entries = logging.entries
141
+ # loop do
142
+ # entries.each do |e|
143
+ # puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
144
+ # end
145
+ # break unless entries.next?
146
+ # entries = entries.next
147
+ # end
148
+ #
149
+ def entries projects: nil, filter: nil, order: nil, token: nil, max: nil
150
+ ensure_service!
151
+ list_grpc = service.list_entries projects: projects, filter: filter,
152
+ order: order, token: token, max: max
153
+ Entry::List.from_grpc list_grpc, service, projects: projects, max: max,
154
+ filter: filter, order: order
155
+ rescue GRPC::BadStatus => e
156
+ raise Gcloud::Error.from_error(e)
157
+ end
158
+ alias_method :find_entries, :entries
159
+
160
+ ##
161
+ # Creates an new Entry instance that may be populated and written to the
162
+ # Cloud Logging service. The {Entry#resource} attribute is pre-populated
163
+ # with a new {Gcloud::Logging::Resource} instance. Equivalent to calling
164
+ # `Gcloud::Logging::Entry.new`.
165
+ #
166
+ # @return [Gcloud::Logging::Entry] a new Entry instance
167
+ #
168
+ # @example
169
+ # require "gcloud"
170
+ #
171
+ # gcloud = Gcloud.new
172
+ # logging = gcloud.logging
173
+ #
174
+ # entry = logging.entry
175
+ # entry.severity = :INFO
176
+ # entry.payload = "Job started."
177
+ #
178
+ # logging.write_entries entry
179
+ #
180
+ def entry
181
+ Entry.new
182
+ end
183
+ alias_method :new_entry, :entry
184
+
185
+ ##
186
+ # Writes log entries to the Cloud Logging service.
187
+ #
188
+ # If you write a collection of log entries, you can provide the log name,
189
+ # resource, and/or labels hash to be used for all of the entries, and omit
190
+ # these values from the individual entries.
191
+ #
192
+ # @param [Gcloud::Logging::Entry, Array<Gcloud::Logging::Entry>] entries
193
+ # One or more entry objects to write. The log entries must have values
194
+ # for all required fields.
195
+ # @param [String] log_name A default log ID for those log entries in
196
+ # `entries` that do not specify their own `log_name`. See also
197
+ # {Entry#log_name=}.
198
+ # @param [Resource] resource A default monitored resource for those log
199
+ # entries in entries that do not specify their own resource. See also
200
+ # {Entry#resource}.
201
+ # @param [Hash{Symbol,String => String}] labels User-defined `key:value`
202
+ # items that are added to the `labels` field of each log entry in
203
+ # `entries`, except when a log entry specifies its own `key:value` item
204
+ # with the same key. See also {Entry#labels=}.
205
+ #
206
+ # @return [Boolean] Returns `true` if the entries were written.
207
+ #
208
+ # @example
209
+ # require "gcloud"
210
+ #
211
+ # gcloud = Gcloud.new
212
+ # logging = gcloud.logging
213
+ #
214
+ # entry = logging.entry
215
+ # entry.payload = "Job started."
216
+ # entry.log_name = "my_app_log"
217
+ # entry.resource.type = "gae_app"
218
+ # entry.resource.labels[:module_id] = "1"
219
+ # entry.resource.labels[:version_id] = "20150925t173233"
220
+ #
221
+ # logging.write_entries entry
222
+ #
223
+ # @example Optionally pass log name, resource, and labels for all entries.
224
+ # require "gcloud"
225
+ #
226
+ # gcloud = Gcloud.new
227
+ # logging = gcloud.logging
228
+ #
229
+ # entry1 = logging.entry
230
+ # entry1.payload = "Job started."
231
+ # entry2 = logging.entry
232
+ # entry2.payload = "Job completed."
233
+ # labels = { job_size: "large", job_code: "red" }
234
+ #
235
+ # resource = logging.resource "gae_app",
236
+ # "module_id" => "1",
237
+ # "version_id" => "20150925t173233"
238
+ #
239
+ # logging.write_entries [entry1, entry2],
240
+ # log_name: "my_app_log",
241
+ # resource: resource,
242
+ # labels: labels
243
+ #
244
+ def write_entries entries, log_name: nil, resource: nil, labels: nil
245
+ ensure_service!
246
+ service.write_entries Array(entries).map(&:to_grpc),
247
+ log_name: log_name, resource: resource,
248
+ labels: labels
249
+ return true
250
+ rescue GRPC::BadStatus => e
251
+ raise Gcloud::Error.from_error(e)
252
+ end
253
+
254
+ ##
255
+ # Creates a logger instance that is API-compatible with Ruby's standard
256
+ # library [Logger](http://ruby-doc.org/stdlib/libdoc/logger/rdoc).
257
+ #
258
+ # @param [String] log_name A log resource name to be associated with the
259
+ # written log entries.
260
+ # @param [Gcloud::Logging::Resource] resource The monitored resource to be
261
+ # associated with written log entries.
262
+ # @param [Hash] labels A set of user-defined data to be associated with
263
+ # written log entries.
264
+ #
265
+ # @return [Gcloud::Logging::Logger] a Logger object that can be used in
266
+ # place of a ruby standard library logger object.
267
+ #
268
+ # @example
269
+ # require "gcloud"
270
+ #
271
+ # gcloud = Gcloud.new
272
+ # logging = gcloud.logging
273
+ #
274
+ # resource = logging.resource "gae_app",
275
+ # module_id: "1",
276
+ # version_id: "20150925t173233"
277
+ #
278
+ # logger = logging.logger "my_app_log", resource, env: :production
279
+ # logger.info "Job started."
280
+ #
281
+ def logger log_name, resource, labels = {}
282
+ Logger.new self, log_name, resource, labels
283
+ end
284
+
285
+ ##
286
+ # Deletes a log and all its log entries. The log will reappear if it
287
+ # receives new entries.
288
+ #
289
+ # @param [String] name The name of the log, which may be the full path
290
+ # including the project ID (`projects/<project-id>/logs/<log-id>`), or
291
+ # just the short name (`<log-id>`), in which case the beginning of the
292
+ # path will be automatically prepended, using the ID of the current
293
+ # project.
294
+ #
295
+ # @return [Boolean] Returns `true` if the log and all its log entries were
296
+ # deleted.
297
+ #
298
+ # @example
299
+ # require "gcloud"
300
+ #
301
+ # gcloud = Gcloud.new
302
+ # logging = gcloud.logging
303
+ # logging.delete_log "my_app_log"
304
+ #
305
+ def delete_log name
306
+ ensure_service!
307
+ service.delete_log name
308
+ return true
309
+ rescue GRPC::BadStatus => e
310
+ raise Gcloud::Error.from_error(e)
311
+ end
312
+
313
+ ##
314
+ # Retrieves the list of monitored resource descriptors that are used by
315
+ # Google Cloud Logging.
316
+ #
317
+ # @see https://cloud.google.com/logging/docs/api/introduction_v2#monitored_resources
318
+ # Monitored Resources
319
+ #
320
+ # @param [String] token A previously-returned page token representing part
321
+ # of the larger set of results to view.
322
+ # @param [Integer] max Maximum number of resource descriptors to return.
323
+ #
324
+ # @return [Array<Gcloud::Logging::ResourceDescriptor>] (See
325
+ # {Gcloud::Logging::ResourceDescriptor::List})
326
+ #
327
+ # @example
328
+ # require "gcloud"
329
+ #
330
+ # gcloud = Gcloud.new
331
+ # logging = gcloud.logging
332
+ # resource_descriptors = logging.resource_descriptors
333
+ # resource_descriptors.each do |rd|
334
+ # label_keys = rd.labels.map(&:key).join(", ")
335
+ # puts "#{rd.type} (#{label_keys})"
336
+ # end
337
+ #
338
+ # @example Pagination: (See {Gcloud::Logging::ResourceDescriptor::List})
339
+ # require "gcloud"
340
+ #
341
+ # gcloud = Gcloud.new
342
+ # logging = gcloud.logging
343
+ # resource_descriptors = logging.resource_descriptors
344
+ # loop do
345
+ # resource_descriptors.each do |rd|
346
+ # puts rd.type
347
+ # end
348
+ # break unless resource_descriptors.next?
349
+ # resource_descriptors = resource_descriptors.next
350
+ # end
351
+ #
352
+ def resource_descriptors token: nil, max: nil
353
+ ensure_service!
354
+ list_grpc = service.list_resource_descriptors token: token, max: max
355
+ ResourceDescriptor::List.from_grpc list_grpc, service
356
+ rescue GRPC::BadStatus => e
357
+ raise Gcloud::Error.from_error(e)
358
+ end
359
+ alias_method :find_resource_descriptors, :resource_descriptors
360
+
361
+ ##
362
+ # Creates a new monitored resource instance.
363
+ #
364
+ # @return [Gcloud::Logging::Resource]
365
+ #
366
+ # @example
367
+ # require "gcloud"
368
+ #
369
+ # gcloud = Gcloud.new
370
+ # logging = gcloud.logging
371
+ #
372
+ # resource = logging.resource "gae_app",
373
+ # "module_id" => "1",
374
+ # "version_id" => "20150925t173233"
375
+ #
376
+ def resource type, labels = {}
377
+ Resource.new.tap do |r|
378
+ r.type = type
379
+ r.labels = labels
380
+ end
381
+ end
382
+ alias_method :new_resource, :resource
383
+
384
+ ##
385
+ # Retrieves the list of sinks belonging to the project.
386
+ #
387
+ # @param [String] token A previously-returned page token representing part
388
+ # of the larger set of results to view.
389
+ # @param [Integer] max Maximum number of sinks to return.
390
+ #
391
+ # @return [Array<Gcloud::Logging::Sink>] (See
392
+ # {Gcloud::Logging::Sink::List})
393
+ #
394
+ # @example
395
+ # require "gcloud"
396
+ #
397
+ # gcloud = Gcloud.new
398
+ # logging = gcloud.logging
399
+ # sinks = logging.sinks
400
+ # sinks.each do |s|
401
+ # puts "#{s.name}: #{s.filter} -> #{s.destination}"
402
+ # end
403
+ #
404
+ # @example With pagination: (See {Gcloud::Logging::Sink::List})
405
+ # require "gcloud"
406
+ #
407
+ # gcloud = Gcloud.new
408
+ # logging = gcloud.logging
409
+ # sinks = logging.sinks
410
+ # loop do
411
+ # sinks.each do |s|
412
+ # puts "#{s.name}: #{s.filter} -> #{s.destination}"
413
+ # end
414
+ # break unless sinks.next?
415
+ # sinks = sinks.next
416
+ # end
417
+ #
418
+ def sinks token: nil, max: nil
419
+ ensure_service!
420
+ list_grpc = service.list_sinks token: token, max: max
421
+ Sink::List.from_grpc list_grpc, service
422
+ rescue GRPC::BadStatus => e
423
+ raise Gcloud::Error.from_error(e)
424
+ end
425
+ alias_method :find_sinks, :sinks
426
+
427
+ ##
428
+ # Creates a new project sink. When you create a sink, only new log entries
429
+ # that match the sink's filter are exported. Cloud Logging does not send
430
+ # previously-ingested log entries to the sink's destination.
431
+ #
432
+ # Before creating the sink, ensure that you have granted
433
+ # `cloud-logs@google.com` permission to write logs to the destination. See
434
+ # [Permissions for writing exported
435
+ # logs](https://cloud.google.com/logging/docs/export/configure_export#setting_product_name_short_permissions_for_writing_exported_logs).
436
+ #
437
+ # @see https://cloud.google.com/logging/docs/api/tasks/exporting-logs
438
+ # Exporting Logs With Sinks
439
+ # @see https://cloud.google.com/logging/docs/api/introduction_v2#kinds_of_log_sinks
440
+ # Kinds of log sinks (API V2)
441
+ # @see https://cloud.google.com/logging/docs/api/#sinks Sinks (API V1)
442
+ # @see https://cloud.google.com/logging/docs/export/configure_export#setting_product_name_short_permissions_for_writing_exported_logs
443
+ # Permissions for writing exported logs
444
+ #
445
+ # @param [String] name The client-assigned sink identifier. Sink
446
+ # identifiers are limited to 1000 characters and can include only the
447
+ # following characters: `A-Z`, `a-z`, `0-9`, and the special characters
448
+ # `_-.`.
449
+ # @param [String] destination The resource name of the export destination.
450
+ # See [About
451
+ # sinks](https://cloud.google.com/logging/docs/api/tasks/exporting-logs#about_sinks)
452
+ # for examples.
453
+ # @param [String, nil] filter An [advanced logs
454
+ # filter](https://cloud.google.com/logging/docs/view/advanced_filters)
455
+ # that defines the log entries to be exported. The filter must be
456
+ # consistent with the log entry format designed by the `version`
457
+ # parameter, regardless of the format of the log entry that was
458
+ # originally written to Cloud Logging.
459
+ # @param [Symbol] version The log entry version used when exporting log
460
+ # entries from this sink. This version does not have to correspond to
461
+ # the version of the log entry when it was written to Cloud Logging.
462
+ # Accepted values are `:unspecified`, `:v2`, and `:v1`. Version 2 is
463
+ # currently the preferred format. An unspecified version format
464
+ # currently defaults to V2 in the service. The default value is
465
+ # `:unspecified`.
466
+ #
467
+ # @return [Gcloud::Logging::Sink] a project sink
468
+ #
469
+ # @example
470
+ # require "gcloud"
471
+ #
472
+ # gcloud = Gcloud.new
473
+ # logging = gcloud.logging
474
+ # storage = gcloud.storage
475
+ #
476
+ # bucket = storage.create_bucket "my-logs-bucket"
477
+ #
478
+ # # Grant owner permission to Cloud Logging service
479
+ # email = "cloud-logs@google.com"
480
+ # bucket.acl.add_owner "group-#{email}"
481
+ #
482
+ # sink = logging.create_sink "my-sink",
483
+ # "storage.googleapis.com/#{bucket.id}"
484
+ #
485
+ def create_sink name, destination, filter: nil, version: :unspecified
486
+ version = Sink.resolve_version version
487
+ ensure_service!
488
+ grpc = service.create_sink name, destination, filter, version
489
+ Sink.from_grpc grpc, service
490
+ rescue GRPC::BadStatus => e
491
+ raise Gcloud::Error.from_error(e)
492
+ end
493
+ alias_method :new_sink, :create_sink
494
+
495
+ ##
496
+ # Retrieves a sink by name.
497
+ #
498
+ # @param [String] sink_name Name of a sink.
499
+ #
500
+ # @return [Gcloud::Logging::Sink, nil] Returns `nil` if the sink does not
501
+ # exist.
502
+ #
503
+ # @example
504
+ # require "gcloud"
505
+ #
506
+ # gcloud = Gcloud.new
507
+ # logging = gcloud.logging
508
+ # sink = logging.sink "existing-sink"
509
+ #
510
+ # @example By default `nil` will be returned if the sink does not exist.
511
+ # require "gcloud"
512
+ #
513
+ # gcloud = Gcloud.new
514
+ # logging = gcloud.logging
515
+ # sink = logging.sink "non-existing-sink" #=> nil
516
+ #
517
+ def sink sink_name
518
+ ensure_service!
519
+ grpc = service.get_sink sink_name
520
+ Sink.from_grpc grpc, service
521
+ rescue GRPC::BadStatus => e
522
+ return nil if e.code == 5
523
+ raise Gcloud::Error.from_error(e)
524
+ end
525
+ alias_method :get_sink, :sink
526
+ alias_method :find_sink, :sink
527
+
528
+ ##
529
+ # Retrieves the list of metrics belonging to the project.
530
+ #
531
+ # @param [String] token A previously-returned page token representing part
532
+ # of the larger set of results to view.
533
+ # @param [Integer] max Maximum number of metrics to return.
534
+ #
535
+ # @return [Array<Gcloud::Logging::Metric>] (See
536
+ # {Gcloud::Logging::Metric::List})
537
+ #
538
+ # @example
539
+ # require "gcloud"
540
+ #
541
+ # gcloud = Gcloud.new
542
+ # logging = gcloud.logging
543
+ # metrics = logging.metrics
544
+ # metrics.each do |m|
545
+ # puts "#{m.name}: #{m.filter}"
546
+ # end
547
+ #
548
+ # @example With pagination: (See {Gcloud::Logging::Metric::List})
549
+ # require "gcloud"
550
+ #
551
+ # gcloud = Gcloud.new
552
+ # logging = gcloud.logging
553
+ # metrics = logging.metrics
554
+ # loop do
555
+ # metrics.each do |m|
556
+ # puts "#{m.name}: #{m.filter}"
557
+ # end
558
+ # break unless metrics.next?
559
+ # metrics = metrics.next
560
+ # end
561
+ #
562
+ def metrics token: nil, max: nil
563
+ ensure_service!
564
+ grpc = service.list_metrics token: token, max: max
565
+ Metric::List.from_grpc grpc, service
566
+ rescue GRPC::BadStatus => e
567
+ raise Gcloud::Error.from_error(e)
568
+ end
569
+ alias_method :find_metrics, :metrics
570
+
571
+ ##
572
+ # Creates a new logs-based metric for Google Cloud Monitoring.
573
+ #
574
+ # @see https://cloud.google.com/logging/docs/view/logs_based_metrics
575
+ # Logs-based Metrics
576
+ # @see https://cloud.google.com/monitoring/docs Google Cloud Monitoring
577
+ #
578
+ # @param [String] name The client-assigned metric identifier. Metric
579
+ # identifiers are limited to 1000 characters and can include only the
580
+ # following characters: `A-Z`, `a-z`, `0-9`, and the special characters
581
+ # `_-.,+!*',()%/\`. The forward-slash character (`/`) denotes a
582
+ # hierarchy of name pieces, and it cannot be the first character of the
583
+ # name.
584
+ # @param [String] filter An [advanced logs
585
+ # filter](https://cloud.google.com/logging/docs/view/advanced_filters).
586
+ # @param [String, nil] description A description of this metric, which is
587
+ # used in documentation.
588
+ #
589
+ # @return [Gcloud::Logging::Metric]
590
+ #
591
+ # @example
592
+ # require "gcloud"
593
+ #
594
+ # gcloud = Gcloud.new
595
+ # logging = gcloud.logging
596
+ # metric = logging.create_metric "errors", "severity>=ERROR"
597
+ #
598
+ def create_metric name, filter, description: nil
599
+ ensure_service!
600
+ grpc = service.create_metric name, filter, description
601
+ Metric.from_grpc grpc, service
602
+ rescue GRPC::BadStatus => e
603
+ raise Gcloud::Error.from_error(e)
604
+ end
605
+ alias_method :new_metric, :create_metric
606
+
607
+ ##
608
+ # Retrieves metric by name.
609
+ #
610
+ # @param [String] name Name of a metric.
611
+ #
612
+ # @return [Gcloud::Logging::Metric, nil] Returns `nil` if metric does not
613
+ # exist.
614
+ #
615
+ # @example
616
+ # require "gcloud"
617
+ #
618
+ # gcloud = Gcloud.new
619
+ # logging = gcloud.logging
620
+ # metric = logging.metric "existing_metric"
621
+ #
622
+ # @example By default `nil` will be returned if the metric does not exist.
623
+ # require "gcloud"
624
+ #
625
+ # gcloud = Gcloud.new
626
+ # logging = gcloud.logging
627
+ # metric = logging.metric "non_existing_metric" #=> nil
628
+ #
629
+ def metric name
630
+ ensure_service!
631
+ grpc = service.get_metric name
632
+ Metric.from_grpc grpc, service
633
+ rescue GRPC::BadStatus => e
634
+ return nil if e.code == 5
635
+ raise Gcloud::Error.from_error(e)
636
+ end
637
+ alias_method :get_metric, :metric
638
+ alias_method :find_metric, :metric
639
+
640
+ protected
641
+
642
+ ##
643
+ # @private Raise an error unless an active connection to the service is
644
+ # available.
645
+ def ensure_service!
646
+ fail "Must have active connection to service" unless service
647
+ end
648
+ end
649
+ end
650
+ end