Repository navigation
Expand file tree
/
Copy pathcf_client.rb
More file actions
421 lines (316 loc) · 11.5 KB
/
Copy pathcf_client.rb
File metadata and controls
421 lines (316 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/env ruby
require 'rubygems'
require 'oauth2'
require 'cgi'
class CFClient
def initialize(client_id, client_secret, uaa_url)
@client = OAuth2::Client.new(
client_id,
client_secret,
:site => uaa_url)
@token = @client.client_credentials.get_token;
end
def api_url
"https://api.#{ENV["DOMAIN_NAME"]}/v3"
end
def get_organization_by_name(org_name)
org = nil
response = @token.get("#{api_url}/organizations?names=#{org_name}")
if response.parsed["pagination"]["total_pages"] == 1
org = response.parsed['resources'][0]
end
return org
end
def get_organization_quota_by_name(org_name)
quota = nil
response = @token.get("#{api_url}/organization_quotas?names=#{org_name}")
if response.parsed["pagination"]["total_pages"] == 1
quota = response.parsed['resources'][0]
end
return quota
end
def get_organization_spaces(org_guid)
response = @token.get("#{api_url}/spaces?organization_guids=#{(org_guid)}")
spaces = response.parsed["resources"]
return spaces
end
def get_users
response = @token.get("#{api_url}/users?order_by=-created_at")
return response.parsed["resources"];
end
def add_user_to_org(user_guid, org_guid)
role_type = "organization_user"
# Add user to org
req = {
type: role_type,
relationships: {
user: {
data: {
guid: user_guid
}
},
organization: {
data: {
guid: org_guid
}
}
}
}
puts "Adding user_guid '#{user_guid}' to org_guid: #{org_guid}"
response = @token.post(
"#{api_url}/roles",
headers: { 'Content-Type' => 'application/json' },
body: req.to_json
)
puts "Added user_guid '#{user_guid}' to org_guid: #{org_guid}, response status: #{response.status}"
return response
end
def create_organization(org_name, org_quota_definition_guid)
# Step 1: Create the org
create_req = {
name: org_name
}
puts "Creating organization '#{org_name}' ..."
response = @token.post(
"#{api_url}/organizations",
headers: { 'Content-Type' => 'application/json' },
body: create_req.to_json
)
org = response.parsed
org_guid = org["guid"]
puts "Created organization '#{org_name}' with GUID: #{org_guid} response status: #{response.status} "
# Step 2: Set quota
self.set_org_quota(org_guid, org_quota_definition_guid)
return org
end
def set_org_quota(org_guid, org_quota_definition_guid)
quota_req = {
data: [{ guid: org_guid }]
}
quota_url = "#{api_url}/organization_quotas/#{org_quota_definition_guid}/relationships/organizations"
puts "Associating org_quota_definition_guid: #{org_quota_definition_guid} with org_guid: #{org_guid}..."
response = @token.post(
quota_url,
headers: { 'Content-Type' => 'application/json' },
body: quota_req.to_json
)
puts "Associated org_quota_definition_guid: #{org_quota_definition_guid} with org_guid: #{org_guid}, response status: #{response.status}"
end
def create_space(name, organization_guid, developer_guid, manager_guid, space_quota_guid)
create_req = {
name: name,
relationships: {
organization: {
data: {
guid: organization_guid
}
}
}
}
puts "Creating space: #{name} in organization_guid: #{organization_guid}"
response = @token.post(
"#{api_url}/spaces",
headers: { 'Content-Type' => 'application/json' },
body: create_req.to_json
)
space = response.parsed
space_guid = space["guid"]
puts "Created space: #{name} in organization_guid: #{organization_guid} response status: #{response.status}"
self.set_space_quota(space_guid, space_quota_guid)
self.add_user_to_space_and_role(developer_guid, space_guid, "space_developer")
self.add_user_to_space_and_role(manager_guid, space_guid, "space_manager")
self.add_space_asg(space_guid, "public_networks_egress")
self.add_space_asg(space_guid, "trusted_local_networks_egress")
end
def set_space_quota(space_guid, space_quota_guid)
quota_req = {
data: [{ guid: space_guid }]
}
puts "Associating space quota space_guid: #{space_guid}, space_quota_guid: #{space_quota_guid}..."
response = @token.post(
"#{api_url}/space_quotas/#{space_quota_guid}/relationships/spaces",
headers: { 'Content-Type' => 'application/json' },
body: quota_req.to_json
)
puts "Associated space quota space_guid: #{space_guid}, space_quota_guid: #{space_quota_guid} response status: #{response.status} "
end
def add_user_to_space_and_role(user_guid, space_guid, role_type)
# Add user to role in space
req = {
type: role_type,
relationships: {
user: {
data: {
guid: user_guid
}
},
space: {
data: {
guid: space_guid
}
}
}
}
puts "Adding user_guid #{user_guid} to space #{space_guid} as role #{role_type}..."
# Send request with explicit JSON headers
response = @token.post(
"#{api_url}/roles",
headers: { 'Content-Type' => 'application/json' },
body: req.to_json
)
puts "Added user_guid #{user_guid} to space #{space_guid} as role #{role_type} response status: #{response.status} "
end
def add_space_asg(space_guid, asg_name)
puts "Finding guid for asg #{asg_name}..."
asg_response = @token.get("#{api_url}/security_groups?names=#{CGI.escape asg_name}")
asg = asg_response.parsed
asg_guid = asg["resources"][0]["guid"]
puts "Found guid for asg #{asg_name} as asg_guid #{asg_guid}"
#Why only running_spaces? Because globally_enabled.staging = true in stage/prod
puts "Adding running_space guid: #{space_guid} to asg_guid #{asg_guid}..."
create_req = {
data: [{ guid: space_guid }]
}
url = "#{api_url}/security_groups/#{asg_guid}/relationships/running_spaces"
response = @token.post(
url,
headers: { 'Content-Type' => 'application/json' },
body: create_req.to_json
)
puts "Added running_space guid: #{space_guid} to asg_guid #{asg_guid} response status: #{response.status}"
return response
end
def create_organization_space_quota_definition(org_guid, space_name)
create_req = {
name: space_name,
relationships: {
organization: {
data: {
guid: org_guid
}
}
},
apps: {
total_memory_in_mb: 1024
},
services: {
paid_services_allowed: false,
total_service_instances: 10
},
routes: {
total_routes: 10
}
}
puts "Creating space quota #{space_name} for org_guid #{org_guid}..."
response = @token.post(
"#{api_url}/space_quotas",
headers: { 'Content-Type' => 'application/json' },
body: create_req.to_json
)
space_quota_definition = response.parsed
space_quota_definition_guid = space_quota_definition["guid"]
puts "Created space quota #{space_name} with GUID: #{space_quota_definition_guid}, response status: #{response.status} "
return space_quota_definition
end
def get_organization_quota(org_guid)
org_quota_definition_guid = get_org_quota_definition_guid(org_guid)
puts "Finding organization quota #{org_quota_definition_guid}..."
response = @token.get("#{api_url}/organization_quotas/#{org_quota_definition_guid}")
quota = response.parsed
puts "Found organization quota #{org_quota_definition_guid} "
return quota
end
def get_org_quota_definition_guid(org_guid)
puts "Finding organization quota guid for organization #{org_guid}..."
response = @token.get("#{api_url}/organizations/#{org_guid}")
organization_quota = response.parsed
org_quota_definition_guid = organization_quota["relationships"]["quota"]["data"]["guid"]
puts "Found organization quota guid for orginization #{org_guid} as #{org_quota_definition_guid}"
return org_quota_definition_guid
end
def increase_org_quota(org)
quota = get_organization_quota(org['guid'])
quota_total_routes = quota["routes"]["total_routes"]
quota_total_services = quota["services"]["total_service_instances"]
quota_memory_limit = quota["apps"]["total_memory_in_mb"]
org_spaces = get_organization_spaces(org['guid'])
space_count = org_spaces.length
computed_total_routes_services = 10 * space_count
computed_memory_limit = 1024 * space_count
create_req = {
apps: {
total_memory_in_mb: quota_memory_limit > computed_memory_limit ? quota_memory_limit : computed_memory_limit
},
services: {
paid_services_allowed: true,
total_service_instances: quota_total_services > computed_total_routes_services ? quota_total_services : computed_total_routes_services
},
routes: {
total_routes: quota_total_routes > computed_total_routes_services ? quota_total_routes : computed_total_routes_services
}
}
puts "Updating org quota definition #{org["name"]}..."
response = @token.patch(
"#{api_url}/organization_quotas/#{quota["guid"]}",
headers: { 'Content-Type' => 'application/json' },
body: create_req.to_json
)
org_quota_definition = response.parsed
puts "Updated org quota #{org["name"]}, response status: #{response.status} "
end
def create_organization_quota(org_name)
create_req = {
name: org_name,
apps: {
total_memory_in_mb: 1024
},
services: {
paid_services_allowed: false,
total_service_instances: 10
},
routes: {
total_routes: 10
}
}
puts "Creating org quota #{org_name}..."
response = @token.post(
"#{api_url}/organization_quotas",
headers: { 'Content-Type' => 'application/json' },
body: create_req.to_json
)
org_quota_definition = response.parsed
org_quota_definition_guid = org_quota_definition["guid"]
puts "Created org quota #{org_name} with GUID: #{org_quota_definition_guid}, response status: #{response.status} "
org_quota = response.parsed
end
def get_organization_space_quota_definitions(org_guid)
space_quota_definitions = nil
response = @token.get("#{api_url}/space_quotas?organization_guids=#{org_guid}")
puts "Finding space quotas for org #{org_guid}..."
if response.parsed["pagination"]["total_results"] != 0
space_quota_definitions = response.parsed['resources']
puts "Found #{response.parsed["pagination"]["total_results"]} space quotas for org #{org_guid}"
end
puts "Returning any found space quotas..."
return space_quota_definitions
end
def get_organization_space_quota_definition_by_name(org_guid, name)
space_quota_definition = nil
space_quota_definitions = get_organization_space_quota_definitions(org_guid)
puts "Finding quota definition named #{name}..."
if space_quota_definitions
space_quota_definitions.each do |quota_definition|
if quota_definition['name'] == name
space_quota_definition = quota_definition
puts "Found quota definition named #{name}"
break
end
end
end
return space_quota_definition
end
def organization_space_name_exists?(org_guid, space_name)
response = @token.get("#{api_url}/spaces?organization_guids=#{org_guid}&names=#{CGI.escape space_name}")
return response.parsed["pagination"]["total_results"] == 1
end
end