lib/3scale_toolbox/entities/metric.rb in 3scale_toolbox-0.17.1 vs lib/3scale_toolbox/entities/metric.rb in 3scale_toolbox-0.18.0

- old
+ new

@@ -1,8 +1,10 @@ module ThreeScaleToolbox module Entities class Metric + include CRD::MetricSerializer + class << self def create(service:, attrs:) metric = service.remote.create_metric service.id, attrs if (errors = metric['errors']) raise ThreeScaleToolbox::ThreeScaleApiError.new('Metric has not been created', errors) @@ -17,14 +19,11 @@ rescue ThreeScale::API::HttpClient::NotFoundError find_by_system_name(service: service, system_name: ref) end def find_by_system_name(service:, system_name:) - metric = service.metrics.find { |m| m['system_name'] == system_name } - return if metric.nil? - - new(id: metric.fetch('id'), service: service, attrs: metric) + service.metrics.find { |m| m.system_name == system_name } end end attr_reader :id, :service, :remote @@ -37,29 +36,45 @@ def attrs @attrs ||= metric_attrs end + def system_name + attrs['system_name'] + end + + def friendly_name + attrs['friendly_name'] + end + + def unit + attrs['unit'] + end + + def description + attrs['description'] + end + def disable # For each plan, get limits for the current metric # if already disabled -> NOOP # if non zero eternity limit exist, update # if non eternity limit exist, create - service_plans.each do |plan| + service.plans.each do |plan| eternity_limit = plan_eternity_limit(plan) if eternity_limit.nil? plan.create_limit(id, zero_eternity_limit_attrs) - elsif !eternity_limit.fetch('value').zero? - plan.update_limit(id, eternity_limit.fetch('id'), zero_eternity_limit_attrs) + elsif !eternity_limit.value.zero? + eternity_limit.update(zero_eternity_limit_attrs) end end end def enable - service_plans.each do |plan| + service.plans.each do |plan| limit = plan_zero_eternity_limit(plan) - plan.delete_limit(id, limit.fetch('id')) unless limit.nil? + limit.delete unless limit.nil? end end def update(new_metric_attrs) new_attrs = remote.update_metric(service.id, id, new_metric_attrs) @@ -88,22 +103,18 @@ metric end def plan_zero_eternity_limit(plan) # only one limit for eternity period is allowed per (plan_id, metric_id) - plan.metric_limits(id).find { |limit| limit > zero_eternity_limit_attrs } + #plan.metric_limits(id).find { |limit| limit.attrs > zero_eternity_limit_attrs } + plan.metric_limits(id).find do |limit| + limit.attrs > zero_eternity_limit_attrs + end end def plan_eternity_limit(plan) # only one limit for eternity period is allowed per (plan_id, metric_id) - plan.metric_limits(id).find { |limit| limit['period'] == 'eternity' } - end - - def service_plans - service.plans.map do |plan_attrs| - ThreeScaleToolbox::Entities::ApplicationPlan.new(id: plan_attrs.fetch('id'), - service: service) - end + plan.metric_limits(id).find { |limit| limit.period == 'eternity' } end def zero_eternity_limit_attrs { 'period' => 'eternity', 'value' => 0 } end