wakame-vdc-webui 10.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (281) hide show
  1. data/INSTALL.txt +21 -0
  2. data/LICENSE +202 -0
  3. data/NOTICE +1 -0
  4. data/README +256 -0
  5. data/Rakefile +42 -0
  6. data/app/controllers/accounts_controller.rb +15 -0
  7. data/app/controllers/application_controller.rb +5 -0
  8. data/app/controllers/dialog_controller.rb +80 -0
  9. data/app/controllers/home_controller.rb +5 -0
  10. data/app/controllers/host_pools_controller.rb +26 -0
  11. data/app/controllers/images_controller.rb +28 -0
  12. data/app/controllers/information_controller.rb +10 -0
  13. data/app/controllers/instances_controller.rb +59 -0
  14. data/app/controllers/keypairs_controller.rb +64 -0
  15. data/app/controllers/security_groups_controller.rb +59 -0
  16. data/app/controllers/sessions_controller.rb +25 -0
  17. data/app/controllers/snapshots_controller.rb +48 -0
  18. data/app/controllers/storage_pools_controller.rb +27 -0
  19. data/app/controllers/users_controller.rb +3 -0
  20. data/app/controllers/volumes_controller.rb +86 -0
  21. data/app/helpers/accounts_helper.rb +2 -0
  22. data/app/helpers/application_helper.rb +23 -0
  23. data/app/helpers/dialog_helper.rb +2 -0
  24. data/app/helpers/home_helper.rb +2 -0
  25. data/app/helpers/host_pools_helper.rb +2 -0
  26. data/app/helpers/images_helper.rb +2 -0
  27. data/app/helpers/information_helper.rb +2 -0
  28. data/app/helpers/instances_helper.rb +2 -0
  29. data/app/helpers/keypairs_helper.rb +2 -0
  30. data/app/helpers/security_groups_helper.rb +2 -0
  31. data/app/helpers/sessions_helper.rb +2 -0
  32. data/app/helpers/snapshots_helper.rb +2 -0
  33. data/app/helpers/storage_pools_helper.rb +2 -0
  34. data/app/helpers/users_helper.rb +2 -0
  35. data/app/helpers/volumes_helper.rb +2 -0
  36. data/app/models/account.rb +104 -0
  37. data/app/models/authz.rb +102 -0
  38. data/app/models/base_new.rb +311 -0
  39. data/app/models/dcmgr_resource/account.rb +5 -0
  40. data/app/models/dcmgr_resource/base.rb +51 -0
  41. data/app/models/dcmgr_resource/host_pool.rb +11 -0
  42. data/app/models/dcmgr_resource/image.rb +11 -0
  43. data/app/models/dcmgr_resource/instance.rb +37 -0
  44. data/app/models/dcmgr_resource/mock.rb +15 -0
  45. data/app/models/dcmgr_resource/netfilter_group.rb +29 -0
  46. data/app/models/dcmgr_resource/ssh_key_pair.rb +25 -0
  47. data/app/models/dcmgr_resource/storage_pool.rb +11 -0
  48. data/app/models/dcmgr_resource/volume.rb +48 -0
  49. data/app/models/dcmgr_resource/volume_snapshot.rb +35 -0
  50. data/app/models/information.rb +9 -0
  51. data/app/models/schema.rb +51 -0
  52. data/app/models/tag.rb +121 -0
  53. data/app/models/tag_mapping.rb +25 -0
  54. data/app/models/user.rb +46 -0
  55. data/app/views/accounts/index.html.erb +68 -0
  56. data/app/views/dialog/attach_volume.html.erb +13 -0
  57. data/app/views/dialog/create_and_edit_security_group.html.erb +32 -0
  58. data/app/views/dialog/create_snapshot.html.erb +7 -0
  59. data/app/views/dialog/create_ssh_keypair.html.erb +15 -0
  60. data/app/views/dialog/create_volume.html.erb +18 -0
  61. data/app/views/dialog/create_volume_from_snapshot.html.erb +7 -0
  62. data/app/views/dialog/delete_security_group.html.erb +4 -0
  63. data/app/views/dialog/delete_snapshot.html.erb +7 -0
  64. data/app/views/dialog/delete_ssh_keypair.html.erb +4 -0
  65. data/app/views/dialog/delete_volume.html.erb +7 -0
  66. data/app/views/dialog/detach_volume.html.erb +7 -0
  67. data/app/views/dialog/launch_instance.html.erb +70 -0
  68. data/app/views/dialog/reboot_instances.html.erb +7 -0
  69. data/app/views/dialog/start_instances.html.erb +8 -0
  70. data/app/views/dialog/stop_instances.html.erb +7 -0
  71. data/app/views/dialog/terminate_instances.html.erb +7 -0
  72. data/app/views/home/index.html.erb +55 -0
  73. data/app/views/host_pools/index.html.erb +103 -0
  74. data/app/views/images/index.html.erb +126 -0
  75. data/app/views/information/index.html.erb +25 -0
  76. data/app/views/instances/index.html.erb +134 -0
  77. data/app/views/keypairs/index.html.erb +104 -0
  78. data/app/views/layouts/_footer.html.erb +4 -0
  79. data/app/views/layouts/_header.html.erb +14 -0
  80. data/app/views/layouts/_informationarea.html.erb +18 -0
  81. data/app/views/layouts/_login_header.erb +4 -0
  82. data/app/views/layouts/_service_health.erb +27 -0
  83. data/app/views/layouts/_sidemenu.html.erb +58 -0
  84. data/app/views/layouts/application.html.erb +34 -0
  85. data/app/views/layouts/login.html.erb +25 -0
  86. data/app/views/security_groups/index.html.erb +114 -0
  87. data/app/views/sessions/new.html.erb +27 -0
  88. data/app/views/snapshots/index.html.erb +110 -0
  89. data/app/views/storage_pools/index.html.erb +98 -0
  90. data/app/views/volumes/index.html.erb +117 -0
  91. data/config.ru +4 -0
  92. data/config/application.rb +48 -0
  93. data/config/boot.rb +13 -0
  94. data/config/database.yml +20 -0
  95. data/config/environment.rb +5 -0
  96. data/config/environments/development.rb +28 -0
  97. data/config/environments/production.rb +49 -0
  98. data/config/environments/test.rb +35 -0
  99. data/config/initializers/backtrace_silencers.rb +7 -0
  100. data/config/initializers/dcmgr_gui.rb +14 -0
  101. data/config/initializers/inflections.rb +10 -0
  102. data/config/initializers/mime_types.rb +5 -0
  103. data/config/initializers/secret_token.rb +7 -0
  104. data/config/initializers/session_store.rb +8 -0
  105. data/config/locales/en.yml +5 -0
  106. data/config/routes.rb +109 -0
  107. data/db/seeds.rb +7 -0
  108. data/extras/authentication.rb +94 -0
  109. data/extras/util.rb +5 -0
  110. data/lib/tasks/dcmgr_gui.rake +51 -0
  111. data/public/404.html +26 -0
  112. data/public/422.html +26 -0
  113. data/public/500.html +26 -0
  114. data/public/favicon.ico +0 -0
  115. data/public/images/app_active.gif +0 -0
  116. data/public/images/app_passive.gif +0 -0
  117. data/public/images/body_bg.gif +0 -0
  118. data/public/images/btn_change.gif +0 -0
  119. data/public/images/btn_create_security_group.gif +0 -0
  120. data/public/images/btn_create_snapshot.gif +0 -0
  121. data/public/images/btn_create_volume.gif +0 -0
  122. data/public/images/btn_createkey.gif +0 -0
  123. data/public/images/btn_de_register.gif +0 -0
  124. data/public/images/btn_delete_security_group.gif +0 -0
  125. data/public/images/btn_delete_snapshot.gif +0 -0
  126. data/public/images/btn_delete_volume.gif +0 -0
  127. data/public/images/btn_deletekey.gif +0 -0
  128. data/public/images/btn_healthdetail.gif +0 -0
  129. data/public/images/btn_hide.gif +0 -0
  130. data/public/images/btn_launch.gif +0 -0
  131. data/public/images/btn_next.gif +0 -0
  132. data/public/images/btn_prev.gif +0 -0
  133. data/public/images/btn_refresh.gif +0 -0
  134. data/public/images/btn_register_new_ami.gif +0 -0
  135. data/public/images/btn_savechange.gif +0 -0
  136. data/public/images/btn_show.gif +0 -0
  137. data/public/images/btn_show_accesskey.png +0 -0
  138. data/public/images/bup_btn_healthdetail.gif +0 -0
  139. data/public/images/bup_btn_refresh.gif +0 -0
  140. data/public/images/clear.gif +0 -0
  141. data/public/images/content_bg.png +0 -0
  142. data/public/images/content_foot_bg.gif +0 -0
  143. data/public/images/content_head_bg.gif +0 -0
  144. data/public/images/footer_bg.gif +0 -0
  145. data/public/images/head_gradation_bg.gif +0 -0
  146. data/public/images/header_logo.gif +0 -0
  147. data/public/images/icon_blockstore.gif +0 -0
  148. data/public/images/icon_caution.gif +0 -0
  149. data/public/images/icon_health.gif +0 -0
  150. data/public/images/icon_images.gif +0 -0
  151. data/public/images/icon_information.gif +0 -0
  152. data/public/images/icon_instances.gif +0 -0
  153. data/public/images/icon_resource.gif +0 -0
  154. data/public/images/icon_resource01.gif +0 -0
  155. data/public/images/icon_resource02.gif +0 -0
  156. data/public/images/icon_resource03.gif +0 -0
  157. data/public/images/icon_resource04.gif +0 -0
  158. data/public/images/icon_rss.gif +0 -0
  159. data/public/images/icon_security.gif +0 -0
  160. data/public/images/icon_service.gif +0 -0
  161. data/public/images/icon_service_stat.gif +0 -0
  162. data/public/images/icon_stop.gif +0 -0
  163. data/public/images/icon_summary.gif +0 -0
  164. data/public/images/icon_ttl_accskey.gif +0 -0
  165. data/public/images/icon_ttl_detail.gif +0 -0
  166. data/public/images/icon_ttl_instance.gif +0 -0
  167. data/public/images/icon_ttl_personal_inf.gif +0 -0
  168. data/public/images/infra_active.gif +0 -0
  169. data/public/images/infra_passive.gif +0 -0
  170. data/public/images/lastelement_bg.gif +0 -0
  171. data/public/images/lastelement_bg.png +0 -0
  172. data/public/images/listicon_useast.gif +0 -0
  173. data/public/images/loader_ball.gif +0 -0
  174. data/public/images/loader_boxes.gif +0 -0
  175. data/public/images/loading.gif +0 -0
  176. data/public/images/mainarea_bottom_bg.gif +0 -0
  177. data/public/images/mainarea_content_bg.gif +0 -0
  178. data/public/images/mainarea_h3_bg.gif +0 -0
  179. data/public/images/mainarea_wide_bottom_bg.gif +0 -0
  180. data/public/images/mainarea_wide_content_bg.gif +0 -0
  181. data/public/images/mainarea_wide_h3_bg.gif +0 -0
  182. data/public/images/menu_list_header.gif +0 -0
  183. data/public/images/menu_list_header_current.gif +0 -0
  184. data/public/images/plat_active.gif +0 -0
  185. data/public/images/plat_passive.gif +0 -0
  186. data/public/images/regionselect_bg.gif +0 -0
  187. data/public/images/select_bg_126.gif +0 -0
  188. data/public/images/select_bg_139.gif +0 -0
  189. data/public/images/sidemenu_bg.gif +0 -0
  190. data/public/images/sidemenu_bottom.gif +0 -0
  191. data/public/images/sidemenu_top.gif +0 -0
  192. data/public/images/ui-bg_diagonals-thick_18_b81900_40x40.png +0 -0
  193. data/public/images/ui-bg_diagonals-thick_20_666666_40x40.png +0 -0
  194. data/public/images/ui-bg_flat_10_000000_40x100.png +0 -0
  195. data/public/images/ui-bg_glass_100_f6f6f6_1x400.png +0 -0
  196. data/public/images/ui-bg_glass_100_fdf5ce_1x400.png +0 -0
  197. data/public/images/ui-bg_glass_55_fcf0ba_1x400.png +0 -0
  198. data/public/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  199. data/public/images/ui-bg_gloss-wave_100_ece8da_500x100.png +0 -0
  200. data/public/images/ui-bg_gloss-wave_35_f6a828_500x100.png +0 -0
  201. data/public/images/ui-bg_highlight-hard_100_f5f3e5_1x100.png +0 -0
  202. data/public/images/ui-bg_highlight-hard_100_fafaf4_1x100.png +0 -0
  203. data/public/images/ui-bg_highlight-hard_15_459e00_1x100.png +0 -0
  204. data/public/images/ui-bg_highlight-hard_95_cccccc_1x100.png +0 -0
  205. data/public/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
  206. data/public/images/ui-bg_highlight-soft_25_67b021_1x100.png +0 -0
  207. data/public/images/ui-bg_highlight-soft_75_ffe45c_1x100.png +0 -0
  208. data/public/images/ui-bg_highlight-soft_95_ffedad_1x100.png +0 -0
  209. data/public/images/ui-bg_inset-soft_15_2b2922_1x100.png +0 -0
  210. data/public/images/ui-icons_222222_256x240.png +0 -0
  211. data/public/images/ui-icons_228ef1_256x240.png +0 -0
  212. data/public/images/ui-icons_808080_256x240.png +0 -0
  213. data/public/images/ui-icons_847e71_256x240.png +0 -0
  214. data/public/images/ui-icons_8dc262_256x240.png +0 -0
  215. data/public/images/ui-icons_cd0a0a_256x240.png +0 -0
  216. data/public/images/ui-icons_eeeeee_256x240.png +0 -0
  217. data/public/images/ui-icons_ef8c08_256x240.png +0 -0
  218. data/public/images/ui-icons_ffd27a_256x240.png +0 -0
  219. data/public/images/ui-icons_ffffff_256x240.png +0 -0
  220. data/public/images/vtip_arrow.png +0 -0
  221. data/public/javascripts/controls.js +965 -0
  222. data/public/javascripts/dcmgr_gui/application.js +91 -0
  223. data/public/javascripts/dcmgr_gui/core.js +539 -0
  224. data/public/javascripts/dcmgr_gui/dashboard_panel.js +34 -0
  225. data/public/javascripts/dcmgr_gui/host_pool_panel.js +83 -0
  226. data/public/javascripts/dcmgr_gui/image_panel.js +242 -0
  227. data/public/javascripts/dcmgr_gui/instance_panel.js +197 -0
  228. data/public/javascripts/dcmgr_gui/security_group_panel.js +196 -0
  229. data/public/javascripts/dcmgr_gui/snapshot_panel.js +168 -0
  230. data/public/javascripts/dcmgr_gui/sshkeypair_panel.js +161 -0
  231. data/public/javascripts/dcmgr_gui/storage_pool_panel.js +99 -0
  232. data/public/javascripts/dcmgr_gui/volume_panel.js +311 -0
  233. data/public/javascripts/dragdrop.js +974 -0
  234. data/public/javascripts/effects.js +1123 -0
  235. data/public/javascripts/jquery-1.4.2.min.js +154 -0
  236. data/public/javascripts/jquery-ui-1.8.4.custom.min.js +763 -0
  237. data/public/javascripts/jquery.loadmask.min.js +10 -0
  238. data/public/javascripts/jquery.tmpl.js +486 -0
  239. data/public/javascripts/jsdeferred.jquery.js +385 -0
  240. data/public/javascripts/prototype.js +6001 -0
  241. data/public/javascripts/rails.js +175 -0
  242. data/public/robots.txt +5 -0
  243. data/public/stylesheets/common.css +1236 -0
  244. data/public/stylesheets/dialog.css +67 -0
  245. data/public/stylesheets/jquery-ui-1.8.4.custom.css +572 -0
  246. data/public/stylesheets/jquery.loadmask.css +40 -0
  247. data/public/stylesheets/reset.css +71 -0
  248. data/public/stylesheets/style.css +7 -0
  249. data/public/stylesheets/vtip.css +28 -0
  250. data/script/rails +6 -0
  251. data/test/functional/accounts_controller_test.rb +8 -0
  252. data/test/functional/dialog_controller_test.rb +8 -0
  253. data/test/functional/home_controller_test.rb +8 -0
  254. data/test/functional/host_pools_controller_test.rb +8 -0
  255. data/test/functional/images_controller_test.rb +8 -0
  256. data/test/functional/information_controller_test.rb +8 -0
  257. data/test/functional/instances_controller_test.rb +8 -0
  258. data/test/functional/keypairs_controller_test.rb +8 -0
  259. data/test/functional/security_groups_controller_test.rb +8 -0
  260. data/test/functional/sessions_controller_test.rb +8 -0
  261. data/test/functional/snapshots_controller_test.rb +8 -0
  262. data/test/functional/storage_pools_controller_test.rb +8 -0
  263. data/test/functional/users_controller_test.rb +8 -0
  264. data/test/functional/volumes_controller_test.rb +8 -0
  265. data/test/performance/browsing_test.rb +9 -0
  266. data/test/test_helper.rb +13 -0
  267. data/test/unit/helpers/accounts_helper_test.rb +4 -0
  268. data/test/unit/helpers/dialog_helper_test.rb +4 -0
  269. data/test/unit/helpers/home_helper_test.rb +4 -0
  270. data/test/unit/helpers/host_pools_helper_test.rb +4 -0
  271. data/test/unit/helpers/images_helper_test.rb +4 -0
  272. data/test/unit/helpers/information_helper_test.rb +4 -0
  273. data/test/unit/helpers/instances_helper_test.rb +4 -0
  274. data/test/unit/helpers/keypairs_helper_test.rb +4 -0
  275. data/test/unit/helpers/security_groups_helper_test.rb +4 -0
  276. data/test/unit/helpers/sessions_helper_test.rb +4 -0
  277. data/test/unit/helpers/snapshots_helper_test.rb +4 -0
  278. data/test/unit/helpers/storage_pools_helper_test.rb +4 -0
  279. data/test/unit/helpers/users_helper_test.rb +4 -0
  280. data/test/unit/helpers/volumes_helper_test.rb +4 -0
  281. metadata +399 -0
@@ -0,0 +1,27 @@
1
+ class StoragePoolsController < ApplicationController
2
+ respond_to :json
3
+
4
+ def index
5
+ end
6
+
7
+ def list
8
+ data = {
9
+ :start => params[:start].to_i - 1,
10
+ :limit => params[:limit]
11
+ }
12
+ strage_pools = DcmgrResource::StoragePool.list(data)
13
+ respond_with(strage_pools[0], :to => [:json])
14
+ end
15
+
16
+ def show
17
+ storage_pool_id = params[:id]
18
+ detail = DcmgrResource::StoragePool.show(storage_pool_id)
19
+ respond_with(detail,:to => [:json])
20
+ end
21
+
22
+ def show_storage_pools
23
+ storage_pools = DcmgrResource::StoragePool.list
24
+ respond_with(storage_pools[0],:to => [:json])
25
+ end
26
+
27
+ end
@@ -0,0 +1,3 @@
1
+ class UsersController < ApplicationController
2
+ skip_before_filter :login_required
3
+ end
@@ -0,0 +1,86 @@
1
+ class VolumesController < ApplicationController
2
+ respond_to :json
3
+ include Util
4
+
5
+ def index
6
+ end
7
+
8
+ def create
9
+ snapshot_ids = params[:ids]
10
+ if snapshot_ids
11
+ response = []
12
+ snapshot_ids.each do |snapshot_id|
13
+ data = {
14
+ :snapshot_id => snapshot_id
15
+ }
16
+ response << DcmgrResource::Volume.create(data)
17
+ end
18
+ render :json => response
19
+ else
20
+ # Convert to MB
21
+ size = case params[:unit]
22
+ when 'gb'
23
+ params[:size].to_i * 1024
24
+ when 'tb'
25
+ params[:size].to_i * 1024 * 1024
26
+ end
27
+
28
+ storage_pool_id = params[:storage_pool_id] #option
29
+
30
+ data = {
31
+ :volume_size => size,
32
+ :storage_pool_id => storage_pool_id
33
+ }
34
+
35
+ @volume = DcmgrResource::Volume.create(data)
36
+
37
+ render :json => @volume
38
+ end
39
+ end
40
+
41
+ def destroy
42
+ volume_ids = params[:ids]
43
+ response = []
44
+ volume_ids.each do |volume_id|
45
+ response << DcmgrResource::Volume.destroy(volume_id)
46
+ end
47
+ render :json => response
48
+ end
49
+
50
+ def list
51
+ data = {
52
+ :start => params[:start].to_i - 1,
53
+ :limit => params[:limit]
54
+ }
55
+ volumes = DcmgrResource::Volume.list(data)
56
+ respond_with(volumes[0],:to => [:json])
57
+ end
58
+
59
+ # GET volumes/vol-24f1af4d.json
60
+ def show
61
+ volume_id = params[:id]
62
+ detail = DcmgrResource::Volume.show(volume_id)
63
+ respond_with(detail,:to => [:json])
64
+ end
65
+
66
+ def attach
67
+ volume_id = params[:volume_id]
68
+ instance_id = params[:instance_id]
69
+ response = DcmgrResource::Volume.attach(volume_id, instance_id)
70
+ render :json => response
71
+ end
72
+
73
+ def detach
74
+ volume_ids = params[:ids]
75
+ response = []
76
+ volume_ids.each do |volume_id|
77
+ response << DcmgrResource::Volume.detach(volume_id)
78
+ end
79
+ render :json => response
80
+ end
81
+
82
+ def total
83
+ total_resource = DcmgrResource::Volume.total_resource
84
+ render :json => total_resource
85
+ end
86
+ end
@@ -0,0 +1,2 @@
1
+ module AccountsHelper
2
+ end
@@ -0,0 +1,23 @@
1
+ module ApplicationHelper
2
+
3
+ def li_link_to_current(name, controller)
4
+ if current_page?(:controller => controller)
5
+ str = '<li class="current">' + link_to(name,controller) + '</li>'
6
+ else
7
+ str = '<li>' + link_to(name,controller) + '</li>'
8
+ end
9
+ str.html_safe
10
+ end
11
+
12
+ def show_accounts
13
+ User.account_name_with_uuid(@current_user.uuid)
14
+ end
15
+
16
+ def primary_account_id
17
+ User.primary_account_id(@current_user.uuid)
18
+ end
19
+
20
+ def nl2br(text)
21
+ text.gsub(/(<.*?>)/, '').gsub(/\n/, '<br />').html_safe
22
+ end
23
+ end
@@ -0,0 +1,2 @@
1
+ module DialogHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module HomeHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module HostPoolsHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module ImagesHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module InformationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module InstancesHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module KeypairsHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module SecurityGroupsHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module SessionsHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module SnapshotsHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module StoragePoolsHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module UsersHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module VolumesHelper
2
+ end
@@ -0,0 +1,104 @@
1
+ # -*- coding: utf-8 -*-
2
+ class Account < BaseNew
3
+ taggable 'a'
4
+ with_timestamps
5
+ plugin :single_table_inheritance, :uuid, :model_map=>{}
6
+ plugin :subclasses
7
+
8
+ # pk has to be overwritten by the STI subclasses.
9
+ unrestrict_primary_key
10
+
11
+ DISABLED=0
12
+ ENABLED=1
13
+
14
+ inheritable_schema do
15
+ primary_key :id, :type=>Integer
16
+ String :name
17
+ Boolean :enable, :default=>true
18
+ DateTime :deleted_at, :null=>true
19
+ Boolean :is_deleted, :default=>false
20
+ end
21
+
22
+ one_to_many :tags
23
+ many_to_many :users,:join_table => :users_accounts
24
+
25
+ def disable?
26
+ self.enabled == DISABLED
27
+ end
28
+
29
+ def enable?
30
+ self.enabled == ENABLED
31
+ end
32
+
33
+ def to_hash_document
34
+ h = self.values.dup
35
+ h[:id] = h[:uuid] = self.canonical_uuid
36
+ h
37
+ end
38
+
39
+
40
+ # STI class variable setter, getter methods.
41
+ class << self
42
+ def default_values
43
+ @default_values ||= {}
44
+ end
45
+
46
+ def pk(pk=nil)
47
+ if pk
48
+ default_values[:id] = pk
49
+ end
50
+ default_values[:id]
51
+ end
52
+
53
+ def uuid(uuid=nil)
54
+ if uuid.is_a?(String)
55
+ uuid = uuid.downcase
56
+ if uuid !~ /^[a-z0-9]{8}$/
57
+ raise "Invalid syntax of uuid: #{uuid}"
58
+ end
59
+ default_values[:uuid] = uuid
60
+ end
61
+ default_values[:uuid] || raise("#{self}.uuid is unset. Set the unique number")
62
+ end
63
+
64
+ def description(description=nil)
65
+ if description
66
+ default_values[:description] = description
67
+ end
68
+ default_values[:description]
69
+ end
70
+ end
71
+
72
+ module SystemAccount
73
+ def self.define_account(class_name, &blk)
74
+ unless class_name.is_a?(Symbol) || class_name.is_a?(String)
75
+ raise ArgumentError
76
+ end
77
+
78
+ c = Class.new(Account, &blk)
79
+ self.const_set(class_name.to_sym, c)
80
+ Account.sti_model_map[c.uuid] = c
81
+ Account.sti_key_map[c.to_s] = c.uuid
82
+ c
83
+ end
84
+ end
85
+
86
+ install_data_hooks do
87
+ Account.subclasses.each { |m|
88
+ Account.create(m.default_values.dup)
89
+ }
90
+ end
91
+
92
+ SystemAccount.define_account(:DatacenterAccount) do
93
+ pk 100
94
+ uuid '00000000'
95
+ description 'datacenter system account'
96
+
97
+ # DatacenterAccount never be disabled
98
+ def before_save
99
+ super
100
+ self.enabled = Account::ENABLED
101
+ end
102
+ end
103
+
104
+ end
@@ -0,0 +1,102 @@
1
+ # -*- coding: utf-8 -*-
2
+ class Authz < BaseNew
3
+ with_timestamps
4
+
5
+ inheritable_schema do
6
+ primary_key :id, :type=>Integer
7
+ Fixnum :user_id, :null => false
8
+ Fixnum :account_id, :null => false
9
+ Fixnum :type_id, :null => false
10
+ end
11
+
12
+ class << self
13
+ def get_my_authz(user_id,account_id)
14
+ self.dataset.where(:user_id => user_id,:account_id => account_id).all
15
+ end
16
+ end
17
+ end
18
+
19
+ module Tags
20
+ class Authz < Tag
21
+ def self.define_authz(klass_name, &blk)
22
+ c = Class.new(Authz, &blk)
23
+ @authz_collections ||= {}
24
+ @authz_collections.store(c.type_id,klass_name)
25
+ self.const_set(klass_name.to_sym, c)
26
+ end
27
+
28
+ def self.authz_collections
29
+ @authz_collections
30
+ end
31
+
32
+ def accept_mapping?(taggable_obj)
33
+ taggable_obj.is_a?(Models::User) && !taggable_obj.accounts_dataset.filter(:account_id=>self.account_id).empty?
34
+ end
35
+
36
+ def self.authz_evaluate?(authz_class)
37
+ unless authz_class < Authz
38
+ raise ArgumentError, "Can't compare the class does not have the root with #{self}"
39
+ end
40
+ authz_class == self
41
+ end
42
+
43
+ def after_initialize
44
+ super
45
+ # Set default name for the :name column.
46
+ self[:name] = self.class.to_s.sub(/^Tags::/, '')
47
+ end
48
+ end
49
+
50
+ Authz.define_authz(:ModifyAccount) do
51
+ type_id 90001
52
+ description ' modify priviledg'
53
+
54
+ def self.authz_evaluate?(authz_class)
55
+ super || [Authz::CreateAccount,Authz::DeleteAccount].any?{|klass| klass == authz_class}
56
+ end
57
+ end
58
+
59
+ Authz.define_authz(:CreateAccount) do
60
+ type_id 90002
61
+ description ''
62
+ end
63
+
64
+ Authz.define_authz(:DeleteAccount) do
65
+ type_id 90003
66
+ description ''
67
+ end
68
+
69
+ Authz.define_authz(:AccountAdministrator) do
70
+ type_id 90004
71
+ description ''
72
+ end
73
+
74
+ Authz.define_authz(:ModifyUser) do
75
+ type_id 90005
76
+ description ''
77
+ end
78
+
79
+ Authz.define_authz(:ViewUser) do
80
+ type_id 90006
81
+ description ''
82
+ end
83
+
84
+ Authz.define_authz(:UserAdministrator) do
85
+ type_id 90007
86
+ description ''
87
+ end
88
+
89
+ Authz.define_authz(:AdminInstance) do
90
+ type_id 90008
91
+ description ''
92
+
93
+ def self.authz_evaluate?(authz_class)
94
+ super || [Authz::RunInstance].any?{|klass| klass == authz_class}
95
+ end
96
+ end
97
+
98
+ Authz.define_authz(:RunInstance) do
99
+ type_id 90009
100
+ description ''
101
+ end
102
+ end
@@ -0,0 +1,311 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'sequel/model'
3
+
4
+ # Sequal::Model plugin to inject the Taggable feature to the model
5
+ # class.
6
+ #
7
+ # Taggable model supports the features below:
8
+ # - Taggable.uuid_prefix to both set and get uuid_prefix for the model.
9
+ # - Collision detection for specified uuid_prefix.
10
+ # - Generate unique value for :uuid column at initialization.
11
+ # - Add column :uuid if the model is capable of :schema plugin methods.
12
+ module Taggable
13
+ UUID_TABLE='abcdefghijklmnopqrstuvwxyz0123456789'.split('').freeze
14
+ UUID_REGEX=%r/^(\w+)-([#{UUID_TABLE.join}]+)/
15
+
16
+ class UUIDPrefixDuplication < StandardError; end
17
+
18
+ def self.uuid_prefix_collection
19
+ @uuid_prefix_collection ||= {}
20
+ end
21
+
22
+ # Find a taggable model object from the
23
+ # given canonical uuid.
24
+ #
25
+ # # Find an account.
26
+ # Taggble.find('a-xxxxxxxx')
27
+ #
28
+ # # Find a user.
29
+ # Taggble.find('u-xxxxxxxx')
30
+ def self.find(uuid)
31
+ raise ArgumentError, "Invalid uuid syntax: #{uuid}" unless uuid =~ UUID_REGEX
32
+ upc = uuid_prefix_collection[$1.downcase]
33
+ raise "Unknown uuid prefix: #{$1.downcase}" if upc.nil?
34
+ upc[:class].find(:uuid=>$2)
35
+ end
36
+
37
+ # Checks if the uuid object stored in the database.
38
+ def self.exists?(uuid)
39
+ !find(uuid).nil?
40
+ end
41
+
42
+ def self.configure(model)
43
+ model.schema_builders << proc {
44
+ unless has_column?(:uuid)
45
+ # add :uuid column with unique index constraint.
46
+ column(:uuid, String, :size=>8, :null=>false, :fixed=>true, :unique=>true)
47
+ end
48
+ }
49
+ end
50
+
51
+ module InstanceMethods
52
+ # read-only instance method to retrieve @uuid_prefix class
53
+ # variable.
54
+ def uuid_prefix
55
+ self.class.uuid_prefix
56
+ end
57
+
58
+ def after_initialize
59
+ super
60
+ # set random generated uuid value
61
+ self[:uuid] ||= Array.new(8) do UUID_TABLE[rand(UUID_TABLE.size)]; end.join
62
+ end
63
+
64
+ # Returns canonicalized uuid which has the form of
65
+ # "{uuid_prefix}-{uuid}".
66
+ def canonical_uuid
67
+ "#{self.uuid_prefix}-#{self[:uuid]}"
68
+ end
69
+ alias_method :cuuid, :canonical_uuid
70
+
71
+ # Put the tag on the object.
72
+ #
73
+ # This method just delegates the method call of Tag#label().
74
+ # @params [Models::Tag,String] tag_or_tag_uuid 'tag-xxxx' is expected when the type is string
75
+ def label_tag(tag_or_tag_uuid)
76
+ tag = case tag_or_tag_uuid
77
+ when String
78
+ Tag[tag_or_tag_uuid]
79
+ when Tag
80
+ tag_or_tag_uuid
81
+ else
82
+ raise ArgumentError, "Invalid type: #{tag_or_tag_uuid.class}"
83
+ end
84
+
85
+ tag.label(self.uuid)
86
+ end
87
+
88
+ # Remove the labeled tag from the object
89
+ #
90
+ # This method just delegates the method call of Tag#unlabel().
91
+ # @params [Models::Tag,String] tag_or_tag_uuid 'tag-xxxx' is expected when the type is string
92
+ def unlabel_tag(tag_or_tag_uuid)
93
+ tag = case tag_or_tag_uuid
94
+ when String
95
+ Tag[tag_or_tag_uuid]
96
+ when Tag
97
+ tag_or_tag_uuid
98
+ else
99
+ raise ArgumentError, "Invalid type: #{tag_or_tag_uuid.class}"
100
+ end
101
+
102
+ tag.unlabel(self.uuid)
103
+ end
104
+
105
+ end
106
+
107
+ module ClassMethods
108
+ # Getter and setter for uuid_prefix of the class.
109
+ #
110
+ # @example
111
+ # class Model1 < Sequel::Model
112
+ # plugin Taggable
113
+ # uuid_prefix('m')
114
+ # end
115
+ #
116
+ # Model1.uuid_prefix # == 'm'
117
+ # Model1.new.canonical_uuid # == 'm-abcd1234'
118
+ def uuid_prefix(prefix=nil)
119
+ if prefix
120
+ raise UUIDPrefixDuplication, "Found collision for uuid_prefix key: #{prefix}" if Taggable.uuid_prefix_collection.has_key?(prefix)
121
+
122
+ Taggable.uuid_prefix_collection[prefix]={:class=>self}
123
+ @uuid_prefix = prefix
124
+ end
125
+ @uuid_prefix || raise("uuid prefix is unset for:#{self}")
126
+ end
127
+
128
+
129
+ # Override Model.[] to add lookup by uuid.
130
+ #
131
+ # @example
132
+ # Account['a-xxxxxx']
133
+ def [](*args)
134
+ if args.size == 1 and args[0].is_a? String
135
+ super(:uuid=>trim_uuid(args[0]))
136
+ else
137
+ super(*args)
138
+ end
139
+ end
140
+
141
+ # Returns the uuid string which is removed prefix part: /^(:?\w+)-/.
142
+ #
143
+ # @example
144
+ # Account.trim_uuid('a-abcd1234') # = 'abcd1234'
145
+ # @example Will get InvalidUUIDError as the uuid with invalid prefix has been tried.
146
+ # Account.trim_uuid('u-abcd1234') # 'u-' prefix is for User model.
147
+ def trim_uuid(p_uuid)
148
+ regex = %r/^#{self.uuid_prefix}-/
149
+ if p_uuid and p_uuid =~ regex
150
+ return p_uuid.sub(regex, '')
151
+ end
152
+ raise InvalidUUIDError, "Invalid uuid or unsupported uuid: #{p_uuid} in #{self}"
153
+ end
154
+
155
+ # Checks the uuid syntax if it is for the Taggable class.
156
+ def check_uuid_format(uuid)
157
+ uuid =~ /^#{self.uuid_prefix}-/
158
+ end
159
+ end
160
+
161
+ end
162
+
163
+ # Sequel::Model plugin extends :schema plugin to merge the column
164
+ # definitions in its parent class.
165
+ #
166
+ # class Model1 < Sequel::Model
167
+ # plugin InheritableSchema
168
+ #
169
+ # inheritable_schema do
170
+ # String :col1
171
+ # end
172
+ # end
173
+ #
174
+ # class Model2 < Model1
175
+ # inheritable_schema do
176
+ # String :col2
177
+ # end
178
+ # end
179
+ #
180
+ # Model2.create_table!
181
+ #
182
+ # Then the schema for Model2 becomes as follows:
183
+ # primary_key :id, :type=>Integer, :unsigned=>true
184
+ # String :col1
185
+ # String :col2
186
+ module InheritableSchema
187
+ def self.apply(model)
188
+ model.plugin :schema
189
+ end
190
+
191
+ module ClassMethods
192
+ def schema_builders
193
+ @schema_builders ||= []
194
+ end
195
+
196
+ def inheritable_schema(&blk)
197
+ if blk
198
+ self.schema_builders << blk
199
+
200
+ if @schema.nil?
201
+ # set_dataset(:tablename) in set_schema() force to overwrite
202
+ # dataset.row_proc to the standard one even if the
203
+ # dataset.row_proc is set something another. This becomes problem when
204
+ # another plugin needed to set its own row_proc.
205
+
206
+ # This is workaround to prevent from above.
207
+ row_proc = dataset.row_proc
208
+ set_schema(implicit_table_name) do
209
+ primary_key :id, :type=>Integer, :unsigned=>true
210
+ end
211
+ dataset.row_proc = row_proc if row_proc.is_a?(Proc)
212
+ end
213
+
214
+ builders = []
215
+ c = self
216
+ begin
217
+ builders << c.schema_builders if c.respond_to?(:schema_builders)
218
+ end while (c = c.superclass) < InheritableSchema
219
+
220
+ builders = builders.reverse.flatten
221
+ builders.delete(nil)
222
+ builders.each { |blk|
223
+ @schema.instance_eval(&blk)
224
+ }
225
+ end
226
+ end
227
+ end
228
+
229
+ end
230
+
231
+ class BaseNew < Sequel::Model
232
+
233
+ def self.Proxy(klass)
234
+ colnames = klass.schema.columns.map {|i| i[:name] }
235
+ colnames.delete_if(klass.primary_key) if klass.restrict_primary_key?
236
+ s = ::Struct.new(*colnames) do
237
+ def to_hash
238
+ n = {}
239
+ self.each_pair { |k,v|
240
+ n[k.to_sym]=v
241
+ }
242
+ n
243
+ end
244
+ end
245
+ s
246
+ end
247
+
248
+
249
+ # Callback when the initial data is setup to the database.
250
+ def self.install_data
251
+ install_data_hooks.each{|h| h.call }
252
+ end
253
+
254
+ # Add callbacks to setup the initial data. The hooks will be
255
+ # called when Model1.install_data() is called.
256
+ #
257
+ # class Model1 < Base
258
+ # install_data_hooks do
259
+ # Model1.create({:col1=>1, :col2=>2})
260
+ # end
261
+ # end
262
+ def self.install_data_hooks(&blk)
263
+ @install_data_hooks ||= []
264
+ if blk
265
+ @install_data_hooks << blk
266
+ end
267
+ @install_data_hooks
268
+ end
269
+
270
+
271
+ private
272
+ def self.inherited(klass)
273
+ super
274
+
275
+ klass.plugin InheritableSchema
276
+ klass.class_eval {
277
+
278
+ # Add timestamp columns and set callbacks using Timestamps
279
+ # plugin.
280
+ #
281
+ # class Model1 < Base
282
+ # with_timestamps
283
+ # end
284
+ def self.with_timestamps
285
+ self.schema_builders << proc {
286
+ unless has_column?(:created_at)
287
+ column(:created_at, Time, :null=>false)
288
+ end
289
+ unless has_column?(:updated_at)
290
+ column(:updated_at, Time, :null=>false)
291
+ end
292
+ }
293
+
294
+ self.plugin :timestamps, :update_on_create=>true
295
+ end
296
+
297
+ # Install Taggable module as Sequel plugin and set uuid_prefix.
298
+ #
299
+ # class Model1 < Base
300
+ # taggable 'm'
301
+ # end
302
+ def self.taggable(uuid_prefix)
303
+ return if self == BaseNew
304
+ self.plugin Taggable
305
+ self.uuid_prefix(uuid_prefix)
306
+ end
307
+
308
+ }
309
+
310
+ end
311
+ end