dtk-shell 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +5 -0
  3. data/Gemfile_dev +13 -0
  4. data/README.md +121 -0
  5. data/bin/dtk-execute +32 -0
  6. data/bin/dtk-run +92 -0
  7. data/bin/dtk-shell +31 -0
  8. data/dtk-shell.gemspec +50 -0
  9. data/lib/auxiliary.rb +61 -0
  10. data/lib/bundler_monkey_patch.rb +26 -0
  11. data/lib/client.rb +58 -0
  12. data/lib/command_helper.rb +33 -0
  13. data/lib/command_helpers/git_repo.rb +589 -0
  14. data/lib/command_helpers/git_repo/merge.rb +153 -0
  15. data/lib/command_helpers/jenkins_client.rb +106 -0
  16. data/lib/command_helpers/jenkins_client/config_xml.rb +288 -0
  17. data/lib/command_helpers/service_importer.rb +251 -0
  18. data/lib/command_helpers/service_link.rb +33 -0
  19. data/lib/command_helpers/test_module_creator.rb +69 -0
  20. data/lib/command_helpers/test_module_templates/dtk.model.yaml.eruby +10 -0
  21. data/lib/command_helpers/test_module_templates/spec_helper.rb.eruby +10 -0
  22. data/lib/command_helpers/test_module_templates/temp_component_spec.rb.eruby +5 -0
  23. data/lib/commands.rb +57 -0
  24. data/lib/commands/common/thor/access_control.rb +133 -0
  25. data/lib/commands/common/thor/action_result_handler.rb +74 -0
  26. data/lib/commands/common/thor/assembly_template.rb +92 -0
  27. data/lib/commands/common/thor/assembly_workspace.rb +1801 -0
  28. data/lib/commands/common/thor/base_command_helper.rb +59 -0
  29. data/lib/commands/common/thor/clone.rb +82 -0
  30. data/lib/commands/common/thor/common.rb +88 -0
  31. data/lib/commands/common/thor/common_base.rb +49 -0
  32. data/lib/commands/common/thor/create_target.rb +70 -0
  33. data/lib/commands/common/thor/edit.rb +255 -0
  34. data/lib/commands/common/thor/inventory_parser.rb +98 -0
  35. data/lib/commands/common/thor/list_diffs.rb +128 -0
  36. data/lib/commands/common/thor/module.rb +1011 -0
  37. data/lib/commands/common/thor/module/import.rb +210 -0
  38. data/lib/commands/common/thor/node.rb +53 -0
  39. data/lib/commands/common/thor/poller.rb +65 -0
  40. data/lib/commands/common/thor/pull_clone_changes.rb +28 -0
  41. data/lib/commands/common/thor/pull_from_remote.rb +152 -0
  42. data/lib/commands/common/thor/puppet_forge.rb +72 -0
  43. data/lib/commands/common/thor/purge_clone.rb +101 -0
  44. data/lib/commands/common/thor/push_clone_changes.rb +162 -0
  45. data/lib/commands/common/thor/push_to_remote.rb +94 -0
  46. data/lib/commands/common/thor/remotes.rb +71 -0
  47. data/lib/commands/common/thor/reparse.rb +40 -0
  48. data/lib/commands/common/thor/set_required_attributes.rb +46 -0
  49. data/lib/commands/thor/account.rb +239 -0
  50. data/lib/commands/thor/assembly.rb +356 -0
  51. data/lib/commands/thor/attribute.rb +79 -0
  52. data/lib/commands/thor/component.rb +70 -0
  53. data/lib/commands/thor/component_module.rb +501 -0
  54. data/lib/commands/thor/component_template.rb +174 -0
  55. data/lib/commands/thor/dependency.rb +34 -0
  56. data/lib/commands/thor/developer.rb +144 -0
  57. data/lib/commands/thor/dtk.rb +152 -0
  58. data/lib/commands/thor/library.rb +125 -0
  59. data/lib/commands/thor/node.rb +504 -0
  60. data/lib/commands/thor/node_template.rb +94 -0
  61. data/lib/commands/thor/project.rb +34 -0
  62. data/lib/commands/thor/provider.rb +233 -0
  63. data/lib/commands/thor/remotes.rb +49 -0
  64. data/lib/commands/thor/service.rb +941 -0
  65. data/lib/commands/thor/service_module.rb +914 -0
  66. data/lib/commands/thor/state_change.rb +25 -0
  67. data/lib/commands/thor/target.rb +250 -0
  68. data/lib/commands/thor/task.rb +116 -0
  69. data/lib/commands/thor/test_module.rb +310 -0
  70. data/lib/commands/thor/utils.rb +21 -0
  71. data/lib/commands/thor/workspace.rb +685 -0
  72. data/lib/config/cacert.pem +3785 -0
  73. data/lib/config/client.conf.header +20 -0
  74. data/lib/config/configuration.rb +99 -0
  75. data/lib/config/default.conf +16 -0
  76. data/lib/config/disk_cacher.rb +80 -0
  77. data/lib/configurator.rb +176 -0
  78. data/lib/context_router.rb +44 -0
  79. data/lib/core.rb +497 -0
  80. data/lib/domain/git_adapter.rb +412 -0
  81. data/lib/domain/git_error_handler.rb +64 -0
  82. data/lib/domain/response.rb +285 -0
  83. data/lib/domain/response/error_handler.rb +86 -0
  84. data/lib/dtk-shell/version.rb +20 -0
  85. data/lib/dtk_constants.rb +40 -0
  86. data/lib/dtk_error.rb +114 -0
  87. data/lib/dtk_logger.rb +126 -0
  88. data/lib/dtk_shell.rb +31 -0
  89. data/lib/error.rb +85 -0
  90. data/lib/execute.rb +29 -0
  91. data/lib/execute/cli_pure/cli_rerouter.rb +102 -0
  92. data/lib/execute/command.rb +40 -0
  93. data/lib/execute/command/api_call.rb +60 -0
  94. data/lib/execute/command/api_call/map.rb +60 -0
  95. data/lib/execute/command/api_call/service.rb +91 -0
  96. data/lib/execute/command/api_call/translation_term.rb +119 -0
  97. data/lib/execute/command/rest_call.rb +37 -0
  98. data/lib/execute/command_processor.rb +30 -0
  99. data/lib/execute/command_processor/rest_call.rb +59 -0
  100. data/lib/execute/error_usage.rb +21 -0
  101. data/lib/execute/execute_context.rb +86 -0
  102. data/lib/execute/execute_context/result_store.rb +37 -0
  103. data/lib/execute/script.rb +64 -0
  104. data/lib/execute/script/add_tenant.rb +121 -0
  105. data/lib/git-logs/git.log +0 -0
  106. data/lib/parser/adapters/option_parser.rb +70 -0
  107. data/lib/parser/adapters/thor.rb +555 -0
  108. data/lib/parser/adapters/thor/common_option_defs.rb +40 -0
  109. data/lib/require_first.rb +104 -0
  110. data/lib/search_hash.rb +44 -0
  111. data/lib/shell.rb +261 -0
  112. data/lib/shell/context.rb +1065 -0
  113. data/lib/shell/context_aux.rb +46 -0
  114. data/lib/shell/domain/active_context.rb +186 -0
  115. data/lib/shell/domain/context_entity.rb +89 -0
  116. data/lib/shell/domain/context_params.rb +223 -0
  117. data/lib/shell/domain/override_tasks.rb +88 -0
  118. data/lib/shell/domain/shadow_entity.rb +76 -0
  119. data/lib/shell/header_shell.rb +44 -0
  120. data/lib/shell/help_monkey_patch.rb +283 -0
  121. data/lib/shell/interactive_wizard.rb +225 -0
  122. data/lib/shell/message_queue.rb +63 -0
  123. data/lib/shell/parse_monkey_patch.rb +39 -0
  124. data/lib/shell/status_monitor.rb +124 -0
  125. data/lib/task_status.rb +83 -0
  126. data/lib/task_status/refresh_mode.rb +77 -0
  127. data/lib/task_status/snapshot_mode.rb +28 -0
  128. data/lib/task_status/stream_mode.rb +48 -0
  129. data/lib/task_status/stream_mode/element.rb +101 -0
  130. data/lib/task_status/stream_mode/element/format.rb +101 -0
  131. data/lib/task_status/stream_mode/element/hierarchical_task.rb +100 -0
  132. data/lib/task_status/stream_mode/element/hierarchical_task/result.rb +72 -0
  133. data/lib/task_status/stream_mode/element/hierarchical_task/result/action.rb +93 -0
  134. data/lib/task_status/stream_mode/element/hierarchical_task/result/components.rb +26 -0
  135. data/lib/task_status/stream_mode/element/hierarchical_task/result/node_level.rb +26 -0
  136. data/lib/task_status/stream_mode/element/hierarchical_task/steps.rb +34 -0
  137. data/lib/task_status/stream_mode/element/hierarchical_task/steps/action.rb +53 -0
  138. data/lib/task_status/stream_mode/element/hierarchical_task/steps/components.rb +53 -0
  139. data/lib/task_status/stream_mode/element/hierarchical_task/steps/node_level.rb +42 -0
  140. data/lib/task_status/stream_mode/element/no_results.rb +26 -0
  141. data/lib/task_status/stream_mode/element/render.rb +59 -0
  142. data/lib/task_status/stream_mode/element/stage.rb +84 -0
  143. data/lib/task_status/stream_mode/element/stage/render.rb +76 -0
  144. data/lib/task_status/stream_mode/element/task_end.rb +35 -0
  145. data/lib/task_status/stream_mode/element/task_start.rb +37 -0
  146. data/lib/util/common_util.rb +37 -0
  147. data/lib/util/console.rb +235 -0
  148. data/lib/util/dtk_puppet.rb +65 -0
  149. data/lib/util/module_util.rb +66 -0
  150. data/lib/util/os_util.rb +385 -0
  151. data/lib/util/permission_util.rb +31 -0
  152. data/lib/util/remote_dependency_util.rb +84 -0
  153. data/lib/util/ssh_util.rb +94 -0
  154. data/lib/view_processor.rb +129 -0
  155. data/lib/view_processor/augmented_simple_list.rb +44 -0
  156. data/lib/view_processor/hash_pretty_print.rb +123 -0
  157. data/lib/view_processor/simple_list.rb +156 -0
  158. data/lib/view_processor/table_print.rb +309 -0
  159. data/lib/violation.rb +86 -0
  160. data/lib/violation/attribute.rb +76 -0
  161. data/lib/violation/fix.rb +26 -0
  162. data/lib/violation/fix/result.rb +73 -0
  163. data/lib/violation/fix/result/error.rb +34 -0
  164. data/lib/violation/fix/set_attribute.rb +41 -0
  165. data/lib/violation/sub_classes.rb +60 -0
  166. data/puppet/manifests/init.pp +72 -0
  167. data/puppet/manifests/params.pp +16 -0
  168. data/puppet/r8meta.puppet.yml +35 -0
  169. data/puppet/templates/bash_profile.erb +2 -0
  170. data/puppet/templates/client.conf.erb +1 -0
  171. data/puppet/templates/dtkclient.erb +2 -0
  172. data/spec/component_module_spec.rb +34 -0
  173. data/spec/dependency_spec.rb +6 -0
  174. data/spec/dtk_shell_spec.rb +13 -0
  175. data/spec/dtk_spec.rb +33 -0
  176. data/spec/lib/spec_helper.rb +10 -0
  177. data/spec/lib/spec_thor.rb +108 -0
  178. data/spec/node_template_spec.rb +24 -0
  179. data/spec/project_spec.rb +6 -0
  180. data/spec/repo_spec.rb +7 -0
  181. data/spec/response_spec.rb +52 -0
  182. data/spec/service_module_spec.rb +38 -0
  183. data/spec/service_spec.rb +50 -0
  184. data/spec/state_change_spec.rb +7 -0
  185. data/spec/table_print_spec.rb +48 -0
  186. data/spec/target_spec.rb +57 -0
  187. data/spec/task_spec.rb +28 -0
  188. data/views/assembly/augmented_simple_list.rb +12 -0
  189. data/views/assembly_template/augmented_simple_list.rb +12 -0
  190. data/views/list_task/augmented_simple_list.rb +12 -0
  191. metadata +421 -0
@@ -0,0 +1,60 @@
1
+ #
2
+ # Copyright (C) 2010-2016 dtk contributors
3
+ #
4
+ # This file is part of the dtk project.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ class DTK::Client::Execute
19
+ class Command::APICall
20
+ class Map < Hash
21
+ def initialize(hash={})
22
+ super()
23
+ replace(hash)
24
+ end
25
+
26
+ def translate(api_params={},opts={})
27
+ if Rest::Post.matches?(type())
28
+ Command::RestCall::Post.new(:path => path(),:body => translate_to_rest_body(api_params,opts))
29
+ else
30
+ raise "Type in following map is not defined not defined: #{self.inspect}"
31
+ end
32
+ end
33
+
34
+ private
35
+ def translate_to_rest_body(api_params,opts)
36
+ body().inject(Hash.new) do |h,(k,v)|
37
+ # if TranslationTerm.matches is false then v is a constant
38
+ processed_v =
39
+ if TranslationTerm.matches?(v)
40
+ v.instance_form().translate(k,api_params,opts)
41
+ else
42
+ v
43
+ end
44
+ h.merge(k => processed_v)
45
+ end
46
+ end
47
+
48
+ def type()
49
+ self[:type]
50
+ end
51
+ def path()
52
+ self[:path]
53
+ end
54
+ def body()
55
+ self[:body]
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,91 @@
1
+ #
2
+ # Copyright (C) 2010-2016 dtk contributors
3
+ #
4
+ # This file is part of the dtk project.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ class DTK::Client::Execute
19
+ class Command::APICall
20
+ class Service < self
21
+ def self.object_type()
22
+ :service
23
+ end
24
+
25
+ CommandMap = {
26
+ :add_component => Map.new(
27
+ :type => Rest::Post,
28
+ :path => 'assembly/add_component',
29
+ :body => {
30
+ :assembly_id => Required(:service),
31
+ :subtype => 'instance',
32
+ :node_id => Required(:node),
33
+ :component_template_id => Required(:component),
34
+ :namespace => Required(:namespace),
35
+ :idempotent => Equal::OrDefault(true),
36
+ :donot_update_workflow => Equal::OrDefault(false)
37
+ }
38
+ ),
39
+
40
+ :set_attribute => Map.new(
41
+ :type => Rest::Post,
42
+ :path => 'assembly/set_attributes',
43
+ :body => {
44
+ :assembly_id => Required(:service),
45
+ :pattern => Required(:attribute_path),
46
+ # TODO: support FN
47
+ # :pattern => FN{"#{Required(:node)}/#{Required(:component)}/#{Required(:attribute)}"
48
+ :value => Required::Equal
49
+ }
50
+ ),
51
+
52
+ :link_components => Map.new(
53
+ :type => Rest::Post,
54
+ :path => 'assembly/add_service_link',
55
+ :body => {
56
+ :assembly_id => Required(:service),
57
+ :input_component_id => Required(:input_component),
58
+ :output_component_id => Required(:output_component),
59
+ }
60
+ ),
61
+
62
+ :execute_workflow =>
63
+ [
64
+ Map.new(
65
+ :type => Rest::Post,
66
+ :path => 'assembly/create_task',
67
+ :body => {
68
+ :assembly_id => Required(:service),
69
+ :task_action => Required(:workflow_name),
70
+ :task_params => Required(:workflow_params)
71
+ }
72
+ ),
73
+ Map.new(
74
+ :type => Rest::Post,
75
+ :path => 'task/execute',
76
+ :body => {
77
+ :task_id => PreviousResponse(:task_id)
78
+ }
79
+ )]
80
+ }
81
+
82
+ module CustomMapping
83
+ # example would be
84
+ # def self.command(params)
85
+ # ...
86
+ # end
87
+ end
88
+
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,119 @@
1
+ #
2
+ # Copyright (C) 2010-2016 dtk contributors
3
+ #
4
+ # This file is part of the dtk project.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ class DTK::Client::Execute
19
+ class Command::APICall
20
+
21
+ # abstract classes
22
+ class TranslationTerm
23
+ def self.matches?(obj)
24
+ obj.kind_of?(self) or
25
+ (obj.kind_of?(Class) and obj <= self)
26
+ end
27
+
28
+ def self.instance_form()
29
+ new()
30
+ end
31
+ def instance_form()
32
+ self
33
+ end
34
+
35
+ class Operation < self
36
+ end
37
+
38
+ class Param < self
39
+ private
40
+ def index(api_params,key)
41
+ api_params[key]
42
+ end
43
+
44
+ def index_required(api_params,key)
45
+ unless has_key?(api_params,key)
46
+ raise ErrorUsage.new("Missing key '#{key}' in params: #{api_params.inspect}")
47
+ end
48
+ index(api_params,key)
49
+ end
50
+
51
+ def has_key?(api_params,key)
52
+ api_params.has_key?(key)
53
+ end
54
+
55
+ end
56
+ end
57
+
58
+ # concrete classes
59
+
60
+ class Rest < TranslationTerm::Operation
61
+ class Post < self
62
+ end
63
+ end
64
+
65
+ class Required < TranslationTerm::Param
66
+ def initialize(input_key=nil)
67
+ @input = input_key
68
+ end
69
+
70
+ def translate(key,api_params,opts={})
71
+ index_required(api_params,@input||key)
72
+ end
73
+
74
+ class Equal < self
75
+ end
76
+ end
77
+
78
+ class Equal < TranslationTerm::Param
79
+ def translate(key,api_params,opts={})
80
+ index(api_params,key)
81
+ end
82
+
83
+ class Required < self
84
+ def translate(key,api_params,opts={})
85
+ index_required(api_params,key)
86
+ end
87
+ end
88
+
89
+ def self.OrDefault(default_value)
90
+ OrDefault.new(default_value)
91
+ end
92
+
93
+ class OrDefault < self
94
+ def initialize(default_value)
95
+ @default_value = default_value
96
+ end
97
+ def translate(key,api_params,opts={})
98
+ has_key?(api_params,key) ? index(api_params,key) : @default_value
99
+ end
100
+ end
101
+ end
102
+
103
+ class PreviousResponse < TranslationTerm::Param
104
+ def initialize(response_key)
105
+ @response_key = response_key
106
+ end
107
+ def translate(key,api_params,opts={})
108
+ unless last_result = opts[:last_result]
109
+ raise ErrorUsage.new("PreviousResponse used before results computed")
110
+ end
111
+ match = nil
112
+ unless matching_key = [@response_key.to_s,@response_key.to_sym].find{|k|last_result.has_key?(k)}
113
+ raise ErrorUsage.new("Key #{@response_key} in PreviousResponse not found in last results (#{last_result.inspect})")
114
+ end
115
+ last_result[matching_key]
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,37 @@
1
+ #
2
+ # Copyright (C) 2010-2016 dtk contributors
3
+ #
4
+ # This file is part of the dtk project.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ class DTK::Client::Execute
19
+ class Command
20
+ class RestCall < self
21
+ # TODO: allow calls like 'converge' which is a macro; we either directly support or have a sub class of RestCall called macro
22
+ attr_reader :path
23
+ def initialize(hash)
24
+ super
25
+ @path = required(:path)
26
+ end
27
+
28
+ class Post < self
29
+ attr_reader :body
30
+ def initialize(hash)
31
+ super
32
+ @body = optional?(:body)||{}
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,30 @@
1
+ #
2
+ # Copyright (C) 2010-2016 dtk contributors
3
+ #
4
+ # This file is part of the dtk project.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ class DTK::Client::Execute
19
+ class CommandProcessor
20
+ dtk_require('command_processor/rest_call')
21
+
22
+ def self.execute(command)
23
+ if command.kind_of?(Command::RestCall)
24
+ RestCall.execute(command)
25
+ else
26
+ raise ErrorUsage.new("Unexpected Command type: #{command.class}")
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,59 @@
1
+ #
2
+ # Copyright (C) 2010-2016 dtk contributors
3
+ #
4
+ # This file is part of the dtk project.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ class DTK::Client::Execute; class CommandProcessor
19
+ class RestCall< self
20
+ extend ::DTK::Client::CommandBase # TODO: have a Base that is not just for commands (CommandBase)
21
+
22
+ def self.execute(rest_command)
23
+ response = raw_execute(rest_command)
24
+ if response.ok?
25
+ response.data
26
+ else
27
+ raise Error.new(response,rest_command)
28
+ end
29
+ end
30
+
31
+ private
32
+ def self.raw_execute(rest_command)
33
+ if rest_command.kind_of?(Command::RestCall::Post)
34
+ post(rest_url(rest_command.path),rest_command.body)
35
+ else
36
+ raise ErrorUsage.new("Unexpected Rest Command type: #{rest_command.class}")
37
+ end
38
+ end
39
+
40
+ class Error < ErrorUsage
41
+ def initialize(response_not_ok,rest_command)
42
+ error_print_form = error_print_form(response_not_ok)
43
+ error_msg = "Bad Rest response from call (#{rest_command.input_hash.inspect}:\n #{error_print_form}"
44
+ super(error_msg)
45
+ end
46
+ private
47
+ def error_print_form(response_not_ok)
48
+ ret_obj =
49
+ if errors = response_not_ok['errors']
50
+ errors.size == 1 ? errors.first : errors
51
+ else
52
+ response_not_ok
53
+ end
54
+ ret_obj.kind_of?(String) ? ret_obj : ret_obj.inspect
55
+ end
56
+ end
57
+
58
+ end
59
+ end; end
@@ -0,0 +1,21 @@
1
+ #
2
+ # Copyright (C) 2010-2016 dtk contributors
3
+ #
4
+ # This file is part of the dtk project.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ class DTK::Client::Execute
19
+ class ErrorUsage < ::DTK::Client::Error
20
+ end
21
+ end
@@ -0,0 +1,86 @@
1
+ #
2
+ # Copyright (C) 2010-2016 dtk contributors
3
+ #
4
+ # This file is part of the dtk project.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ class DTK::Client::Execute
19
+ class ExecuteContext
20
+ dtk_require('execute_context/result_store')
21
+
22
+ module ClassMixin
23
+ def ExecuteContext(opts={},&block)
24
+ ExecuteContext.new(opts).execute(&block)
25
+ end
26
+ end
27
+
28
+ def initialize(opts={})
29
+ @print_results = opts[:print_results]
30
+ @proxy = Proxy.new()
31
+ end
32
+
33
+ def execute(&block)
34
+ result, command = instance_eval(&block)
35
+ result
36
+ end
37
+
38
+ def method_missing(m, *args, &block)
39
+ result, command = @proxy.send(m, *args, &block)
40
+ if @print_results
41
+ pp(:command => command.input_hash(),:result => result)
42
+ end
43
+ [result, command]
44
+ end
45
+
46
+ # commands in the execute context
47
+ class Proxy
48
+ def initialize()
49
+ @result_store = ResultStore.new()
50
+ end
51
+
52
+ def call(object_type__method,params={})
53
+ object_type, method = split_object_type__method(object_type__method)
54
+ api_command = Command::APICall.new(:object_type => object_type, :method => method, :params => params)
55
+ result = nil
56
+ api_command.raw_executable_commands() do |raw_command|
57
+ last_result = @result_store.get_last_result?()
58
+ command = raw_command.translate(params,:last_result => last_result)
59
+ result = CommandProcessor.execute(command)
60
+ @result_store.store(result)
61
+ end
62
+ [result, api_command]
63
+ end
64
+
65
+ def post_rest_call(path,body={})
66
+ command = Command::RestCall::Post.new(:path => path, :body => body, :last_result => @last_result)
67
+ result = CommandProcessor.execute(command)
68
+ [result, command]
69
+ end
70
+
71
+ private
72
+ # returns [object_type, method]
73
+ def split_object_type__method(str)
74
+ DelimitersObjectTypeMethod.each do |d|
75
+ if str =~ Regexp.new("(^[^#{d}]+)#{d}([^#{d}]+$)")
76
+ return [$1,$2]
77
+ end
78
+ end
79
+ raise ErrorUsage.new("Illegal term '#{str}'")
80
+ end
81
+
82
+ DelimitersObjectTypeMethod = ['\/']
83
+ end
84
+
85
+ end
86
+ end