Class: ThreeScaleToolbox::Entities::BackendUsage

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

Overview

BackendUsage represents Product - Backend mapping entry

Constant Summary collapse

CREATE_PARAMS =
%w[path backend_api_id].freeze
UPDATE_PARAMS =
%w[path].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CRD::BackendUsageSerializer

#to_cr

Constructor Details

#initialize(id:, product:, attrs: nil) ⇒ BackendUsage

Returns a new instance of BackendUsage.



42
43
44
45
46
47
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 42

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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



40
41
42
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 40

def id
  @id
end

#productObject (readonly)

Returns the value of attribute product.



40
41
42
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 40

def product
  @product
end

#remoteObject (readonly)

Returns the value of attribute remote.



40
41
42
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 40

def remote
  @remote
end

Class Method Details

.create(product:, attrs:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 14

def create(product:, attrs:)
  resp = product.remote.create_backend_usage(
    product.id,
    Helper.filter_params(CREATE_PARAMS, attrs)
  )
  if (errors = resp['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend usage has not been created',
                                                    errors)
  end

  new(id: resp.fetch('id'), product: product, attrs: resp)
end

.find_by_path(product:, path:) ⇒ Object



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

def find_by_path(product:, path:)
  resp = product.remote.list_backend_usages product.id
  if resp.respond_to?(:has_key?) && (errors = resp['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend usage list error', errors)
  end

  backend_usage_attrs = resp.find { |bus| bus['path'] == path }
  return if backend_usage_attrs.nil?

  new(id: backend_usage_attrs.fetch('id'), product: product, attrs: backend_usage_attrs)
end

Instance Method Details

#attrsObject



49
50
51
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 49

def attrs
  @attrs ||= fetch_attrs
end

#backendObject



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

def backend
  Backend.new(id: backend_id, remote: remote)
end

#backend_idObject



57
58
59
60
61
62
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 57

def backend_id
  # 3scale API returns 'backend_id'
  # 3scale API only accepts 'backend_api_id' as params on create endpoint
  # good job
  attrs['backend_id']
end

#deleteObject



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

def delete
  remote.delete_backend_usage product.id, id
end

#pathObject



53
54
55
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 53

def path
  attrs['path']
end

#update(usage_attrs) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/3scale_toolbox/entities/backend_usage.rb', line 64

def update(usage_attrs)
  new_attrs = remote.update_backend_usage(
    product.id, id,
    Helper.filter_params(UPDATE_PARAMS, usage_attrs)
  )
  if (errors = new_attrs['errors'])
    raise ThreeScaleToolbox::ThreeScaleApiError.new('Backend usage not been updated', errors)
  end

  if new_attrs['service_id'] != product.id
    raise ThreeScaleToolbox::Error, 'Backend usage product updated'
  end

  # update current attrs
  @attrs = new_attrs

  new_attrs
end