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,95 @@
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
+ # # Resource
20
+ #
21
+ # A monitored resource is an abstraction used to characterize many kinds of
22
+ # objects in your cloud infrastructure, including Google Cloud SQL
23
+ # databases, Google App Engine apps, Google Compute Engine virtual machine
24
+ # instances, and so forth. Each of those kinds of objects is described by an
25
+ # instance of {ResourceDescriptor}.
26
+ #
27
+ # For use with {Gcloud::Logging::Entry#resource},
28
+ # {Gcloud::Logging::Project#resource}, and
29
+ # {Gcloud::Logging::Project#write_entries}.
30
+ #
31
+ # @example
32
+ # require "gcloud"
33
+ #
34
+ # gcloud = Gcloud.new
35
+ # logging = gcloud.logging
36
+ # resource = logging.resource "gae_app",
37
+ # "module_id" => "1",
38
+ # "version_id" => "20150925t173233"
39
+ #
40
+ class Resource
41
+ ##
42
+ # Create an empty Resource object.
43
+ def initialize
44
+ @labels = {}
45
+ end
46
+
47
+ ##
48
+ # The type of resource, as represented by a {ResourceDescriptor}.
49
+ attr_accessor :type
50
+
51
+ ##
52
+ # A set of labels that can be used to describe instances of this monitored
53
+ # resource type.
54
+ attr_accessor :labels
55
+
56
+ ##
57
+ # @private Determines if the Resource has any data.
58
+ def empty?
59
+ type.nil? && (labels.nil? || labels.empty?)
60
+ end
61
+
62
+ ##
63
+ # @private Exports the Resource to a Google::Api::MonitoredResource
64
+ # object.
65
+ def to_grpc
66
+ return nil if empty?
67
+ Google::Api::MonitoredResource.new(
68
+ type: type,
69
+ labels: Hash[labels.map { |k, v| [String(k), String(v)] }]
70
+ )
71
+ end
72
+
73
+ ##
74
+ # @private New Resource from a Google::Api::MonitoredResource object.
75
+ def self.from_grpc grpc
76
+ return new if grpc.nil?
77
+ new.tap do |r|
78
+ r.type = grpc.type
79
+ r.labels = map_to_hash(grpc.labels)
80
+ end
81
+ end
82
+
83
+ ##
84
+ # @private Convert a Google::Protobuf::Map to a Hash
85
+ def self.map_to_hash map
86
+ if map.respond_to? :to_h
87
+ map.to_h
88
+ else
89
+ # Enumerable doesn't have to_h on ruby 2.0...
90
+ Hash[map.to_a]
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,140 @@
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_descriptor/list"
17
+
18
+ module Gcloud
19
+ module Logging
20
+ ##
21
+ # # ResourceDescriptor
22
+ #
23
+ # Describes a type of monitored resource supported by Cloud Logging. Each
24
+ # ResourceDescriptor has a type name, such as `cloudsql_database`,
25
+ # `gae_app`, or `gce_instance`. It also specifies a set of labels that must
26
+ # all be given values in a {Resource} instance to represent an actual
27
+ # instance of the type.
28
+ #
29
+ # ResourceDescriptor instances are read-only. You cannot create your own
30
+ # instances, but you can list them with {Project#resource_descriptors}.
31
+ #
32
+ # @example
33
+ # require "gcloud"
34
+ #
35
+ # gcloud = Gcloud.new
36
+ # logging = gcloud.logging
37
+ # resource_descriptor = logging.resource_descriptors.first
38
+ # resource_descriptor.type #=> "cloudsql_database"
39
+ # resource_descriptor.name #=> "Cloud SQL Database"
40
+ # resource_descriptor.labels.map &:key #=> ["database_id", "region"]
41
+ #
42
+ class ResourceDescriptor
43
+ ##
44
+ # @private New ResourceDescriptor from a Google API Client object.
45
+ def initialize
46
+ @labels = []
47
+ end
48
+
49
+ ##
50
+ # The monitored resource type. For example, `cloudsql_database`.
51
+ attr_reader :type
52
+
53
+ ##
54
+ # A display name for the monitored resource type. For example,
55
+ # `Cloud SQL Database`.
56
+ attr_reader :name
57
+
58
+ ##
59
+ # A detailed description of the monitored resource type, which is used in
60
+ # documentation.
61
+ attr_reader :description
62
+
63
+ ##
64
+ # A set of definitions of the labels that can be used to describe
65
+ # instances of this monitored resource type. For example, Cloud SQL
66
+ # databases must be labeled with their `database_id` and their `region`.
67
+ #
68
+ # @return [Array<LabelDescriptor>]
69
+ #
70
+ attr_reader :labels
71
+
72
+ ##
73
+ # @private New ResourceDescriptor from a
74
+ # Google::Api::MonitoredResourceDescriptor object.
75
+ def self.from_grpc grpc
76
+ r = new
77
+ r.instance_eval do
78
+ @type = grpc.type
79
+ @name = grpc.display_name
80
+ @description = grpc.description
81
+ @labels = Array(grpc.labels).map do |g|
82
+ LabelDescriptor.from_grpc g
83
+ end
84
+ end
85
+ r
86
+ end
87
+
88
+ ##
89
+ # # LabelDescriptor
90
+ #
91
+ # A definition of a label that can be used to describe instances of a
92
+ # {Resource}. For example, Cloud SQL databases must be labeled with their
93
+ # `database_id`. See {ResourceDescriptor#labels}.
94
+ #
95
+ # @example
96
+ # require "gcloud"
97
+ #
98
+ # gcloud = Gcloud.new
99
+ # logging = gcloud.logging
100
+ # resource_descriptor = logging.resource_descriptors.first
101
+ # label_descriptor = resource_descriptor.labels.first
102
+ # label_descriptor.key #=> "database_id"
103
+ # label_descriptor.description #=> "The ID of the database."
104
+ #
105
+ class LabelDescriptor
106
+ ##
107
+ # The key (name) of the label.
108
+ attr_reader :key
109
+
110
+ ##
111
+ # The type of data that can be assigned to the label.
112
+ #
113
+ # @return [Symbol, nil] Returns `:string`, `:boolean`, `:integer`, or
114
+ # `nil` if there is no type.
115
+ #
116
+ attr_reader :type
117
+
118
+ ##
119
+ # A human-readable description for the label.
120
+ attr_reader :description
121
+
122
+ ##
123
+ # @private New LabelDescriptor from a Google::Api::LabelDescriptor
124
+ # object.
125
+ def self.from_grpc grpc
126
+ type_sym = { STRING: :string,
127
+ BOOL: :boolean,
128
+ INT64: :integer }[grpc.value_type]
129
+ l = new
130
+ l.instance_eval do
131
+ @key = grpc.key
132
+ @type = type_sym
133
+ @description = grpc.description
134
+ end
135
+ l
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,78 @@
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 ResourceDescriptor
21
+ ##
22
+ # ResourceDescriptor::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 ResourceDescriptor::List with an array of
31
+ # ResourceDescriptor instances.
32
+ def initialize arr = []
33
+ super arr
34
+ end
35
+
36
+ ##
37
+ # Whether there is a next page of resource descriptors.
38
+ def next?
39
+ !token.nil?
40
+ end
41
+
42
+ ##
43
+ # Retrieve the next page of resource descriptors.
44
+ def next
45
+ return nil unless next?
46
+ ensure_service!
47
+ list_grpc = @service.list_resource_descriptors token: token
48
+ self.class.from_grpc list_grpc, @service
49
+ rescue GRPC::BadStatus => e
50
+ raise Gcloud::Error.from_error(e)
51
+ end
52
+
53
+ ##
54
+ # @private New ResourceDescriptor::List from a
55
+ # Google::Logging::V2::ListMonitoredResourceDescriptorsResponse object.
56
+ def self.from_grpc grpc_list, service
57
+ sinks = new(Array(grpc_list.resource_descriptors).map do |grpc|
58
+ ResourceDescriptor.from_grpc grpc
59
+ end)
60
+ sinks.instance_eval do
61
+ @token = grpc_list.next_page_token
62
+ @token = nil if @token == ""
63
+ @service = service
64
+ end
65
+ sinks
66
+ end
67
+
68
+ protected
69
+
70
+ ##
71
+ # Raise an error unless an active service is available.
72
+ def ensure_service!
73
+ fail "Must have active service" unless @service
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,258 @@
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 "google/logging/v2/logging_services"
17
+ require "google/logging/v2/logging_config_services"
18
+ require "google/logging/v2/logging_metrics_services"
19
+ require "gcloud/backoff"
20
+
21
+ module Gcloud
22
+ module Logging
23
+ ##
24
+ # @private Represents the gRPC Logging service, including all the API
25
+ # methods.
26
+ class Service
27
+ attr_accessor :project, :credentials, :host
28
+
29
+ ##
30
+ # Creates a new Service instance.
31
+ def initialize project, credentials
32
+ @project = project
33
+ @credentials = credentials
34
+ @host = "logging.googleapis.com"
35
+ end
36
+
37
+ def creds
38
+ GRPC::Core::ChannelCredentials.new.compose \
39
+ GRPC::Core::CallCredentials.new credentials.client.updater_proc
40
+ end
41
+
42
+ def logging
43
+ return mocked_logging if mocked_logging
44
+ @logging ||= Google::Logging::V2::LoggingServiceV2::Stub.new host, creds
45
+ end
46
+ attr_accessor :mocked_logging
47
+
48
+ def sinks
49
+ return mocked_sinks if mocked_sinks
50
+ @sinks ||= Google::Logging::V2::ConfigServiceV2::Stub.new host, creds
51
+ end
52
+ attr_accessor :mocked_sinks
53
+
54
+ def metrics
55
+ return mocked_metrics if mocked_metrics
56
+ @metrics ||= Google::Logging::V2::MetricsServiceV2::Stub.new host, creds
57
+ end
58
+ attr_accessor :mocked_metrics
59
+
60
+ def list_entries projects: nil, filter: nil, order: nil, token: nil,
61
+ max: nil
62
+ list_params = { project_ids: Array(projects || @project),
63
+ filter: filter,
64
+ order_by: order,
65
+ page_token: token,
66
+ page_size: max
67
+ }.delete_if { |_, v| v.nil? }
68
+
69
+ list_req = Google::Logging::V2::ListLogEntriesRequest.new(list_params)
70
+
71
+ backoff { logging.list_log_entries list_req }
72
+ end
73
+
74
+ def write_entries entries, log_name: nil, resource: nil, labels: nil
75
+ # Fix log names so they are the full path
76
+ entries = Array(entries).each do |entry|
77
+ entry.log_name = log_path(entry.log_name)
78
+ end
79
+ resource = resource.to_grpc if resource
80
+ labels = Hash[labels.map { |k, v| [String(k), String(v)] }] if labels
81
+
82
+ write_params = { entries: entries,
83
+ log_name: log_path(log_name),
84
+ resource: resource, labels: labels
85
+ }.delete_if { |_, v| v.nil? }
86
+
87
+ write_req = Google::Logging::V2::WriteLogEntriesRequest.new write_params
88
+
89
+ backoff { logging.write_log_entries write_req }
90
+ end
91
+
92
+ def delete_log name
93
+ delete_req = Google::Logging::V2::DeleteLogRequest.new(
94
+ log_name: log_path(name)
95
+ )
96
+
97
+ backoff { logging.delete_log delete_req }
98
+ end
99
+
100
+ def list_resource_descriptors token: nil, max: nil
101
+ list_params = { page_token: token,
102
+ page_size: max
103
+ }.delete_if { |_, v| v.nil? }
104
+
105
+ list_req = \
106
+ Google::Logging::V2::ListMonitoredResourceDescriptorsRequest.new(
107
+ list_params)
108
+
109
+ backoff { logging.list_monitored_resource_descriptors list_req }
110
+ end
111
+
112
+ def list_sinks token: nil, max: nil
113
+ list_params = { project_name: project_path,
114
+ page_token: token,
115
+ page_size: max
116
+ }.delete_if { |_, v| v.nil? }
117
+
118
+ list_req = Google::Logging::V2::ListSinksRequest.new(list_params)
119
+
120
+ backoff { sinks.list_sinks list_req }
121
+ end
122
+
123
+ def create_sink name, destination, filter, version
124
+ sink_params = {
125
+ name: name, destination: destination,
126
+ filter: filter, output_version_format: version
127
+ }.delete_if { |_, v| v.nil? }
128
+
129
+ create_req = Google::Logging::V2::CreateSinkRequest.new(
130
+ project_name: project_path,
131
+ sink: Google::Logging::V2::LogSink.new(sink_params)
132
+ )
133
+
134
+ backoff { sinks.create_sink create_req }
135
+ end
136
+
137
+ def get_sink name
138
+ get_req = Google::Logging::V2::GetSinkRequest.new(
139
+ sink_name: sink_path(name)
140
+ )
141
+
142
+ backoff { sinks.get_sink get_req }
143
+ end
144
+
145
+ def update_sink name, destination, filter, version
146
+ sink_params = {
147
+ name: name, destination: destination,
148
+ filter: filter, output_version_format: version
149
+ }.delete_if { |_, v| v.nil? }
150
+
151
+ update_req = Google::Logging::V2::UpdateSinkRequest.new(
152
+ sink_name: sink_path(name),
153
+ sink: Google::Logging::V2::LogSink.new(sink_params)
154
+ )
155
+
156
+ backoff { sinks.update_sink update_req }
157
+ end
158
+
159
+ def delete_sink name
160
+ delete_req = Google::Logging::V2::DeleteSinkRequest.new(
161
+ sink_name: sink_path(name)
162
+ )
163
+
164
+ backoff { sinks.delete_sink delete_req }
165
+ end
166
+
167
+ def list_metrics token: nil, max: nil
168
+ list_params = { project_name: project_path,
169
+ page_token: token,
170
+ page_size: max
171
+ }.delete_if { |_, v| v.nil? }
172
+
173
+ list_req = Google::Logging::V2::ListLogMetricsRequest.new(list_params)
174
+
175
+ backoff { metrics.list_log_metrics list_req }
176
+ end
177
+
178
+ def create_metric name, filter, description
179
+ metric_params = {
180
+ name: name,
181
+ description: description,
182
+ filter: filter
183
+ }.delete_if { |_, v| v.nil? }
184
+
185
+ create_req = Google::Logging::V2::CreateLogMetricRequest.new(
186
+ project_name: project_path,
187
+ metric: Google::Logging::V2::LogMetric.new(metric_params)
188
+ )
189
+
190
+ backoff { metrics.create_log_metric create_req }
191
+ end
192
+
193
+ def get_metric name
194
+ get_req = Google::Logging::V2::GetLogMetricRequest.new(
195
+ metric_name: metric_path(name)
196
+ )
197
+
198
+ backoff { metrics.get_log_metric get_req }
199
+ end
200
+
201
+ def update_metric name, description, filter
202
+ metric_params = {
203
+ name: name,
204
+ description: description,
205
+ filter: filter
206
+ }.delete_if { |_, v| v.nil? }
207
+
208
+ update_req = Google::Logging::V2::UpdateLogMetricRequest.new(
209
+ metric_name: metric_path(name),
210
+ metric: Google::Logging::V2::LogMetric.new(metric_params)
211
+ )
212
+
213
+ backoff { metrics.update_log_metric update_req }
214
+ end
215
+
216
+ def delete_metric name
217
+ delete_req = Google::Logging::V2::DeleteLogMetricRequest.new(
218
+ metric_name: metric_path(name)
219
+ )
220
+
221
+ backoff { metrics.delete_log_metric delete_req }
222
+ end
223
+
224
+ def inspect
225
+ "#{self.class}(#{@project})"
226
+ end
227
+
228
+ protected
229
+
230
+ def project_path
231
+ "projects/#{@project}"
232
+ end
233
+
234
+ def log_path log_name
235
+ return nil if log_name.nil?
236
+ return log_name if log_name.empty?
237
+ return log_name if log_name.to_s.include? "/"
238
+ "#{project_path}/logs/#{log_name}"
239
+ end
240
+
241
+ def sink_path sink_name
242
+ return sink_name if sink_name.to_s.include? "/"
243
+ "#{project_path}/sinks/#{sink_name}"
244
+ end
245
+
246
+ def metric_path metric_name
247
+ return metric_name if metric_name.to_s.include? "/"
248
+ "#{project_path}/metrics/#{metric_name}"
249
+ end
250
+
251
+ def backoff options = {}
252
+ Gcloud::Backoff.new(options).execute_grpc do
253
+ yield
254
+ end
255
+ end
256
+ end
257
+ end
258
+ end