usd 0.1.5 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +20 -0
  3. data/README.md +22 -21
  4. data/bin/rusdc +205 -59
  5. data/change_log.md +76 -0
  6. data/lib/usd.rb +11 -5
  7. data/usd.gemspec +2 -2
  8. metadata +4 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eeb6a5b08aafdc142ff2fa07c8dcf1859ca4993a2e2749c44aec12bc4bd2488a
4
- data.tar.gz: 4d9a1b2f4dd5364b344f2e80f2e0e886698105734121a466e378a7710631f96e
3
+ metadata.gz: d3fcd67e1d03c8438152bd6a247c644d72b4d7300ae8e434d5e0920772df6473
4
+ data.tar.gz: 49289a822d2906027417ac0d56d621a1cd1b53766ff651b5069858a8bb221d6e
5
5
  SHA512:
6
- metadata.gz: a9f89726f11a5c925b63e21ed7a6ce9c7acbd3d7af152a1d80aae41afb1ceaa0cde4cee27ee0ccc50ebc7b787152a515b06157fbbe73f4536218759d5d47fb90
7
- data.tar.gz: 6cd80c617bb13cdf72e7720811f83632eaa4865c33181e43ea764a68a7a84b3ad3dc158d04de870b75a97d397010846248eb8bc962ef006acf8c11fc1dd4b83a
6
+ metadata.gz: 4e4f7fde898d1b6477b9c08e96811b738d45e40fffffd16f3c0d474c1629d3e2a0ba4079b453623917a1d111f11b6989e83378b63e728c334936c4ecce271f5b
7
+ data.tar.gz: b517f049992b629a745e23c0af441814a7f45486a9a0617b6a69ae319cd6c68df9cf0561ac1033d1d4bc63e045d281836067f40e24fc6ca6c2985d9f401967e0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2019 Oliver Gaida
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -8,7 +8,7 @@ There is a ruby class and a commandline tool rusdc.
8
8
  just install the gem:
9
9
 
10
10
  ```bash
11
- sudo gem install usd
11
+ gem install usd
12
12
  ```
13
13
 
14
14
  Be aware that you need some compiler-tools and the ruby headers:
@@ -27,34 +27,35 @@ If you use the `rusdc find` command with `--format mlr` option then you need `ml
27
27
 
28
28
  - [mlr - Miller](http://johnkerl.org/miller/doc/index.html) - a great tool for data-transforming to and from json, csv and many more
29
29
 
30
- Place the `mlr`-binary in a path, which is in your PATH-Environment.
30
+ Place the `mlr`-binary in a path, which is in your PATH-Environment. Download-Url for mlr-releases: [https://github.com/johnkerl/miller/releases](https://github.com/johnkerl/miller/releases)
31
31
 
32
32
  # functions from the commandline-tool `rusdc`
33
33
 
34
34
  ```
35
35
  rusdc
36
36
  Commands:
37
- rusdc chg_add_nr # <CO> <CI> - add a CI to a changeorder
38
- rusdc chg_list_nr # <CO> - list all CIs of one changeorder
39
- rusdc create # pipe json-data to create object
40
- rusdc field_names # <object-type> list all fields of an object including its format
41
- rusdc find # <object-type> <where-clause> [fields - Komma getrennt (id,name)] - finds each object which meets the wc-condition
42
- rusdc get # <object-type> <common_name> shows one object
43
- rusdc get_attachment_of_ci # <ci_name> <filename>, download an attachment of a CI and print it out on stdout
44
- rusdc get_attachment_of_co # <co_name> <filename>, download an attachment of a changeorder and print it out on stdout
45
- rusdc help [COMMAND] # Describe available commands or one specific command
46
- rusdc list_attachments_of_ci # <ci_name>, list all attachments of a CI
47
- rusdc list_attachments_of_co # <co_name>, list all attachments of a changeorder
48
- rusdc nr_add_child # <nr-name> <child-name>, add one child CI to another CI
49
- rusdc nr_changes # <nr> [inactive-too] - list all open changeorders of one CI
50
- rusdc nr_childs # <ci-name> , lists all childs CIs of a specific CI
51
- rusdc nr_incidents # <nr> [inactive-too] - lists all incident of a specific CI
52
- rusdc nr_parents # nr_parents <ci-name> , lists all parent CIs of a specific CI
53
- rusdc update # pipe json-data to update object
37
+ rusdc chg_add_nr <co> <ci> # add a CI to a changeorder
38
+ rusdc chg_list_nr <co> # list all CIs of one changeorder
39
+ rusdc create # pipe json-data to create object
40
+ rusdc field_names <object-type> # list all fields of an object including its format
41
+ rusdc find <object-type> [where-clause] [fields, comma separated] [options] # finds each object which meets the wc-condition
42
+ rusdc get <object-type> <common_name> # shows one object
43
+ rusdc get_attachment_of_ci <ci_name> <filename> # download an attachment of a CI and print it out on stdout
44
+ rusdc get_attachment_of_co <co_name> <filename> # download an attachment of a changeorder and print it out on stdout
45
+ rusdc help [COMMAND] # Describe available commands or one specific command
46
+ rusdc in_list_child_ins <in> # list all child-incidents of one incident
47
+ rusdc list_attachments_of_ci <ci_name> # list all attachments of a CI
48
+ rusdc list_attachments_of_co <co_name> # list all attachments of a changeorder
49
+ rusdc nr_add_child <nr-name> <child-name> # add one child CI to another CI
50
+ rusdc nr_changes <nr> [inactive-too] # list all open changeorders of one CI
51
+ rusdc nr_childs <ci-name> # lists all childs CIs of a specific CI
52
+ rusdc nr_incidents <nr> [inactive-too] # lists all incident of a specific CI
53
+ rusdc nr_parents <ci-name> # lists all parent CIs of a specific CI
54
+ rusdc update # pipe json-data to update object
55
+ rusdc update_attr <obj> <common_name> <key> <value> # updates a direct (not referenced) attribute of one object.
56
+ rusdc update_ref_attr <obj> <common_name> <key> <value> # updates a referenced attribute of one object.
54
57
  ```
55
58
 
56
- the german text will be translated soon.
57
-
58
59
  # environment-variables
59
60
 
60
61
  rusdc needs some environment-variables. there are:
data/bin/rusdc CHANGED
@@ -7,7 +7,7 @@ require "usd"
7
7
 
8
8
  class Rusdc < Thor
9
9
 
10
- desc "chg_add_nr", "<CO> <CI> - add a CI to a changeorder"
10
+ desc "chg_add_nr <co> <ci>", "add a CI to a changeorder"
11
11
  def chg_add_nr(co, ci)
12
12
  template = ERB.new <<-EOF
13
13
  {
@@ -25,9 +25,9 @@ class Rusdc < Thor
25
25
  puts loadcon.create({:type => "json", :data => json})
26
26
  end
27
27
 
28
- desc "chg_list_nr", "<CO> - list all CIs of one changeorder"
28
+ desc "chg_list_nr <co>", "list all CIs of one changeorder"
29
29
  def chg_list_nr(co)
30
- invoke 'find', ['lrel_asset_chgnr', %(chg.chg_ref_num = '#{co}'), "nr"], {:format => "mlr", :mlr_format => "xtab"}
30
+ invoke 'find', ['lrel_asset_chgnr', %(chg.chg_ref_num = '#{co}'), "nr"], {:format => "mlr"}
31
31
  end
32
32
 
33
33
  desc "create", "pipe json-data to create object"
@@ -35,64 +35,79 @@ class Rusdc < Thor
35
35
  puts loadcon.create({:type => "json", :data => STDIN.read})
36
36
  end
37
37
 
38
- desc "field_names","<object-type> list all fields of an object including its format"
39
- def field_names(object)
40
- e = loadcon.search(object,{'fields' => "*","start" => "1", "size" => "1"})[0]
38
+ desc "field_names <object-type> [wc]","list all fields of an object including its format"
39
+ def field_names(object,wc="")
40
+ e = loadcon.search(object,{'fields' => "*","wc"=>wc,"start" => "1", "size" => "1"})[0]
41
41
  e.keys.sort.each do |k|
42
- puts "#{k} (#{e[k].class})"
43
42
  if e[k].class == Hash
43
+ puts "#{k} (#{e[k].class}):"
44
44
  puts e[k].jp
45
+ else
46
+ puts "#{k} (#{e[k].class}) : #{e[k]}"
45
47
  end
46
48
  end
47
49
  end
48
50
 
49
- desc "find", "<object-type> <where-clause> [fields - Komma getrennt (id,name)] - finds each object which meets the wc-condition"
51
+ desc "find <object-type> [where-clause] [fields, comma separated] [options]", "finds each object which meets the wc-condition"
50
52
  long_desc <<-LONGDESC
51
53
 
52
54
 
53
- Mit --mlr_format kann man das output-Format von Miller (mlr) ändern auf csv, md (markdown), xtab (sehr übersichtlich), dkvp (Delimited key-value pairs) ..., siehe `mlr --help`
55
+ with --mlr_format you may change the output-format of Miller (mlr) to csv, md (markdown), xtab , dkvp (delimited key-value pairs) ..., refer `mlr --help`
56
+
57
+ Examples:
54
58
 
55
- Beispiele:
59
+ - all grc objects (! don't use this kind of query for objecttype like nr, in, chg etc., whose number of objects is too big)
60
+ rusdc find grc
56
61
 
57
- - alle aktiven CIs die mit sygosrv beginnen und die Zahl 58 enthalten:
58
- ./rusdc find nr "name like 'servername' and delete_flag.sym = 'Active' " "id,name" --format mlr
62
+ - all aktive ci which start with the string `servername`, only print the fields @id and name:
63
+ rusdc find nr "name like 'servername%' and delete_flag.sym = 'Active' " "@id,name" --format mlr
59
64
 
60
- - meine aktiven Incidents:
61
- ./rusdc find --format mlr in "assignee.last_name = 'gaida' and status.sym in ('Service pending','Work in progress.','Customer Pending','Open','Hold','Resolved')" ref_num,z_project,summary,status,call_back_date,affected_resource,orig_user_organization
65
+ - my active incidents with most importent fields:
66
+ rusdc find --format mlr in "assignee.last_name = 'gaida' and status.sym in ('Service pending','Work in progress.','Customer Pending','Open','Hold','Resolved')" ref_num,z_project,summary,status,call_back_date,affected_resource,orig_user_organization
62
67
 
63
- - Activity-Log eines Incidents:
64
- ./rusdc find alg "call_req_id.ref_num = 'I00001'" "call_req_id,action_desc,analyst,description,time_stamp,type" --format mlr --mlr_format xtab
68
+ - activity-Log of an incident in xtab format:
69
+ rusdc find alg "call_req_id.ref_num = 'I00001'" "call_req_id,action_desc,analyst,description,time_stamp,type" --format mlr --mlr_format xtab
65
70
 
66
- - nur die Beschreibungen Activity-Log eines Incidents:
67
- ./rusdc find alg "call_req_id.ref_num = 'I00001'" "description" --format mlr --mlr_format csv
71
+ - the same but only the field description in csv-format:
72
+ rusdc find alg "call_req_id.ref_num = 'I00001'" "description" --format mlr --mlr_format csv
68
73
 
69
- - Incidents einer Maschine
70
- ./rusdc find in "affected_resource.name = 'servername'" "ref_num,summary" --format mlr
74
+ - incidents of the ci with the name `servername`
75
+ rusdc find in "affected_resource.name = 'servername'" "ref_num,summary" --format mlr
71
76
 
72
- - Kombiniert, alle Activity-Log-Beschreibungen aller Incidents einer Maschine:
73
- ./rusdc find alg "call_req_id.affected_resource.name = 'servername'" "description" --format mlr
77
+ - combination of all activity-log-description of all incidents of a specific ci in pprint-Miller-format:
78
+ rusdc find alg "call_req_id.affected_resource.name = 'servername'" "description" --format mlr
74
79
 
75
- - Changes einer Maschine:
80
+ - changeorders of a secific ci:
76
81
  rusdc find chg "asset.nr.name = 'servername'" "@COMMON_NAME,summary,status" --format mlr
77
82
 
78
- - Alle Maschinen finden die mit goes beginnen und deren childs auf Linux und Windows prüfen:
83
+ - find all active ci whose name start with dc and check if they have children like Windows or Linux server:
79
84
  for ci in $(rusdc find nr "name like 'dc%' and delete_flag.sym = 'active'" "name" --format mlr | sed '1 d'); do rusdc nr_childs $ci > help ; echo "$ci: $(grep -iP '^(Linux|.*MS windows).*server' help)" ; done
80
85
 
81
86
  LONGDESC
82
- option :format, :type => :string, :default => "json"
83
- option :mlr_format, :type => :string, :default => "pprint"
84
- option :nice_time, :type => :boolean, :default => true
85
- option :debug, :type => :boolean, :default => false
86
- def find(object, wc, fields="*")
87
+ option :format, :type => :string, :default => "json", :banner => "[json|yaml|mlr]"
88
+ option :mlr_format, :type => :string, :default => "pprint",:banner => "[pprint|json|md|csv|xtab]"
89
+ option :nice_time, :type => :boolean, :default => true,:banner => "[true|false]"
90
+ option :debug, :type => :boolean, :default => false,:banner => "[true|false]"
91
+ def find(object, wc="", fields="@COMMON_NAME")
87
92
  unless (fields =~ /^[\w\_,@]+$/)
88
- puts "fields-problem: there must be only letters, numbers, comma and underscore, nothing else!"
93
+ puts "fields-problem: there must be only letters, numbers, comma, the @-sign or underscore, nothing else!"
89
94
  else
90
95
  con = loadcon
96
+ mlr_array = []
91
97
  con.debug = options[:debug]
92
98
  e = con.search(object,{'fields' => fields,"wc" => wc})
93
99
  fields_array=fields.split(/,/)
94
100
  e.each do |elem|
101
+ # init all requested keys with empty strings, better for mlr format and needed for fields-order
102
+ fields_array.each do |elem_key|
103
+ elem[elem_key]="" unless elem.has_key?(elem_key)
104
+ end
95
105
  elem.keys.each do |elem_key|
106
+ if options[:nice_time]
107
+ if elem_key =~ /(date|last_mod|warranty_start|warranty_end|time_stamp)$/
108
+ elem[elem_key]=Time.at(elem[elem_key]).to_s
109
+ end
110
+ end
96
111
  if fields_array.include?(elem_key)
97
112
  if elem[elem_key].class == Hash
98
113
  elem_value = elem[elem_key]["@COMMON_NAME"]
@@ -103,32 +118,21 @@ class Rusdc < Thor
103
118
  elem.delete(elem_key)
104
119
  end
105
120
  end
106
- # fill up not found keys
107
- fields_array.each do |elem_key|
108
- unless elem.has_key?(elem_key)
109
- elem[elem_key]=""
110
- end
111
- end
112
- end
113
- if options[:nice_time]
114
- e.each_index do |i|
115
- e[i].keys.each do |key|
116
- if key =~ /(date|last_mod|warranty_start|warranty_end|time_stamp)$/
117
- e[i][key]=Time.at(e[i][key]).to_s
118
- end
119
- end
120
- end
121
+ # sort the keys
122
+ mlr_array << Hash[fields_array.map {|k| [k, elem[k]]}].jp
121
123
  end
124
+ mlr = "[" + mlr_array.join(",") + "]"
122
125
  case options[:format]
123
126
  when "json"
124
- puts e.jp
127
+ puts mlr
125
128
  when "yaml"
129
+ # keys not sorted, maybe a todo
126
130
  puts e.to_yaml
127
131
  when "mlr"
128
132
  # hierfuer muss mlr verfuegbar sein
129
133
  file = Tempfile.new('json')
130
134
  cmd = %(mlr --ijson --o#{options[:mlr_format]} cat #{file.path})
131
- file.write(e.jp)
135
+ file.write(mlr)
132
136
  file.close
133
137
  puts `#{cmd}`
134
138
  file.unlink
@@ -138,10 +142,15 @@ class Rusdc < Thor
138
142
  end # fields check
139
143
  end
140
144
 
141
- desc "get", "<object-type> <common_name> shows one object"
145
+ desc "get <object-type> <common_name|id>", "shows one object by name or id"
146
+ option :by_id, :type => :boolean, :default => false
142
147
  option :yaml, :type => :boolean, :default => false
143
- def get(object, cn)
144
- e = loadcon.request("/caisd-rest/#{object}/COMMON_NAME-#{cn}")
148
+ def get(object, ident)
149
+ if options[:by_id] || ident =~ /^U'[0-9A-F]/i
150
+ e = loadcon.request("/caisd-rest/#{object}/#{ident}")
151
+ else
152
+ e = loadcon.request("/caisd-rest/#{object}/COMMON_NAME-#{ident}")
153
+ end
145
154
  if e.class == Hash
146
155
  puts options[:yaml] ? e[object].to_yaml : e[object].jp
147
156
  elsif e.message == "409 Conflict"
@@ -151,7 +160,23 @@ class Rusdc < Thor
151
160
  end
152
161
  end
153
162
 
154
- desc "get_attachment_of_ci", "<ci_name> <filename>, download an attachment of a CI and print it out on stdout"
163
+ desc "get_all_attachments_of_co <co_name>", "get all attachments of a changeorder and save all these to current folder"
164
+ def get_all_attachments_of_co(coname)
165
+ # todo : care more about eml-multipart: https://github.com/mikel/mail
166
+ chg_id=loadcon.request("/caisd-rest/chg/COMMON_NAME-#{coname}")["chg"]["@id"]
167
+ att_nr = loadcon.search("lrel_attachments_changes",{'fields' => "attmnt","wc" => "chg = #{chg_id}"})
168
+ att_nr.each do |att|
169
+ attmnt = loadcon.request("/caisd-rest/attmnt/COMMON_NAME-#{att["attmnt"]["@COMMON_NAME"]}")["attmnt"]
170
+ filename = attmnt["orig_file_name"]
171
+ puts "save #{filename}"
172
+ f = File.open(filename, 'w')
173
+ f.write(loadcon.request("/caisd-rest/attmnt/#{attmnt["@id"]}/file-resource",{:unchanged => true}))
174
+ f.close
175
+ end
176
+ puts "all attachments were saved"
177
+ end
178
+
179
+ desc "get_attachment_of_ci <ci_name> <filename>", "download an attachment of a CI and print it out on stdout"
155
180
  def get_attachment_of_ci(ciname, filename)
156
181
  att_nr = loadcon.search("lrel_attachments_nr",{'fields' => "attmnt","wc" => "nr.name = '#{ciname}'"})
157
182
  att_nr.each do |att|
@@ -162,7 +187,7 @@ class Rusdc < Thor
162
187
  end
163
188
  end
164
189
 
165
- desc "get_attachment_of_co", "<co_name> <filename>, download an attachment of a changeorder and print it out on stdout"
190
+ desc "get_attachment_of_co <co_name> <filename>", "download an attachment of a changeorder and print it out on stdout"
166
191
  def get_attachment_of_co(coname, filename)
167
192
  chg_id=loadcon.request("/caisd-rest/chg/COMMON_NAME-#{coname}")["chg"]["@id"]
168
193
  att_nr = loadcon.search("lrel_attachments_changes",{'fields' => "attmnt","wc" => "chg = #{chg_id}"})
@@ -174,7 +199,12 @@ class Rusdc < Thor
174
199
  end
175
200
  end
176
201
 
177
- desc "list_attachments_of_ci", "<ci_name>, list all attachments of a CI"
202
+ desc "in_list_child_ins <in>", "list all child-incidents of one incident"
203
+ def in_list_child_ins(incident)
204
+ invoke('find',['cr', "parent.ref_num = '#{incident}'", "ref_num"], {:format => "mlr"})
205
+ end
206
+
207
+ desc "list_attachments_of_ci <ci_name>", "list all attachments of a CI"
178
208
  def list_attachments_of_ci(ciname)
179
209
  att_nr = loadcon.search("lrel_attachments_nr",{'fields' => "attmnt","wc" => "nr.name = '#{ciname}'"})
180
210
  att_nr.each do |att|
@@ -182,7 +212,7 @@ class Rusdc < Thor
182
212
  end
183
213
  end
184
214
 
185
- desc "list_attachments_of_co", "<co_name>, list all attachments of a changeorder"
215
+ desc "list_attachments_of_co <co_name>", "list all attachments of a changeorder"
186
216
  def list_attachments_of_co(coname)
187
217
  chg_id=loadcon.request("/caisd-rest/chg/COMMON_NAME-#{coname}")["chg"]["@id"]
188
218
  att_nr = loadcon.search("lrel_attachments_changes",{'fields' => "attmnt","wc" => "chg = #{chg_id}"})
@@ -191,7 +221,7 @@ class Rusdc < Thor
191
221
  end
192
222
  end
193
223
 
194
- desc "nr_add_child", "<nr-name> <child-name>, add one child CI to another CI"
224
+ desc "nr_add_child <nr-name> <child-name>", "add one child CI to another CI"
195
225
  def nr_add_child(nr, child)
196
226
  template = ERB.new <<-EOF
197
227
  {
@@ -209,7 +239,7 @@ class Rusdc < Thor
209
239
  puts loadcon.create({:type => "json", :data => json})
210
240
  end
211
241
 
212
- desc "nr_changes", "<nr> [inactive-too] - list all open changeorders of one CI"
242
+ desc "nr_changes <nr> [inactive-too]", "list all open changeorders of one CI"
213
243
  option :inactive_too, :type => :boolean, :default => false
214
244
  def nr_changes(nr)
215
245
  wc_add = (options[:inactive_too] ? "" : " and status.sym in ('Service pending','Work in progress','Customer Pending','Open','Hold')")
@@ -217,20 +247,20 @@ class Rusdc < Thor
217
247
  invoke 'find', ['chg', wc, 'chg_ref_num,summary,status'], {:format => "mlr"}
218
248
  end
219
249
 
220
- desc "nr_childs", "<ci-name> , lists all childs CIs of a specific CI"
250
+ desc "nr_childs <ci-name>", "lists all childs CIs of a specific CI"
221
251
  def nr_childs(name)
222
252
  loadcon.search("hier",{'fields' => "*","wc" => "parent.name = '#{name}'"}).each do |c|
223
253
  puts c["child"]["@COMMON_NAME"]
224
254
  end
225
255
  end
226
256
 
227
- desc "nr_incidents", "<nr> [inactive-too] - lists all incident of a specific CI"
257
+ desc "nr_incidents <nr> [inactive-too]", "lists all incident of a specific CI"
228
258
  option :inactive_too, :type => :boolean, :default => false
229
259
  def nr_incidents(nr)
230
260
  invoke 'find', ['in', %(affected_resource.name = '#{nr}'#{options[:inactive_too] ? "" : " and not status.sym in ('Cancelled','Closed')"}), "ref_num,summary,status"], {:format => "mlr"}
231
261
  end
232
262
 
233
- desc "nr_parents", "nr_parents <ci-name> , lists all parent CIs of a specific CI"
263
+ desc "nr_parents <ci-name>", "lists all parent CIs of a specific CI"
234
264
  def nr_parents(name)
235
265
  loadcon.search("hier",{'fields' => "*","wc" => "child.name = '#{name}'"}).each do |c|
236
266
  puts c["parent"]["@COMMON_NAME"]
@@ -250,6 +280,122 @@ class Rusdc < Thor
250
280
  puts loadcon.update({:type => "json", :data => STDIN.read})
251
281
  end
252
282
 
283
+ desc "update_attr <obj> <common_name|wc> <key> <value>", "updates a direct (not referenced) attribute of one or more objects."
284
+ option :plain_text, :type => :boolean, :default => false
285
+ def update_attr(obj, cn, k, v)
286
+ if k =~ /(date|last_mod|warranty_start|warranty_end|time_stamp)$/
287
+ v=Time.parse(v).to_i
288
+ end
289
+ if cn =~ /([<>=]| like )/
290
+ obj_arr=loadcon.search(obj,{'fields' => "@id","wc"=>"#{cn} and delete_flag.sym = 'Active'"})
291
+ obj_arr.each do |inner_obj|
292
+ inner_id=inner_obj["@id"]
293
+ template = ERB.new <<-EOF
294
+ {
295
+ "<%= obj %>": {
296
+ "@id": "<%= id %>",
297
+ "<%= k %>": "<%= v %>"
298
+ }
299
+ }
300
+ EOF
301
+ json = template.result_with_hash({
302
+ :obj => obj,
303
+ :id => inner_id,
304
+ :k => k,
305
+ :v => v
306
+ })
307
+ puts loadcon.update({:type => "json", :data => json})
308
+ end
309
+ else
310
+ if options[:plain_text]
311
+ data={
312
+ obj => {
313
+ "@COMMON_NAME" => cn,
314
+ k => v
315
+ }
316
+ }
317
+ json = JSON.pretty_generate(data)
318
+ else
319
+ template = ERB.new <<-EOF
320
+ {
321
+ "<%= obj %>": {
322
+ "@COMMON_NAME": "<%= cn %>",
323
+ "<%= k %>": "<%= v %>"
324
+ }
325
+ }
326
+ EOF
327
+ json = template.result_with_hash({
328
+ :obj => obj,
329
+ :cn => cn,
330
+ :k => k,
331
+ :v => v
332
+ })
333
+ end
334
+ puts loadcon.update({:type => "json", :data => json})
335
+ end
336
+ end
337
+
338
+ desc "update_attr_by_id <obj> <id> <key> <value>", "updates a plain attribute of one object by id, does not work"
339
+ def update_attr_by_id(obj, id, k, v)
340
+ template = ERB.new <<-EOF
341
+ {
342
+ "<%= obj %>": {
343
+ "@id": "<%= id %>",
344
+ "<%= k %>": "<%= v %>"
345
+ }
346
+ }
347
+ EOF
348
+ json = template.result_with_hash({
349
+ :obj => obj,
350
+ :id => id,
351
+ :k => k,
352
+ :v => v
353
+ })
354
+ puts loadcon.update({:type => "json", :data => json})
355
+ end
356
+
357
+ desc "update_ref_attr <obj> <common_name> <key> <value>", "updates a referenced attribute of one object."
358
+ def update_ref_attr(obj, cn, k, v)
359
+ template = ERB.new <<-EOF
360
+ {
361
+ "<%= obj %>": {
362
+ "@COMMON_NAME": "<%= cn %>",
363
+ "<%= k %>": {
364
+ "@COMMON_NAME": "<%= v %>"
365
+ }
366
+ }
367
+ }
368
+ EOF
369
+ json = template.result_with_hash({
370
+ :obj => obj,
371
+ :cn => cn,
372
+ :k => k,
373
+ :v => v
374
+ })
375
+ puts loadcon.update({:type => "json", :data => json})
376
+ end
377
+
378
+ desc "update_ref_attr_by_id <obj> <id> <key> <value>", "updates a referenced attribute of one object by id"
379
+ def update_ref_attr_by_id(obj, id, k, v)
380
+ template = ERB.new <<-EOF
381
+ {
382
+ "<%= obj %>": {
383
+ "@id": "<%= id %>",
384
+ "<%= k %>": {
385
+ "@id": "<%= v %>"
386
+ }
387
+ }
388
+ }
389
+ EOF
390
+ json = template.result_with_hash({
391
+ :obj => obj,
392
+ :id => id,
393
+ :k => k,
394
+ :v => v
395
+ })
396
+ puts loadcon.update({:type => "json", :data => json})
397
+ end
398
+
253
399
 
254
400
  private
255
401
 
@@ -0,0 +1,76 @@
1
+ # changelog
2
+
3
+ ## 0.2.1
4
+
5
+ - function `update_attr` has a new option for putting plain text with linebreaks in it.
6
+
7
+ - new funciton `update_ref_attr_by_id` sometimes there are problem if you try to update via common_name, so here you can use the `@id` instead.
8
+
9
+ - `rusdc get` now supports id too
10
+
11
+ - rusdc field_names , now there is a second parameter [wc]. So that's a way to show all fields of a special opbject.
12
+
13
+ Example:
14
+
15
+ ```
16
+ rusdc field_names wf "@id = 1535123" | head -3
17
+ @COMMON_NAME (String) :
18
+ @REL_ATTR (String) : 1535123
19
+ @id (String) : 1535123
20
+ ```
21
+
22
+ ## 0.1.9
23
+
24
+ - function update_attr will parse with Time-Module if the key match the regular expression: `(date|last_mod|warranty_start|warranty_end|time_stamp)`. And then the value will be change to epoche-seconds
25
+
26
+ Example:
27
+
28
+ ```
29
+ rusdc update_attr chg CO000001 call_back_date "2020-01-01 11:11:11 +0100"
30
+ ```
31
+
32
+ - new function in rusdc `get_all_attachments_of_co`. In the first implementation it will save all attachments with its original name in the current folder. existing files may be overwritten. Caution!
33
+
34
+ ## 0.1.8
35
+
36
+ - new function in rusdc `update_attr`
37
+
38
+ Example for `update_attr`:
39
+
40
+ ```bash
41
+ > rusdc help update_attr
42
+ Usage:
43
+ rusdc update_attr <obj> <common_name> <key> <value>
44
+
45
+ updates a direct (not referenced) attribute of one object.
46
+
47
+ > rusdc get cnt 'gaida, oliver' | grep alt_phone
48
+ "alt_phone": "",
49
+ > rusdc update_attr cnt 'gaida, oliver' alt_phone "+49123456789"
50
+ > rusdc get cnt 'gaida, oliver' | grep alt_phone
51
+ "alt_phone": "+49123456789",
52
+ ```
53
+
54
+ - new function in rusdc `update_ref_attr`
55
+
56
+ Example for `update_ref_attr`:
57
+
58
+ ```bash
59
+ > rusdc help update_ref_attr
60
+ Usage:
61
+ rusdc update_ref_attr <obj> <common_name> <key> <value>
62
+
63
+ updates a referenced attribute of one object.
64
+ > rusdc find chg "chg_ref_num = 'CO000001'" status --format mlr
65
+ status
66
+ Work in progress
67
+ > rusdc update_ref_attr chg CO000001 status "Service pending"
68
+ > rusdc find chg "chg_ref_num = 'CO000001'" status --format mlr
69
+ status
70
+ Service pending
71
+ ```
72
+
73
+ ## 0.1.7
74
+
75
+ - fix sorting the output of `rusdc find`
76
+ - add new rusdc function `rusdc in_list_child_ins`
data/lib/usd.rb CHANGED
@@ -21,8 +21,9 @@ class Usd
21
21
  @base_url = base_url
22
22
  @user = user
23
23
  @debug = false
24
- if File.exist?(".usd") and hash[:save_access_key]
25
- tt = YAML.load(File.open(".usd","r"))
24
+ remfile = "#{ENV["HOME"]}/.usd"
25
+ if File.exist?(remfile) and hash[:save_access_key]
26
+ tt = YAML.load(File.open(remfile,"r"))
26
27
  if (tt.expiration_date - Time.now.to_i ) > 900 and tt.base_url == base_url
27
28
  @access_key = tt.access_key
28
29
  @expiration_date = tt.expiration_date
@@ -56,7 +57,7 @@ class Usd
56
57
  @access_key = authData['rest_access']['access_key']
57
58
  @expiration_date = authData['rest_access']['expiration_date']
58
59
  if hash[:save_access_key]
59
- f=File.open(".usd","w")
60
+ f=File.open(remfile,"w")
60
61
  f.puts self.to_yaml
61
62
  f.close
62
63
  end
@@ -146,8 +147,13 @@ class Usd
146
147
  end
147
148
  puts "update - data: #{JSON.pretty_generate(data)}" if @debug
148
149
  object = data.keys[0]
149
- cn = data[object]["@COMMON_NAME"]
150
- request("/caisd-rest/#{object}/COMMON_NAME-#{cn}",{:method => "put", :json => data.to_json, :header => header({'X-Obj-Attrs' => 'COMMON_NAME'})})
150
+ if data[object].has_key?("@id")
151
+ request("/caisd-rest/#{object}/#{data[object]["@id"]}",{:method => "put", :json => data.to_json, :header => header({'X-Obj-Attrs' => 'COMMON_NAME'})})
152
+ elsif data[object].has_key?("@COMMON_NAME")
153
+ request("/caisd-rest/#{object}/COMMON_NAME-#{data[object]["@COMMON_NAME"]}",{:method => "put", :json => data.to_json, :header => header({'X-Obj-Attrs' => 'COMMON_NAME'})})
154
+ else
155
+ puts "specify @COMMON_NAME or @id at least."
156
+ end
151
157
  end
152
158
 
153
159
  def set_url_parm(params_hash,attribute_name,default)
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'usd'
3
- spec.version = '0.1.5'
4
- spec.date = '2019-10-31'
3
+ spec.version = '0.2.2'
4
+ spec.date = '2019-11-26'
5
5
  spec.summary = "SDM REST-API-Calls"
6
6
  spec.description = "a Ruby class and a commandlinetool for SDM REST-API-Calls"
7
7
  spec.authors = ["Oliver Gaida"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oliver Gaida
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-31 00:00:00.000000000 Z
11
+ date: 2019-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -77,9 +77,11 @@ executables:
77
77
  extensions: []
78
78
  extra_rdoc_files: []
79
79
  files:
80
+ - LICENSE.md
80
81
  - README.md
81
82
  - bin/rusdc
82
83
  - bin/set_env
84
+ - change_log.md
83
85
  - lib/usd.rb
84
86
  - usd.gemspec
85
87
  homepage: https://github.com/ogaida/usd