google-cloud-resource_manager 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/google-cloud-resource_manager.rb +116 -0
- data/lib/google/cloud/resource_manager.rb +248 -0
- data/lib/google/cloud/resource_manager/credentials.rb +32 -0
- data/lib/google/cloud/resource_manager/manager.rb +270 -0
- data/lib/google/cloud/resource_manager/policy.rb +214 -0
- data/lib/google/cloud/resource_manager/project.rb +486 -0
- data/lib/google/cloud/resource_manager/project/list.rb +169 -0
- data/lib/google/cloud/resource_manager/project/updater.rb +134 -0
- data/lib/google/cloud/resource_manager/service.rb +132 -0
- data/lib/google/cloud/resource_manager/version.rb +22 -0
- metadata +197 -0
@@ -0,0 +1,169 @@
|
|
1
|
+
# Copyright 2015 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 Google
|
17
|
+
module Cloud
|
18
|
+
module ResourceManager
|
19
|
+
class Project
|
20
|
+
##
|
21
|
+
# Project::List is a special case Array with additional values.
|
22
|
+
class List < DelegateClass(::Array)
|
23
|
+
##
|
24
|
+
# If not empty, indicates that there are more projects that match
|
25
|
+
# the request and this value should be passed to continue.
|
26
|
+
attr_accessor :token
|
27
|
+
|
28
|
+
##
|
29
|
+
# @private Create a new Project::List with an array of Project
|
30
|
+
# instances.
|
31
|
+
def initialize arr = []
|
32
|
+
super arr
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Whether there is a next page of projects.
|
37
|
+
#
|
38
|
+
# @return [Boolean]
|
39
|
+
#
|
40
|
+
# @example
|
41
|
+
# require "google/cloud"
|
42
|
+
#
|
43
|
+
# gcloud = Google::Cloud.new
|
44
|
+
# resource_manager = gcloud.resource_manager
|
45
|
+
#
|
46
|
+
# projects = resource_manager.projects
|
47
|
+
# if projects.next?
|
48
|
+
# next_projects = projects.next
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
def next?
|
52
|
+
!token.nil?
|
53
|
+
end
|
54
|
+
|
55
|
+
##
|
56
|
+
# Retrieve the next page of projects.
|
57
|
+
#
|
58
|
+
# @return [Project::List]
|
59
|
+
#
|
60
|
+
# @example
|
61
|
+
# require "google/cloud"
|
62
|
+
#
|
63
|
+
# gcloud = Google::Cloud.new
|
64
|
+
# resource_manager = gcloud.resource_manager
|
65
|
+
#
|
66
|
+
# projects = resource_manager.projects
|
67
|
+
# if projects.next?
|
68
|
+
# next_projects = projects.next
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
def next
|
72
|
+
return nil unless next?
|
73
|
+
ensure_manager!
|
74
|
+
@manager.projects token: token, filter: @filter, max: @max
|
75
|
+
end
|
76
|
+
|
77
|
+
##
|
78
|
+
# Retrieves all projects by repeatedly loading {#next} until {#next?}
|
79
|
+
# returns `false`. Calls the given block once for each project, which
|
80
|
+
# is passed as the parameter.
|
81
|
+
#
|
82
|
+
# An Enumerator is returned if no block is given.
|
83
|
+
#
|
84
|
+
# This method may make several API calls until all projects are
|
85
|
+
# retrieved. Be sure to use as narrow a search criteria as possible.
|
86
|
+
# Please use with caution.
|
87
|
+
#
|
88
|
+
# @param [Integer] request_limit The upper limit of API requests to
|
89
|
+
# make to load all projects. Default is no limit.
|
90
|
+
# @yield [project] The block for accessing each project.
|
91
|
+
# @yieldparam [Project] project The project object.
|
92
|
+
#
|
93
|
+
# @return [Enumerator]
|
94
|
+
#
|
95
|
+
# @example Iterating each project by passing a block:
|
96
|
+
# require "google/cloud"
|
97
|
+
#
|
98
|
+
# gcloud = Google::Cloud.new
|
99
|
+
# resource_manager = gcloud.resource_manager
|
100
|
+
# projects = resource_manager.projects
|
101
|
+
#
|
102
|
+
# projects.all do |project|
|
103
|
+
# puts project.project_id
|
104
|
+
# end
|
105
|
+
#
|
106
|
+
# @example Using the enumerator by not passing a block:
|
107
|
+
# require "google/cloud"
|
108
|
+
#
|
109
|
+
# gcloud = Google::Cloud.new
|
110
|
+
# resource_manager = gcloud.resource_manager
|
111
|
+
# projects = resource_manager.projects
|
112
|
+
#
|
113
|
+
# all_project_ids = projects.all.map do |project|
|
114
|
+
# project.project_id
|
115
|
+
# end
|
116
|
+
#
|
117
|
+
# @example Limit the number of API calls made:
|
118
|
+
# require "google/cloud"
|
119
|
+
#
|
120
|
+
# gcloud = Google::Cloud.new
|
121
|
+
# resource_manager = gcloud.resource_manager
|
122
|
+
# projects = resource_manager.projects
|
123
|
+
#
|
124
|
+
# projects.all(request_limit: 10) do |project|
|
125
|
+
# puts project.project_id
|
126
|
+
# end
|
127
|
+
#
|
128
|
+
def all request_limit: nil
|
129
|
+
request_limit = request_limit.to_i if request_limit
|
130
|
+
unless block_given?
|
131
|
+
return enum_for(:all, request_limit: request_limit)
|
132
|
+
end
|
133
|
+
results = self
|
134
|
+
loop do
|
135
|
+
results.each { |r| yield r }
|
136
|
+
if request_limit
|
137
|
+
request_limit -= 1
|
138
|
+
break if request_limit < 0
|
139
|
+
end
|
140
|
+
break unless results.next?
|
141
|
+
results = results.next
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
##
|
146
|
+
# @private New Projects::List from a response object.
|
147
|
+
def self.from_gapi gapi_list, manager, filter = nil, max = nil
|
148
|
+
projects = new(Array(gapi_list.projects).map do |gapi_object|
|
149
|
+
Project.from_gapi gapi_object, manager.service
|
150
|
+
end)
|
151
|
+
projects.instance_variable_set :@token, gapi_list.next_page_token
|
152
|
+
projects.instance_variable_set :@manager, manager
|
153
|
+
projects.instance_variable_set :@filter, filter
|
154
|
+
projects.instance_variable_set :@max, max
|
155
|
+
projects
|
156
|
+
end
|
157
|
+
|
158
|
+
protected
|
159
|
+
|
160
|
+
##
|
161
|
+
# Raise an error unless an active connection is available.
|
162
|
+
def ensure_manager!
|
163
|
+
fail "Must have active connection" unless @manager
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
@@ -0,0 +1,134 @@
|
|
1
|
+
# Copyright 2015 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 "time"
|
17
|
+
|
18
|
+
module Google
|
19
|
+
module Cloud
|
20
|
+
module ResourceManager
|
21
|
+
class Project
|
22
|
+
##
|
23
|
+
# # Project Updater
|
24
|
+
#
|
25
|
+
# This object is used by Project#update when passed a block. These
|
26
|
+
# methods are used to update the project data in a single API call.
|
27
|
+
#
|
28
|
+
# @example
|
29
|
+
# require "google/cloud"
|
30
|
+
#
|
31
|
+
# gcloud = Google::Cloud.new
|
32
|
+
# resource_manager = gcloud.resource_manager
|
33
|
+
# project = resource_manager.project "tokyo-rain-123"
|
34
|
+
# project.update do |p|
|
35
|
+
# p.name = "My Project"
|
36
|
+
# p.labels["env"] = "production"
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
class Updater < DelegateClass(Project)
|
40
|
+
##
|
41
|
+
# @private Create an Updater object.
|
42
|
+
def initialize project
|
43
|
+
super project
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# Updates the user-assigned name of the project. This field is
|
48
|
+
# optional and can remain unset.
|
49
|
+
#
|
50
|
+
# Allowed characters are: lowercase and uppercase letters, numbers,
|
51
|
+
# hyphen, single-quote, double-quote, space, and exclamation point.
|
52
|
+
#
|
53
|
+
# @example
|
54
|
+
# require "google/cloud"
|
55
|
+
#
|
56
|
+
# gcloud = Google::Cloud.new
|
57
|
+
# resource_manager = gcloud.resource_manager
|
58
|
+
# project = resource_manager.project "tokyo-rain-123"
|
59
|
+
# project.update do |p|
|
60
|
+
# p.name = "My Project"
|
61
|
+
# end
|
62
|
+
#
|
63
|
+
def name= new_name
|
64
|
+
gapi.name = new_name
|
65
|
+
end
|
66
|
+
|
67
|
+
##
|
68
|
+
# The labels associated with this project.
|
69
|
+
#
|
70
|
+
# Label keys must be between 1 and 63 characters long and must conform
|
71
|
+
# to the following regular expression:
|
72
|
+
# <code>[a-z]([-a-z0-9]*[a-z0-9])?</code>.
|
73
|
+
#
|
74
|
+
# Label values must be between 0 and 63 characters long and must
|
75
|
+
# conform to the regular expression
|
76
|
+
# <code>([a-z]([-a-z0-9]*[a-z0-9])?)?</code>.
|
77
|
+
#
|
78
|
+
# No more than 256 labels can be associated with a given resource.
|
79
|
+
# (`Hash`)
|
80
|
+
#
|
81
|
+
# @example
|
82
|
+
# require "google/cloud"
|
83
|
+
#
|
84
|
+
# gcloud = Google::Cloud.new
|
85
|
+
# resource_manager = gcloud.resource_manager
|
86
|
+
# project = resource_manager.project "tokyo-rain-123"
|
87
|
+
# project.update do |p|
|
88
|
+
# p.labels["env"] = "production"
|
89
|
+
# end
|
90
|
+
#
|
91
|
+
def labels
|
92
|
+
gapi.labels
|
93
|
+
end
|
94
|
+
|
95
|
+
##
|
96
|
+
# Updates the labels associated with this project.
|
97
|
+
#
|
98
|
+
# Label keys must be between 1 and 63 characters long and must conform
|
99
|
+
# to the following regular expression:
|
100
|
+
# <code>[a-z]([-a-z0-9]*[a-z0-9])?</code>.
|
101
|
+
#
|
102
|
+
# Label values must be between 0 and 63 characters long and must
|
103
|
+
# conform to the regular expression
|
104
|
+
# <code>([a-z]([-a-z0-9]*[a-z0-9])?)?</code>.
|
105
|
+
#
|
106
|
+
# No more than 256 labels can be associated with a given resource.
|
107
|
+
# (`Hash`)
|
108
|
+
#
|
109
|
+
# @example
|
110
|
+
# require "google/cloud"
|
111
|
+
#
|
112
|
+
# gcloud = Google::Cloud.new
|
113
|
+
# resource_manager = gcloud.resource_manager
|
114
|
+
# project = resource_manager.project "tokyo-rain-123"
|
115
|
+
# project.update do |p|
|
116
|
+
# p.labels = { "env" => "production" }
|
117
|
+
# end
|
118
|
+
#
|
119
|
+
def labels= new_labels
|
120
|
+
gapi.labels = new_labels
|
121
|
+
end
|
122
|
+
|
123
|
+
##
|
124
|
+
# @private Create an Updater object.
|
125
|
+
def self.from_project project
|
126
|
+
dupe_gapi = project.gapi.class.new project.gapi.to_h
|
127
|
+
dupe_project = Project.from_gapi dupe_gapi, nil # no way to update
|
128
|
+
Updater.new dupe_project
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,132 @@
|
|
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/cloud/errors"
|
17
|
+
require "google/cloud/resource_manager/version"
|
18
|
+
require "google/apis/cloudresourcemanager_v1"
|
19
|
+
|
20
|
+
module Google
|
21
|
+
module Cloud
|
22
|
+
module ResourceManager
|
23
|
+
##
|
24
|
+
# @private
|
25
|
+
# Represents the service to Resource Manager, as well as expose the API
|
26
|
+
# calls.
|
27
|
+
class Service
|
28
|
+
##
|
29
|
+
# Alias to the Google Client API module
|
30
|
+
API = Google::Apis::CloudresourcemanagerV1
|
31
|
+
|
32
|
+
attr_accessor :credentials
|
33
|
+
|
34
|
+
##
|
35
|
+
# Creates a new Service instance.
|
36
|
+
def initialize credentials, retries: nil, timeout: nil
|
37
|
+
@credentials = credentials
|
38
|
+
@service = API::CloudResourceManagerService.new
|
39
|
+
@service.client_options.application_name = \
|
40
|
+
"google-cloud-resource_manager"
|
41
|
+
@service.client_options.application_version = \
|
42
|
+
Google::Cloud::ResourceManager::VERSION
|
43
|
+
@service.request_options.retries = retries || 3
|
44
|
+
@service.request_options.timeout_sec = timeout if timeout
|
45
|
+
@service.authorization = @credentials.client
|
46
|
+
end
|
47
|
+
|
48
|
+
def service
|
49
|
+
return mocked_service if mocked_service
|
50
|
+
@service
|
51
|
+
end
|
52
|
+
attr_accessor :mocked_service
|
53
|
+
|
54
|
+
##
|
55
|
+
# Returns API::ListProjectsResponse
|
56
|
+
def list_project filter: nil, token: nil, max: nil
|
57
|
+
execute do
|
58
|
+
service.list_projects page_token: token, page_size: max,
|
59
|
+
filter: filter
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
##
|
64
|
+
# Returns API::Project
|
65
|
+
def get_project project_id
|
66
|
+
execute { service.get_project project_id }
|
67
|
+
end
|
68
|
+
|
69
|
+
##
|
70
|
+
# Returns API::Project
|
71
|
+
def create_project project_id, name, labels
|
72
|
+
project_attrs = { projectId: project_id, name: name,
|
73
|
+
labels: labels }.delete_if { |_, v| v.nil? }
|
74
|
+
execute { service.create_project API::Project.new(project_attrs) }
|
75
|
+
end
|
76
|
+
|
77
|
+
##
|
78
|
+
# Updated the project, given a API::Project.
|
79
|
+
# Returns API::Project
|
80
|
+
def update_project project_gapi
|
81
|
+
execute do
|
82
|
+
service.update_project project_gapi.project_id, project_gapi
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def delete_project project_id
|
87
|
+
execute { service.delete_project project_id }
|
88
|
+
end
|
89
|
+
|
90
|
+
def undelete_project project_id
|
91
|
+
execute { service.undelete_project project_id }
|
92
|
+
end
|
93
|
+
|
94
|
+
##
|
95
|
+
# Returns API::Policy
|
96
|
+
def get_policy project_id
|
97
|
+
execute { service.get_project_iam_policy "projects/#{project_id}" }
|
98
|
+
end
|
99
|
+
|
100
|
+
##
|
101
|
+
# Returns API::Policy
|
102
|
+
def set_policy project_id, new_policy
|
103
|
+
req = API::SetIamPolicyRequest.new policy: new_policy
|
104
|
+
execute do
|
105
|
+
service.set_project_iam_policy "projects/#{project_id}", req
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
##
|
110
|
+
# Returns API::TestIamPermissionsResponse
|
111
|
+
def test_permissions project_id, permissions
|
112
|
+
req = API::TestIamPermissionsRequest.new permissions: permissions
|
113
|
+
execute do
|
114
|
+
service.test_project_iam_permissions "projects/#{project_id}", req
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def inspect
|
119
|
+
"#{self.class}"
|
120
|
+
end
|
121
|
+
|
122
|
+
protected
|
123
|
+
|
124
|
+
def execute
|
125
|
+
yield
|
126
|
+
rescue Google::Apis::Error => e
|
127
|
+
raise Google::Cloud::Error.from_error(e)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,22 @@
|
|
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 Google
|
17
|
+
module Cloud
|
18
|
+
module ResourceManager
|
19
|
+
VERSION = "0.20.0"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|