idcfcloud 0.1.4 → 0.2.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 (42) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +1 -0
  3. data/.gitignore +5 -3
  4. data/.rubocop.yml +16 -1
  5. data/README.md +61 -6
  6. data/exe/idcfcloud +9 -1
  7. data/idcfcloud.gemspec +9 -7
  8. data/lib/idcf/cli/conf/conf.yml +39 -5
  9. data/lib/idcf/cli/conf/const.rb +42 -17
  10. data/lib/idcf/cli/controller/base.rb +130 -60
  11. data/lib/idcf/cli/controller/extend/command.rb +18 -16
  12. data/lib/idcf/cli/controller/extend/init.rb +2 -0
  13. data/lib/idcf/cli/controller/extend/override.rb +1 -8
  14. data/lib/idcf/cli/controller/extend/search_module.rb +14 -0
  15. data/lib/idcf/cli/controller/ilb.rb +5 -34
  16. data/lib/idcf/cli/controller/include/command.rb +7 -80
  17. data/lib/idcf/cli/controller/include/init.rb +2 -4
  18. data/lib/idcf/cli/controller/include/util.rb +20 -63
  19. data/lib/idcf/cli/controller/include/validate.rb +7 -1
  20. data/lib/idcf/cli/controller/your.rb +0 -33
  21. data/lib/idcf/cli/error/cli_error.rb +7 -5
  22. data/lib/idcf/cli/extend/configure.rb +38 -33
  23. data/lib/idcf/cli/extend/update.rb +139 -54
  24. data/lib/idcf/cli/extend/update_file.rb +113 -18
  25. data/lib/idcf/cli/gem_ext/thor/init_util.rb +20 -7
  26. data/lib/idcf/cli/index.rb +21 -11
  27. data/lib/idcf/cli/lib/convert/formatter/base.rb +1 -1
  28. data/lib/idcf/cli/lib/convert/formatter/xml_format.rb +1 -1
  29. data/lib/idcf/cli/lib/convert/helper.rb +43 -7
  30. data/lib/idcf/cli/lib/util/ini_conf.rb +30 -8
  31. data/lib/idcf/cli/lib/util/yml_conf.rb +13 -2
  32. data/lib/idcf/cli/service/base.rb +121 -21
  33. data/lib/idcf/cli/service/ilb/add_server_for_protocol.rb +8 -3
  34. data/lib/idcf/cli/service/ilb/base_server_for_protocol.rb +21 -35
  35. data/lib/idcf/cli/service/ilb/delete_server_for_protocol.rb +9 -4
  36. data/lib/idcf/cli/validate/define/base.rb +3 -1
  37. data/lib/idcf/cli/validate/define/ilb/base.rb +0 -5
  38. data/lib/idcf/cli/version.rb +1 -1
  39. metadata +62 -23
  40. data/lib/idcf/cli/conf/service/.gitkeep +0 -0
  41. data/lib/idcf/cli/controller/include/override.rb +0 -30
  42. data/lib/idcf/cli/controller/include/sdk.rb +0 -94
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b9f986487dfb3ff47acc56240bc460299e52d033
4
- data.tar.gz: 348870564693ad1dd4d18010248ac03fc7d5397f
3
+ metadata.gz: 4a3321422f5844f5854dde7ff528e58f4ffda585
4
+ data.tar.gz: 7061912c1a39b1f141673fbc7b56b0e81f9b7d84
5
5
  SHA512:
6
- metadata.gz: 1be1810fe84896c0db62636764e848e361e6457563098882da75fcbf2a978735a100e36191d0c8b1e96dd4d3047aab6760759a4169b1241f2095fc9939b3083f
7
- data.tar.gz: 60709df4f07f000059b18001fece33b075358d3ae2d57fd869d58196df37927a88f1140074db074c2c6b7dc2b55a54455a31914f04d4974a2ca8d35d940368fa
6
+ metadata.gz: a14919f258cd60e092fc8c1ef466301b505032f41078e1ce76864a19f392c33d569f14297d860d5f5501fb8297d1d0b54190f33b9638ddcc656925e7846b1e38
7
+ data.tar.gz: 644f7237434d2a3e0ff7d3e89c70a30695c13e2480ee4624a1bd9cc6dd04987e299df7a0523edc168ced200e2e651a91b2b09a0a50bf631946663f82aa933e2c
data/.codeclimate.yml CHANGED
@@ -7,6 +7,7 @@ engines:
7
7
  - ruby
8
8
  exclude_paths:
9
9
  - 'test/**/*'
10
+ - 'lib/idcf/cli/validate/define/**/*'
10
11
  fixme:
11
12
  enabled: true
12
13
  rubocop:
data/.gitignore CHANGED
@@ -12,6 +12,7 @@
12
12
  /output/
13
13
  /config/config.ini
14
14
  /lib/idcf/cli/conf/service/*.yml
15
+ /lib/idcf/cli/conf/service/*.json
15
16
 
16
17
  .idea/*
17
18
  *.iml
@@ -38,7 +39,7 @@ build-iPhoneSimulator/
38
39
  ## Documentation cache and generated files:
39
40
  /.yardoc/
40
41
  /_yardoc/
41
- /doc/
42
+ /doc/*
42
43
  /rdoc/
43
44
 
44
45
  ## Environment normalization:
@@ -48,9 +49,10 @@ build-iPhoneSimulator/
48
49
 
49
50
  # for a library or gem, you might want to ignore these files since the code is
50
51
  # intended to run in multiple environments; otherwise, check them in:
51
- # Gemfile.lock
52
- # .ruby-version
52
+ Gemfile.lock
53
+ .ruby-version
53
54
  # .ruby-gemset
55
+ .DS_Store
54
56
 
55
57
  # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
56
58
  .rvmrc
data/.rubocop.yml CHANGED
@@ -8,6 +8,7 @@ Metrics/LineLength:
8
8
  Exclude:
9
9
  - 'test/**/*'
10
10
  Metrics/MethodLength:
11
+ Max: 14
11
12
  Exclude:
12
13
  - 'test/**/*'
13
14
  Metrics/ClassLength:
@@ -23,4 +24,18 @@ Metrics/AbcSize:
23
24
  Exclude:
24
25
  - 'test/**/*'
25
26
  LineLength:
26
- Max: 100
27
+ Max: 100
28
+ Exclude:
29
+ - 'test/**/*'
30
+ Style/ClassLength:
31
+ Max: 130
32
+ Exclude:
33
+ - 'test/**/*'
34
+ Style/ModuleLength:
35
+ Max: 150
36
+ Exclude:
37
+ - 'test/**/*'
38
+ ParameterLists:
39
+ Max: 6
40
+ Style/DoubleNegation:
41
+ Enabled: false
data/README.md CHANGED
@@ -35,20 +35,56 @@ $ idcfcloud <serviceName> <command> [attributes] [option]
35
35
 
36
36
  If you want to set more than one attribute, use the json form.
37
37
 
38
- ```ex)
38
+ ``` ex)
39
39
  '{"ipaddress": "0.0.0.0", "port": 80}'
40
40
  ```
41
41
 
42
+ Or you can also define the GET or POST parameter in the JSON file and hand it using xargs.
43
+
44
+ ``` ex) xargs
45
+ cat [attributes file path] | echo -e "'"$(cat)"'" | xargs -L1 idcfcloud <serviceName> <command> [option]
46
+ ```
47
+
48
+ ### Service Options
49
+
50
+ | option | alias | default | note |
51
+ |:---|:---|:---|:---|
52
+ | output | o | json | Output formats (table/json/xml/csv) |
53
+ | profile | | default | Switching profiles |
54
+ | api-key | | [Configuration file description] | API Key |
55
+ | secret-key | | [Configuration file description] | Secret Key |
56
+ | no-ssl | | | Not using SSL |
57
+ | no-vssl | | | Not using Verify SSL |
58
+ | json-path | | | Narrowing the data part of a return value using json-path<br/>https://github.com/joshbuddy/jsonpath |
59
+ | fields | | | Limiting the return value to be displayed (applied only to the latest hash)<br/>Punctuation: comma (",") |
60
+
61
+
62
+ #### How to Narrow Down (Examples)
63
+ ```
64
+ idcfcloud your list_billing_history --json-path '$.data[?(@.month=="2017-10")]' --fields month,billing_amount
65
+ ```
66
+
42
67
  ## ServiceName
68
+ [Compute](#compute)
69
+
43
70
  [ILB](#ilb)
44
71
 
72
+ [CDN](#cdn)
73
+
74
+ [DNS](#dns)
75
+
45
76
  [Your](#your)
46
77
 
47
- #### <a name="ilb"></a>ILB
78
+ ### Compute(Bata)<a name="compute"></a>
79
+ \* If you encounter any problem, please contact us or file a pull request.
80
+
81
+ http://docs.idcf.jp/cloud/api/
82
+
83
+ ### ILB<a name="ilb"></a>
48
84
 
49
85
  https://github.com/idcf/idcf-ilb-ruby
50
86
 
51
- ##### ILB Extend Commands
87
+ #### ILB Extend Commands
52
88
 
53
89
  add server
54
90
 
@@ -67,17 +103,36 @@ delete server
67
103
  $ idcfcloud ilb delete_server_for_protocol <lb_id> <protocol> <protocol_port> <data> [option]
68
104
  ```
69
105
 
70
- #### <a name="your"></a>Your
106
+ check_job
71
107
 
72
- http://docs.idcf.jp/cloud/billing/
108
+ ``` code
109
+ $ idcfcloud ilb check_job <job_id>
110
+ ```
111
+
112
+ sslalgorithms_ids
113
+
114
+ ``` code
115
+ $ idcfcloud ilb sslalgorithms_ids
116
+ ```
117
+
118
+ ### CDN<a name="cdn"></a>
119
+
120
+ https://www.idcf.jp/help/cache/docs/pdf/cache_api.pdf?cl=rd_0046
121
+
122
+ ### DNS<a name="dns"></a>
73
123
 
124
+ http://docs.idcf.jp/cloud/dns/#s_fid=4FEB16B56007BA5C-0BFB42ABA668ADA8
125
+
126
+ ### Your<a name="your"></a>
127
+
128
+ http://docs.idcf.jp/cloud/billing/
74
129
 
75
130
  ## Development
76
131
 
77
132
  Running the following test code is possible, but not recommended nor supported. Run only a code of a target.
78
133
 
79
134
  ```code
80
- $ bundle exec ruby test/run_test.rb idcf/cli/controller/test_billing.rb
135
+ $ bundle exec ruby test/run_test.rb idcf/cli/controller/test_your.rb
81
136
  ```
82
137
 
83
138
  In order to avoid collapsed setting in ILB, make sure you understand how things work before running a code.
data/exe/idcfcloud CHANGED
@@ -1,4 +1,12 @@
1
1
  require 'idcf/cli/index'
2
2
 
3
- Idcf::Cli::Index.init
3
+ if ARGV[0] && %w(morio).include?(ARGV[0].downcase)
4
+ cls = "idcf/cli/command/#{ARGV[0].downcase}"
5
+ require cls
6
+ cls.classify.constantize.new.exec
7
+
8
+ exit
9
+ end
10
+
11
+ Idcf::Cli::Index.init(ARGV)
4
12
  Idcf::Cli::Index.start(ARGV)
data/idcfcloud.gemspec CHANGED
@@ -26,18 +26,20 @@ Gem::Specification.new do |spec|
26
26
 
27
27
  spec.add_development_dependency 'bundler', '~> 1.14'
28
28
  spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'test-unit', '~> 3.2', '>= 3.2.5'
30
+ spec.add_development_dependency 'test-unit-notify', '~> 1.0', '>= 1.0.4'
29
31
 
30
- spec.add_dependency 'test-unit', '~> 3.2', '>= 3.2.5'
31
- spec.add_dependency 'test-unit-notify', '~> 1.0', '>= 1.0.4'
32
-
33
- spec.add_dependency 'activerecord', '~> 4.2', '>= 4.2.3'
32
+ spec.add_dependency 'activemodel', '~> 4.2', '>= 4.2.3'
34
33
  spec.add_dependency 'activesupport', '~> 4.2', '>= 4.2.3'
35
34
  spec.add_dependency 'inifile', '~> 3.0', '>= 3.0.0'
36
35
  spec.add_dependency 'thor', '~> 0.19.4'
37
36
  spec.add_dependency 'builder', '~> 3.1'
38
37
  spec.add_dependency 'kosi', '~> 1.0', '>= 1.0.0'
39
38
 
40
- spec.add_dependency 'idcf-your'
41
- spec.add_dependency 'idcf-faraday_middleware'
42
- spec.add_dependency 'idcf-ilb', '>= 0.0.3'
39
+ spec.add_dependency 'open_uri_redirections', '~> 0.2.1'
40
+ spec.add_dependency 'idcf-json_hyper_schema', '~> 0.1.0'
41
+ spec.add_dependency 'idcf-faraday_middleware', '~> 0.0.2'
42
+ spec.add_dependency 'jsonpath', '~> 0.8.10'
43
+ spec.add_dependency 'facter', '~> 2.5.1'
44
+ spec.add_dependency 'CFPropertyList', '~> 2.3.5'
43
45
  end
@@ -1,10 +1,44 @@
1
1
  output: json #json xml csv table
2
+ log_limit: 20
2
3
  ilb:
3
4
  v1:
4
- host:
5
- jp-east: ilb.jp-east.idcfcloud.com
6
- jp-east-2: ilb.jp-east-2.idcfcloud.com
7
- jp-west: ilb.jp-west.idcfcloud.com
5
+ region:
6
+ jp-east:
7
+ schema: https://ilb.jp-east.idcfcloud.com/api/v1/schema
8
+ host: ilb.jp-east.idcfcloud.com
9
+ jp-east-2:
10
+ schema: https://ilb.jp-east-2.idcfcloud.com/api/v1/schema
11
+ host: ilb.jp-east-2.idcfcloud.com
12
+ jp-west:
13
+ schema: https://ilb.jp-west.idcfcloud.com/api/v1/schema
14
+ host: ilb.jp-west.idcfcloud.com
8
15
  your:
9
16
  v1:
10
- host: your.idcfcloud.com
17
+ region:
18
+ default:
19
+ schema: https://your.idcfcloud.com/api/v1/schema
20
+ host: your.idcfcloud.com
21
+ cdn:
22
+ v0:
23
+ region:
24
+ default:
25
+ schema: https://cdn.idcfcloud.com/api/v1/schema
26
+ host: cdn.idcfcloud.com
27
+ dns:
28
+ v1:
29
+ region:
30
+ default:
31
+ schema: https://dns.idcfcloud.com/api/v1/schema
32
+ host: dns.idcfcloud.com
33
+ compute:
34
+ v1:
35
+ region:
36
+ jp-east:
37
+ schema: https://compute.jp-east.idcfcloud.com/api/v1/schema
38
+ host: compute.jp-east.idcfcloud.com
39
+ jp-east-2:
40
+ schema: https://compute.jp-east-2.idcfcloud.com/api/v1/schema
41
+ host: compute.jp-east-2.idcfcloud.com
42
+ jp-west:
43
+ schema: https://compute.jp-west.idcfcloud.com/api/v1/schema
44
+ host: compute.jp-west.idcfcloud.com
@@ -1,36 +1,61 @@
1
+ require 'date'
2
+ require 'logger'
3
+ require 'idcf/cli/version'
1
4
  module Idcf
2
5
  module Cli
3
6
  module Conf
4
7
  # const
5
8
  class Const
9
+ VERSION_STR = "idcfcloud version #{Idcf::Cli::VERSION}".freeze
10
+ now = DateTime.now.strftime('%Y%m%d%H%M%S%6N')
6
11
  dir_path = File.dirname(__FILE__)
7
12
  BASE_PATH = File.expand_path("#{dir_path}/../../../")
8
13
  LOCAL_ERROR_PREFIX = '[idcfcli]'.freeze
9
- HEADERS = {
10
- 'User-Agent': "IDCF CLI v#{Idcf::Cli::VERSION}"
11
- }.freeze
14
+ HEADERS =
15
+ {
16
+ 'User-Agent': "IDCF CLI v#{Idcf::Cli::VERSION}"
17
+ }.freeze
18
+ USER_DIR_PATH = '~/.idcfcloud'.freeze
12
19
  VALIDATOR_DIR_PATH = 'idcf/cli/validate/define'.freeze
13
20
  CODE_CONF_PATH = "#{BASE_PATH}/idcf/cli/conf/conf.yml".freeze
14
- USER_CONF_PATH = '~/.idcfcloud/config.ini'.freeze
21
+ USER_CONF_PATH = "#{USER_DIR_PATH}/config.ini".freeze
15
22
  USER_CONF_GLOBAL = "#{BASE_PATH}/../config/config.ini".freeze
16
23
  REGIONS = %w(jp-east jp-east-2 jp-west).freeze
17
- USER_CONF_ITEMS = {
18
- api_key: nil,
19
- secret_key: nil,
20
- region: REGIONS
21
- }.freeze
24
+ GLOBAL_CONF_ITEMS =
25
+ {
26
+ output_log: {
27
+ default: 'Y',
28
+ list: %w(Y n)
29
+ },
30
+ log_path: {
31
+ default: '~/.idcfcloud/log'
32
+ }
33
+ }.freeze
34
+ USER_CONF_ITEMS =
35
+ {
36
+ api_key: nil,
37
+ secret_key: nil,
38
+ region: {
39
+ list: REGIONS
40
+ }
41
+ }.freeze
42
+ FULL_HREF_REGEXP = Regexp.new('\A[a-zA-Z]*:?//').freeze
22
43
  CMD_FILE_DIR = "#{BASE_PATH}/idcf/cli/conf/service".freeze
23
- CMD_FILE_EXT = 'yml'.freeze
44
+ CMD_FILE_EXT = 'json'.freeze
24
45
  SERVICE_PATH = 'idcf/cli/service'.freeze
25
46
  TEMPLATE_DIR = "#{BASE_PATH}/idcf/cli/templates".freeze
26
47
  OUT_DIR = "#{BASE_PATH}/../output".freeze
27
- COMP_NOTIFICATION = <<EOT.freeze
28
- please write in
29
- ~/.bash_profile
30
- source %s
31
- ~/.zprofile
32
- source %s
33
- EOT
48
+ COMP_PATHS = {
49
+ bash: '~/.bash_profile',
50
+ zsh: '~/.zprofile'
51
+ }.freeze
52
+ LOG_FILE_PREFIX = 'cli_log_'.freeze
53
+ LOG_FILE_NAME = "#{LOG_FILE_PREFIX}#{now}.log".freeze
54
+ LOG_LEVEL = Logger::INFO.freeze
55
+ CLASSIFY_RULE = [
56
+ %w(sslalgorithms_ids SslalgorithmsIds),
57
+ %w(dns Dns)
58
+ ].freeze
34
59
  end
35
60
  end
36
61
  end
@@ -3,12 +3,14 @@ require 'json'
3
3
  require 'active_support'
4
4
  require 'active_support/core_ext'
5
5
  require 'idcf/cli/conf/const'
6
- require 'idcf/cli/lib/util/ini_conf'
7
- require 'idcf/cli/lib/util/yml_conf'
8
- require 'active_record'
9
6
  require 'idcf/cli/lib/convert/helper'
7
+ require 'idcf/json_hyper_schema'
8
+ require 'idcf/json_hyper_schema/expands/link_info_base'
9
+ require 'idcf/cli/gem_ext/idcf/json_hyper_schema/expands/link_info_base_extension'
10
10
  require_relative './extend/init'
11
11
  require_relative './include/init'
12
+ require 'jsonpath'
13
+ require 'idcf/cli/lib/include/recurring_calling'
12
14
 
13
15
  module Idcf
14
16
  module Cli
@@ -32,105 +34,173 @@ module Idcf
32
34
  desc: 'not use ssl'
33
35
  class_option :no_vssl,
34
36
  desc: 'not use verify ssl'
37
+ class_option :json_path,
38
+ desc: 'data filter json_path'
39
+ class_option :fields,
40
+ desc: 'field filter'
35
41
 
36
42
  attr_reader :config, :code_conf, :cmd, :m_classes
37
43
 
38
44
  extend Idcf::Cli::Controller::Extend::Init
39
45
  include Idcf::Cli::Controller::Include::Init
46
+ include Idcf::Cli::Lib::Include::RecurringCalling
40
47
 
41
48
  class << self
42
- def init
43
- fn = to_s.underscore.split('/').pop
44
- dir = Idcf::Cli::Conf::Const::CMD_FILE_DIR
45
- ext = Idcf::Cli::Conf::Const::CMD_FILE_EXT
46
- path = "#{dir}/#{fn}.#{ext}"
47
-
48
- if File.exist?(path)
49
- cf = Idcf::Cli::Lib::Util::YmlConf.new(path)
50
- register_sdk_method! cf.find('')
49
+ attr_accessor :links
50
+
51
+ def init(argv)
52
+ o = Thor::Options.new(class_options).parse(argv)
53
+ register_schema_method(o)
54
+
55
+ make_module_classes.each do |cn, c|
56
+ register_module_method!(cn, c)
51
57
  end
58
+ end
52
59
 
53
- register_service_method!
60
+ def register_schema_method(o)
61
+ path = make_schema_path(o)
62
+
63
+ return nil unless File.exist?(path)
64
+
65
+ analyst = Idcf::JsonHyperSchema::Analyst.new
66
+ @links = analyst.load(path).links
67
+ @links.each do |link|
68
+ register_schema_method_by_link!(link)
69
+ end
54
70
  end
55
71
 
56
- # regist method
72
+ # service name
57
73
  #
58
- # @param methods [Hash]
59
- def register_sdk_method!(methods)
60
- return unless methods
61
- methods.each do |command, s|
62
- register_method!(command, s[:params], s[:options])
63
- end
74
+ # @return String
75
+ def service_name
76
+ name.underscore.split('/').last
64
77
  end
65
78
 
66
- # regist service method
67
- def register_service_method!
68
- make_module_classes.each do |cn, c|
69
- register_method!(cn, c.params, c.options)
70
- end
79
+ # service version
80
+ #
81
+ # @param o [Hash]
82
+ # @return String
83
+ def service_version(o)
84
+ sn = service_name
85
+ list = Idcf::Cli::Lib::Configure.get_code_conf(sn, o)
86
+ result = o[:version].nil? ? list.keys.last : o[:version]
87
+ msg = "not found input version[#{o[:version]}]"
88
+ raise Idcf::Cli::Error::CliError, msg unless list.keys.include?(result)
89
+ result
71
90
  end
72
91
  end
73
92
 
74
93
  protected
75
94
 
76
95
  def execute(cmd, *args)
77
- data = run(cmd, args)
96
+ data = run(cmd, args, options)
78
97
  puts make_result_str(data, false, options)
98
+ Idcf::Cli::Lib::Util::CliLogger.delete
99
+ Idcf::Cli::Lib::Util::CliLogger.cleaning(options)
79
100
  rescue => e
80
- STDERR.puts e.message
81
- exit 1
101
+ self.class.error_exit(e)
82
102
  end
83
103
 
84
- def run(cmd, args)
104
+ def run(cmd, args, o)
85
105
  @cmd = cmd
86
- check_params(self.class.make_module_classes, cmd, *args)
87
- o = options
88
106
  data = do_validate(o)
89
- if data
90
- STDERR.puts data[:message].flatten
91
- exit 1
92
- end
107
+ raise Idcf::Cli::Error::CliError, data[:message].flatten if data
108
+
93
109
  data = make_result_data(do_command(cmd, *args, o), o)
94
110
  data
95
111
  end
96
112
 
113
+ # do command
114
+ #
115
+ # @param cmd [String]
116
+ # @param *args [Array]
117
+ # @param o [Hash]
118
+ # @return Mixed
119
+ # @raise
97
120
  def do_command(cmd, *args, o)
98
- m_class = self.class.search_module_class(cmd)
121
+ self_class = self.class
122
+ s_class = self_class.search_module_class(cmd)
99
123
 
100
124
  arg = convert_arg_json(*args)
101
- client = make_client(o)
102
- return m_class.new.do(client, *arg, o) unless m_class.nil?
103
- do_sdk(client, cmd, *arg, o)
125
+ client = make_client(o, self_class.get_region(o))
126
+ api = Idcf::Cli::Lib::Api.new(links: self_class.links, client: client)
127
+ return do_service(s_class, api, o, arg) unless s_class.nil?
128
+ do_api(cmd, api, o, arg)
104
129
  end
105
130
 
106
- # make response base data
131
+ # do service
107
132
  #
108
- # @return Hash
109
- def make_base_response
110
- {
111
- status: 200,
112
- message: '',
113
- data: []
114
- }
133
+ # @param service_class [Idcf::Cli::Service::Base]
134
+ # @param api [Idcf::Cli::Lib::Api]
135
+ # @param arg [Array]
136
+ # @param o [Hash]
137
+ # @return Mixed
138
+ # @raise
139
+ def do_service(service_class, api, o, arg)
140
+ obj = service_class.new
141
+ obj.between_param?(arg.size)
142
+ result = obj.do(api, o, *arg)
143
+ return result if obj.last_command.empty?
144
+ do_api(obj.last_command, api, o, obj.last_command_args)
115
145
  end
116
146
 
117
- # make error result
147
+ # do api
118
148
  #
119
- # @param error [Exception]
120
- # @return Hash
121
- def make_error(error)
122
- error.to_s =~ /status code: ?(\d+)/
123
- match = Regexp.last_match(1)
124
- result = make_base_response
125
- result[:status] = match ? match.to_i : 400
126
- result[:message] = error.message
127
- result
149
+ # @param cmd [String]
150
+ # @param api [Idcf::Cli::Lib::Api]
151
+ # @param arg [Array]
152
+ # @param o [Hash]
153
+ # @return Mixed
154
+ # @raise
155
+ def do_api(cmd, api, o, arg)
156
+ result = api_result(api.do(cmd, *arg))
157
+ link = api.find_link(cmd)
158
+ resource = link.async? ? do_async(api, result, o, link) : nil
159
+ sync_result = api.result_api(cmd, arg, result, async_id(resource))
160
+ return sync_result if sync_result
161
+ link.async? ? resource : result
162
+ rescue Idcf::Cli::Error::ApiError => e
163
+ raise e, raise_api_error_msg(e)
164
+ end
165
+
166
+ def api_result(val)
167
+ val
168
+ end
169
+
170
+ def async_id(resource)
171
+ return resource['resource_id'] if !resource.nil? && resource.class == Hash
172
+ nil
173
+ end
174
+
175
+ # do_async
176
+ #
177
+ # @param _api [Idcf::Cli::Lib::Api]
178
+ # @param _api_result [Mixed]
179
+ # @param _o [Hash]
180
+ # @param _executed_link [Idcf::JsonHyperSchema::Expands::LinkInfoBase]
181
+ # @result Mixed
182
+ def do_async(_api, _api_result, _o, _executed_link)
183
+ nil
184
+ end
185
+
186
+ # raise api error msg
187
+ #
188
+ # @param res [Faraday::Responce]
189
+ # @return [String]
190
+ def raise_api_error_msg(res)
191
+ "HTTP status code: #{res.status}, " \
192
+ "Error message: #{res.body['message']}, " \
193
+ "Reference: #{res.body['reference']}"
128
194
  end
129
195
 
130
196
  def make_result_data(data, o)
131
- result = data
132
- format = output_format(o, '')
133
- if format == 'csv' || format == 'table'
197
+ format = output_format(o, '')
198
+ table_flag = %w(csv table).include?(format)
199
+ helper = Idcf::Cli::Lib::Convert::Helper.new
200
+
201
+ if helper.filter_target?(o)
202
+ result = helper.filter(data, o, table_flag)
203
+ elsif table_flag
134
204
  result = make_table_data(data)
135
205
  else
136
206
  result = make_base_response
@@ -1,38 +1,40 @@
1
1
  require 'idcf/cli/conf/const'
2
+ require 'idcf/cli/lib/api'
2
3
  module Idcf
3
4
  module Cli
4
5
  module Controller
5
6
  module Extend
6
7
  # command
7
8
  module Command
8
- def register_method!(name, params, options)
9
- register_method_option! options
10
- cp = make_param_s params
11
- desc "#{name} #{cp}", ''
12
- define_method name.to_sym do |*args|
9
+ # register schema method by link
10
+ #
11
+ # @param link [Idcf::JsonHyperSchema::Expands::LinkInfoBase]
12
+ def register_schema_method_by_link!(link)
13
+ param_str = Idcf::Cli::Lib::Api.command_param_str(link)
14
+ method_desc = "#{link.title} #{param_str}"
15
+ desc method_desc.strip, link.description
16
+ define_method link.title.to_sym do |*args|
13
17
  execute(__method__, *args)
14
18
  end
15
19
  end
16
20
 
17
- # make param string
21
+ # register module mothod
18
22
  #
19
- # @param params[Array]
20
- # @return String
21
- def make_param_s(params)
22
- cp = []
23
- return '' if params.nil?
24
- params.each do |param|
25
- f = param[:arg_type].to_s == 'req' ? '<%s>' : '[%s]'
26
- cp << format(f, param[:name])
23
+ # @param name [String] command_name
24
+ # @param cls [Idcf::Cli::Service::Base]
25
+ def register_module_method!(name, cls)
26
+ register_method_option! cls.options
27
+ desc "#{name} #{cls.make_param_s}", cls.description
28
+ define_method name.to_sym do |*args|
29
+ execute(__method__, *args)
27
30
  end
28
- cp.join(' ')
29
31
  end
30
32
 
31
33
  # regist method option
32
34
  #
33
35
  # @param values [Hash] name => desc
34
36
  def register_method_option!(values)
35
- return if values.nil?
37
+ return nil if values.nil?
36
38
  values.each do |opn, op|
37
39
  option = {}
38
40
  op.each do |ok, ov|
@@ -1,6 +1,7 @@
1
1
  require_relative './override'
2
2
  require_relative './search_module'
3
3
  require_relative './command'
4
+ require_relative './util'
4
5
  module Idcf
5
6
  module Cli
6
7
  module Controller
@@ -10,6 +11,7 @@ module Idcf
10
11
  include Override
11
12
  include SearchModule
12
13
  include Command
14
+ include Util
13
15
  end
14
16
  end
15
17
  end