Ilya Tyaptin | aeff755 | 2016-11-17 15:57:36 +0000 | [diff] [blame] | 1 | -- Copyright 2016 Mirantis, Inc. |
| 2 | -- |
| 3 | -- Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | -- you may not use this file except in compliance with the License. |
| 5 | -- You may obtain a copy of the License at |
| 6 | -- |
| 7 | -- http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | -- |
| 9 | -- Unless required by applicable law or agreed to in writing, software |
| 10 | -- distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | -- See the License for the specific language governing permissions and |
| 13 | -- limitations under the License. |
| 14 | |
| 15 | local cjson = cjson |
| 16 | local string = string |
Ilya Tyaptin | aeff755 | 2016-11-17 15:57:36 +0000 | [diff] [blame] | 17 | local setmetatable = setmetatable |
| 18 | local ipairs = ipairs |
| 19 | local pairs = pairs |
| 20 | local pcall = pcall |
| 21 | local type = type |
| 22 | |
| 23 | local patt = require 'patterns' |
| 24 | local utils = require 'lma_utils' |
| 25 | local l = require 'lpeg' |
| 26 | l.locale(l) |
| 27 | |
| 28 | function normalize_uuid(uuid) |
| 29 | return patt.Uuid:match(uuid) |
| 30 | end |
| 31 | |
Ilya Tyaptin | aeff755 | 2016-11-17 15:57:36 +0000 | [diff] [blame] | 32 | local ResourcesDecoder = {} |
| 33 | ResourcesDecoder.__index = ResourcesDecoder |
| 34 | |
| 35 | setfenv(1, ResourcesDecoder) -- Remove external access to contain everything in the module |
| 36 | |
| 37 | function normalize_uuid(uuid) |
| 38 | return patt.Uuid:match(uuid) |
| 39 | end |
| 40 | |
| 41 | -- Mapping table defining transformation functions to be applied, keys are the |
| 42 | -- attributes in the notification's payload and values are Lua functions |
| 43 | local transform_functions = { |
| 44 | created_at = utils.format_datetime, |
| 45 | launched_at = utils.format_datetime, |
| 46 | deleted_at = utils.format_datetime, |
| 47 | terminated_at = utils.format_datetime, |
| 48 | user_id = normalize_uuid, |
| 49 | project_id = normalize_uuid, |
| 50 | } |
| 51 | |
| 52 | function map(func, tbl) |
| 53 | local mapped_table = {} |
| 54 | for i,v in pairs(tbl) do |
| 55 | mapped_table[i] = func(v) |
| 56 | end |
| 57 | return mapped_table |
| 58 | end |
| 59 | |
| 60 | local resource_msg = { |
| 61 | Timestamp = nil, |
| 62 | Type = "ceilometer_resources", |
| 63 | Payload = nil |
| 64 | } |
| 65 | |
| 66 | function add_resource_to_payload(sample, payload) |
| 67 | local counter_name, _ = string.gsub(sample.counter_name, "%.", "\\") |
| 68 | |
Ilya Tyaptin | 4364b7e | 2017-02-14 17:19:09 +0400 | [diff] [blame] | 69 | local metadata = sample.resource_metadata |
| 70 | local local_metadata = {} |
| 71 | |
| 72 | if type(metadata) == 'table' then |
| 73 | for name, value in ipairs(metadata) do |
| 74 | local transform = transform_functions[name] |
| 75 | if value ~= '' and value ~= nil then |
| 76 | if transform ~= nil then |
| 77 | value = transform(value) |
| 78 | end |
| 79 | local_metadata[name] = value |
| 80 | end |
| 81 | end |
| 82 | end |
| 83 | |
Ilya Tyaptin | aeff755 | 2016-11-17 15:57:36 +0000 | [diff] [blame] | 84 | local resource_data = { |
Ilya Tyaptin | 4364b7e | 2017-02-14 17:19:09 +0400 | [diff] [blame] | 85 | timestamp = utils.format_datetime(sample.timestamp), |
Ilya Tyaptin | aeff755 | 2016-11-17 15:57:36 +0000 | [diff] [blame] | 86 | resource_id = sample.resource_id, |
| 87 | source = sample.source or "", |
Ilya Tyaptin | 4364b7e | 2017-02-14 17:19:09 +0400 | [diff] [blame] | 88 | metadata = local_metadata, |
Ilya Tyaptin | aeff755 | 2016-11-17 15:57:36 +0000 | [diff] [blame] | 89 | user_id = sample.user_id, |
| 90 | project_id = sample.project_id, |
| 91 | meter = { |
| 92 | [counter_name] = { |
| 93 | type = sample.counter_type, |
| 94 | unit = sample.counter_unit |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | payload[sample.resource_id] = resource_data |
| 99 | end |
| 100 | |
| 101 | function ResourcesDecoder.new() |
| 102 | local e = {} |
| 103 | setmetatable(e, ResourcesDecoder) |
| 104 | return e |
| 105 | end |
| 106 | |
| 107 | -- Decode Ceilometer samples to resource messages |
| 108 | |
| 109 | -- data: oslo.messaging message with Ceilometer samples |
| 110 | -- returns ok and resource or error message |
Ildar Svetlov | 8f33dbb | 2017-07-21 16:01:18 +0400 | [diff] [blame] | 111 | function ResourcesDecoder:decode (message_body) |
Ilya Tyaptin | aeff755 | 2016-11-17 15:57:36 +0000 | [diff] [blame] | 112 | local resource_payload = {} |
| 113 | if message_body['payload'] then |
| 114 | for _, sample in ipairs(message_body["payload"]) do |
| 115 | add_resource_to_payload(sample, resource_payload) |
| 116 | end |
Ildar Svetlov | bbe37be | 2017-07-14 13:57:49 +0400 | [diff] [blame] | 117 | local ok, payload = pcall(cjson.encode, resource_payload) |
| 118 | if not ok then |
| 119 | return -1, "Cannot encode resource_payload" |
| 120 | end |
| 121 | resource_msg.Payload = payload |
Ilya Tyaptin | aeff755 | 2016-11-17 15:57:36 +0000 | [diff] [blame] | 122 | resource_msg.Timestamp = patt.Timestamp:match(message_body.timestamp) |
| 123 | return 0, resource_msg |
| 124 | end |
Ilya Tyaptin | 7ee0341 | 2016-12-20 18:25:32 +0400 | [diff] [blame] | 125 | return -1, "Empty message" |
Ilya Tyaptin | aeff755 | 2016-11-17 15:57:36 +0000 | [diff] [blame] | 126 | end |
| 127 | |
| 128 | return ResourcesDecoder |