tng-gtk-utils 0.4.1 → 0.5.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
- SHA256:
3
- metadata.gz: 49c965e9e3a31a115b50d4e553c9b287e9289e9cc0de312882ef5fa762254ede
4
- data.tar.gz: 290a0a066ee706e752791a5c748c6338b6f14a7889dec2398a17168c11a7c546
2
+ SHA1:
3
+ metadata.gz: 4d95e0157be5e4a3b5b90f31492a469ab218b13b
4
+ data.tar.gz: c60b428906b6bed69e473067b36976ed178c82f5
5
5
  SHA512:
6
- metadata.gz: 1677767e5d19e1ff4990a7c98a8e8ed96c1b37dfd72eab55a3c322cccc52edc1424af9456ead6fd4e839af6f81acc9c34d14fdba7bdc3951c379296774c1a2a7
7
- data.tar.gz: 82eb5dc8ff9b5f67f0232854f90e429f95af431ec48df5222a28263ac7f7e51cd2362191b251571ccbfe02fc48fc73845285a5203bccf702305fb20eb1d0449c
6
+ metadata.gz: d7de031d9e209a0f98a00191636145d9594d7464597b8eb52830920f9136a652dc1b13c82f8f7fd87620e4f32633cc1515e0c8783b193956063096e16797e14e
7
+ data.tar.gz: 84d72ce21457caf7e09e9250b4f7f55e43fd41cdd33c2a2b30bc8e465435fdb035445096af3ceaeb321d6abb9f459ab08075784203e112b67ea5ea796e8dfbbc
@@ -41,8 +41,6 @@ module Tng
41
41
  class ApplicationController < Sinatra::Base
42
42
  LOGGER=Tng::Gtk::Utils::Logger
43
43
  LOGGED_COMPONENT=self.name
44
- @@began_at = Time.now.utc
45
- LOGGER.info(component:LOGGED_COMPONENT, operation:'initializing', start_stop: 'START', message:"Started at #{@@began_at}")
46
44
 
47
45
  register Sinatra::ConfigFile
48
46
  register Sinatra::CrossOrigin
@@ -54,7 +52,6 @@ module Tng
54
52
  set :environment, ENV.fetch('RACK_ENV', :development)
55
53
 
56
54
  before { content_type :json}
57
- LOGGER.info(component:LOGGED_COMPONENT, operation:'initializing', start_stop: 'STOP', message:"Ended at #{Time.now.utc}", time_elapsed:"#{Time.now.utc-began_at}")
58
55
  end
59
56
  end
60
57
  end
@@ -75,28 +75,23 @@ module Tng
75
75
  class << self; attr_accessor :strategy; end
76
76
 
77
77
  self.strategy = ENV['REDIS_URL'] ? STRATEGIES[:redis] : STRATEGIES[:memory]
78
- LOGGER.info(start_stop: 'START', component:LOG_COMPONENT, operation:'class definition', message:"Strategy used: #{self.strategy}")
79
78
 
80
79
  def self.cache(record)
81
80
  unless record.key?(:uuid)
82
81
  LOGGER.error(component: LOG_COMPONENT, operation:__method__.to_s, message:"key :uuid is missing in record #{record}")
83
82
  return nil
84
83
  end
85
- LOGGER.info(component: LOG_COMPONENT, operation:__method__.to_s, message:"key '#{CACHE_PREFIX}:#{record[:uuid]}' with value '#{record}' (strategy #{self.strategy})")
86
84
  self.strategy.set("#{CACHE_PREFIX}:#{record[:uuid]}", record.to_json)
87
85
  record
88
86
  end
89
87
  def self.cached?(key)
90
- LOGGER.info(component: LOG_COMPONENT, operation:__method__.to_s, message:"key '#{CACHE_PREFIX}:#{key}' (strategy #{self.strategy})")
91
88
  data = self.strategy.get("#{CACHE_PREFIX}:#{key}")
92
89
  return '' if data.nil?
93
90
  JSON.parse(data, symbolize_names: :true)
94
91
  end
95
92
  def self.clear(key)
96
- LOGGER.info(component: LOG_COMPONENT, operation:__method__.to_s, message:"key '#{key}' (strategy #{self.strategy})")
97
93
  self.strategy.del(key)
98
94
  end
99
- LOGGER.info(start_stop: 'STOP', component:LOG_COMPONENT, operation:'class definition', message:"Strategy used: #{self.strategy}")
100
95
  end
101
96
  end
102
97
  end
@@ -49,7 +49,6 @@ module Tng
49
49
  def self.call(params)
50
50
  msg=self.name+'#'+__method__.to_s
51
51
  began_at=Time.now.utc
52
- LOGGER.info(start_stop: 'START', component:self.name, operation:__method__.to_s, message:"params=#{params} site=#{self.site}")
53
52
  original_params = params.dup
54
53
  begin
55
54
  if params.key?(:uuid)
@@ -66,21 +65,16 @@ module Tng
66
65
  uri = URI.parse(self.site)
67
66
  uri.query = URI.encode_www_form(sanitize(params))
68
67
  end
69
- LOGGER.debug(component:self.name, operation:__method__.to_s, message:"uri=#{uri}")
70
68
  request = Net::HTTP::Get.new(uri)
71
69
  request['content-type'] = 'application/json'
72
70
  response = Net::HTTP.start(uri.hostname, uri.port) {|http| http.request(request)}
73
- LOGGER.debug(component:self.name, operation:__method__.to_s, message:"response=#{response.inspect}")
74
71
  case response
75
72
  when Net::HTTPSuccess
76
73
  body = response.read_body
77
- LOGGER.debug(component:self.name, operation:__method__.to_s, message:"body=#{body}", status: '200')
78
74
  result = JSON.parse(body, quirks_mode: true, symbolize_names: true)
79
75
  cache_result(result)
80
- LOGGER.info(start_stop: 'STOP', component:self.name, operation:__method__.to_s, message:"result=#{result} site=#{self.site}", time_elapsed: Time.now.utc - began_at)
81
76
  return result
82
77
  when Net::HTTPNotFound
83
- LOGGER.info(start_stop: 'STOP', component:self.name, operation:__method__.to_s, message:"body=#{body}", status:'404', time_elapsed: Time.now.utc - began_at)
84
78
  return {} unless uuid.nil?
85
79
  return []
86
80
  else
@@ -101,15 +95,11 @@ module Tng
101
95
  end
102
96
 
103
97
  def self.cache_result(result)
104
- LOGGER.debug(component:self.name, operation:__method__.to_s, message:"result=#{result}")
105
98
  if result.is_a?(Hash)
106
- LOGGER.info(component:self.name, operation:__method__.to_s, message:"Caching #{result}")
107
99
  Tng::Gtk::Utils::Cache.cache(result)
108
100
  return
109
101
  end
110
- LOGGER.info(component:self.name, operation:__method__.to_s, message:"#{result} is not an Hash")
111
102
  result.each do |record|
112
- LOGGER.info(component:self.name, operation:__method__.to_s, message:"Caching #{record}")
113
103
  Tng::Gtk::Utils::Cache.cache(record)
114
104
  end
115
105
  end
@@ -37,11 +37,11 @@ require 'tng/gtk/utils/application_controller'
37
37
  require 'tng/gtk/utils/logger'
38
38
  require 'tng/gtk/utils/fetch'
39
39
 
40
- LOGGER=Tng::Gtk::Utils::Logger
41
40
 
42
41
  class FetchVNFDsService < Tng::Gtk::Utils::Fetch
43
42
  NO_CATALOGUE_URL_DEFINED_ERROR='The CATALOGUE_URL ENV variable needs to defined and pointing to the Catalogue where to fetch functions'
44
43
  LOGGED_COMPONENT=self.name
44
+ LOGGER=Tng::Gtk::Utils::Logger
45
45
 
46
46
  CATALOGUE_URL = ENV.fetch('CATALOGUE_URL', '')
47
47
  if CATALOGUE_URL == ''
@@ -49,61 +49,41 @@ class FetchVNFDsService < Tng::Gtk::Utils::Fetch
49
49
  raise ArgumentError.new(NO_CATALOGUE_URL_DEFINED_ERROR)
50
50
  end
51
51
  self.site=CATALOGUE_URL+'/vnfs'
52
- LOGGER.info(component:LOGGED_COMPONENT, operation:'site definition', message:"self.site=#{self.site}")
53
52
  end
54
53
 
55
54
  class FunctionsController < Tng::Gtk::Utils::ApplicationController
56
55
 
57
56
  ERROR_FUNCTION_NOT_FOUND="No function with UUID '%s' was found"
58
57
  LOGGED_COMPONENT=self.name
59
- @@began_at = Time.now.utc
60
- LOGGER.info(component:LOGGED_COMPONENT, operation:'initializing', start_stop: 'START', message:"Started at #{@@began_at}")
61
58
 
62
59
  get '/?' do
63
60
  msg='#get (many)'
64
- began_at = Time.now.utc
65
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'START', message:"Started at #{began_at}")
66
61
  captures=params.delete('captures') if params.key? 'captures'
67
- LOGGER.debug(component:LOGGED_COMPONENT, operation:msg, message:"params=#{params}")
68
62
  result = FetchVNFDsService.call(symbolized_hash(params))
69
- LOGGER.debug(component:LOGGED_COMPONENT, operation:msg, message:"result=#{result}")
70
63
  if result.to_s.empty? # covers nil
71
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'STOP', message:"Ended at #{Time.now.utc}", status: '404', time_elapsed:"#{Time.now.utc-began_at}")
72
64
  halt 404, {}, {error: "No functions fiting the provided parameters ('#{params}') were found"}.to_json
73
65
  end
74
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'STOP', message:"Ended at #{Time.now.utc}", status: '200', time_elapsed:"#{Time.now.utc-began_at}")
75
66
  halt 200, {}, result.to_json
76
67
  end
77
68
 
78
69
  get '/:function_uuid/?' do
79
70
  msg='#get (single)'
80
- began_at = Time.now.utc
81
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'START', message:"Started at #{began_at}")
82
71
  captures=params.delete('captures') if params.key? 'captures'
83
- LOGGER.debug(component:LOGGED_COMPONENT, operation:msg, message:"params['function_uuid']='#{params['function_uuid']}'")
84
72
  result = FetchVNFDsService.call(uuid: params['function_uuid'])
85
- LOGGER.debug(component:LOGGED_COMPONENT, operation:msg, message:"result=#{result}")
86
73
  if result == {}
87
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'STOP', message:"Ended at #{Time.now.utc}", status: '404', time_elapsed:"#{Time.now.utc-began_at}")
88
74
  halt 404, {}, {error: ERROR_FUNCTION_NOT_FOUND % params['function_uuid']}.to_json
89
75
  end
90
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'STOP', message:"Ended at #{Time.now.utc}", status: '200', time_elapsed:"#{Time.now.utc-began_at}")
91
76
  halt 200, {}, result.to_json
92
77
  end
93
78
 
94
79
  options '/?' do
95
80
  msg='#options'
96
- began_at = Time.now.utc
97
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'START', message:"Started at #{began_at}")
98
81
  response.headers['Access-Control-Allow-Origin'] = '*'
99
82
  response.headers['Access-Control-Allow-Methods'] = 'GET,DELETE'
100
83
  response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With'
101
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'STOP', message:"Ended at #{Time.now.utc}", status: '200', time_elapsed:"#{Time.now.utc-began_at}")
102
84
  halt 200
103
85
  end
104
86
 
105
- LOGGER.info(component:LOGGED_COMPONENT, operation:'initializing', start_stop: 'STOP', message:"Ending at #{Time.now.utc}", time_elapsed: Time.now.utc - @@began_at)
106
-
107
87
  private
108
88
  def uuid_valid?(uuid)
109
89
  return true if (uuid =~ /[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}/) == 0
@@ -39,7 +39,6 @@ module Tng
39
39
  class Logger
40
40
  LOGFILE = ENV.fetch('LOGFILE', STDOUT)
41
41
  LOGLEVEL = ENV.fetch('LOGLEVEL', 'info')
42
- #LOGGER_LEVELS = ['debug', 'info', 'warn', 'error', 'fatal', 'unknown'].freeze
43
42
  LOGGER_LEVELS = ['D', 'I', 'W', 'E', 'F', 'U'].freeze
44
43
 
45
44
  class << self
@@ -66,13 +65,13 @@ module Tng
66
65
  def generic(type:, start_stop:, component:, operation:, message:, status:, time_elapsed:)
67
66
  message = {
68
67
  type: type, # mandatory, can be I(nfo), W(arning), D(ebug), E(rror), F(atal) or U(nknown)
69
- timestamp: Time.now.utc, # mandatory
70
- start_stop: start_stop, # optional, can be empty, 'START' or 'STOP'
71
- component: component, # mandatory
72
- operation: operation, # mandatory
73
- message: message, # mandatory
74
- status: status, # optional, makes sense for start_stop='END'
75
- time_elapsed: time_elapsed # optional, makes sense for start_stop='END'
68
+ app_timestamp: Time.now.utc.strftime("%Y-%m-%e %H:%M:%S.%6N %Z" ), # mandatory, with miliseconds
69
+ start_stop: start_stop, # optional, can be empty, 'START' or 'STOP'
70
+ component: component, # mandatory
71
+ operation: operation, # mandatory
72
+ message: message, # mandatory
73
+ status: status, # optional, makes sense for start_stop='END'
74
+ time_elapsed: time_elapsed # optional, makes sense for start_stop='END'
76
75
  }
77
76
  LOGFILE.puts "#{message.to_json}" if log?(type)
78
77
  message
@@ -86,47 +85,3 @@ module Tng
86
85
  end
87
86
  end
88
87
  end
89
- =begin
90
- LOGLEVEL
91
- Unknown: a message that should always be logged, whatever the logging level set;
92
- Fatal: an unhandleable error ocurred;
93
- Error: a handleable error occurred. The service the component delivers should not be interrupted (i.e., it should be able to recover from the error);
94
- Warning: a warning message;
95
- Info: a generic (but useful) information about system operation;
96
- Debug: a low-level information for developers;
97
-
98
- {
99
- "type": "I", # mandatory, can be I(nfo), W(arning), D(ebug), E(rror), F(atal) or U(nknown)
100
- "timestamp": "2018-10-18 15:49:08 UTC", # mandatory
101
- "start_stop": "END", # optional, can be empty, 'START' or 'STOP'
102
- "component": "tng-api-gtw", # mandatory
103
- "operation": "package upload", # mandatory
104
- "message": "package uploaded 201", # mandatory
105
- "status": "201", # optional, makes sense for start_stop='END'
106
- "time_elapsed": "00:01:20" # optional, makes sense for start_stop='END'
107
- }
108
-
109
- LOGGER_LEVELS = ['debug', 'info', 'warn', 'error', 'fatal', 'unknown'].freeze
110
- FORMAT = %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f\n}
111
-
112
- def initialize(app, options = {})
113
- @app = app
114
- @error_io = options[:logger_io]
115
- @logger = Logger.new(@error_io)
116
- @error_io.sync = true
117
- @logger_level = LOGGER_LEVELS.find_index(options[:logger_level].downcase ||= 'debug')
118
- end
119
-
120
-
121
- def call(env)
122
- msg = self.class.name+'#'+__method__.to_s
123
- env['5gtango.error']=@error_io
124
- env['5gtango.logger']=@logger
125
- @logger.info(msg) {"Called"}
126
-
127
- status, headers, body = @app.call(env)
128
- @logger.info(msg) {"Finishing with status #{status}"}
129
- [status, headers, body]
130
- end
131
-
132
- =end
@@ -40,11 +40,10 @@ require 'tng/gtk/utils/application_controller'
40
40
  require 'tng/gtk/utils/logger'
41
41
  require 'tng/gtk/utils/fetch'
42
42
 
43
- LOGGER=Tng::Gtk::Utils::Logger
44
-
45
43
  class FetchNSDService < Tng::Gtk::Utils::Fetch
46
44
  NO_CATALOGUE_URL_DEFINED_ERROR='The CATALOGUE_URL ENV variable needs to defined and pointing to the Catalogue where to fetch services'
47
45
  LOGGED_COMPONENT=self.name
46
+ LOGGER=Tng::Gtk::Utils::Logger
48
47
 
49
48
  CATALOGUE_URL = ENV.fetch('CATALOGUE_URL', '')
50
49
  if CATALOGUE_URL == ''
@@ -52,7 +51,6 @@ class FetchNSDService < Tng::Gtk::Utils::Fetch
52
51
  raise ArgumentError.new(NO_CATALOGUE_URL_DEFINED_ERROR)
53
52
  end
54
53
  self.site=CATALOGUE_URL+'/network-services'
55
- LOGGER.info(component:LOGGED_COMPONENT, operation:'site definition', message:"self.site=#{self.site}")
56
54
  end
57
55
 
58
56
  class ServicesController < Tng::Gtk::Utils::ApplicationController
@@ -60,55 +58,35 @@ class ServicesController < Tng::Gtk::Utils::ApplicationController
60
58
  ERROR_SERVICE_NOT_FOUND="No service with UUID '%s' was found"
61
59
  LOGGER=Tng::Gtk::Utils::Logger
62
60
  LOGGED_COMPONENT=self.name
63
-
64
- @@began_at = Time.now.utc
65
- LOGGER.info(component:LOGGED_COMPONENT, operation:'initializing', start_stop: 'START', message:"Started at #{@@began_at}")
66
61
 
67
62
  get '/?' do
68
63
  msg='#get (many)'
69
- began_at = Time.now.utc
70
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'START', message:"Started at #{began_at}")
71
64
  captures=params.delete('captures') if params.key? 'captures'
72
- LOGGER.debug(component:LOGGED_COMPONENT, operation:msg, message:"params=#{params}")
73
65
  result = FetchNSDService.call(symbolized_hash(params))
74
- LOGGER.debug(component:LOGGED_COMPONENT, operation:msg, message:"result=#{result}")
75
66
  if result.to_s.empty? # covers nil
76
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'STOP', message:"Ended at #{Time.now.utc}", status: '404', time_elapsed:"#{Time.now.utc-began_at}")
77
67
  halt 404, {}, {error: "No packages fiting the provided parameters ('#{params}') were found"}.to_json
78
68
  end
79
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'STOP', message:"Ended at #{Time.now.utc}", status: '200', time_elapsed:"#{Time.now.utc-began_at}")
80
69
  halt 200, {}, result.to_json
81
70
  end
82
71
 
83
72
  get '/:uuid/?' do
84
73
  msg='#get (single)'
85
- began_at = Time.now.utc
86
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'START', message:"Started at #{began_at}")
87
74
  captures=params.delete('captures') if params.key? 'captures'
88
- LOGGER.debug(component:LOGGED_COMPONENT, operation:msg, message:"params['uuid']='#{params['uuid']}'")
89
75
  result = FetchNSDService.call(symbolized_hash(params))
90
- LOGGER.debug(component:LOGGED_COMPONENT, operation:msg, message:"result=#{result}")
91
76
  if result.to_s.empty? # covers nil
92
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'STOP', message:"Ended at #{Time.now.utc}", status: '404', time_elapsed:"#{Time.now.utc-began_at}")
93
77
  halt 404, {}, {error: ERROR_SERVICE_NOT_FOUND % params[:uuid]}.to_json
94
78
  end
95
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'STOP', message:"Ended at #{Time.now.utc}", status: '200', time_elapsed:"#{Time.now.utc-began_at}")
96
79
  halt 200, {}, result.to_json
97
80
  end
98
81
 
99
82
  options '/?' do
100
83
  msg='#options'
101
- began_at = Time.now.utc
102
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'START', message:"Started at #{began_at}")
103
84
  response.headers['Access-Control-Allow-Origin'] = '*'
104
85
  response.headers['Access-Control-Allow-Methods'] = 'GET,DELETE'
105
86
  response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With'
106
- LOGGER.info(component:LOGGED_COMPONENT, operation:msg, start_stop: 'STOP', message:"Ended at #{Time.now.utc}", status: '200', time_elapsed:"#{Time.now.utc-began_at}")
107
87
  halt 200
108
88
  end
109
89
 
110
- LOGGER.info(component:LOGGED_COMPONENT, operation:'initializing', start_stop: 'STOP', message:"Ending at #{Time.now.utc}", time_elapsed: Time.now.utc - @@began_at)
111
-
112
90
  private
113
91
  def uuid_valid?(uuid)
114
92
  return true if (uuid =~ /[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}/) == 0
@@ -1,7 +1,7 @@
1
1
  module Tng
2
2
  module Gtk
3
3
  module Utils
4
- VERSION = "0.4.1"
4
+ VERSION = "0.5.1"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tng-gtk-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - José Bonnet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-28 00:00:00.000000000 Z
11
+ date: 2019-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -150,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
150
  version: '0'
151
151
  requirements: []
152
152
  rubyforge_project:
153
- rubygems_version: 2.7.7
153
+ rubygems_version: 2.5.2.3
154
154
  signing_key:
155
155
  specification_version: 4
156
156
  summary: A small library with utility features for 5GTANGO Gatekeeper