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
VALID_UPDATE_PARAMS =
%w[name description 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.



73
74
75
76
77
# File 'lib/3scale_toolbox/entities/backend.rb', line 73

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.



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

def id
  @id
end

#remoteObject (readonly)

Returns the value of attribute remote.



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

def remote
  @remote
end

Class Method Details

.create(remote:, attrs:) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/3scale_toolbox/entities/backend.rb', line 13

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



23
24
25
26
27
# File 'lib/3scale_toolbox/entities/backend.rb', line 23

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



29
30
31
32
33
34
35
36
# File 'lib/3scale_toolbox/entities/backend.rb', line 29

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



172
173
174
# File 'lib/3scale_toolbox/entities/backend.rb', line 172

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

#attrsObject



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

def attrs
  @attrs ||= fetch_backend_attrs
end

#deleteObject



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

def delete
  remote.delete_backend id
end

#descriptionObject



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

def description
  attrs['description']
end

#find_metric_or_method(system_name) ⇒ Object



176
177
178
# File 'lib/3scale_toolbox/entities/backend.rb', line 176

def find_metric_or_method(system_name)
  (metrics + methods).find { |m| m.system_name == system_name }
end

#hitsObject



107
108
109
110
111
112
113
114
# File 'lib/3scale_toolbox/entities/backend.rb', line 107

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



129
130
131
132
133
134
135
136
137
138
# File 'lib/3scale_toolbox/entities/backend.rb', line 129

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)


118
119
120
121
122
123
124
125
126
127
# File 'lib/3scale_toolbox/entities/backend.rb', line 118

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



99
100
101
102
103
104
105
# File 'lib/3scale_toolbox/entities/backend.rb', line 99

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



166
167
168
169
170
# File 'lib/3scale_toolbox/entities/backend.rb', line 166

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



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

def name
  attrs['name']
end

#private_endpointObject



95
96
97
# File 'lib/3scale_toolbox/entities/backend.rb', line 95

def private_endpoint
  attrs['private_endpoint']
end

#system_nameObject



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

def system_name
  attrs['system_name']
end

#update(b_attrs) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/3scale_toolbox/entities/backend.rb', line 140

def update(b_attrs)
  valid_b_attrs = Helper.filter_params(VALID_UPDATE_PARAMS, b_attrs)

  # Only update different attrs
  update_attrs = valid_b_attrs.keys.each_with_object({}) do |key, target|
    target[key] = valid_b_attrs.fetch(key) unless attrs[key].nil? || attrs.fetch(key) == valid_b_attrs.fetch(key)
  end

  return attrs if update_attrs.empty?

  new_attrs = remote.update_backend id, update_attrs
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend not updated', errors)
  end

  # update current attrs
  @attrs = new_attrs

  new_attrs
end