new_cfoundry 4.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (269) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.gitmodules +3 -0
  4. data/.rspec +2 -0
  5. data/.ruby-gemset +1 -0
  6. data/.ruby-version +1 -0
  7. data/.travis.yml +11 -0
  8. data/Checkfile +2 -0
  9. data/Gemfile +3 -0
  10. data/LICENSE +987 -0
  11. data/NOTICE +10 -0
  12. data/README.md +9 -0
  13. data/Rakefile +14 -0
  14. data/VERSION +1 -0
  15. data/cfoundry.gemspec +40 -0
  16. data/config/locales/en.yml +168 -0
  17. data/lib/cc_api_stub/app_usage_events.rb +11 -0
  18. data/lib/cc_api_stub/applications.rb +57 -0
  19. data/lib/cc_api_stub/domains.rb +32 -0
  20. data/lib/cc_api_stub/events.rb +11 -0
  21. data/lib/cc_api_stub/frameworks.rb +22 -0
  22. data/lib/cc_api_stub/helper.rb +148 -0
  23. data/lib/cc_api_stub/login.rb +21 -0
  24. data/lib/cc_api_stub/organization_users.rb +27 -0
  25. data/lib/cc_api_stub/organizations.rb +82 -0
  26. data/lib/cc_api_stub/routes.rb +26 -0
  27. data/lib/cc_api_stub/runtimes.rb +22 -0
  28. data/lib/cc_api_stub/service_bindings.rb +22 -0
  29. data/lib/cc_api_stub/service_instances.rb +22 -0
  30. data/lib/cc_api_stub/services.rb +21 -0
  31. data/lib/cc_api_stub/space_users.rb +27 -0
  32. data/lib/cc_api_stub/spaces.rb +58 -0
  33. data/lib/cc_api_stub/users.rb +85 -0
  34. data/lib/cc_api_stub.rb +20 -0
  35. data/lib/cfoundry/auth_token.rb +63 -0
  36. data/lib/cfoundry/baseclient.rb +216 -0
  37. data/lib/cfoundry/chatty_hash.rb +46 -0
  38. data/lib/cfoundry/client.rb +42 -0
  39. data/lib/cfoundry/concerns/login_helpers.rb +14 -0
  40. data/lib/cfoundry/concerns/proxy_options.rb +17 -0
  41. data/lib/cfoundry/errors.rb +163 -0
  42. data/lib/cfoundry/rest_client.rb +317 -0
  43. data/lib/cfoundry/test_support.rb +3 -0
  44. data/lib/cfoundry/trace_helpers.rb +64 -0
  45. data/lib/cfoundry/uaaclient.rb +160 -0
  46. data/lib/cfoundry/upload_helpers.rb +222 -0
  47. data/lib/cfoundry/v2/app.rb +303 -0
  48. data/lib/cfoundry/v2/app_event.rb +13 -0
  49. data/lib/cfoundry/v2/app_instance.rb +74 -0
  50. data/lib/cfoundry/v2/app_usage_event.rb +14 -0
  51. data/lib/cfoundry/v2/base.rb +108 -0
  52. data/lib/cfoundry/v2/client.rb +104 -0
  53. data/lib/cfoundry/v2/domain.rb +20 -0
  54. data/lib/cfoundry/v2/event.rb +17 -0
  55. data/lib/cfoundry/v2/helper.rb +11 -0
  56. data/lib/cfoundry/v2/managed_service_instance.rb +12 -0
  57. data/lib/cfoundry/v2/model.rb +211 -0
  58. data/lib/cfoundry/v2/model_magic/attribute.rb +49 -0
  59. data/lib/cfoundry/v2/model_magic/client_extensions.rb +170 -0
  60. data/lib/cfoundry/v2/model_magic/has_summary.rb +49 -0
  61. data/lib/cfoundry/v2/model_magic/query_multi_value_helper.rb +21 -0
  62. data/lib/cfoundry/v2/model_magic/query_value_helper.rb +33 -0
  63. data/lib/cfoundry/v2/model_magic/queryable_by.rb +39 -0
  64. data/lib/cfoundry/v2/model_magic/to_many.rb +138 -0
  65. data/lib/cfoundry/v2/model_magic/to_one.rb +81 -0
  66. data/lib/cfoundry/v2/model_magic.rb +134 -0
  67. data/lib/cfoundry/v2/organization.rb +44 -0
  68. data/lib/cfoundry/v2/quota_definition.rb +12 -0
  69. data/lib/cfoundry/v2/route.rb +25 -0
  70. data/lib/cfoundry/v2/service.rb +22 -0
  71. data/lib/cfoundry/v2/service_auth_token.rb +9 -0
  72. data/lib/cfoundry/v2/service_binding.rb +10 -0
  73. data/lib/cfoundry/v2/service_broker.rb +12 -0
  74. data/lib/cfoundry/v2/service_instance.rb +14 -0
  75. data/lib/cfoundry/v2/service_plan.rb +16 -0
  76. data/lib/cfoundry/v2/space.rb +30 -0
  77. data/lib/cfoundry/v2/stack.rb +10 -0
  78. data/lib/cfoundry/v2/user.rb +93 -0
  79. data/lib/cfoundry/v2/user_provided_service_instance.rb +15 -0
  80. data/lib/cfoundry/validator.rb +41 -0
  81. data/lib/cfoundry/version.rb +4 -0
  82. data/lib/cfoundry/zip.rb +56 -0
  83. data/lib/cfoundry.rb +5 -0
  84. data/release_notes/release_1_5_3.md +177 -0
  85. data/release_notes/release_2.3.1.md +14 -0
  86. data/release_notes/release_2.3.3.md +38 -0
  87. data/release_notes/release_2.3.4.md +16 -0
  88. data/release_notes/release_2.3.5.md +14 -0
  89. data/release_notes/release_2.3.6.md +14 -0
  90. data/release_notes/release_2.3.6.rc1.md +29 -0
  91. data/release_notes/release_2.3.6.rc2.md +19 -0
  92. data/release_notes/release_2.3.7.rc1.md +14 -0
  93. data/release_notes/release_2.4.0.md +41 -0
  94. data/release_notes/release_2.4.1.rc1.md +58 -0
  95. data/release_notes/release_3.0.0.md +21 -0
  96. data/release_notes/release_3.0.1.md +16 -0
  97. data/release_notes/release_3.0.2.rc1.md +19 -0
  98. data/release_notes/release_4.0.0.md +19 -0
  99. data/release_notes/release_4.0.1.md +14 -0
  100. data/release_notes/release_4.0.2.rc1.md +14 -0
  101. data/release_notes/release_4.0.2.rc2.md +11 -0
  102. data/release_notes/release_4.0.2.rc3.md +11 -0
  103. data/release_notes/release_4.0.2.rc4.md +19 -0
  104. data/release_notes/release_4.0.2.rc5.md +14 -0
  105. data/release_notes/release_4.0.3.md +19 -0
  106. data/release_notes/release_4.0.4.rc1.md +14 -0
  107. data/release_notes/release_4.0.4.rc2.md +14 -0
  108. data/release_notes/release_4.1.0.md +19 -0
  109. data/release_notes/release_4.2.0.rc.md +24 -0
  110. data/release_notes/release_4.3.0.md +22 -0
  111. data/release_notes/release_4.3.1.md +14 -0
  112. data/release_notes/release_4.3.10.md +14 -0
  113. data/release_notes/release_4.3.11.md +14 -0
  114. data/release_notes/release_4.3.12.md +14 -0
  115. data/release_notes/release_4.3.2.rc1.md +21 -0
  116. data/release_notes/release_4.3.3.md +24 -0
  117. data/release_notes/release_4.3.4.md +11 -0
  118. data/release_notes/release_4.3.4.rc1.md +24 -0
  119. data/release_notes/release_4.3.5.md +36 -0
  120. data/release_notes/release_4.3.5.rc1.md +21 -0
  121. data/release_notes/release_4.3.6.md +14 -0
  122. data/release_notes/release_4.3.7.md +50 -0
  123. data/release_notes/release_4.3.8.md +19 -0
  124. data/release_notes/release_4.3.9.md +14 -0
  125. data/release_notes/release_4.4.0.md +16 -0
  126. data/release_notes/release_4.5.1.md +11 -0
  127. data/release_notes/release_4.5.2.md +14 -0
  128. data/release_notes/release_4.5.3.md +29 -0
  129. data/release_notes/release_4.6.0.md +24 -0
  130. data/release_notes/release_4.6.1.md +14 -0
  131. data/release_notes/release_4.6.2.md +16 -0
  132. data/release_notes/release_4.6.3.rc1.md +14 -0
  133. data/release_notes/release_4.6.3.rc2.md +14 -0
  134. data/release_notes/release_4.6.3.rc3.md +17 -0
  135. data/release_notes/release_4.7.0.md +11 -0
  136. data/release_notes/release_4.7.1.md +14 -0
  137. data/release_notes/release_4.7.1.rc.1.md +14 -0
  138. data/release_notes/release_4.7.1.rc1.md +19 -0
  139. data/script/gpp +3 -0
  140. data/spec/cc_api_stub/app_usage_events_spec.rb +12 -0
  141. data/spec/cc_api_stub/applications_spec.rb +69 -0
  142. data/spec/cc_api_stub/domains_spec.rb +40 -0
  143. data/spec/cc_api_stub/events_spec.rb +12 -0
  144. data/spec/cc_api_stub/frameworks_spec.rb +19 -0
  145. data/spec/cc_api_stub/login_spec.rb +20 -0
  146. data/spec/cc_api_stub/organization_users_spec.rb +35 -0
  147. data/spec/cc_api_stub/organizations_spec.rb +118 -0
  148. data/spec/cc_api_stub/routes_spec.rb +19 -0
  149. data/spec/cc_api_stub/runtimes_spec.rb +19 -0
  150. data/spec/cc_api_stub/service_bindings_spec.rb +13 -0
  151. data/spec/cc_api_stub/service_instances_spec.rb +19 -0
  152. data/spec/cc_api_stub/services_spec.rb +11 -0
  153. data/spec/cc_api_stub/space_users_spec.rb +35 -0
  154. data/spec/cc_api_stub/spaces_spec.rb +38 -0
  155. data/spec/cc_api_stub/users_spec.rb +107 -0
  156. data/spec/cfoundry/auth_token_spec.rb +154 -0
  157. data/spec/cfoundry/baseclient_spec.rb +298 -0
  158. data/spec/cfoundry/client_spec.rb +13 -0
  159. data/spec/cfoundry/errors_spec.rb +117 -0
  160. data/spec/cfoundry/rest_client_spec.rb +349 -0
  161. data/spec/cfoundry/trace_helpers_spec.rb +91 -0
  162. data/spec/cfoundry/uaaclient_spec.rb +435 -0
  163. data/spec/cfoundry/upload_helpers_spec.rb +182 -0
  164. data/spec/cfoundry/v2/app_event_spec.rb +97 -0
  165. data/spec/cfoundry/v2/app_instance_spec.rb +31 -0
  166. data/spec/cfoundry/v2/app_spec.rb +354 -0
  167. data/spec/cfoundry/v2/app_usage_event_spec.rb +15 -0
  168. data/spec/cfoundry/v2/base_spec.rb +375 -0
  169. data/spec/cfoundry/v2/client_spec.rb +121 -0
  170. data/spec/cfoundry/v2/domain_spec.rb +63 -0
  171. data/spec/cfoundry/v2/event_spec.rb +15 -0
  172. data/spec/cfoundry/v2/managed_service_instance_spec.rb +149 -0
  173. data/spec/cfoundry/v2/model_magic/attribute_spec.rb +123 -0
  174. data/spec/cfoundry/v2/model_magic/has_summary_spec.rb +17 -0
  175. data/spec/cfoundry/v2/model_magic/to_many_spec.rb +53 -0
  176. data/spec/cfoundry/v2/model_magic/to_one_spec.rb +106 -0
  177. data/spec/cfoundry/v2/model_magic_spec.rb +43 -0
  178. data/spec/cfoundry/v2/model_spec.rb +434 -0
  179. data/spec/cfoundry/v2/organization_spec.rb +135 -0
  180. data/spec/cfoundry/v2/quota_definition_spec.rb +50 -0
  181. data/spec/cfoundry/v2/route_spec.rb +42 -0
  182. data/spec/cfoundry/v2/service_plan_spec.rb +53 -0
  183. data/spec/cfoundry/v2/service_spec.rb +58 -0
  184. data/spec/cfoundry/v2/space_spec.rb +13 -0
  185. data/spec/cfoundry/v2/user_provided_service_instance_spec.rb +57 -0
  186. data/spec/cfoundry/v2/user_spec.rb +206 -0
  187. data/spec/cfoundry/validator_spec.rb +94 -0
  188. data/spec/factories/app_events_factory.rb +7 -0
  189. data/spec/factories/app_usage_events_factory.rb +32 -0
  190. data/spec/factories/apps_factory.rb +11 -0
  191. data/spec/factories/clients_factory.rb +7 -0
  192. data/spec/factories/domains_factory.rb +10 -0
  193. data/spec/factories/events_factory.rb +50 -0
  194. data/spec/factories/organizations_factory.rb +12 -0
  195. data/spec/factories/quota_definitions_factory.rb +8 -0
  196. data/spec/factories/routes_factory.rb +10 -0
  197. data/spec/factories/service_instances_factory.rb +10 -0
  198. data/spec/factories/service_plans_factory.rb +10 -0
  199. data/spec/factories/services_factory.rb +10 -0
  200. data/spec/factories/spaces_factory.rb +10 -0
  201. data/spec/factories/user_provided_service_instances_factory.rb +10 -0
  202. data/spec/factories/users_factory.rb +10 -0
  203. data/spec/fixtures/apps/with_cfignore/.cfignore +4 -0
  204. data/spec/fixtures/apps/with_cfignore/.hidden_file +1 -0
  205. data/spec/fixtures/apps/with_cfignore/ambiguous_ignored +0 -0
  206. data/spec/fixtures/apps/with_cfignore/ignored_dir/file_in_ignored_dir.txt +1 -0
  207. data/spec/fixtures/apps/with_cfignore/ignored_file.txt +1 -0
  208. data/spec/fixtures/apps/with_cfignore/non_ignored_dir/file_in_non_ignored_dir.txt +1 -0
  209. data/spec/fixtures/apps/with_cfignore/non_ignored_dir/ignored_file.txt +1 -0
  210. data/spec/fixtures/apps/with_cfignore/non_ignored_dir/toplevel_ignored.txt +0 -0
  211. data/spec/fixtures/apps/with_cfignore/non_ignored_file.txt +1 -0
  212. data/spec/fixtures/apps/with_cfignore/toplevel_ignored.txt +0 -0
  213. data/spec/fixtures/apps/with_dotfiles/.dotfile +1 -0
  214. data/spec/fixtures/apps/with_dotfiles/xyz +1 -0
  215. data/spec/fixtures/apps/with_external_symlink/foo +1 -0
  216. data/spec/fixtures/apps/with_ignored_external_symlink/.cfignore +1 -0
  217. data/spec/fixtures/apps/with_ignored_external_symlink/foo +1 -0
  218. data/spec/fixtures/apps/with_nested_directories/foo/bar/baz/fizz +0 -0
  219. data/spec/fixtures/apps/with_nested_directories/xyz +0 -0
  220. data/spec/fixtures/empty_file +0 -0
  221. data/spec/fixtures/fake_cc_app_usage_events.json +152 -0
  222. data/spec/fixtures/fake_cc_application.json +20 -0
  223. data/spec/fixtures/fake_cc_application_summary.json +56 -0
  224. data/spec/fixtures/fake_cc_created_application.json +11 -0
  225. data/spec/fixtures/fake_cc_created_domain.json +15 -0
  226. data/spec/fixtures/fake_cc_created_organization.json +11 -0
  227. data/spec/fixtures/fake_cc_created_route.json +13 -0
  228. data/spec/fixtures/fake_cc_created_service_instance.json +11 -0
  229. data/spec/fixtures/fake_cc_created_space.json +11 -0
  230. data/spec/fixtures/fake_cc_created_user.json +11 -0
  231. data/spec/fixtures/fake_cc_domain.json +55 -0
  232. data/spec/fixtures/fake_cc_domain_spaces.json +27 -0
  233. data/spec/fixtures/fake_cc_empty_search.json +7 -0
  234. data/spec/fixtures/fake_cc_events.json +419 -0
  235. data/spec/fixtures/fake_cc_frameworks.json +20 -0
  236. data/spec/fixtures/fake_cc_managed_service_instance.json +83 -0
  237. data/spec/fixtures/fake_cc_organization.json +161 -0
  238. data/spec/fixtures/fake_cc_organization_domains.json +59 -0
  239. data/spec/fixtures/fake_cc_organization_search.json +37 -0
  240. data/spec/fixtures/fake_cc_organization_spaces.json +99 -0
  241. data/spec/fixtures/fake_cc_organization_summary.json +20 -0
  242. data/spec/fixtures/fake_cc_organization_users.json +81 -0
  243. data/spec/fixtures/fake_cc_route.json +16 -0
  244. data/spec/fixtures/fake_cc_runtimes.json +20 -0
  245. data/spec/fixtures/fake_cc_service_binding.json +22 -0
  246. data/spec/fixtures/fake_cc_service_bindings.json +24 -0
  247. data/spec/fixtures/fake_cc_service_instance.json +83 -0
  248. data/spec/fixtures/fake_cc_service_instances.json +72 -0
  249. data/spec/fixtures/fake_cc_services.json +160 -0
  250. data/spec/fixtures/fake_cc_space.json +45 -0
  251. data/spec/fixtures/fake_cc_space_apps.json +49 -0
  252. data/spec/fixtures/fake_cc_space_summary.json +84 -0
  253. data/spec/fixtures/fake_cc_spaces.json +92 -0
  254. data/spec/fixtures/fake_cc_stats.json +29 -0
  255. data/spec/fixtures/fake_cc_user.json +139 -0
  256. data/spec/fixtures/fake_cc_user_organizations.json +92 -0
  257. data/spec/fixtures/fake_cc_user_provided_service_instance.json +51 -0
  258. data/spec/fixtures/fake_cc_user_with_managers.json +85 -0
  259. data/spec/integration/client_spec.rb +38 -0
  260. data/spec/spec_helper.rb +22 -0
  261. data/spec/support/factory_girl.rb +6 -0
  262. data/spec/support/shared_examples/cc_api_stub_request_examples.rb +79 -0
  263. data/spec/support/shared_examples/client_login_examples.rb +46 -0
  264. data/spec/support/shared_examples/model_summary_examples.rb +34 -0
  265. data/spec/support/test_model_builder.rb +10 -0
  266. data/vendor/errors/README.md +4 -0
  267. data/vendor/errors/v1.yml +189 -0
  268. data/vendor/errors/v2.yml +384 -0
  269. metadata +693 -0
@@ -0,0 +1,26 @@
1
+ module CcApiStub
2
+ module Routes
3
+ extend Helper
4
+
5
+ class << self
6
+ def succeed_to_load_none
7
+ stub_get(collection_endpoint, {}, response(200, {:resources => []}))
8
+ end
9
+
10
+ def succeed_to_create
11
+ response_body = Helper.load_fixtures("fake_cc_created_route")
12
+ stub_post(collection_endpoint, {}, response(201, response_body))
13
+ end
14
+
15
+ private
16
+
17
+ def collection_endpoint
18
+ %r{/v2/routes/?.*$}
19
+ end
20
+
21
+ def object_endpoint(id = nil)
22
+ %r{/v2/routes/#{id}[^/]+$}
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ module CcApiStub
2
+ module Runtimes
3
+ extend Helper
4
+
5
+ class << self
6
+ def succeed_to_load
7
+ response_body = Helper.load_fixtures("fake_cc_runtimes")
8
+ stub_get(collection_endpoint, {}, response(200, response_body))
9
+ end
10
+
11
+ def fail_to_load
12
+ stub_get(collection_endpoint, {}, response(500))
13
+ end
14
+
15
+ private
16
+
17
+ def collection_endpoint
18
+ %r{/v2/runtimes\?inline-relations-depth=1$}
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ module CcApiStub
2
+ module ServiceBindings
3
+ extend Helper
4
+
5
+ class << self
6
+ def succeed_to_create
7
+ response_body = Helper.load_fixtures("fake_cc_service_binding")
8
+ stub_post(collection_endpoint, {}, response(201, response_body))
9
+ end
10
+
11
+ private
12
+
13
+ def object_endpoint(id = nil)
14
+ %r{/v2/service_bindings/#{id}[^/]+$}
15
+ end
16
+
17
+ def collection_endpoint
18
+ %r{/v2/service_bindings$}
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ module CcApiStub
2
+ module ServiceInstances
3
+ extend Helper
4
+
5
+ class << self
6
+ def succeed_to_create
7
+ response_body = Helper.load_fixtures("fake_cc_created_service_instance")
8
+ stub_post(collection_endpoint, {}, response(201, response_body))
9
+ end
10
+
11
+ private
12
+
13
+ def object_endpoint(id = nil)
14
+ %r{/v2/service_instances/#{id}[^/]+$}
15
+ end
16
+
17
+ def collection_endpoint
18
+ %r{/v2/service_instances$}
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ module CcApiStub
2
+ module Services
3
+ extend Helper
4
+
5
+ class << self
6
+ def service_fixture_hash
7
+ MultiJson.load(services_fixture["resources"].first.to_json, :symbolize_keys => true)
8
+ end
9
+
10
+ private
11
+
12
+ def collection_endpoint
13
+ %r{/v2/services\?inline-relations-depth=1}
14
+ end
15
+
16
+ def services_fixture
17
+ Helper.load_fixtures("fake_cc_services")
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ module CcApiStub
2
+ module SpaceUsers
3
+ extend Helper
4
+
5
+ class << self
6
+ def succeed_to_delete(options = {})
7
+ if options.has_key? :roles
8
+ options[:roles].each do |role|
9
+ stub_delete(object_endpoint(options[:id], role.to_s.pluralize), {}, response(200, ""))
10
+ end
11
+ end
12
+
13
+ stub_delete(object_endpoint(options[:id]), {}, response(200, ""))
14
+ end
15
+
16
+ def fail_to_delete(options = {})
17
+ stub_delete(object_endpoint(options[:id]), {}, response(500))
18
+ end
19
+
20
+ private
21
+
22
+ def object_endpoint(id = nil, role="users")
23
+ %r{/v2/spaces/[^/]+/#{role}/#{id}[^/]+$}
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,58 @@
1
+ module CcApiStub
2
+ module Spaces
3
+ extend Helper
4
+
5
+ class << self
6
+ def succeed_to_load(options={})
7
+ response_body = Helper.load_fixtures(options.delete(:fixture) || "fake_cc_#{object_name}", options)
8
+ stub_get(object_endpoint(options[:id]), {}, response(200, response_body))
9
+ end
10
+
11
+ def succeed_to_create
12
+ response_body = Helper.load_fixtures("fake_cc_created_space")
13
+ stub_post(collection_endpoint, {}, response(201, response_body))
14
+ end
15
+
16
+ def summary_fixture
17
+ Helper.load_fixtures("fake_cc_space_summary")
18
+ end
19
+
20
+ def succeed_to_load_summary(options={})
21
+ response_body = summary_fixture
22
+ response_body["services"] = [] if options.delete(:no_services)
23
+ stub_get(%r{/v2/spaces/[^/]+/summary$}, {}, response(200, response_body))
24
+ end
25
+
26
+ def succeed_to_load_apps(options={})
27
+ response = response_from_options(options.reverse_merge!({:fixture => "fake_cc_space_apps"}))
28
+ stub_get(%r{/v2/spaces/[^/]+/apps\?inline-relations-depth=1}, {}, response(200, response))
29
+ end
30
+
31
+ def fail_to_find(space_id)
32
+ stub_get(%r{/v2/spaces/#{space_id}}, {}, response(404, {:code => 40004, :description => "The app space could not be found:"}))
33
+ end
34
+
35
+ def space_fixture_hash
36
+ {
37
+ :metadata => {
38
+ :guid => "space-id-1",
39
+ :url => "/v2/spaces/space-id-1"
40
+ },
41
+ :entity => {
42
+ :name => "space-name-1"
43
+ }
44
+ }
45
+ end
46
+
47
+ private
48
+
49
+ def collection_endpoint
50
+ %r{/v2/spaces$}
51
+ end
52
+
53
+ def object_endpoint(id = nil)
54
+ %r{/v2/spaces/#{id}[^/]+$}
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,85 @@
1
+ module CcApiStub
2
+ module Users
3
+ extend Helper
4
+
5
+ class << self
6
+ def succeed_to_load(options={})
7
+ response_body = Helper.load_fixtures(options[:fixture] || "fake_cc_user")
8
+ response_body["metadata"]["guid"] = options[:id] || "user-id-1"
9
+
10
+ if options[:no_organizations]
11
+ response_body["entity"]["organizations"] = []
12
+ else
13
+ def space(space_id) {"metadata" => { "guid" => space_id }, "entity" => {}} end
14
+
15
+ organization = response_body["entity"]["organizations"].first
16
+ organization["metadata"]["guid"] = options[:organization_id] || "organization-id-1"
17
+ organization["entity"]["spaces"] = [] if options[:no_spaces]
18
+
19
+ permissions = options[:permissions] || [:organization_manager]
20
+
21
+ response_body["entity"]["managed_organizations"] << organization if permissions.include?(:organization_manager)
22
+ response_body["entity"]["billing_managed_organizations"] << organization if permissions.include?(:organization_billing_manager)
23
+ response_body["entity"]["audited_organizations"] << organization if permissions.include?(:organization_auditor)
24
+
25
+ unless options[:no_spaces]
26
+ space = space("space-id-1")
27
+ response_body["entity"]["spaces"] << space if permissions.include?(:space_developer)
28
+ response_body["entity"]["managed_spaces"] << space if permissions.include?(:space_manager)
29
+ response_body["entity"]["audited_spaces"] << space if permissions.include?(:space_auditor)
30
+
31
+ space2 = space("space-id-2")
32
+ response_body["entity"]["spaces"] << space2 if permissions.include?(:space2_developer)
33
+ response_body["entity"]["managed_spaces"] << space2 if permissions.include?(:space2_manager)
34
+ response_body["entity"]["audited_spaces"] << space2 if permissions.include?(:space2_auditor)
35
+ end
36
+ end
37
+
38
+ stub_get(%r{/v2/users/[^/]+\?inline-relations-depth=2$}, {}, response(200, response_body))
39
+ stub_get(%r{/v2/users/[^/]+/summary(\?inline-relations-depth=\d)?$}, {}, response(200, response_body))
40
+ end
41
+
42
+ def fail_to_find(options = {})
43
+ stub_get(object_endpoint(options[:id]), {}, response(404, {:code => 20003, :description => "The user could not be found"}))
44
+ end
45
+
46
+ def succeed_to_create
47
+ response_body = Helper.load_fixtures("fake_cc_created_user")
48
+ stub_post(collection_endpoint, {}, response(201, response_body))
49
+ end
50
+
51
+ def fail_to_create
52
+ CcApiStub::Helper.fail_request(:post, 500, {}, /users/)
53
+ end
54
+
55
+ def succeed_to_replace_permissions(options = {})
56
+ stub_put(object_endpoint(options[:id]), {}, response(200, ""))
57
+ end
58
+
59
+ def fail_to_replace_permissions(options = {})
60
+ stub_put(object_endpoint(options[:id]), {}, response(500))
61
+ end
62
+
63
+ def organizations_fixture
64
+ Helper.load_fixtures("fake_cc_user")["entity"]["organizations"]
65
+ end
66
+
67
+ def organization_fixture_hash(options={})
68
+ fixture = organizations_fixture.first
69
+ fixture["entity"].delete("spaces") if options[:no_spaces]
70
+ fixture["entity"].delete("managers") if options[:no_managers]
71
+ MultiJson.load(fixture.to_json, :symbolize_keys => true)
72
+ end
73
+
74
+ private
75
+
76
+ def collection_endpoint
77
+ %r{/v2/users$}
78
+ end
79
+
80
+ def object_endpoint(id = nil)
81
+ %r{/v2/users/#{id}[^/]+(/summary)?}
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,20 @@
1
+ require "cc_api_stub/helper"
2
+ require "cc_api_stub/organizations"
3
+ require "cc_api_stub/login"
4
+ require "cc_api_stub/app_usage_events"
5
+ require "cc_api_stub/applications"
6
+ require "cc_api_stub/domains"
7
+ require "cc_api_stub/events"
8
+ require "cc_api_stub/frameworks"
9
+ require "cc_api_stub/organization_users"
10
+ require "cc_api_stub/routes"
11
+ require "cc_api_stub/runtimes"
12
+ require "cc_api_stub/service_bindings"
13
+ require "cc_api_stub/service_instances"
14
+ require "cc_api_stub/services"
15
+ require "cc_api_stub/spaces"
16
+ require "cc_api_stub/space_users"
17
+ require "cc_api_stub/users"
18
+
19
+ module CcApiStub
20
+ end
@@ -0,0 +1,63 @@
1
+ module CFoundry
2
+ class AuthToken
3
+ class << self
4
+ def from_uaa_token_info(token_info)
5
+ new(
6
+ token_info.auth_header,
7
+ token_info.info[:refresh_token]
8
+ )
9
+ end
10
+
11
+ def from_hash(hash)
12
+ new(
13
+ hash[:token],
14
+ hash[:refresh_token]
15
+ )
16
+ end
17
+ end
18
+
19
+ def initialize(auth_header, refresh_token = nil)
20
+ @auth_header = auth_header
21
+ @refresh_token = refresh_token
22
+ end
23
+
24
+ attr_accessor :auth_header
25
+ attr_reader :refresh_token
26
+
27
+ def to_hash
28
+ {
29
+ :token => auth_header,
30
+ :refresh_token => @refresh_token
31
+ }
32
+ end
33
+
34
+ JSON_HASH = /\{.*?\}/.freeze
35
+
36
+ # TODO: rename to #data
37
+ def token_data
38
+ return @token_data if @token_data
39
+ return {} unless @auth_header
40
+
41
+ json_hashes = Base64.decode64(@auth_header.split(" ", 2).last)
42
+ data_json = json_hashes.sub(JSON_HASH, "")[JSON_HASH]
43
+ return {} unless data_json
44
+
45
+ @token_data = MultiJson.load data_json, :symbolize_keys => true
46
+ rescue MultiJson::DecodeError
47
+ {}
48
+ end
49
+
50
+ def auth_header=(auth_header)
51
+ @token_data = nil
52
+ @auth_header = auth_header
53
+ end
54
+
55
+ def expiration
56
+ Time.at(token_data[:exp])
57
+ end
58
+
59
+ def expires_soon?
60
+ (expiration.to_i - Time.now.to_i) < 60
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,216 @@
1
+ require "cfoundry/trace_helpers"
2
+ require "net/https"
3
+ require "net/http/post/multipart"
4
+ require "multi_json"
5
+ require "fileutils"
6
+ require "forwardable"
7
+
8
+ module CFoundry
9
+ class BaseClient # :nodoc:
10
+ include CFoundry::ProxyOptions
11
+
12
+ extend Forwardable
13
+
14
+ attr_reader :rest_client
15
+
16
+ def_delegators :rest_client, :target, :target=, :token,
17
+ :trace, :backtrace, :backtrace=, :log, :log=,
18
+ :http_proxy, :http_proxy=, :https_proxy, :https_proxy=
19
+
20
+ def initialize(target, token = nil)
21
+ @rest_client = CFoundry::RestClient.new(target, token)
22
+ self.trace = false
23
+ self.backtrace = false
24
+ self.log = false
25
+ end
26
+
27
+ def uaa
28
+ @uaa ||= begin
29
+ endpoint = info[:authorization_endpoint]
30
+
31
+ if endpoint
32
+ uaa = CFoundry::UAAClient.new(endpoint, "cf", http_proxy: http_proxy, https_proxy: https_proxy)
33
+ uaa.trace = trace
34
+ uaa.token = token
35
+ uaa
36
+ else
37
+ nil
38
+ end
39
+ end
40
+ end
41
+
42
+ def password_score(password)
43
+ uaa ? uaa.password_score(password) : :unknown
44
+ end
45
+
46
+ def token=(token)
47
+ if token.is_a?(String)
48
+ token = CFoundry::AuthToken.new(token)
49
+ end
50
+
51
+ @rest_client.token = token
52
+ @uaa.token = token if @uaa
53
+ end
54
+
55
+ def trace=(trace)
56
+ @rest_client.trace = trace
57
+ @uaa.trace = trace if @uaa
58
+ end
59
+
60
+ # Cloud metadata
61
+ def info
62
+ get("info", :accept => :json)
63
+ end
64
+
65
+ def get(*args)
66
+ request("GET", *args)
67
+ end
68
+
69
+ def delete(*args)
70
+ request("DELETE", *args)
71
+ end
72
+
73
+ def post(*args)
74
+ request("POST", *args)
75
+ end
76
+
77
+ def put(*args)
78
+ request("PUT", *args)
79
+ end
80
+
81
+ def request(method, *args)
82
+ if needs_token_refresh?
83
+ token.auth_header = nil
84
+ refresh_token!
85
+ end
86
+
87
+ path, options = normalize_arguments(args)
88
+ request, response = request_raw(method, path, options)
89
+ handle_response(response, options, request)
90
+ end
91
+
92
+ def request_raw(method, path, options)
93
+ @rest_client.request(method, path, options)
94
+ end
95
+
96
+ def refresh_token!
97
+ self.token = uaa.try_to_refresh_token!
98
+ end
99
+
100
+ def stream_url(url, &blk)
101
+ uri = URI.parse(url)
102
+
103
+ opts = {}
104
+
105
+ if uri.scheme == "https"
106
+ opts[:use_ssl] = true
107
+ opts[:verify_mode] = OpenSSL::SSL::VERIFY_NONE
108
+ end
109
+
110
+ Net::HTTP.start(uri.host, uri.port, *proxy_options_for(uri), opts) do |http|
111
+ http.read_timeout = 5
112
+
113
+ req = Net::HTTP::Get.new(uri.request_uri)
114
+ req["Authorization"] = token.auth_header if token
115
+
116
+ http.request(req) do |response|
117
+ case response
118
+ when Net::HTTPOK
119
+ response.read_body(&blk)
120
+ when Net::HTTPNotFound
121
+ raise CFoundry::NotFound.new(response.body, 404)
122
+ when Net::HTTPForbidden
123
+ raise CFoundry::Denied.new(response.body, 403)
124
+ when Net::HTTPUnauthorized
125
+ raise CFoundry::Unauthorized.new(response.body, 401)
126
+ else
127
+ raise CFoundry::BadResponse.new(response.body, response.code)
128
+ end
129
+ end
130
+ end
131
+ end
132
+
133
+ private
134
+
135
+ def needs_token_refresh?
136
+ token && token.auth_header && token.refresh_token && \
137
+ token.expires_soon?
138
+ end
139
+
140
+ def status_is_successful?(code)
141
+ (code >= 200) && (code < 400)
142
+ end
143
+
144
+ def handle_response(response, options, request)
145
+ if status_is_successful?(response[:status].to_i)
146
+ handle_successful_response(response, options)
147
+ else
148
+ handle_error_response(response, request)
149
+ end
150
+ end
151
+
152
+ def handle_successful_response(response, options)
153
+ if options[:return_response]
154
+ response
155
+ elsif options[:accept] == :json
156
+ parse_json(response[:body])
157
+ else
158
+ response[:body]
159
+ end
160
+ end
161
+
162
+ def handle_error_response(response, request)
163
+ body_json = parse_json(response[:body])
164
+ body_code = body_json && body_json[:code]
165
+ code = body_code || response[:status].to_i
166
+
167
+ if body_code
168
+ error_class = CFoundry::APIError.error_classes[body_code] || CFoundry::APIError
169
+ raise error_class.new(body_json[:description], body_code, request, response)
170
+ end
171
+
172
+ case code
173
+ when 404
174
+ raise CFoundry::NotFound.new(nil, code, request, response)
175
+ when 403
176
+ raise CFoundry::Denied.new(nil, code, request, response)
177
+ when 401
178
+ raise CFoundry::Unauthorized.new(nil, code, request, response)
179
+ else
180
+ raise CFoundry::BadResponse.new(nil, code, request, response)
181
+ end
182
+ end
183
+
184
+ def normalize_arguments(args)
185
+ if args.last.is_a?(Hash)
186
+ options = args.pop
187
+ else
188
+ options = {}
189
+ end
190
+
191
+ [normalize_path(args), options]
192
+ end
193
+
194
+ URI_ENCODING_PATTERN = Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")
195
+
196
+ def normalize_path(segments)
197
+ if segments.size == 1 && segments.first =~ /^\//
198
+ segments.first
199
+ else
200
+ segments.flatten.collect { |x|
201
+ URI.encode(x.to_s, URI_ENCODING_PATTERN)
202
+ }.join("/")
203
+ end
204
+ end
205
+
206
+ def parse_json(x)
207
+ if x.nil? || x.empty?
208
+ raise MultiJson::DecodeError.new("Empty JSON string")
209
+ else
210
+ MultiJson.load(x, :symbolize_keys => true)
211
+ end
212
+ rescue MultiJson::DecodeError
213
+ nil
214
+ end
215
+ end
216
+ end
@@ -0,0 +1,46 @@
1
+ module CFoundry
2
+ class ChattyHash
3
+ include Enumerable
4
+
5
+ def initialize(callback, hash = {})
6
+ @callback = callback
7
+ @hash = hash
8
+ end
9
+
10
+ def [](name)
11
+ @hash[name]
12
+ end
13
+
14
+ def []=(name, value)
15
+ @hash[name] = value
16
+ @callback.call(self)
17
+ value
18
+ end
19
+
20
+ def each(&blk)
21
+ @hash.each(&blk)
22
+ end
23
+
24
+ def delete(key)
25
+ value = @hash.delete(key)
26
+ @callback.call(self)
27
+ value
28
+ end
29
+
30
+ def to_json(*args)
31
+ @hash.to_json(*args)
32
+ end
33
+
34
+ def to_hash
35
+ @hash
36
+ end
37
+
38
+ def to_s
39
+ @hash.to_s
40
+ end
41
+
42
+ def inspect
43
+ @hash.inspect
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,42 @@
1
+ require "cfoundry/concerns/proxy_options"
2
+
3
+ require "cfoundry/baseclient"
4
+ require "cfoundry/rest_client"
5
+ require "cfoundry/auth_token"
6
+
7
+ require "cfoundry/v2/app"
8
+ require "cfoundry/v2/app_instance"
9
+ require "cfoundry/v2/service"
10
+ require "cfoundry/v2/service_binding"
11
+ require "cfoundry/v2/managed_service_instance"
12
+ require "cfoundry/v2/user_provided_service_instance"
13
+ require "cfoundry/v2/service_plan"
14
+ require "cfoundry/v2/service_auth_token"
15
+ require "cfoundry/v2/user"
16
+ require "cfoundry/v2/organization"
17
+ require "cfoundry/v2/space"
18
+ require "cfoundry/v2/domain"
19
+ require "cfoundry/v2/route"
20
+ require "cfoundry/v2/stack"
21
+ require "cfoundry/v2/quota_definition"
22
+ require "cfoundry/v2/app_event"
23
+ require "cfoundry/v2/app_usage_event"
24
+ require "cfoundry/v2/event"
25
+ require "cfoundry/v2/service_broker"
26
+
27
+ require "cfoundry/v2/base"
28
+ require "cfoundry/v2/client"
29
+ #require "cfoundry/v2/fake_client"
30
+
31
+ module CFoundry
32
+ class Client < BaseClient
33
+ def self.new(*args)
34
+ warn "DEPRECATION WARNING: Please use CFoundry::Client.get instead of CFoundry::Client.new"
35
+ get(*args)
36
+ end
37
+
38
+ def self.get(*args)
39
+ CFoundry::V2::Client.new(*args).tap { |client| client.info }
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,14 @@
1
+ require "base64"
2
+
3
+ module CFoundry
4
+ module LoginHelpers
5
+ def login_prompts
6
+ @base.uaa.prompts
7
+ end
8
+
9
+ def login(credentials)
10
+ token_info = @base.uaa.authorize(credentials)
11
+ @base.token = AuthToken.from_uaa_token_info(token_info)
12
+ end
13
+ end
14
+ end