gcloud 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +8 -8
  2. data/CHANGELOG.md +8 -0
  3. data/lib/gcloud.rb +48 -30
  4. data/lib/gcloud/bigquery.rb +4 -6
  5. data/lib/gcloud/bigquery/connection.rb +2 -14
  6. data/lib/gcloud/bigquery/dataset.rb +41 -42
  7. data/lib/gcloud/bigquery/project.rb +50 -46
  8. data/lib/gcloud/bigquery/query_job.rb +7 -8
  9. data/lib/gcloud/bigquery/table.rb +54 -55
  10. data/lib/gcloud/bigquery/table/schema.rb +30 -40
  11. data/lib/gcloud/bigquery/view.rb +10 -11
  12. data/lib/gcloud/credentials.rb +19 -25
  13. data/lib/gcloud/datastore.rb +4 -6
  14. data/lib/gcloud/datastore/dataset.rb +3 -5
  15. data/lib/gcloud/dns.rb +4 -6
  16. data/lib/gcloud/dns/connection.rb +17 -16
  17. data/lib/gcloud/dns/importer.rb +5 -11
  18. data/lib/gcloud/dns/project.rb +11 -12
  19. data/lib/gcloud/dns/zone.rb +52 -92
  20. data/lib/gcloud/dns/zone/transaction.rb +2 -2
  21. data/lib/gcloud/pubsub.rb +4 -6
  22. data/lib/gcloud/pubsub/connection.rb +1 -12
  23. data/lib/gcloud/pubsub/project.rb +30 -36
  24. data/lib/gcloud/pubsub/subscription.rb +18 -26
  25. data/lib/gcloud/pubsub/topic.rb +16 -26
  26. data/lib/gcloud/resource_manager.rb +5 -6
  27. data/lib/gcloud/resource_manager/connection.rb +4 -4
  28. data/lib/gcloud/resource_manager/manager.rb +10 -14
  29. data/lib/gcloud/resource_manager/project.rb +3 -5
  30. data/lib/gcloud/search.rb +295 -0
  31. data/lib/gcloud/search/api_client.rb +144 -0
  32. data/lib/gcloud/search/connection.rb +146 -0
  33. data/lib/gcloud/search/credentials.rb +30 -0
  34. data/lib/gcloud/search/document.rb +301 -0
  35. data/lib/gcloud/search/document/list.rb +85 -0
  36. data/lib/gcloud/search/errors.rb +67 -0
  37. data/lib/gcloud/search/field_value.rb +164 -0
  38. data/lib/gcloud/search/field_values.rb +263 -0
  39. data/lib/gcloud/search/fields.rb +267 -0
  40. data/lib/gcloud/search/index.rb +613 -0
  41. data/lib/gcloud/search/index/list.rb +90 -0
  42. data/lib/gcloud/search/project.rb +197 -0
  43. data/lib/gcloud/search/result.rb +169 -0
  44. data/lib/gcloud/search/result/list.rb +95 -0
  45. data/lib/gcloud/storage.rb +4 -6
  46. data/lib/gcloud/storage/bucket.rb +55 -43
  47. data/lib/gcloud/storage/bucket/cors.rb +5 -7
  48. data/lib/gcloud/storage/file.rb +35 -30
  49. data/lib/gcloud/storage/file/acl.rb +12 -16
  50. data/lib/gcloud/storage/project.rb +56 -22
  51. data/lib/gcloud/version.rb +1 -1
  52. metadata +20 -3
@@ -0,0 +1,90 @@
1
+ #--
2
+ # Copyright 2015 Google Inc. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ module Gcloud
17
+ module Search
18
+ class Index
19
+ ##
20
+ # Index::List is a special case Array with additional values.
21
+ class List < DelegateClass(::Array)
22
+ ##
23
+ # If not empty, indicates that there are more records that match
24
+ # the request and this value should be passed to continue.
25
+ attr_accessor :token
26
+
27
+ ##
28
+ # Create a new Index::List with an array of Index instances.
29
+ def initialize arr = []
30
+ super arr
31
+ end
32
+
33
+ ##
34
+ # Whether there a next page of indexes.
35
+ def next?
36
+ !token.nil?
37
+ end
38
+
39
+ ##
40
+ # Retrieve the next page of indexes.
41
+ def next
42
+ return nil unless next?
43
+ ensure_connection!
44
+ resp = @connection.list_indexes token: token
45
+ if resp.success?
46
+ Index::List.from_response resp, @connection
47
+ else
48
+ fail ApiError.from_response(resp)
49
+ end
50
+ end
51
+
52
+ ##
53
+ # Retrieves all indexes by repeatedly loading pages until #next?
54
+ # returns false. Returns the list instance for method chaining.
55
+ def all
56
+ while next?
57
+ next_indexes = self.next
58
+ push(*next_indexes)
59
+ self.token = next_indexes.token
60
+ end
61
+ self
62
+ end
63
+
64
+ ##
65
+ # New Indexs::List from a response object.
66
+ def self.from_response resp, conn #:nodoc:
67
+ data = JSON.parse resp.body
68
+ indexes = new(Array(data["indexes"]).map do |raw_index|
69
+ Index.from_raw raw_index, conn
70
+ end)
71
+ indexes.instance_eval do
72
+ @token = data["nextPageToken"]
73
+ @connection = conn
74
+ end
75
+ indexes
76
+ rescue JSON::ParserError
77
+ raise ApiError.from_response(resp)
78
+ end
79
+
80
+ protected
81
+
82
+ ##
83
+ # Raise an error unless an active connection is available.
84
+ def ensure_connection!
85
+ fail "Must have active connection" unless @connection
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,197 @@
1
+ #--
2
+ # Copyright 2015 Google Inc. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require "gcloud/gce"
17
+ require "gcloud/search/connection"
18
+ require "gcloud/search/credentials"
19
+ require "gcloud/search/index"
20
+ require "gcloud/search/errors"
21
+
22
+ module Gcloud
23
+ module Search
24
+ ##
25
+ # = Project
26
+ #
27
+ # Projects are top-level containers in Google Cloud Platform. They store
28
+ # information about billing and authorized users, and they control access to
29
+ # Google Cloud Search resources. Each project has a friendly name and a
30
+ # unique ID. Projects can be created only in the {Google Developers
31
+ # Console}[https://console.developers.google.com].
32
+ #
33
+ # require "gcloud"
34
+ #
35
+ # gcloud = Gcloud.new
36
+ # search = gcloud.search
37
+ # index = search.index "books"
38
+ #
39
+ # See Gcloud#search
40
+ class Project
41
+ ##
42
+ # The Connection object.
43
+ attr_accessor :connection #:nodoc:
44
+
45
+ ##
46
+ # Creates a new Connection instance.
47
+ #
48
+ # See Gcloud.search
49
+ def initialize project, credentials #:nodoc:
50
+ project = project.to_s # Always cast to a string
51
+ fail ArgumentError, "project is missing" if project.empty?
52
+ @connection = Connection.new project, credentials
53
+ end
54
+
55
+ ##
56
+ # The ID of the current project.
57
+ #
58
+ # === Example
59
+ #
60
+ # require "gcloud"
61
+ #
62
+ # gcloud = Gcloud.new "my-project", "/path/to/keyfile.json"
63
+ # search = gcloud.search
64
+ #
65
+ # search.project #=> "my-project"
66
+ #
67
+ def project
68
+ connection.project
69
+ end
70
+
71
+ ##
72
+ # Default project.
73
+ def self.default_project #:nodoc:
74
+ ENV["SEARCH_PROJECT"] ||
75
+ ENV["GCLOUD_PROJECT"] ||
76
+ ENV["GOOGLE_CLOUD_PROJECT"] ||
77
+ Gcloud::GCE.project_id
78
+ end
79
+
80
+ ##
81
+ # Retrieves an existing index by ID.
82
+ #
83
+ # === Parameters
84
+ #
85
+ # +index_id+::
86
+ # The ID of an index. (+String+)
87
+ # +skip_lookup+::
88
+ # Optionally create an Index object without verifying the index resource
89
+ # exists on the Search service. Documents saved on this object will
90
+ # create the index resource if the resource does not yet exist. Default
91
+ # is +false+. (+Boolean+)
92
+ #
93
+ # === Returns
94
+ #
95
+ # Gcloud::Search::Index or nil if the index does not exist
96
+ #
97
+ # === Examples
98
+ #
99
+ # require "gcloud"
100
+ #
101
+ # gcloud = Gcloud.new
102
+ # search = gcloud.search
103
+ #
104
+ # index = search.index "books"
105
+ # index.index_id #=> "books"
106
+ #
107
+ # A new index can be created by providing the desired +index_id+ and the
108
+ # +skip_lookup+ option:
109
+ #
110
+ # require "gcloud"
111
+ #
112
+ # gcloud = Gcloud.new
113
+ # search = gcloud.search
114
+ #
115
+ # index = search.index "more-books"
116
+ # index #=> nil
117
+ # index = search.index "more-books", skip_lookup: true
118
+ # index.index_id #=> "more-books"
119
+ #
120
+ def index index_id, skip_lookup: false
121
+ if skip_lookup
122
+ index_hash = { "indexId" => index_id, "projectId" => project }
123
+ return Gcloud::Search::Index.from_raw index_hash, connection
124
+ end
125
+ indexes(prefix: index_id).all.detect do |ix|
126
+ ix.index_id == index_id
127
+ end
128
+ end
129
+
130
+ ##
131
+ # Retrieves the list of indexes belonging to the project.
132
+ #
133
+ # === Parameters
134
+ #
135
+ # +prefix+::
136
+ # The prefix of the index name. It is used to list all indexes with
137
+ # names that have this prefix. (+String+)
138
+ # +token+::
139
+ # A previously-returned page token representing part of the larger set
140
+ # of results to view. (+String+)
141
+ # +max+::
142
+ # Maximum number of indexes to return. The default is +100+. (+Integer+)
143
+ #
144
+ # === Returns
145
+ #
146
+ # Array of Gcloud::Search::Index (See Gcloud::Search::Index::List)
147
+ #
148
+ # === Examples
149
+ #
150
+ # require "gcloud"
151
+ #
152
+ # gcloud = Gcloud.new
153
+ # search = gcloud.search
154
+ #
155
+ # indexes = search.indexes
156
+ # indexes.each do |index|
157
+ # puts index.index_id
158
+ # end
159
+ #
160
+ # If you have a significant number of indexes, you may need to paginate
161
+ # through them: (See Gcloud::Search::Index::List)
162
+ #
163
+ # require "gcloud"
164
+ #
165
+ # gcloud = Gcloud.new
166
+ # search = gcloud.search
167
+ #
168
+ # indexes = search.indexes
169
+ # loop do
170
+ # indexes.each do |index|
171
+ # puts index.index_id
172
+ # end
173
+ # break unless indexes.next?
174
+ # indexes = indexes.next
175
+ # end
176
+ #
177
+ def indexes prefix: nil, token: nil, max: nil
178
+ ensure_connection!
179
+ options = { prefix: prefix, token: token, max: max }
180
+ resp = connection.list_indexes options
181
+ if resp.success?
182
+ Index::List.from_response resp, connection
183
+ else
184
+ fail ApiError.from_response(resp)
185
+ end
186
+ end
187
+
188
+ protected
189
+
190
+ ##
191
+ # Raise an error unless an active connection is available.
192
+ def ensure_connection!
193
+ fail "Must have active connection" unless connection
194
+ end
195
+ end
196
+ end
197
+ end
@@ -0,0 +1,169 @@
1
+ #--
2
+ # Copyright 2015 Google Inc. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require "gcloud/search/result/list"
17
+ require "gcloud/search/fields"
18
+
19
+ module Gcloud
20
+ module Search
21
+ ##
22
+ # = Result
23
+ #
24
+ # See Gcloud#search
25
+ class Result
26
+ ##
27
+ # Creates a new Result instance.
28
+ def initialize #:nodoc:
29
+ @fields = Fields.new
30
+ @raw = {}
31
+ end
32
+
33
+ ##
34
+ # The unique identifier of the document referenced in the search result.
35
+ def doc_id
36
+ @raw["docId"]
37
+ end
38
+
39
+ ##
40
+ # The token for the next page of results.
41
+ def token
42
+ @raw["nextPageToken"]
43
+ end
44
+
45
+ ##
46
+ # Retrieve the field values associated to a field name.
47
+ #
48
+ # === Parameters
49
+ #
50
+ # +name+::
51
+ # The name of the field. New values will be configured with this name.
52
+ # (+String+)
53
+ #
54
+ # === Returns
55
+ #
56
+ # FieldValue
57
+ #
58
+ # === Example
59
+ #
60
+ # require "gcloud"
61
+ #
62
+ # gcloud = Gcloud.new
63
+ # search = gcloud.search
64
+ # index = search.index "products"
65
+ #
66
+ # documents = index.search "best T-shirt ever"
67
+ # document = documents.first
68
+ # puts "The best match for your search is:"
69
+ # document["description"].each do |value|
70
+ # puts "* #{value} (#{value.type}) [#{value.lang}]"
71
+ # end
72
+ #
73
+ def [] name
74
+ @fields[name]
75
+ end
76
+
77
+ # rubocop:disable Style/TrivialAccessors
78
+ # Disable rubocop because we want .fields to be listed with the other
79
+ # methods on the class.
80
+
81
+ ##
82
+ # The fields in the search result. Each field has a name (String) and a
83
+ # list of values (FieldValues). (See Fields)
84
+ def fields
85
+ @fields
86
+ end
87
+
88
+ # rubocop:enable Style/TrivialAccessors
89
+
90
+ ##
91
+ # Calls block once for each field, passing the field name and values pair
92
+ # as parameters. If no block is given an enumerator is returned instead.
93
+ # (See Fields#each)
94
+ #
95
+ # === Example
96
+ #
97
+ # require "gcloud"
98
+ #
99
+ # gcloud = Gcloud.new
100
+ # search = gcloud.search
101
+ # index = search.index "products"
102
+ #
103
+ # documents = index.search "best T-shirt ever"
104
+ # document = documents.first
105
+ # puts "The best match for your search is:"
106
+ # document.each do |name, values|
107
+ # puts "* #{name}:"
108
+ # values.each do |value|
109
+ # puts " * #{value} (#{value.type})"
110
+ # end
111
+ # end
112
+ #
113
+ def each &block
114
+ @fields.each(&block)
115
+ end
116
+
117
+ ##
118
+ # Returns a new array populated with all the field names.
119
+ # (See Fields#names)
120
+ #
121
+ # require "gcloud"
122
+ #
123
+ # gcloud = Gcloud.new
124
+ # search = gcloud.search
125
+ # index = search.index "products"
126
+ #
127
+ # documents = index.search "best T-shirt ever"
128
+ # document = documents.first
129
+ # puts "The best match has the following fields:"
130
+ # document.names.each do |name|
131
+ # puts "* #{name}:"
132
+ # end
133
+ #
134
+ def names
135
+ @fields.names
136
+ end
137
+
138
+ ##
139
+ # Override to keep working in interactive shells manageable.
140
+ def inspect #:nodoc:
141
+ insp_token = ""
142
+ if token
143
+ trunc_token = "#{token[0, 8]}...#{token[-5..-1]}"
144
+ trunc_token = token if token.length < 20
145
+ insp_token = ", token: #{trunc_token.inspect}"
146
+ end
147
+ insp_fields = ", fields: (#{fields.names.map(&:inspect).join ', '})"
148
+ "#{self.class}(doc_id: #{doc_id.inspect}#{insp_token}#{insp_fields})"
149
+ end
150
+
151
+ ##
152
+ # New Result from a raw data object.
153
+ def self.from_hash hash #:nodoc:
154
+ result = new
155
+ result.instance_variable_set "@raw", hash
156
+ result.instance_variable_set "@fields", Fields.from_raw(hash["fields"])
157
+ result
158
+ end
159
+
160
+ ##
161
+ # Returns the Result data as a hash
162
+ def to_hash #:nodoc:
163
+ hash = @raw.dup
164
+ hash["fields"] = @fields.to_raw
165
+ hash
166
+ end
167
+ end
168
+ end
169
+ end