vzcdn 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9553f5ffbf91a52d204bb02bb1ee54279d79cc5c
4
- data.tar.gz: 77b658f0f08c733c4911d64c060edb852bb4d3a3
3
+ metadata.gz: c28ee09c103b4974e38dba49773a359e40ce951c
4
+ data.tar.gz: 5d0d324809e812c3ae1f72ade9eec631b9208ef7
5
5
  SHA512:
6
- metadata.gz: 19ac2c9d596ee6c4b053037971b9b821e8ae46d84af885751fcb1343646bb4e5223f0230c8a4566f312d4dc2e1ed5c05c6a8ddad76cb85ea7a510d196c38b5b6
7
- data.tar.gz: a8b69bac8d65998236be840021784a53dda7d37bacb6683a8393b5f0c424caac32f9e5fa6c26c4063b01764a6d3f7a896eb287a7e788a3eb63c30edf5ae988e7
6
+ metadata.gz: bd6e695da176af1359ecd409a4d8f40b4ae5c3fc0e6af82060004ae7c3f6910b110d9a508977f396db78b82f48cad43ef10504e85a7ce1f5719c56f6e6234831
7
+ data.tar.gz: 7eeb4be4cf567de6ea2eab1e103c62d5897b09527c7d570b005c1c50b39ee3473301bed9d84310a48495e569517e99a1880fe8560bd571bae42e8aa9e3b163a7
data/bin/ec ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'vzcdn'
4
+ require 'vzcdn/version'
5
+
6
+ if ARGV[0] == "-v"
7
+ puts "vzcdn version:" + Vzcdn::VERSION
8
+ exit
9
+ end
10
+
11
+ args = []
12
+ ARGV.each { |arg|
13
+ args << arg
14
+ }
15
+
16
+ VzcdnApp.invoke(args)
data/bin/vzcdn CHANGED
@@ -1,10 +1,16 @@
1
- #!user/bin/env/ruby
1
+ #!/usr/bin/env ruby
2
2
 
3
3
  require 'vzcdn'
4
+ require 'vzcdn/version'
5
+
6
+ if ARGV[0] == "-v"
7
+ puts "vzcdn version:" + Vzcdn::VERSION
8
+ exit
9
+ end
4
10
 
5
11
  args = []
6
12
  ARGV.each { |arg|
7
13
  args << arg
8
14
  }
9
15
 
10
- Vzcdn.invoke(args)
16
+ VzcdnApp.invoke(args)
@@ -0,0 +1,280 @@
1
+ require_relative 'route'
2
+
3
+ class Arg
4
+ attr_accessor :desc, :required, :default, :validator, :name, :next_node, :value
5
+
6
+ def initialize(name, hash={ })
7
+ @name = name
8
+ hash.each {|k,v| instance_variable_set("@#{k}",v)}
9
+ end
10
+
11
+ # return true on success
12
+ # error message on failure
13
+
14
+ def validate?(value)
15
+ vclass = @validator.class
16
+ if vclass == Regexp
17
+ test = (value =~ @validator)
18
+ on_error = "Invalid Format for " + @name
19
+ elsif vclass == Array
20
+ test = @validator.include? value
21
+ on_error = "Invalid choice for " + @name + ", must be one of " + @validator.to_s
22
+ elsif vclass == Hash
23
+ test = @validator.has_key? value
24
+ on_error = "Invalid choice for " + @name + ", must be one of " + @validator.keys.to_s
25
+ elsif vclass == String
26
+ result = {}
27
+ Route.new.send("get_available_" + @validator).each { |hash|
28
+ result[hash["Name"]] = hash["Id"]
29
+ }
30
+ @validator = result
31
+ test = validate? value
32
+ if (test.class == String)
33
+ on_error = test
34
+ test = false
35
+ end
36
+ else
37
+ test = false
38
+ on_error = "unknown type of validator:" + @validator.to_s + ", class=" + @validator.class.to_s
39
+ end
40
+ if test
41
+ true
42
+ else
43
+ on_error
44
+ end
45
+ end
46
+ end
47
+
48
+ class Decision
49
+ attr_accessor :flow
50
+
51
+ def initialize(decider, *symbols)
52
+ @decider = decider
53
+ @tree = { }
54
+ symbols.each { |symbol|
55
+ @tree[symbol] = Flow.new(nil)
56
+ }
57
+ end
58
+
59
+ def chosen
60
+ vars = @flow.find(self)
61
+ arr = @decider.decide(vars, @tree.keys).map { |symbol|
62
+ @tree[symbol].root
63
+ }
64
+ arr
65
+ end
66
+
67
+ def children
68
+ @tree.values
69
+ end
70
+
71
+ def add(child, arg)
72
+ @tree[child].add arg
73
+ end
74
+
75
+ def to_s
76
+ result = "branch names:"
77
+ @tree.keys.each { |child|
78
+ result = result + child.to_s + " "
79
+ }
80
+ result += "\nbranches are of classes:"
81
+ @tree.keys.each { |child|
82
+ result = result + @tree[child].class.to_s + " "
83
+ }
84
+ result
85
+ end
86
+ end
87
+
88
+ class Flow
89
+ attr_reader :root
90
+
91
+ def initialize(root)
92
+ @root = root
93
+ end
94
+
95
+ def iargs(node, args)
96
+ if node.nil?
97
+ return args
98
+ end
99
+ if (node.class == Arg)
100
+ if node.value
101
+ new_args = args.clone
102
+ new_args[node.name] = node.value
103
+ args = new_args
104
+ end
105
+ iargs(node.next_node, args)
106
+ elsif (node.class == Decision)
107
+ node.chosen.each { |child|
108
+ return iargs(child, args)
109
+ }
110
+ end
111
+ end
112
+
113
+ def ifind(elt, node, args)
114
+ if node.nil?
115
+ return nil
116
+ end
117
+ if (node == elt)
118
+ return args
119
+ elsif (node.class == Arg)
120
+ if node.value
121
+ new_args = args.clone
122
+ new_args[node.name] = node.value
123
+ args = new_args
124
+ end
125
+ ifind(elt, node.next_node, args)
126
+ elsif (node.class == Decision)
127
+ found = false
128
+ node.children.each { |child|
129
+ result = ifind(elt, child, args)
130
+ if (result)
131
+ return result
132
+ end
133
+ }
134
+ return nil
135
+ end
136
+ end
137
+
138
+
139
+ def find(elt)
140
+ return ifind(elt, @root, {})
141
+ end
142
+
143
+ # return true for success
144
+ # false for unable to find arg name
145
+ # String for invalid arg
146
+
147
+ def iset(name, value, node)
148
+ if node.nil?
149
+ return false
150
+ end
151
+ if (node.class == Arg )
152
+ if (node.name == name)
153
+ response = node.validate?(value)
154
+ if (response.class == String)
155
+ return response
156
+ else
157
+ node.value = value
158
+ return true
159
+ end
160
+ else
161
+ iset(name, value, node.next_node)
162
+ end
163
+ elsif (node.class == Decision)
164
+ result = false
165
+ node.children.each { |flow|
166
+ response = iset(name, value, flow.root)
167
+ if (response == true)
168
+ result = true
169
+ elsif (response.class == String)
170
+ return response
171
+ end
172
+ }
173
+ result
174
+ end
175
+ end
176
+
177
+ def set(name, value)
178
+ response = iset(name, value, @root)
179
+ if (response == false)
180
+ puts "ERROR: could not find argument with name " + name
181
+ elsif (response.class == String)
182
+ puts "ERROR: " + response
183
+ end
184
+ response
185
+ end
186
+
187
+ def iadd(arg, node)
188
+ if node.next_node.nil?
189
+ node.next_node = arg
190
+ else
191
+ iadd(arg, node.next_node)
192
+ end
193
+ end
194
+
195
+ def add(arg)
196
+ if @root.nil?
197
+ @root = arg
198
+ else
199
+ iadd(arg, @root)
200
+ end
201
+ end
202
+
203
+ def to_s
204
+ args = iargs(@root, {})
205
+ args.each { |name, value|
206
+ puts name + ":" + value
207
+ }
208
+ end
209
+
210
+ def print_flow(node)
211
+ if node.nil?
212
+ return
213
+ end
214
+ result = node.name + " - " + node.desc
215
+ if (node.value)
216
+ result += ":" + node.value
217
+ end
218
+ puts result
219
+ print_flow(node.next_node)
220
+ end
221
+ end
222
+
223
+
224
+ def get_named(array, name)
225
+ array.each { |elt|
226
+ if elt.name == name
227
+ return elt
228
+ end
229
+ }
230
+ end
231
+
232
+ class HTTPDecider
233
+ def decide(args, children)
234
+ result = children
235
+ if (args.has_key? "type")
236
+ result =
237
+ if (args["type"] == "CNAME")
238
+ [:url]
239
+ else
240
+ [:ip]
241
+ end
242
+ end
243
+ result
244
+ end
245
+ end
246
+
247
+ http_decider = HTTPDecider.new
248
+
249
+ flow = Flow.new(Arg.new("root", desc: "description of method"))
250
+ flow.add(Arg.new("ttl", desc: "time to live in seconds", required: true, validator: /\A\d+\Z/))
251
+ flow.add(Arg.new("ipv", desc: "ip version", required: true, validator: "ip_versions"))
252
+ flow.add(Arg.new("type", desc: "record type", required: true, validator: ["A", "AAAA", "CNAME"]))
253
+ http_decision = Decision.new(http_decider, :ip, :url)
254
+ flow.add(http_decision)
255
+ http_decision.add(:ip, Arg.new("ip", desc: "IP address", required: false, validator: /\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z/))
256
+ http_decision.add(:url, Arg.new("url", desc: "URL of cname", required: false, validator: /.*/))
257
+ email = Arg.new("email", desc: "Email Address", required: false, validator: /.*/)
258
+ http_decision.add(:url, email)
259
+ http_decision.add(:ip, email)
260
+
261
+ http_decision.flow = flow
262
+
263
+ puts flow.to_s
264
+
265
+ def parse_line(line)
266
+ idx = line.index('=')
267
+ if (idx)
268
+ return line[0...idx], line[idx+1..-1]
269
+ else
270
+ line
271
+ end
272
+ end
273
+
274
+ ARGV.each { |arg|
275
+ name, value = parse_line(arg)
276
+ flow.set(name,value)
277
+ }
278
+
279
+ puts "after args:"
280
+ puts flow.to_s
@@ -1,59 +1,11 @@
1
1
  require 'fileutils'
2
2
  require_relative 'command_proc'
3
3
 
4
- class Configure
4
+ class ConfigCommand
5
5
  include CommandProc
6
6
 
7
7
  def initialize(name)
8
- @usage = {
9
- :init => "Initializes config file with <acct-num> and <token>.",
10
- :get => "get a configuration parameter.",
11
- :set => "set a configuration parameter."
12
- }
13
- end
14
-
15
- CONFIG_DIRECTORY = ".vdms.d"
16
- CONFIG_FILENAME = "config"
17
-
18
- def get_config_file
19
- if (! File.directory?(CONFIG_DIRECTORY))
20
- FileUtils.mkdir(CONFIG_DIRECTORY)
21
- end
22
- CONFIG_DIRECTORY + File::SEPARATOR + CONFIG_FILENAME
23
- end
24
-
25
- def parse_line(line)
26
- idx = line.index('=')
27
- if (idx)
28
- return line[0...idx], line[idx+1..-1]
29
- else
30
- line
31
- end
32
- end
33
-
34
- def replace_nv(name, value, new_file, old_file_contents)
35
- new_config_line = name + '=' + value
36
- wrote_line = false
37
- old_file_contents.each { |line|
38
- old_name, old_value = parse_line(line)
39
- if (old_name == name)
40
- new_file.puts(new_config_line)
41
- wrote_line = true
42
- else
43
- new_file.puts(line)
44
- end
45
- }
46
- if ( ! wrote_line)
47
- new_file.puts(new_config_line)
48
- end
49
- end
50
-
51
- def file_contents(filename)
52
- if (File.file? filename)
53
- File.open(filename, "r")
54
- else
55
- []
56
- end
8
+ @handler = ConfigHandler.new
57
9
  end
58
10
 
59
11
  def init(args)
@@ -61,42 +13,18 @@ class Configure
61
13
  puts "usage: init <account-num> <token>"
62
14
  puts "go to my.edgecast.com --> Settings to find it"
63
15
  else
64
- set(["acct-num", args[0]])
65
- set(["token", args[1]])
66
- set(["rest_base_url", "https://api.edgecast.com/v2/"])
16
+ @handler.set("acct-num", args[0])
17
+ @handler.set("token", args[1])
18
+ @handler.set("rest_base_url", "https://api.edgecast.com/v2/")
67
19
  end
68
20
  end
69
21
 
70
- def load_config_values
71
- if defined? @@config_values
72
- return
73
- else
74
- @@config_values = {}
75
- if (! File.file?(get_config_file))
76
- return
77
- end
78
- File.open(get_config_file).each { |line|
79
- line = line.strip
80
- if line[0] == '#'
81
- continue
82
- end
83
- name, value = parse_line(line)
84
- if value
85
- @@config_values[name] = value
86
- end
87
- }
88
- end
89
- end
90
-
91
- def self.config_parameter(name)
92
- instance = get_instance(nil)
93
- instance.load_config_values
94
- result = @@config_values[name]
95
- result = result ? result : ""
96
- end
97
-
98
22
  def get(args)
99
-
23
+ if (args.length != 1)
24
+ puts "usage: get <name>"
25
+ error
26
+ end
27
+ puts @handler.get(args[0])
100
28
  end
101
29
 
102
30
  def set(args)
@@ -104,13 +32,14 @@ class Configure
104
32
  puts "usage: set <name> <value>"
105
33
  error
106
34
  end
107
- name = args[0]
108
- value = args[1]
109
- config_file = get_config_file
110
- new_config_file = config_file + ".tmp"
111
- updated_file = File.open(new_config_file, "w")
112
- replace_nv(name, value, updated_file, file_contents(config_file))
113
- updated_file.close
114
- FileUtils.mv(new_config_file, config_file, :force => true)
35
+ @handler.set(args[0], args[1])
36
+ end
37
+
38
+ def delete(args)
39
+ if (args.length != 1)
40
+ puts "usage: delete <name>"
41
+ error
42
+ end
43
+ @handler.delete(args[0])
115
44
  end
116
45
  end
@@ -1,8 +1,4 @@
1
- require_relative 'config_reader'
2
-
3
1
  module CommandProc
4
- include ConfigReader
5
-
6
2
  def help(args)
7
3
  puts "Available commands:"
8
4
  @usage.each { |method|
@@ -48,7 +44,8 @@ module CommandProc
48
44
  else
49
45
  name = command
50
46
  instance = get_instance(name)
51
- puts "instance is " + instance.to_s
47
+ command = args.shift
48
+ instance.send(command, args)
52
49
  end
53
50
  end
54
51
 
@@ -1,7 +1,9 @@
1
1
  require 'rest_client'
2
2
  require 'json'
3
+ require 'config_reader'
3
4
 
4
5
  class CommonRestUtils
6
+ include ConfigReader
5
7
  def callMethod(url_suffix, http_method, body=nil)
6
8
  url = @uri_prefix + url_suffix
7
9
  if (body)
@@ -40,12 +42,12 @@ class CommonRestUtils
40
42
 
41
43
  def initialize(uri_prefix)
42
44
  @uri_prefix = uri_prefix
43
- proxy = Configure.config_parameter("proxy")
45
+ proxy = param("proxy")
44
46
  if proxy != ""
45
47
  RestClient.proxy = proxy
46
48
  end
47
49
  @headers = {:Content_type => "Application/json",
48
- :Authorization => "TOK:" + Configure.config_parameter("token"),
50
+ :Authorization => "TOK:" + param("token"),
49
51
  :Accept => "Application/json"}
50
52
  end
51
53
 
@@ -0,0 +1,94 @@
1
+ class ConfigHandler
2
+ CONFIG_DIRECTORY = ".vdms.d"
3
+ CONFIG_FILENAME = "config"
4
+
5
+ def get_config_file
6
+ if (! File.directory?(CONFIG_DIRECTORY))
7
+ FileUtils.mkdir(CONFIG_DIRECTORY)
8
+ end
9
+ CONFIG_DIRECTORY + File::SEPARATOR + CONFIG_FILENAME
10
+ end
11
+
12
+ def parse_line(line)
13
+ idx = line.index('=')
14
+ if (idx)
15
+ return line[0...idx], line[idx+1..-1]
16
+ else
17
+ line
18
+ end
19
+ end
20
+
21
+ def replace_nv(name, value, new_file, old_file_contents)
22
+ new_config_line = name + '=' + value
23
+ wrote_line = false
24
+ old_file_contents.each { |line|
25
+ old_name, old_value = parse_line(line)
26
+ if (old_name == name)
27
+ if ! value.nil?
28
+ new_file.puts(new_config_line)
29
+ end
30
+ wrote_line = true
31
+ else
32
+ new_file.puts(line)
33
+ end
34
+ }
35
+ if ( ! wrote_line)
36
+ new_file.puts(new_config_line)
37
+ end
38
+ end
39
+
40
+ def file_contents(filename)
41
+ if (File.file? filename)
42
+ File.open(filename, "r")
43
+ else
44
+ []
45
+ end
46
+ end
47
+
48
+ def load_config_values
49
+ if defined? @@config_values
50
+ return
51
+ else
52
+ @@config_values = {}
53
+ if (! File.file?(get_config_file))
54
+ return
55
+ end
56
+ File.open(get_config_file).each { |line|
57
+ line = line.strip
58
+ if line[0] == '#'
59
+ continue
60
+ end
61
+ name, value = parse_line(line)
62
+ if value
63
+ @@config_values[name] = value
64
+ end
65
+ }
66
+ end
67
+ end
68
+
69
+ def self.config_parameter(name)
70
+ instance = ConfigHandler.new
71
+ instance.load_config_values
72
+ result = @@config_values[name]
73
+ result = result ? result : ""
74
+ end
75
+
76
+ def get(name)
77
+ load_config_values
78
+ @@config_values[name]
79
+ end
80
+
81
+ def set(name, value)
82
+ config_file = get_config_file
83
+ new_config_file = config_file + ".tmp"
84
+ updated_file = File.open(new_config_file, "w")
85
+ replace_nv(name, value, updated_file, file_contents(config_file))
86
+ updated_file.close
87
+ FileUtils.mv(new_config_file, config_file, :force => true)
88
+ end
89
+
90
+ def delete(name)
91
+ set(name, nil)
92
+ end
93
+
94
+ end
@@ -1,5 +1,6 @@
1
+ require_relative 'config_handler'
1
2
  module ConfigReader
2
3
  def param(name)
3
- Configure.config_parameter(name)
4
+ ConfigHandler.config_parameter(name)
4
5
  end
5
6
  end
@@ -1,12 +1,24 @@
1
1
  #!/usr/bin/env ruby
2
- require_relative 'common.rb'
2
+ require_relative 'config_reader'
3
+ require_relative 'common'
3
4
  require 'pp'
4
5
 
5
6
  class Route
6
7
  include ConfigReader
7
8
 
9
+ @@instance = nil
10
+
11
+ def self.do
12
+ if @@instance
13
+ @@instance
14
+ else
15
+ @@instance = Route.new
16
+ end
17
+ end
18
+
8
19
  def initialize
9
20
  @common = CommonRestUtils.new(param("rest_base_url") + "mcc/")
21
+ @availables = { }
10
22
  end
11
23
 
12
24
  def customer_suffix(suffix)
@@ -31,52 +43,85 @@ class Route
31
43
 
32
44
  def get_zone(id, name)
33
45
  suffix = "dns/routezone?id=" + id + "&name=" + name
34
- callMethod(customer_suffix(suffix), :get)
46
+ @common.callMethod(customer_suffix(suffix), :get)
35
47
  end
36
48
 
37
49
  def copy_zone(from, to)
38
50
  suffix = customer_suffix("dns/routezone/copy")
39
51
  body = {:FromDomainName => from, :ToDomainName => to}
40
- callMethod(suffix, :post, body)
52
+ @common.callMethod(suffix, :post, body)
41
53
  end
42
54
 
43
55
  def delete_zone(id)
44
56
  suffix = customer_suffix("dns/routezone/" + id)
45
- callMethod(suffix, :delete)
57
+ @common.callMethod(suffix, :delete)
46
58
  end
47
59
 
48
60
  def add_zone()
49
61
  end
50
62
 
63
+ def xlate_name(symbol, name)
64
+ get_available(symbol).each { |hash|
65
+ if hash["Name"] == name
66
+ return hash["Id"]
67
+ end
68
+ }
69
+ end
70
+
71
+ def xlate_id(symbol, id)
72
+ get_available(symbol).each { |hash|
73
+ if hash["Id"] == id
74
+ return hash["Name"]
75
+ end
76
+ }
77
+ end
78
+
79
+ def get_available(data)
80
+ if @availables[data]
81
+ @availables[data]
82
+ else
83
+ suffix =
84
+ {:zone_type => "routezonetypes",
85
+ :record_type => "routezonerecordtypes",
86
+ :zone_status => "routezonestatus",
87
+ :route_group => "routegrouptypes",
88
+ :http_method => "routehttpmethodtypes",
89
+ :ip_version => "routehealthcheckipversion",
90
+ :hc_type => "routehealthchecktypes",
91
+ :hc_rm => "routereintegrationmethodtypes"}[data]
92
+ @availables[data] = @common.callMethod(non_customer_suffix(suffix), :get)
93
+ end
94
+ end
95
+
51
96
  def get_available_zone_types()
52
- callMethod(non_customer_suffix("routezonetypes"), :get)
97
+ get_available(:zone_type)
53
98
  end
54
99
 
55
100
  def get_available_record_types()
56
- callMethod(non_customer_suffix("routezonerecordtypes"), :get)
101
+ get_available(:record_type, "routezonerecordtypes")
57
102
  end
58
103
 
59
104
  def get_available_zone_statuses()
60
- callMethod(non_customer_suffix("routezonestatus"), :get)
105
+ get_available(:zone_status, "routezonestatus")
61
106
  end
62
107
 
63
108
  def get_available_route_groups()
64
- callMethod(non_customer_suffix("routegrouptypes"), :get)
109
+ get_available(:route_group, "routegrouptypes")
65
110
  end
66
111
 
67
112
  def get_available_http_methods()
68
- callMethod(non_customer_suffix("routehttpmethodtypes"), :get)
113
+ get_available(:http_method, "routehttpmethodtypes")
69
114
  end
70
115
 
71
116
  def get_available_ip_versions()
72
- callMethod(non_customer_suffix("routehealthcheckipversion"), :get)
117
+ get_available(:ip_version, "routehealthcheckipversion")
73
118
  end
74
119
 
75
- def get_available_health_check_types()
76
- callMethod(non_customer_suffix("routehealthchecktypes"), :get)
120
+ def get_available_hc_types()
121
+ get_available(:hc_type, "routehealthchecktypes")
77
122
  end
78
123
 
79
- def get_available_health_check_reintegration_methods()
80
- callMethod(non_customer_suffix("routereintegrationmethodtypes"), :get)
124
+ def get_available_hc_rm()
125
+ get_available(:hc_rm, "routereintegrationmethodtypes")
81
126
  end
82
127
  end
@@ -3,7 +3,7 @@ require_relative 'command_proc'
3
3
  require_relative 'clui_config'
4
4
  require_relative 'zone'
5
5
 
6
- class Vzcdn
6
+ class VzcdnApp
7
7
  include CommandProc
8
8
 
9
9
  def initialize(name)
@@ -11,6 +11,8 @@ class Vzcdn
11
11
  :zone => "zone related command",
12
12
  :config => "config related command"
13
13
  }
14
+ @argtree = [ {:name => "zone", :usage => "zone related commands", :args=>[]},
15
+ {:name => "config", :usage => "config related commands", :args=>[]}]
14
16
  end
15
17
 
16
18
  def zone(args)
@@ -18,7 +20,7 @@ class Vzcdn
18
20
  end
19
21
 
20
22
  def config(args)
21
- Configure.invoke(args)
23
+ ConfigCommand.invoke(args)
22
24
  end
23
25
  end
24
26
 
@@ -1,3 +1,3 @@
1
1
  module Vzcdn
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -4,38 +4,156 @@ class Zone
4
4
  include CommandProc
5
5
 
6
6
  def initialize(name)
7
+ @lines = nil
7
8
  @zname = name
8
9
  @usage = { :list => "list zones"}
9
-
10
+ @argflows = [ {:name => "list", :usage => "list zones", :args=>[]},
11
+ {:name => "add", :usage => "", :args=>[]}]
10
12
  end
11
13
 
12
14
  def list(args)
13
15
  if @zname
14
16
  puts "unimplemented"
15
17
  else
16
- zones = Route.new.get_all_zones
17
- lines = []
18
- lines << ["ZONE", "ID"]
18
+ zones = Route.do.get_all_zones
19
+ pp_table_start("ZONE", "ID", "STATUS")
19
20
  zones.each {|zone|
20
- lines << [zone["DomainName"], zone["ZoneId"].to_s]
21
+ pp_table_append(zone["DomainName"], zone["ZoneId"].to_s, Route.do.xlate_id(:zone_status, zone["Status"]))
22
+ }
23
+ pp_table
24
+ end
25
+ end
26
+
27
+ def structure_print(struct, args, header)
28
+ if (struct.class == Array)
29
+ # puts "printing array of " + struct.length.to_s + " elts"
30
+ struct.each { |elt|
31
+ # puts "calling struct_print with header = " + header.to_s + ", and elt = " + elt.to_s
32
+ structure_print(elt, args, header)
33
+ header = false
34
+ }
35
+ elsif (struct.class == Hash)
36
+ if (args.length == 0)
37
+ # print top level elements of hash
38
+ if (header)
39
+ pp_table_start_cols struct.keys
40
+ end
41
+ values = []
42
+ struct.keys.each { |key|
43
+ value = struct[key]
44
+ if (value.class == Array)
45
+ value = value.length.to_s + " elements"
46
+ elsif (value.class == String)
47
+ elsif (value.class == Fixnum)
48
+ value = value.to_s
49
+ elsif (value.class == Hash)
50
+ value = "object"
51
+ else
52
+ value = "<" + value.class.to_s + ">"
53
+ end
54
+ values << value
21
55
  }
22
- pp_table lines
56
+ pp_table_append_cols(values)
57
+ else
58
+ key = args.shift
59
+ structure_print(struct[key], args, header)
60
+ end
61
+ else
62
+ if (args.length == 0)
63
+ puts struct.to_s
64
+ else
65
+ puts "unknown class <" + struct.class.to_s + ">"
66
+ end
67
+ end
68
+ end
69
+
70
+ def print(args)
71
+ if @zname
72
+ if @zname =~ /\d+/
73
+ zone = Route.do.get_zone(@zname, "")
74
+ else
75
+ zone = Route.do.get_zone("", @zname)
76
+ end
77
+ if (args.length > 0 && args[0] == "-l")
78
+ puts JSON.pretty_generate(zone)
79
+ else
80
+ structure_print(zone, args, true)
81
+ pp_table
82
+ end
83
+ else
84
+ puts "pull command needs zone name or id"
85
+ end
86
+ end
87
+
88
+ def add(args)
89
+ if @zname
90
+ puts "unimplemented"
91
+ else
92
+ @zone = {
93
+ :Comment => "",
94
+ :DomainName => @zname,
95
+ :FailoverGroups => [ ],
96
+ :LoadBalancingGroups => [ ],
97
+ :Records => null,
98
+ :Status => 1,
99
+ :ZoneType => 1
100
+ }
101
+
23
102
  end
24
103
  end
25
104
 
26
- def pp_table(lines)
27
- ncols = lines[0].length
105
+ def char_repeat(n, char)
106
+ result = ""
107
+ (1..n).each { |i|
108
+ result = result + char
109
+ }
110
+ result
111
+ end
112
+
113
+ def spaces(n)
114
+ char_repeat(n, ' ')
115
+ end
116
+
117
+ def pp_table_start_cols(titles)
118
+ @lines = []
119
+ @lines << titles
120
+ separator = []
121
+ titles.each { |title|
122
+ separator << char_repeat(title.length, '-')
123
+ }
124
+ @lines << separator
125
+ end
126
+
127
+ def pp_table_start(*titles)
128
+ pp_table_start_cols(titles)
129
+ end
130
+
131
+ def pp_table_append_cols(columns)
132
+ @lines << columns
133
+ end
134
+
135
+ def pp_table_append(*columns)
136
+ @lines << columns
137
+ end
138
+
139
+ def pp_table
140
+ if @lines.nil?
141
+ return
142
+ end
143
+ ncols = @lines[0].length
28
144
  maxwidths = Array.new(ncols, 0)
29
- lines.each { |line|
145
+ @lines.each { |line|
30
146
  line.each_with_index { |col, index|
31
- maxwidths[index] = [col.length, maxwidths[index]].max
147
+ maxwidths[index] = [col.to_s.length, maxwidths[index]].max
32
148
  }
33
149
  }
34
- lines.each { |line|
150
+ @lines.each { |line|
151
+ output = ""
35
152
  line.each_with_index {|col, index|
36
- printf("%#{maxwidths[index]}s ", col)
37
- }
38
- printf("\n")
153
+ output = output + col.to_s + spaces(maxwidths[index] - col.to_s.length + 2)
154
+ }
155
+ puts output
39
156
  }
157
+ @lines = nil
40
158
  end
41
159
  end
@@ -1,8 +1,28 @@
1
1
  require 'clui_config'
2
+ require 'route'
2
3
  require 'test/unit'
4
+ require 'tmpdir'
5
+ require 'tempfile'
3
6
 
4
7
  class TestClui < Test::Unit::TestCase
5
8
  def test_config
6
- CluiConfig.set("aname", "anothervalue")
9
+ Dir.mktmpdir do |dir|
10
+ Dir.chdir(dir)
11
+ `ec config init BED3 e22db149-7369-4d7b-9c53-38d38746ccea`
12
+ assert(File.directory?(".vdms.d"), "missing .vdms.d directory")
13
+ assert(File.exists?(".vdms.d/config"), "missing config file")
14
+ assert_equal("BED3", `ec config get acct-num`.chomp, "get account-num failed")
15
+ end
16
+ end
17
+ def ctest_availables
18
+ route = Route.new
19
+ puts "zone types:" + route.get_available_zone_types.to_s
20
+ puts "record types:" + route.get_available_record_types.to_s
21
+ puts "zone status:" + route.get_available_zone_statuses.to_s
22
+ puts "route groups:" + route.get_available_route_groups.to_s
23
+ puts "http:" + route.get_available_http_methods.to_s
24
+ puts "ip vers:" + route.get_available_ip_versions.to_s
25
+ puts "hc types:" + route.get_available_health_check_types.to_s
26
+ puts "hc reints:" + route.get_available_health_check_reintegration_methods.to_s
7
27
  end
8
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vzcdn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Preston
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-03-31 00:00:00.000000000 Z
13
+ date: 2014-04-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client
@@ -60,6 +60,7 @@ email:
60
60
  - thomas.goes@verizon.com
61
61
  - nitin.khanna@verizon.com
62
62
  executables:
63
+ - ec
63
64
  - vzcdn
64
65
  extensions: []
65
66
  extra_rdoc_files: []
@@ -68,10 +69,13 @@ files:
68
69
  - LICENSE.txt
69
70
  - README.md
70
71
  - Rakefile
72
+ - bin/ec
71
73
  - bin/vzcdn
74
+ - lib/args.rb
72
75
  - lib/clui_config.rb
73
76
  - lib/command_proc.rb
74
77
  - lib/common.rb
78
+ - lib/config_handler.rb
75
79
  - lib/config_reader.rb
76
80
  - lib/mediaManagment.rb
77
81
  - lib/realTimeStats.rb