idcfcloud 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +21 -0
  3. data/.gitignore +56 -0
  4. data/.rubocop.yml +26 -0
  5. data/Gemfile +7 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +91 -0
  8. data/Rakefile +8 -0
  9. data/bin/idcfcloud +17 -0
  10. data/config/config_sample.ini +16 -0
  11. data/exe/idcfcloud +4 -0
  12. data/idcfcloud.gemspec +43 -0
  13. data/lib/idcf/cli/conf/conf.yml +10 -0
  14. data/lib/idcf/cli/conf/const.rb +37 -0
  15. data/lib/idcf/cli/conf/service/.gitkeep +0 -0
  16. data/lib/idcf/cli/controller/base.rb +151 -0
  17. data/lib/idcf/cli/controller/extend/command.rb +48 -0
  18. data/lib/idcf/cli/controller/extend/init.rb +17 -0
  19. data/lib/idcf/cli/controller/extend/override.rb +21 -0
  20. data/lib/idcf/cli/controller/extend/search_module.rb +54 -0
  21. data/lib/idcf/cli/controller/ilb.rb +62 -0
  22. data/lib/idcf/cli/controller/include/command.rb +106 -0
  23. data/lib/idcf/cli/controller/include/init.rb +21 -0
  24. data/lib/idcf/cli/controller/include/override.rb +30 -0
  25. data/lib/idcf/cli/controller/include/sdk.rb +83 -0
  26. data/lib/idcf/cli/controller/include/util.rb +101 -0
  27. data/lib/idcf/cli/controller/include/validate.rb +79 -0
  28. data/lib/idcf/cli/controller/your.rb +57 -0
  29. data/lib/idcf/cli/error/cli_error.rb +11 -0
  30. data/lib/idcf/cli/extend/configure.rb +71 -0
  31. data/lib/idcf/cli/extend/init.rb +13 -0
  32. data/lib/idcf/cli/extend/update.rb +109 -0
  33. data/lib/idcf/cli/extend/update_file.rb +53 -0
  34. data/lib/idcf/cli/gem_ext/thor/init_util.rb +92 -0
  35. data/lib/idcf/cli/index.rb +71 -0
  36. data/lib/idcf/cli/lib/convert/formatter/base.rb +21 -0
  37. data/lib/idcf/cli/lib/convert/formatter/csv_format.rb +115 -0
  38. data/lib/idcf/cli/lib/convert/formatter/json_format.rb +19 -0
  39. data/lib/idcf/cli/lib/convert/formatter/table_format.rb +19 -0
  40. data/lib/idcf/cli/lib/convert/formatter/xml_format.rb +21 -0
  41. data/lib/idcf/cli/lib/convert/helper.rb +28 -0
  42. data/lib/idcf/cli/lib/util/ini_conf.rb +67 -0
  43. data/lib/idcf/cli/lib/util/template.rb +29 -0
  44. data/lib/idcf/cli/lib/util/yml_conf.rb +37 -0
  45. data/lib/idcf/cli/service/base.rb +56 -0
  46. data/lib/idcf/cli/service/ilb/add_server_for_protocol.rb +20 -0
  47. data/lib/idcf/cli/service/ilb/base_server_for_protocol.rb +111 -0
  48. data/lib/idcf/cli/service/ilb/delete_server_for_protocol.rb +29 -0
  49. data/lib/idcf/cli/templates/bash/complement.erb +37 -0
  50. data/lib/idcf/cli/templates/zsh/complement.erb +46 -0
  51. data/lib/idcf/cli/validate/custom/init.rb +14 -0
  52. data/lib/idcf/cli/validate/custom/month_check_validator.rb +39 -0
  53. data/lib/idcf/cli/validate/custom/require_relation_validator.rb +50 -0
  54. data/lib/idcf/cli/validate/define/base.rb +36 -0
  55. data/lib/idcf/cli/validate/define/ilb/base.rb +27 -0
  56. data/lib/idcf/cli/version.rb +5 -0
  57. metadata +317 -0
@@ -0,0 +1,56 @@
1
+ module Idcf
2
+ module Cli
3
+ module Service
4
+ # model base
5
+ class Base
6
+ ARG_TYPE_REQ = :req
7
+ ARG_TYPE_OPT = :opt
8
+ ARG_TYPE_REST = :rest
9
+ ARG_TYPE_BLOCK = :block
10
+ class << self
11
+ attr_reader :options, :params
12
+ # option
13
+ #
14
+ # @param name [String]
15
+ # @param attr [Hash]
16
+ # @option attr [String] :type
17
+ # @option attr [Boolean] :required
18
+ # @option attr [String] :desc
19
+ def option(name, *attr)
20
+ @options ||= {}
21
+ @options[name] = attr
22
+ end
23
+
24
+ # params
25
+ #
26
+ # @param values [String]
27
+ # @param arg_type [String]
28
+ def param(name, arg_type = ARG_TYPE_REQ)
29
+ @params ||= []
30
+ s = @params.select do |v|
31
+ v[:name] == name
32
+ end
33
+
34
+ param = {
35
+ name: name,
36
+ arg_type: arg_type
37
+ }
38
+
39
+ @params << param if s.size.zero?
40
+ end
41
+ end
42
+
43
+ # do
44
+ #
45
+ # @param _client [Mixed]
46
+ # @param *_args [Array]
47
+ # @param _o [Hash] options
48
+ # @return Hash
49
+ # @raise
50
+ def do(_client, *_args, _o)
51
+ raise Idcf::Cli::CliError, 'Require override'
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,20 @@
1
+ require_relative './base_server_for_protocol'
2
+
3
+ module Idcf
4
+ module Cli
5
+ module Service
6
+ module Ilb
7
+ # add server
8
+ class AddServerForProtocol < BaseServerForProtocol
9
+ init
10
+
11
+ protected
12
+
13
+ def do_command(lb_id, config, target)
14
+ client.add_server(lb_id, config['id'], target)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,111 @@
1
+ require_relative '../base'
2
+
3
+ module Idcf
4
+ module Cli
5
+ module Service
6
+ module Ilb
7
+ # [add/delete] server for protocol
8
+ class BaseServerForProtocol < Idcf::Cli::Service::Base
9
+ class << self
10
+ def init
11
+ param :lb_id
12
+ param :protocol
13
+ param :protocol_port
14
+ param :data, Base::ARG_TYPE_REQ
15
+ end
16
+ end
17
+
18
+ attr_reader :client
19
+
20
+ # do
21
+ #
22
+ # @param client [Idcf::Ilb::Client]
23
+ # @param args [Array]
24
+ # @option args [String] lb id
25
+ # @option args [Stirng] configure protocol
26
+ # @option args [int] configure port
27
+ # @option args [Hash] server info {ipaddress: '0.0.0.0', port: 80}
28
+ # @param o [Hash] options
29
+ def do(client, *args, o)
30
+ @client = client
31
+ lb_id = args[0]
32
+ lb = search_lb(lbs, lb_id)
33
+ config = search_config(lb, args[1], args[2])
34
+
35
+ if config.nil?
36
+ not_param = o[:protocol].nil? ? 'conf_id' : 'protocol'
37
+ raise Idcf::Cli::CliError, "Target not found (#{not_param})"
38
+ end
39
+
40
+ do_command(lb_id, config, args[3])
41
+ client.list_servers(lb_id, config['id'])
42
+ end
43
+
44
+ protected
45
+
46
+ def do_command(_lb_id, _config, _target)
47
+ raise Idcf::Cli::CliError, 'override required'
48
+ end
49
+
50
+ # get lb list [Idcf::Ilb::Client]
51
+ #
52
+ # @param client
53
+ # @return Array in Hash
54
+ # @raise
55
+ def lbs
56
+ lb = client.get(:loadbalancers)
57
+ msg = "Status: #{lb.status}"
58
+ raise Idcf::Cli::CliError, msg if !lb.success? || lb.status != 200
59
+
60
+ lb.body
61
+ end
62
+
63
+ # search lb target
64
+ #
65
+ # @param list
66
+ # @param id
67
+ # @return [Hash]
68
+ # @raise
69
+ def search_lb(list, id)
70
+ lbs = list.select do |v|
71
+ v['id'] == id
72
+ end
73
+
74
+ msg = "Target lb_id not found ( #{id} )"
75
+ raise Idcf::Cli::CliError, msg if lbs.empty?
76
+
77
+ lbs[0]
78
+ end
79
+
80
+ # config search
81
+ #
82
+ # @param lb_info [Hash]
83
+ # @param o [Hash]
84
+ # @return Hash
85
+ # @raise
86
+ def search_config(lb_info, protocol, p_port)
87
+ lb_info['configs'].each do |v|
88
+ next unless target_config?(v, protocol, p_port)
89
+ unless v['state'].casecmp('running').zero?
90
+ msg = 'The operation is impossible because the target is currently being processed'
91
+ raise Idcf::Cli::CliError, msg
92
+ end
93
+ return v
94
+ end
95
+ end
96
+
97
+ # is target config
98
+ #
99
+ # @param config [Hash]
100
+ # @param protocol [String]
101
+ # @param p_port [String]
102
+ # @return boolean
103
+ def target_config?(config, purotocol, p_port)
104
+ config['frontend_protocol'] == purotocol &&
105
+ config['port'].to_s == p_port.to_s
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,29 @@
1
+ require_relative './base_server_for_protocol'
2
+
3
+ module Idcf
4
+ module Cli
5
+ module Service
6
+ module Ilb
7
+ # delete server
8
+ class DeleteServerForProtocol < BaseServerForProtocol
9
+ init
10
+
11
+ protected
12
+
13
+ def do_command(lb_id, config, target)
14
+ server_id = search_server_id(config['servers'], target)
15
+ client.delete_server(lb_id, config['id'], server_id)
16
+ end
17
+
18
+ def search_server_id(servers, target)
19
+ result = servers.select do |v|
20
+ v['ipaddress'] == target[:ipaddress] &&
21
+ v['port'] == target[:port]
22
+ end
23
+ result.first['id'] if result.size == 1
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,37 @@
1
+ #!/bin/bash
2
+ #set -eu
3
+
4
+ _idcfcloud()
5
+ {
6
+ <% @variables.each do |k, v| %>local <%= k %>="<%= v.join(' ') %>"
7
+ <% end %>
8
+
9
+ local cur=${COMP_WORDS[COMP_CWORD]}
10
+ local list=""
11
+ local names=()
12
+ local index=$(( $COMP_CWORD - 1 ))
13
+ local val_name=""
14
+
15
+ if [ $COMP_CWORD = 1 ]; then
16
+ list=$top
17
+ else
18
+ for (( i=1; i <= $index; i++)); do
19
+ names+=( ${COMP_WORDS[i]} )
20
+ done
21
+
22
+ val_name="$(IFS=_; echo "${names[*]}")"
23
+ if [ ${COMP_WORDS[index]} = "help" ]; then
24
+ names=(${names[@]:0:((${#names[@]}-1))})
25
+ val_name="$(IFS=_; echo "${names[*]}")"
26
+ fi
27
+
28
+ if [ "${val_name}" = "" ]; then
29
+ val_name="top"
30
+ fi
31
+ list=${!val_name:-}
32
+ fi
33
+
34
+ COMPREPLY=( $(compgen -W "$list" -- $cur) )
35
+ }
36
+
37
+ complete -F _idcfcloud idcfcloud
@@ -0,0 +1,46 @@
1
+ _idcfcloud()
2
+ {
3
+ _arguments -C \
4
+ '1: :__idcfcloud_services' \
5
+ '*: :__idcfcloud_commands' \
6
+ && ret=0
7
+ }
8
+
9
+ __idcfcloud_services()
10
+ {
11
+ local -a _c
12
+ _c=(<% @variables['top'].each do |v| %>"<%= v %>" <% end %>)
13
+
14
+ _describe -t commands Commands _c
15
+ }
16
+
17
+ __idcfcloud_commands()
18
+ {
19
+ local -a _c
20
+
21
+ local names=()
22
+ for (( i=2; i < $CURRENT; i++)); do
23
+ names+=( ${words[i]} )
24
+ done
25
+ local val_name="$(IFS=_; echo "${names[*]}")"
26
+ if [ ${words[$CURRENT - 1]} = "help" ]; then
27
+ names=${names[1, -2]}
28
+ val_name="$(IFS=_; echo "${names[*]}")"
29
+ fi
30
+
31
+ case $val_name in<%
32
+ @variables.each do |k, v|
33
+ next if k == 'top'
34
+ %>
35
+ (<%= k %>)
36
+ _c=(<% v.each do |command| %>"<%= command %>" <% end %>)
37
+ ;;<% end %>
38
+ ("")
39
+ _c=(<% @variables['top'].each do |v| %>"<%= v %>" <% end %>)
40
+ ;;
41
+ esac
42
+
43
+ _describe -t commands Commands _c
44
+ }
45
+
46
+ compdef _idcfcloud idcfcloud
@@ -0,0 +1,14 @@
1
+ module Idcf
2
+ module Cli
3
+ module Validate
4
+ # custom validation load sesettings
5
+ module Custom
6
+ path = 'idcf/cli/validate/custom/'
7
+ autoload :RequireRelationValidator,
8
+ "#{path}require_relation_validator"
9
+ autoload :MonthCheckValidator,
10
+ "#{path}month_check_validator"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,39 @@
1
+ require 'active_model'
2
+ require 'idcf/cli/lib/util/option'
3
+ require 'ipaddr'
4
+
5
+ module Idcf
6
+ module Cli
7
+ module Validate
8
+ module Custom
9
+ # target validator
10
+ class MonthCheckValidator < ActiveModel::EachValidator
11
+ MESSAGES = {
12
+ format: 'different format ex)YYYY-MM',
13
+ invalid: 'An invalid date'
14
+ }.freeze
15
+
16
+ def validate_each(record, attr, val)
17
+ str = MESSAGES[:format]
18
+ return record.errors.add(attr, str, {}) unless format?(val)
19
+ str = MESSAGES[:invalid]
20
+ return record.errors.add(attr, str, {}) unless valid?(val)
21
+ end
22
+
23
+ protected
24
+
25
+ def format?(val)
26
+ !(val =~ /\A\d{4}-\d{1,2}\Z/).nil?
27
+ end
28
+
29
+ def valid?(val)
30
+ Date.parse("#{val}-01")
31
+ true
32
+ rescue
33
+ false
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,50 @@
1
+ require 'active_model'
2
+
3
+ module Idcf
4
+ module Cli
5
+ module Validate
6
+ module Custom
7
+ # require relation validator
8
+ class RequireRelationValidator < ActiveModel::EachValidator
9
+ MESSAGES = {
10
+ message: 'A related parameter isn\'t input. (%s)'
11
+ }.freeze
12
+
13
+ def validate(record)
14
+ return unless process?(record)
15
+
16
+ nones = make_nones(record)
17
+ return if nones.empty?
18
+
19
+ msg = MESSAGES[:message] % nones.join('/')
20
+ record.errors.add(nones.first, msg, {})
21
+ end
22
+
23
+ protected
24
+
25
+ def process?(record)
26
+ list = []
27
+ op = options
28
+ attributes.each do |attr|
29
+ value = record.read_attribute_for_validation(attr)
30
+ next if value.blank? && (op[:allow_nil] || op[:allow_blank])
31
+ list << attr
32
+ end
33
+
34
+ list.present?
35
+ end
36
+
37
+ def make_nones(record)
38
+ result = []
39
+ attributes.each do |attr|
40
+ value = record.read_attribute_for_validation(attr)
41
+ result << attr if value.blank? || value == attr.to_s
42
+ end
43
+
44
+ result
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,36 @@
1
+ require 'active_model'
2
+ require 'active_model/model'
3
+ module Idcf
4
+ module Cli
5
+ module Validate
6
+ module Define
7
+ # Base Validate
8
+ class Base
9
+ include ActiveModel::Model
10
+ include Idcf::Cli::Validate::Custom
11
+
12
+ attr_accessor :output,
13
+ :profile,
14
+ :api_key,
15
+ :secret_key,
16
+ :no_ssl,
17
+ :no_vssl
18
+
19
+ validates :output,
20
+ allow_blank: true,
21
+ inclusion: {
22
+ in: %w(table json xml csv),
23
+ message: 'from this [table/json/xml/csv]'
24
+ }
25
+
26
+ validates :api_key,
27
+ :secret_key,
28
+ allow_blank: true,
29
+ require_relation: {
30
+ in: true
31
+ }
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,27 @@
1
+ require 'active_model'
2
+ require 'active_model/model'
3
+ require_relative '../base'
4
+ module Idcf
5
+ module Cli
6
+ module Validate
7
+ module Define
8
+ module Ilb
9
+ # Base Validate
10
+ class Base < Idcf::Cli::Validate::Define::Base
11
+ include ActiveModel::Model
12
+ include Idcf::Cli::Validate::Custom
13
+
14
+ attr_accessor :region
15
+
16
+ validates :region,
17
+ allow_blank: true,
18
+ inclusion: {
19
+ in: Idcf::Cli::Conf::Const::REGIONS,
20
+ message: "from this [#{Idcf::Cli::Conf::Const::REGIONS.join('/')}]"
21
+ }
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ module Idcf
2
+ module Cli
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end