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,170 @@
1
+ module CFoundry
2
+ module V2
3
+ module ModelMagic
4
+ module ClientExtensions
5
+ def add_client_methods(klass)
6
+ singular = klass.object_name
7
+ plural = klass.plural_object_name
8
+
9
+ define_base_client_methods do
10
+ #
11
+ # def client.MODEL
12
+ #
13
+ define_method(singular) do |guid, *args|
14
+ get("v2", plural, guid,
15
+ :accept => :json,
16
+ :params => ModelMagic.params_from(args)
17
+ )
18
+ end
19
+
20
+ #
21
+ # def client.MODELs
22
+ #
23
+ define_method(plural) do |*args|
24
+ all_pages(
25
+ get("v2", plural,
26
+ :accept => :json,
27
+ :params => ModelMagic.params_from(args)
28
+ )
29
+ )
30
+ end
31
+
32
+ #
33
+ # def client.MODELs_first_page
34
+ #
35
+ define_method(:"#{plural}_first_page") do |*args|
36
+ get("v2", plural,
37
+ :accept => :json,
38
+ :params => ModelMagic.params_from(args)
39
+ )
40
+ end
41
+
42
+ #
43
+ # def client.MODELs_for_each
44
+ #
45
+ define_method(:"#{plural}_for_each") do |*args, &block|
46
+ for_each(get("v2", plural,
47
+ :accept => :json,
48
+ :params => ModelMagic.params_from(args)
49
+ ),
50
+ &block
51
+ )
52
+ end
53
+ end
54
+
55
+
56
+ define_client_methods do
57
+ #
58
+ # def client.MODEL
59
+ #
60
+ define_method(singular) do |*args|
61
+ guid, partial, _ = args
62
+
63
+ x = klass.new(guid, self, nil, partial)
64
+
65
+ # when creating an object, automatically set the org/space
66
+ unless guid
67
+ if klass.scoped_organization && current_organization
68
+ x.send(:"#{klass.scoped_organization}=", current_organization)
69
+ end
70
+
71
+ if klass.scoped_space && current_space
72
+ x.send(:"#{klass.scoped_space}=", current_space)
73
+ end
74
+ end
75
+
76
+ x
77
+ end
78
+
79
+ #
80
+ # def client.MODELs
81
+ #
82
+ define_method(plural) do |*args|
83
+ # use current org/space
84
+ if klass.scoped_space && current_space
85
+ current_space.send(plural, *args)
86
+ elsif klass.scoped_organization && current_organization
87
+ current_organization.send(plural, *args)
88
+ else
89
+ @base.send(plural, *args).collect do |json|
90
+ send(:"make_#{singular}", json)
91
+ end
92
+ end
93
+ end
94
+
95
+ #
96
+ # def client.MODELs_first_page
97
+ #
98
+ define_method(:"#{plural}_first_page") do |*args|
99
+ response = @base.send(:"#{plural}_first_page", *args)
100
+ results = response[:resources].collect do |json|
101
+ send(:"make_#{singular}", json)
102
+ end
103
+ {
104
+ :next_page => !!response[:next_url],
105
+ :results => results
106
+ }
107
+ end
108
+
109
+ #
110
+ # def client.MODELs_for_each
111
+ #
112
+ define_method(:"#{plural}_for_each") do |*args, &block|
113
+ @base.send(:"#{plural}_for_each", *args) do |json|
114
+ result = send(:"make_#{singular}", json)
115
+ block.call(result)
116
+ end
117
+ end
118
+
119
+ #
120
+ # def client.MODEL_from
121
+ #
122
+ define_method(:"#{singular}_from") do |path, *args|
123
+ send(
124
+ :"make_#{singular}",
125
+ @base.get(
126
+ path,
127
+ :accept => :json,
128
+ :params => ModelMagic.params_from(args)))
129
+ end
130
+
131
+ #
132
+ # def client.MODELs_from
133
+ #
134
+ define_method(:"#{plural}_from") do |path, *args|
135
+ objs = @base.all_pages(
136
+ @base.get(
137
+ path,
138
+ :accept => :json,
139
+ :params => ModelMagic.params_from(args)))
140
+
141
+ objs.collect do |json|
142
+ send(:"make_#{singular}", json)
143
+ end
144
+ end
145
+
146
+ #
147
+ # def client.make_MODEL
148
+ #
149
+ define_method(:"make_#{singular}") do |json|
150
+ klass.new(
151
+ json[:metadata][:guid],
152
+ self,
153
+ json)
154
+ end
155
+ end
156
+ end
157
+
158
+ private
159
+
160
+ def define_client_methods(&blk)
161
+ ClientMethods.module_eval(&blk)
162
+ end
163
+
164
+ def define_base_client_methods(&blk)
165
+ BaseClientMethods.module_eval(&blk)
166
+ end
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,49 @@
1
+ module CFoundry::V2::ModelMagic
2
+ module HasSummary
3
+ def has_summary(actions = {})
4
+ #
5
+ # def summary
6
+ #
7
+ define_method(:summary) do
8
+ @client.base.get("v2", plural_object_name, @guid, "summary", :accept => :json)
9
+ end
10
+
11
+ #
12
+ # def summarize!
13
+ #
14
+ define_method(:summarize!) do |*args|
15
+ body, _ = args
16
+
17
+ body ||= summary
18
+
19
+ body.each do |key, val|
20
+ if act = actions[key]
21
+ instance_exec(val, &act)
22
+
23
+ elsif self.class.attributes[key]
24
+ self.send(:"#{key}=", val)
25
+
26
+ elsif self.class.to_many_relations[key]
27
+ singular = key.to_s.sub(/s$/, "").to_sym
28
+
29
+ vals = val.collect do |sub|
30
+ obj = @client.send(singular, sub[:guid], true)
31
+ obj.summarize! sub
32
+ obj
33
+ end
34
+
35
+ self.send(:"#{key}=", vals)
36
+
37
+ elsif self.class.to_one_relations[key]
38
+ obj = @client.send(key, val[:guid], true)
39
+ obj.summarize! val
40
+
41
+ self.send(:"#{key}=", obj)
42
+ end
43
+ end
44
+
45
+ nil
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,21 @@
1
+ module CFoundry::V2
2
+ module ModelMagic
3
+ module QueryValueHelper
4
+ class QueryMultiValue
5
+ attr_accessor :query_values
6
+
7
+ def initialize(*query_hashes)
8
+ self.query_values = query_hashes.collect do |query_hash|
9
+ QueryValue.new query_hash
10
+ end
11
+ end
12
+
13
+ def collect_values(key)
14
+ query_values.collect do |value|
15
+ "#{key}#{value}"
16
+ end.join(';')
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,33 @@
1
+ module CFoundry::V2
2
+ module ModelMagic
3
+ module QueryValueHelper
4
+ class QueryValue
5
+ attr_accessor :comparator, :value
6
+ def initialize(params)
7
+ self.comparator = params[:comparator] || params[:comp] || ':'
8
+ self.value = params[:value]
9
+ end
10
+
11
+ def to_s
12
+ "#{comparator_string}#{value_string}"
13
+ end
14
+
15
+ def comparator_string
16
+ if comparator.downcase == 'in' || value.is_a?(Array)
17
+ " IN "
18
+ else
19
+ comparator
20
+ end
21
+ end
22
+
23
+ def value_string
24
+ if value.is_a? Array
25
+ value.join(",")
26
+ else
27
+ value
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,39 @@
1
+ module CFoundry::V2
2
+ module ModelMagic::QueryableBy
3
+ def queryable_by(*names)
4
+ klass = self
5
+ singular = object_name
6
+ plural = plural_object_name
7
+
8
+ query = ::CFoundry::V2::QUERIES[singular]
9
+
10
+ query.module_eval do
11
+ names.each do |name|
12
+ #
13
+ # def MODEL_by_ATTRIBUTE
14
+ #
15
+ define_method(:"#{singular}_by_#{name}") do |*args|
16
+ send(:"#{plural}_by_#{name}", *args).first
17
+ end
18
+
19
+ #
20
+ # def MODELs_by_ATTRIBUTE
21
+ #
22
+ define_method(:"#{plural}_by_#{name}") do |val, *args|
23
+ options, _ = args
24
+ options ||= {}
25
+ options[:query] = [name, val]
26
+
27
+ query_target(klass).send(plural, options)
28
+ end
29
+ end
30
+ end
31
+
32
+ const_set(:Queries, query)
33
+
34
+ ClientMethods.module_eval do
35
+ include query
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,138 @@
1
+ module CFoundry::V2::ModelMagic
2
+ module ToMany
3
+ def to_many(plural, opts = {})
4
+ to_many_relations[plural] = opts
5
+
6
+ singular = plural.to_s.sub(/s$/, "").to_sym
7
+
8
+ include ::CFoundry::V2::QUERIES[singular]
9
+
10
+ object = opts[:as] || singular
11
+
12
+ kls = object.to_s.camelcase
13
+
14
+ #
15
+ # def MODELs
16
+ #
17
+ define_method(plural) do |*args|
18
+ klass = CFoundry::V2.const_get(kls)
19
+
20
+ opts, _ = args
21
+ opts ||= {}
22
+
23
+ if opts.empty? && cache = @cache[plural]
24
+ return cache
25
+ end
26
+
27
+ if @manifest && @manifest[:entity].key?(plural) && opts.empty?
28
+ objs = @manifest[:entity][plural]
29
+
30
+ if query = opts[:query]
31
+ find_by, find_val = query
32
+ objs = objs.select { |o| o[:entity][find_by] == find_val }
33
+ end
34
+
35
+ res =
36
+ objs.collect do |json|
37
+ @client.send(:"make_#{object}", json)
38
+ end
39
+ else
40
+ res =
41
+ @client.send(
42
+ :"#{klass.plural_object_name}_from",
43
+ "/v2/#{plural_object_name}/#@guid/#{plural}",
44
+ opts)
45
+ end
46
+
47
+ if opts.empty?
48
+ @cache[plural] = res
49
+ end
50
+
51
+ res
52
+ end
53
+
54
+ #
55
+ # def MODELs_url
56
+ #
57
+ define_method(:"#{plural}_url") do
58
+ manifest[:entity][:"#{plural}_url"]
59
+ end
60
+
61
+ #
62
+ # def add_MODEL
63
+ #
64
+ define_method(:"add_#{singular}") do |x|
65
+ klass = self.class.objects[object]
66
+
67
+ CFoundry::Validator.validate_type(x, klass)
68
+
69
+ if cache = @cache[plural]
70
+ cache << x unless cache.include?(x)
71
+ end
72
+
73
+ @client.base.put("v2", plural_object_name, @guid, plural, x.guid, :accept => :json)
74
+ end
75
+
76
+ #
77
+ # def create_MODEL
78
+ #
79
+ define_method("create_#{singular}") do |*args|
80
+ associated_instance = @client.send(:"#{singular}")
81
+ args.first.each do |name, value|
82
+ associated_instance.send("#{name}=", value)
83
+ end if args.first.is_a? Hash
84
+
85
+ associated_instance.create!
86
+ self.send(:"add_#{singular}", associated_instance)
87
+ associated_instance
88
+ end
89
+
90
+ #
91
+ # def remove_MODEL
92
+ #
93
+ define_method(:"remove_#{singular}") do |x|
94
+ klass = self.class.objects[object]
95
+
96
+ CFoundry::Validator.validate_type(x, klass)
97
+
98
+ if cache = @cache[plural]
99
+ cache.delete(x)
100
+ end
101
+
102
+ @client.base.delete("v2", plural_object_name, @guid, plural, x.guid, :accept => :json)
103
+ end
104
+
105
+ #
106
+ # def MODELs=
107
+ #
108
+ define_method(:"#{plural}=") do |xs|
109
+ klass = self.class.objects[object]
110
+
111
+ CFoundry::Validator.validate_type(xs, [klass])
112
+
113
+ @manifest ||= {}
114
+ @manifest[:entity] ||= {}
115
+
116
+ old = @manifest[:entity][:"#{singular}_guids"]
117
+ if old != xs.collect(&:guid)
118
+ old_objs =
119
+ @cache[plural] ||
120
+ if all = @manifest[:entity][plural]
121
+ all.collect do |m|
122
+ klass.new(@client, m[:metadata][:guid], m)
123
+ end
124
+ elsif old
125
+ old.collect { |id| klass.new(@client, id) }
126
+ end
127
+
128
+ @changes[plural] = [old_objs, xs]
129
+ end
130
+
131
+ @cache[plural] = xs
132
+
133
+ @manifest[:entity][:"#{singular}_guids"] =
134
+ @diff[:"#{singular}_guids"] = xs.collect(&:guid)
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,81 @@
1
+ module CFoundry::V2::ModelMagic
2
+ module ToOne
3
+ def to_one(name, opts = {})
4
+ to_one_relations[name] = opts
5
+
6
+ association_name = opts[:as] || name
7
+ default = opts[:default]
8
+
9
+ if has_default = opts.key?(:default)
10
+ defaults[:"#{name}_guid"] = default
11
+ end
12
+
13
+ #
14
+ # def MODEL
15
+ #
16
+ define_method(name) do
17
+ return @cache[name] if @cache.key?(name)
18
+ return @client.send(association_name) unless persisted?
19
+
20
+ @cache[name] =
21
+ if @manifest && @manifest[:entity].key?(name)
22
+ @client.send(:"make_#{association_name}", @manifest[:entity][name])
23
+ elsif url = send("#{name}_url")
24
+ @client.send(:"#{association_name}_from", url)
25
+ else
26
+ default
27
+ end
28
+ end
29
+
30
+ #
31
+ # def create_MODEL
32
+ #
33
+ define_method("create_#{name}") do |*args|
34
+ associated_instance = @client.send(:"#{association_name}")
35
+ args.first.each do |name, value|
36
+ associated_instance.send("#{name}=", value)
37
+ end if args.first.is_a? Hash
38
+
39
+ associated_instance.create!
40
+ self.send("#{name}=", associated_instance)
41
+ end
42
+
43
+ #
44
+ # def MODEL_url
45
+ #
46
+ define_method(:"#{name}_url") do
47
+ manifest[:entity][:"#{name}_url"]
48
+ end
49
+
50
+ #
51
+ # def MODEL=
52
+ #
53
+ define_method(:"#{name}=") do |assigned_value|
54
+ klass = self.class.objects[association_name]
55
+
56
+ unless has_default && assigned_value == default
57
+ CFoundry::Validator.validate_type(assigned_value, klass)
58
+ end
59
+
60
+ @manifest ||= {}
61
+ @manifest[:entity] ||= {}
62
+
63
+ old_guid = @manifest[:entity][:"#{name}_guid"]
64
+ association_guid = assigned_value ? assigned_value.guid : nil
65
+
66
+ if old_guid != (association_guid)
67
+ old_obj =
68
+ @cache[name] || klass.new(@client, old_guid, @manifest[:entity][name])
69
+
70
+ @changes[name] = [old_obj, assigned_value]
71
+ end
72
+
73
+ @cache[name] = assigned_value
74
+
75
+ @manifest[:entity][:"#{name}_guid"] = association_guid
76
+ @diff[:"#{name}_guid"] = association_guid
77
+ assigned_value
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,134 @@
1
+ require "cfoundry/validator"
2
+ require "cfoundry/v2/model_magic/client_extensions"
3
+ require "cfoundry/v2/model_magic/has_summary"
4
+ require "cfoundry/v2/model_magic/attribute"
5
+ require "cfoundry/v2/model_magic/to_one"
6
+ require "cfoundry/v2/model_magic/to_many"
7
+ require "cfoundry/v2/model_magic/queryable_by"
8
+ require "cfoundry/v2/model_magic/query_value_helper"
9
+ require "cfoundry/v2/model_magic/query_multi_value_helper"
10
+
11
+ module CFoundry::V2
12
+ # object name -> module containing query methods
13
+ #
14
+ # e.g. app -> { app_by_name, app_by_space_guid, ... }
15
+ QUERIES = Hash.new do |h, k|
16
+ h[k] = Module.new
17
+ end
18
+
19
+ module BaseClientMethods
20
+ end
21
+
22
+ module ClientMethods
23
+ end
24
+
25
+ module ModelMagic
26
+ include ModelMagic::ClientExtensions
27
+ include ModelMagic::HasSummary
28
+ include ModelMagic::Attribute
29
+ include ModelMagic::ToOne
30
+ include ModelMagic::ToMany
31
+ include ModelMagic::QueryableBy
32
+ include ModelMagic::QueryValueHelper
33
+
34
+ attr_reader :scoped_organization, :scoped_space
35
+
36
+ def object_name
37
+ @object_name ||=
38
+ name.split("::").last.gsub(
39
+ /([a-z])([A-Z])/,
40
+ '\1_\2').downcase.to_sym
41
+ end
42
+
43
+ def plural_object_name
44
+ :"#{object_name}s"
45
+ end
46
+
47
+ def defaults
48
+ @defaults ||= {}
49
+ end
50
+
51
+ def attributes
52
+ @attributes ||= {}
53
+ end
54
+
55
+ def to_one_relations
56
+ @to_one_relations ||= {}
57
+ end
58
+
59
+ def to_many_relations
60
+ @to_many_relations ||= {}
61
+ end
62
+
63
+ def inherited(klass)
64
+ add_client_methods(klass)
65
+ has_summary
66
+ end
67
+
68
+ def scoped_to_organization(relation = :organization)
69
+ @scoped_organization = relation
70
+ end
71
+
72
+ def scoped_to_space(relation = :space)
73
+ @scoped_space = relation
74
+ end
75
+
76
+ # To query a single attribute using equality,
77
+ # you can use :query => ["attribute", "value"]
78
+ #
79
+ # To query multiple attributes, you can specify
80
+ # a hash of attributes to values, where the values
81
+ # can be:
82
+ # A single value with equality
83
+ # :query => {attr1: 'value1', attr2: 'value2'}
84
+ # Multiple values for an attribute
85
+ # :query => {attr1: ['value1', 'value2']}
86
+ # Complex comparisons i.e ('<', '>', '<=', '>=')
87
+ # :query => {attr1: QueryValue.new(comparator: '>', value: 'VALUE')}
88
+ #
89
+ # QueryValue can be found in CFoundry::V2::ModelMagic::QueryValueHelper
90
+ # You can include this module in your class to access QueryValue directly
91
+ # priting is handled by #to_s
92
+ def self.params_from(args)
93
+ options, _ = args
94
+ options ||= {}
95
+ options[:depth] ||= 1
96
+
97
+ params = {}
98
+ options.each do |k, v|
99
+ case k
100
+ when :depth
101
+ params[:"inline-relations-depth"] = v
102
+ when :query
103
+ if v.is_a? Array
104
+ params[:q] = v.join(":")
105
+ else
106
+ params[:q] = query_from_hash(v)
107
+ end
108
+ when :user_provided
109
+ params[:"return_user_provided_service_instances"] = v
110
+ else
111
+ params[k] = v
112
+ end
113
+ end
114
+
115
+ params
116
+ end
117
+
118
+ def self.query_from_hash(query_params)
119
+ query_params.collect do |key, value|
120
+ case value
121
+ when Array
122
+ qv = QueryValue.new(:comp => 'IN', :value => value)
123
+ "#{key}#{qv}"
124
+ when QueryValue
125
+ "#{key}#{value}"
126
+ when QueryMultiValue
127
+ value.collect_values(key)
128
+ else
129
+ "#{key}:#{value}"
130
+ end
131
+ end.join(";")
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,44 @@
1
+ require 'cfoundry/v2/model'
2
+ require "cfoundry/v2/helper"
3
+
4
+ module CFoundry::V2
5
+ class Organization < Model
6
+ extend Helper
7
+
8
+ attribute :name, :string
9
+ attribute :billing_enabled, :boolean
10
+ attribute :status, :string
11
+
12
+ to_many :spaces
13
+ to_many :domains
14
+ to_many :users
15
+ to_many :managers, :as => :user
16
+ to_many :billing_managers, :as => :user
17
+ to_many :auditors, :as => :user
18
+
19
+ to_many_support :spaces
20
+ to_many_support :domains
21
+ to_many_support :users
22
+ to_many_support :managers
23
+ to_many_support :billing_managers
24
+ to_many_support :auditors
25
+
26
+ to_one :quota_definition
27
+
28
+ queryable_by :name, :space_guid, :user_guid, :manager_guid,
29
+ :billing_manager_guid, :auditor_guid
30
+
31
+ def delete_user_from_all_roles(user)
32
+ remove_user(user)
33
+ remove_manager(user)
34
+ remove_billing_manager(user)
35
+ remove_auditor(user)
36
+
37
+ spaces.each do |space|
38
+ space.remove_developer(user)
39
+ space.remove_auditor(user)
40
+ space.remove_manager(user)
41
+ end
42
+ end
43
+ end
44
+ end