idcfcloud 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4a3321422f5844f5854dde7ff528e58f4ffda585
4
- data.tar.gz: 7061912c1a39b1f141673fbc7b56b0e81f9b7d84
3
+ metadata.gz: 006c01ce8038f63174fb320bc37e4ad21ad455e0
4
+ data.tar.gz: 36bacb9aeebc05e83339f76ebc7a5a25f597bba5
5
5
  SHA512:
6
- metadata.gz: a14919f258cd60e092fc8c1ef466301b505032f41078e1ce76864a19f392c33d569f14297d860d5f5501fb8297d1d0b54190f33b9638ddcc656925e7846b1e38
7
- data.tar.gz: 644f7237434d2a3e0ff7d3e89c70a30695c13e2480ee4624a1bd9cc6dd04987e299df7a0523edc168ced200e2e651a91b2b09a0a50bf631946663f82aa933e2c
6
+ metadata.gz: bc4302a93e69966e28aceb52c4c657ee8a0a5348f2c5485c7a9dd6f9ed92994b9c463cffc59769a0af2d178a416fa47a7e9d2004b4f43d4d7a64f7870dbdcebc
7
+ data.tar.gz: e667b317e5cd40612bd101be4679cac8a3a829e54e28b76d9e76c95e4e2f9df5c44f61a92ddcad25580acc8f917db23816c29fbe87d0bcfa1e325125c8916f06
@@ -0,0 +1,118 @@
1
+ require 'idcf/cli/conf/const'
2
+ require 'rbconfig'
3
+ require 'faraday'
4
+ require 'etc'
5
+ require 'objspace'
6
+ require 'open-uri'
7
+ require 'json'
8
+ require 'facter'
9
+ module Idcf
10
+ module Cli
11
+ module Command
12
+ # morio command
13
+ class Morio
14
+ MORIO_TXT = <<-'EOS'.freeze
15
+ -+sso+-
16
+ .... -ssssssso`
17
+ `-/ossssss+/. `ssssssso`
18
+ `/ssssssssssssso- /sssss+
19
+ .-:ossssssssssssssss` sssssss`
20
+ `/sssssssssssssssssssss.+sssssss.
21
+ `ossssssssssssssssssssss`osssssss`
22
+ /sssssssssssssssssssssss-ossssss+
23
+ :sssssssssssssssssssssssssssssso`
24
+ /ssssssssssssssssssssssssssss+`
25
+ ./osssssssssssssssssssssss+.
26
+ `-:+oosssssssssssso+:.
27
+ ``````````
28
+ EOS
29
+ ATTRIBUTES_OFF = "\e[0m".freeze
30
+ MORIO_COLOR = "\e[38;5;45m".freeze
31
+ TITLE_COLOR = "\e[38;5;45m".freeze
32
+ TEXT_COLOR = "\e[37m".freeze
33
+
34
+ attr_reader :client
35
+
36
+ def exec
37
+ morio_list = MORIO_TXT.split("\n")
38
+ max = max_length(morio_list)
39
+ contents = make_contents
40
+
41
+ morio_list.each_with_index do |line, idx|
42
+ fill_cnt = max - line.bytesize
43
+ fill = fill_cnt > 0 ? ''.ljust(fill_cnt, ' ') : ''
44
+ base_str = "#{MORIO_COLOR}#{line} #{fill}"
45
+ puts make_out_str(base_str, contents, idx)
46
+ end
47
+ puts ATTRIBUTES_OFF
48
+ end
49
+
50
+ private
51
+
52
+ def max_length(lists)
53
+ result = 0
54
+ lists.each do |line|
55
+ size = line.bytesize
56
+ result = size if result < size
57
+ end
58
+ result
59
+ end
60
+
61
+ def make_contents
62
+ result = [
63
+ ['IDCF Cloud', ''],
64
+ ['CLI', Idcf::Cli::Conf::Const::VERSION_STR]
65
+ ]
66
+ result.concat(find_data_server)
67
+ result << []
68
+ result.concat(find_spec_data)
69
+ end
70
+
71
+ def find_data_server
72
+ [].tap do |result|
73
+ @client ||= create_client('http://data-server')
74
+
75
+ {
76
+ VMID: 'vm-id',
77
+ SPEC: 'service-offering',
78
+ ZONE: 'availability-zone'
79
+ }.each do |k, v|
80
+ data = @client.get("latest/meta-data/#{v}")
81
+ result << [k.to_s, data.body] if data.success?
82
+ end
83
+ end
84
+ rescue
85
+ []
86
+ end
87
+
88
+ def find_spec_data
89
+ data = Etc.uname
90
+ [
91
+ ['Host', data[:nodename]],
92
+ ['OS', Facter.value(:operatingsystem)],
93
+ ['Kernel', "#{data[:sysname]} #{data[:release]} #{data[:machine]}"],
94
+ ['CPU(s)', Etc.nprocessors],
95
+ ['MEM', Facter.value(:memorysize)]
96
+ ]
97
+ end
98
+
99
+ def create_client(url)
100
+ Faraday.new(url: url) do |faraday|
101
+ faraday.adapter Faraday.default_adapter
102
+ end
103
+ end
104
+
105
+ def make_out_str(base_str, contents, idx)
106
+ add_str = ''
107
+ content = contents[idx]
108
+ if content && content.class == Array && content.size == 2
109
+ title = content[1].present? ? "#{content[0]} : " : content[0]
110
+ txt = content[1].present? ? content[1] : ''
111
+ add_str = format(" #{TITLE_COLOR}%s#{TEXT_COLOR}%s", title, txt)
112
+ end
113
+ "#{base_str}#{add_str}"
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,37 @@
1
+ require_relative './base'
2
+ require 'idcf/faraday_middleware/cdn_signature'
3
+
4
+ module Idcf
5
+ module Cli
6
+ module Controller
7
+ # CDN
8
+ class Cdn < Base
9
+ default_command :help
10
+
11
+ class << self
12
+ def description
13
+ 'Contents Cache Service'
14
+ end
15
+ end
16
+
17
+ protected
18
+
19
+ # signeture
20
+ #
21
+ # @return String
22
+ def signature
23
+ Idcf::FaradayMiddleware::CdnSignature
24
+ end
25
+
26
+ # raise api error msg
27
+ #
28
+ # @param res [Faraday::Responce]
29
+ # @return [String]
30
+ def raise_api_error_msg(res)
31
+ "HTTP status code: #{res.status}, " \
32
+ "Error message: #{res.body['messages']}, "
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,79 @@
1
+ require_relative './base'
2
+ require 'idcf/faraday_middleware/cdn_signature'
3
+
4
+ module Idcf
5
+ module Cli
6
+ module Controller
7
+ # compute
8
+ class Compute < Base
9
+ default_command :help
10
+
11
+ class_option :region,
12
+ type: :string,
13
+ aliases: '-r',
14
+ require: true,
15
+ desc: 'region: jp-east/jp-east-2/jp-west'
16
+
17
+ class << self
18
+ def description
19
+ 'Computeing Service(beta)'
20
+ end
21
+ end
22
+
23
+ protected
24
+
25
+ # signeture
26
+ #
27
+ # @return String
28
+ def signature
29
+ Idcf::FaradayMiddleware::ComputingSignature
30
+ end
31
+
32
+ def faraday_request
33
+ :url_encoded
34
+ end
35
+
36
+ def faraday_options(faraday)
37
+ faraday.options.params_encoder = Faraday::FlatParamsEncoder
38
+ faraday
39
+ end
40
+
41
+ # raise api error msg
42
+ #
43
+ # @param res [Faraday::Responce]
44
+ # @return [String]
45
+ def raise_api_error_msg(res)
46
+ body = api_result(res.body)
47
+ "HTTP status code: #{res.status}, " \
48
+ "Error message: #{body['errortext']}"
49
+ end
50
+
51
+ def api_result(val)
52
+ val[val.keys.first]
53
+ end
54
+
55
+ def async_id(resource)
56
+ return resource['jobid'] if !resource.nil? && resource.class == Hash
57
+ nil
58
+ end
59
+
60
+ def do_async(api, api_result, _o, _executed_link)
61
+ result = recurring_calling(:query_async, [api, async_id(api_result)], 24) do |res|
62
+ api_result(res)['jobstatus'] != 0
63
+ end
64
+ result = api_result(result)
65
+ result = result['jobresult'] if result.class == Hash
66
+ return result if result.nil? || result['errorcode'].present?
67
+ api_result(result)
68
+ end
69
+
70
+ def query_async(api, job_id)
71
+ params = {
72
+ 'jobid' => job_id
73
+ }
74
+ api.do(:queryAsyncJobResult, params)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,18 @@
1
+ require_relative './base'
2
+
3
+ module Idcf
4
+ module Cli
5
+ module Controller
6
+ # DNS/GSLB
7
+ class Dns < Base
8
+ default_command :help
9
+
10
+ class << self
11
+ def description
12
+ 'DNS Service'
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,34 @@
1
+ require 'idcf/cli/lib/configure'
2
+ module Idcf
3
+ module Cli
4
+ module Controller
5
+ module Extend
6
+ # util
7
+ module Util
8
+ # get region
9
+ #
10
+ # @param o [Hash] options
11
+ # @param read_conf [Boolean]
12
+ # @return String
13
+ def get_region(o)
14
+ flg = !class_options[:region].nil?
15
+ Idcf::Cli::Lib::Configure.get_region(o, flg)
16
+ end
17
+
18
+ # make schema path
19
+ #
20
+ # @param o [Hash]
21
+ # @return String
22
+ def make_schema_path(o)
23
+ fn = name.underscore.split('/').pop
24
+ dir = Idcf::Cli::Conf::Const::CMD_FILE_DIR
25
+ ext = Idcf::Cli::Conf::Const::CMD_FILE_EXT
26
+ region = get_region(o)
27
+ v = service_version(o)
28
+ "#{dir}/#{fn}_#{v}_#{region}.#{ext}"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,86 @@
1
+ require 'idcf/faraday_middleware'
2
+
3
+ module Idcf
4
+ module Cli
5
+ module Controller
6
+ module Include
7
+ # client
8
+ module Client
9
+ protected
10
+
11
+ # Create a new client
12
+ #
13
+ # @param o [Hash] other options
14
+ # @param region [String]
15
+ # @return [Response] Mixed
16
+ def make_client(o, region)
17
+ Faraday::Request.register_middleware(
18
+ signature: signature
19
+ )
20
+
21
+ Faraday.new(url: make_host(o, region), ssl: make_ssl_option(o)) do |faraday|
22
+ faraday_setting(faraday, o)
23
+ end
24
+ end
25
+
26
+ # signeture
27
+ #
28
+ # @return String
29
+ def signature
30
+ Idcf::FaradayMiddleware::Signature
31
+ end
32
+
33
+ def make_host(o, region)
34
+ protocol = o.key?(:no_ssl) ? 'http' : 'https'
35
+ v = self.class.service_version(o)
36
+ path = "#{self.class.service_name}.#{v}.region.#{region}.host"
37
+ host = Idcf::Cli::Lib::Configure.get_code_conf(path, o)
38
+ "#{protocol}://#{host}"
39
+ end
40
+
41
+ # ssl option
42
+ #
43
+ # @param o [Hash] options
44
+ # @return Hash
45
+ def make_ssl_option(o)
46
+ { verify: o.key?(:no_vssl) ? false : true }
47
+ end
48
+
49
+ # setting
50
+ #
51
+ # @param faraday [Faraday]
52
+ # @param o [Hash] options
53
+ # @return Faraday
54
+ def faraday_setting(faraday, o)
55
+ prof = Idcf::Cli::Lib::Configure.get_profile(o)
56
+ op = {}
57
+ [:api_key, :secret_key].each do |v|
58
+ op[v] = o.key?(v) ? o[v] : Idcf::Cli::Lib::Configure.get_user_conf(v.to_s, prof)
59
+ end
60
+ faraday.request(faraday_request)
61
+ faraday.request(:signature, op)
62
+ faraday.response(:json)
63
+ faraday.adapter Faraday.default_adapter
64
+ faraday_headers(faraday)
65
+ faraday_options(faraday)
66
+ end
67
+
68
+ def faraday_request
69
+ :json
70
+ end
71
+
72
+ def faraday_headers(faraday)
73
+ Idcf::Cli::Conf::Const::HEADERS.each do |k, v|
74
+ faraday.headers[k] = v
75
+ end
76
+ faraday
77
+ end
78
+
79
+ def faraday_options(faraday)
80
+ faraday
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,32 @@
1
+ module Idcf
2
+ module Cli
3
+ module Error
4
+ # api error
5
+ class ApiError < StandardError
6
+ attr_reader :responce
7
+
8
+ def initialize(res)
9
+ @responce = res
10
+ end
11
+
12
+ def status
13
+ responce.status
14
+ end
15
+
16
+ def message
17
+ msg = super
18
+ return msg unless msg.empty?
19
+ responce.body.to_param
20
+ end
21
+
22
+ def body
23
+ responce.body
24
+ end
25
+
26
+ def headers
27
+ responce.headers
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,2 @@
1
+ require_relative './api_error'
2
+ require_relative './cli_error'
@@ -0,0 +1,129 @@
1
+ module Idcf
2
+ module JsonHyperSchema
3
+ module Expands
4
+ # Link Info Base ext
5
+ module LinkInfoBaseExtension
6
+ # is async
7
+ #
8
+ # @return Boolean
9
+ def async?
10
+ !!@data.data['$async']
11
+ end
12
+
13
+ # result api link title
14
+ #
15
+ # @return [String]
16
+ def result_api_title
17
+ res = @data.data['$result']
18
+ res && res['title'] ? res['title'] : ''
19
+ end
20
+
21
+ # result api params
22
+ #
23
+ # @params args [Array]
24
+ # @params api_result [Hash]
25
+ # @params resource_id [String]
26
+ # @return [Array]
27
+ def result_api_params(args, api_result, resource_id = nil)
28
+ return [] if @data.data['$result'].nil?
29
+ arg = Marshal.load(Marshal.dump(args))
30
+ target = @data.data['$result']['params']
31
+ target = target.nil? ? [] : target
32
+ raise Idcf::Cli::Error::CliError, 'params is not Array' unless target.class == Array
33
+ result = make_result_api_params(target, arg, api_result, resource_id)
34
+ result
35
+ end
36
+
37
+ def make_uri_for_cli(args)
38
+ param_size = url_param_names.size
39
+ url_params = args.slice(0, param_size)
40
+ params = args[param_size]
41
+ make_uri(url_params, params)
42
+ end
43
+
44
+ def make_params_for_cli(args)
45
+ make_params(args[url_param_names.size])
46
+ end
47
+
48
+ protected
49
+
50
+ def make_result_api_params(target, args, api_result, resource_id)
51
+ case target
52
+ when Hash
53
+ result_api_hash(target, args, api_result, resource_id)
54
+ when Array
55
+ result_api_array(target, args, api_result, resource_id)
56
+ when String
57
+ result_api_str(target, args, api_result, resource_id)
58
+ else
59
+ target
60
+ end
61
+ end
62
+
63
+ def result_api_hash(target, args, api_result, resource_id)
64
+ {}.tap do |result|
65
+ target.each do |k, v|
66
+ result[k] = make_result_api_params(v, args, api_result, resource_id)
67
+ end
68
+ end
69
+ end
70
+
71
+ def result_api_array(target, args, api_result, resource_id)
72
+ [].tap do |result|
73
+ target.each do |v|
74
+ result << make_result_api_params(v, args, api_result, resource_id)
75
+ end
76
+ end
77
+ end
78
+
79
+ def result_api_str(target, args, api_result, resource_id)
80
+ return resource_id if target == '#{resource_id}'
81
+ result_api_str_regexp(target, args, api_result)
82
+ rescue => e
83
+ raise Idcf::Cli::Error::CliError, e.message
84
+ end
85
+
86
+ def result_api_str_regexp(target, args, api_result)
87
+ result = target
88
+ case target
89
+ when Regexp.new('\A#\{(.+)\}\Z')
90
+ result = args[url_param_names.index(Regexp.last_match(1))]
91
+ when Regexp.new('\Aresult\[(.*)\]\Z')
92
+ result = result_api_str_result(api_result, Regexp.last_match(1))
93
+ end
94
+ result
95
+ end
96
+
97
+ def result_api_str_result(data, path)
98
+ result = data.deep_dup
99
+ path.split('.').each do |k|
100
+ case result
101
+ when Array
102
+ result = result[k.to_i]
103
+ when Hash
104
+ raise Idcf::Cli::Error::CliError, 'not found result key' unless result.key?(k)
105
+ result = result[k]
106
+ end
107
+ end
108
+ result
109
+ end
110
+
111
+ def make_query_params(param)
112
+ {}.tap do |result|
113
+ next unless param.class == Hash
114
+ query_param_names.each do |k|
115
+ result[k] = param[k] if param.key?(k)
116
+ reg = Regexp.new("#{k}[\\.\\[].+")
117
+ param.keys.each do |pk|
118
+ result[pk] = param[pk] if (reg =~ pk).present?
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ link_base = Idcf::JsonHyperSchema::Expands::LinkInfoBase
129
+ link_base.prepend(Idcf::JsonHyperSchema::Expands::LinkInfoBaseExtension)