Class: ThreeScaleToolbox::Entities::Backend

Inherits:
Object
  • Object
show all
Includes:
CRD::BackendSerializer
Defined in:
lib/3scale_toolbox/entities/backend.rb

Constant Summary collapse

VALID_PARAMS =
%w[name description system_name private_endpoint].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CRD::BackendSerializer

#cr_name, #to_cr

Constructor Details

#initialize(id:, remote:, attrs: nil) ⇒ Backend

Returns a new instance of Backend.



70
71
72
73
74
# File 'lib/3scale_toolbox/entities/backend.rb', line 70

def initialize(id:, remote:, attrs: nil)
  @id = id.to_i
  @remote = remote
  @attrs = attrs
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



68
69
70
# File 'lib/3scale_toolbox/entities/backend.rb', line 68

def id
  @id
end

#remoteObject (readonly)

Returns the value of attribute remote.



68
69
70
# File 'lib/3scale_toolbox/entities/backend.rb', line 68

def remote
  @remote
end

Class Method Details

.create(remote:, attrs:) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/3scale_toolbox/entities/backend.rb', line 10

def create(remote:, attrs:)
  b_attrs = remote.create_backend Helper.filter_params(VALID_PARAMS, attrs)
  if (errors = b_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend has not been created', errors)
  end

  new(id: b_attrs.fetch('id'), remote: remote, attrs: b_attrs)
end

.find(remote:, ref:) ⇒ Object

ref can be system_name or backend_id



20
21
22
23
24
# File 'lib/3scale_toolbox/entities/backend.rb', line 20

def find(remote:, ref:)
  new(id: ref, remote: remote).tap(&:attrs)
rescue ThreeScaleToolbox::InvalidIdError, ThreeScale::API::HttpClient::NotFoundError
  find_by_system_name(remote: remote, system_name: ref)
end

.find_by_system_name(remote:, system_name:) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/3scale_toolbox/entities/backend.rb', line 26

def find_by_system_name(remote:, system_name:)
  attrs = list_backends(remote: remote).find do |backend|
    backend['system_name'] == system_name
  end
  return if attrs.nil?

  new(id: attrs.fetch('id'), remote: remote, attrs: attrs)
end

Instance Method Details

#==(other) ⇒ Object



160
161
162
# File 'lib/3scale_toolbox/entities/backend.rb', line 160

def ==(other)
  remote.http_client.endpoint == other.remote.http_client.endpoint && id == other.id
end

#attrsObject



76
77
78
# File 'lib/3scale_toolbox/entities/backend.rb', line 76

def attrs
  @attrs ||= fetch_backend_attrs
end

#deleteObject



149
150
151
# File 'lib/3scale_toolbox/entities/backend.rb', line 149

def delete
  remote.delete_backend id
end

#descriptionObject



84
85
86
# File 'lib/3scale_toolbox/entities/backend.rb', line 84

def description
  attrs['description']
end

#hitsObject



104
105
106
107
108
109
110
111
# File 'lib/3scale_toolbox/entities/backend.rb', line 104

def hits
  metric_list = metrics_and_methods.map do |metric_attrs|
    BackendMetric.new(id: metric_attrs.fetch('id'), backend: self, attrs: metric_attrs)
  end
  metric_list.find { |metric| metric.system_name == 'hits' }.tap do |hits_metric|
    raise ThreeScaleToolbox::Error, 'missing hits metric' if hits_metric.nil?
  end
end

#mapping_rulesObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/3scale_toolbox/entities/backend.rb', line 126

def mapping_rules
  m_r = remote.list_backend_mapping_rules id
  if m_r.respond_to?(:has_key?) && (errors = m_r['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend mapping rules not read', errors)
  end

  m_r.map do |mr_attrs|
    BackendMappingRule.new(id: mr_attrs.fetch('id'), backend: self, attrs: mr_attrs)
  end
end

#methodsList

Returns:

  • (List)


115
116
117
118
119
120
121
122
123
124
# File 'lib/3scale_toolbox/entities/backend.rb', line 115

def methods
  method_attr_list = remote.list_backend_methods id, hits.id
  if method_attr_list.respond_to?(:has_key?) && (errors = method_attr_list['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend methods not read', errors)
  end

  method_attr_list.map do |method_attrs|
    BackendMethod.new(id: method_attrs.fetch('id'), backend: self, attrs: method_attrs)
  end
end

#metricsObject



96
97
98
99
100
101
102
# File 'lib/3scale_toolbox/entities/backend.rb', line 96

def metrics
  metric_attr_list = metrics_and_methods.select { |metric_attrs| metric_attrs['parent_id'].nil? }

  metric_attr_list.map do |metric_attrs|
    BackendMetric.new(id: metric_attrs.fetch('id'), backend: self, attrs: metric_attrs)
  end
end

#metrics_mapping(other) ⇒ Object

Compute matrics mapping



154
155
156
157
158
# File 'lib/3scale_toolbox/entities/backend.rb', line 154

def metrics_mapping(other)
  (metrics + methods).product(other.metrics + other.methods).select do |m_a, m_b|
    m_a.system_name == m_b.system_name
  end.map { |m_a, m_b| [m_a.id, m_b.id] }.to_h
end

#nameObject



88
89
90
# File 'lib/3scale_toolbox/entities/backend.rb', line 88

def name
  attrs['name']
end

#private_endpointObject



92
93
94
# File 'lib/3scale_toolbox/entities/backend.rb', line 92

def private_endpoint
  attrs['private_endpoint']
end

#system_nameObject



80
81
82
# File 'lib/3scale_toolbox/entities/backend.rb', line 80

def system_name
  attrs['system_name']
end

#update(b_attrs) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/3scale_toolbox/entities/backend.rb', line 137

def update(b_attrs)
  new_attrs = remote.update_backend id, Helper.filter_params(VALID_PARAMS, b_attrs)
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend not updated', errors)
  end

  # update current attrs
  @attrs = new_attrs

  new_attrs
end