Class: ServiceConsolidator

Inherits:
Object
  • Object
show all
Includes:
MrMurano::Verbose
Defined in:
lib/MrMurano/commands/service.rb

Overview

*** A ‘service` command helper class.

Constant Summary collapse

SERVICE_BLACKLIST =

SYNC_ME: See Yeti UI’s removeServiceIconBlacklist.

{
  asset: true,
  device: true,
  device2: true,
  gateway: true,
  products: true,
}.freeze

Constants included from MrMurano::Verbose

MrMurano::Verbose::TABULARIZE_DATA_FORMAT_ERROR

Instance Method Summary collapse

Methods included from MrMurano::Verbose

ask_yes_no, #ask_yes_no, #assert, assert, cmd_confirm_delete!, #cmd_confirm_delete!, debug, #debug, dump_file_json, dump_file_plain, dump_file_yaml, #dump_output_file, #error, error, #error_file_format!, fancy_ticks, #fancy_ticks, #load_file_json, #load_file_plain, #load_file_yaml, #load_input_file, outf, #outf, #outformat_engine, #pluralize?, pluralize?, #prepare_hash_csv, #read_hashf!, #tabularize, tabularize, verbose, #verbose, warning, #warning, #whirly_interject, whirly_interject, #whirly_linger, whirly_linger, #whirly_msg, whirly_msg, #whirly_pause, whirly_pause, #whirly_start, whirly_start, #whirly_stop, whirly_stop, #whirly_unpause, whirly_unpause

Constructor Details

#initialize(options) ⇒ ServiceConsolidator

Returns a new instance of ServiceConsolidator.



22
23
24
25
26
27
28
# File 'lib/MrMurano/commands/service.rb', line 22

def initialize(options)
  @include_all = options.show_all
  @abbrev_desc = options.abbrev_desc
  @include_type = options.show_type
  @exclude_added = options.hide_added
  @exclude_avail = options.only_added
end

Instance Method Details

#add_simplify_svc_configuration(sol_services, scfg) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/MrMurano/commands/service.rb', line 94

def add_simplify_svc_configuration(sol_services, scfg)
  svc_alias = scfg[:service]
  svc_meta = {
    name: scfg[:name],
    alias: svc_alias,
    description: '',
    status: scfg[:status],
    state: '',
    source: 'Solution',
  }
  svc_meta[:status] = add_type(svc_meta[:status], scfg[:type])
  unless sol_services[svc_alias].nil?
    warning %(Unexpectedly overwriting entry with /serviceconfig/: #{svc_alias})
  end
  sol_services[svc_alias] = svc_meta
end

#add_simplify_svc_definitions(sol_services, svc_standard) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/MrMurano/commands/service.rb', line 75

def add_simplify_svc_definitions(sol_services, svc_standard)
  svc_standard.each do |defn|
    svc_alias = defn[:alias]
    svc_meta = {
      name: defn[:name],
      alias: svc_alias,
      description: defn[:description],
      status: '',
      state: defn[:status],
      source: 'Platform',
    }
    svc_meta[:state] = add_type(svc_meta[:state], defn[:type])
    unless sol_services[svc_alias].nil?
      warning %(Unexpectedly overwriting entry with /service/: #{svc_alias})
    end
    sol_services[svc_alias] = svc_meta
  end
end

#add_simplify_svc_exchange(sol_services, elements) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/MrMurano/commands/service.rb', line 55

def add_simplify_svc_exchange(sol_services, elements)
  elements.each do |elem|
    svc_alias = elem[:element][:source][:name]
    svc_meta = {
      name: elem[:element][:name],
      alias: svc_alias,
      description: elem[:element][:description],
      status: '',
      state: '',
      source: 'Exchange',
    }
    sol_services[svc_alias] = svc_meta
    # (lb): Just curious:
    if svc_meta[:approval] != 'approved'
      debug %(Unexpected: Exchange element !approved: #{svc_meta})
    end
    debug %(Unexpected: Exchange element !active: #{svc_meta}) unless svc_meta[:active]
  end
end

#add_type(state, type) ⇒ Object



111
112
113
114
# File 'lib/MrMurano/commands/service.rb', line 111

def add_type(state, type)
  return state if !@include_type || type.to_s.empty?
  %(#{state} (#{type}))
end

#reconcile_services(svc_cfgs, svc_defs, purchased) ⇒ Object

NOTE:

svc_cfgs  is from /solution/<sid>/serviceconfig/
svc_defs  is from /solution/<sid>/service/
purchased is from /exchange/<bid>/purchased/


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/MrMurano/commands/service.rb', line 43

def reconcile_services(svc_cfgs, svc_defs, purchased)
  # The 3 API calls have 3 distinct responses, which we transform into 1.
  sol_services = {}
  # Add Exchange purchases to the list first.
  add_simplify_svc_exchange(sol_services, purchased)
  # Next add Platforms services, and possibly update sol_services.
  svc_standard = services_from_definitions(sol_services, svc_defs)
  add_simplify_svc_definitions(sol_services, svc_standard)
  # Combine the active service data with the Exchange and Platform lists.
  services_combine_with_active(sol_services, svc_cfgs)
end

#service_blacklisted?(svc_alias) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/MrMurano/commands/service.rb', line 116

def service_blacklisted?(svc_alias)
  !@include_all && !SERVICE_BLACKLIST[svc_alias.to_sym].nil?
end

#services_combine_with_active(sol_services, svc_cfgs) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/MrMurano/commands/service.rb', line 150

def services_combine_with_active(sol_services, svc_cfgs)
  # Fill in status for active services.
  svc_cfgs.each do |svc|
    # (lb): Sanity check. Just clarifying my understanding of APIs and data.
    if svc[:service] != svc[:script_key] && svc[:type] != 'P'
      debug("MISMATCH: :service != :scriptkey / #{svc}")
    end
    if @exclude_added || service_blacklisted?(svc[:service])
      sol_services.delete(svc[:service])
      next
    end
    # The :alias is <sid>_<service>.
    # Did we learn about this service from Exchange or the Platform?
    svc_meta = sol_services[svc[:service]]
    if !svc_meta.nil?
      # Check if 'available', meaning... not 'hidden'.
      # Note that /service/ calls it :status, but so as not to conflict
      # with attr from /seviceconfig/ of same name, we called it :state.
      if !@include_all && svc_meta[:state] == 'hidden'
        sol_services.delete(svc[:service])
        next
      end
      warning('Status not null?') unless svc_meta[:status] == ''
      svc_meta[:status] = svc[:status]
      svc_meta[:source] = add_type(svc_meta[:source], svc[:type])
      # The /serviceconfig/ is generally personalized for the solution, e.g.,
      # /service/ might have name "Timer Service" but /serviceconfig/ will
      # have "<App Name> Timer and Scheduling Service".
      svc_meta[:name] = svc[:name] unless svc[:name].to_s.empty?
    else
      # Make sure the service is not blacklisted/!!whitelisted.
      add_simplify_svc_configuration(sol_services, svc)
    end
  end
  if @exclude_avail
    sol_services.reject! { |_svc_alias, svc| svc[:status].to_s.empty? }
  end
  # This was a weird bit of plumbing. And now we're done.
  sol_services.values
end

#services_from_definitions(sol_services, svc_defs) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/MrMurano/commands/service.rb', line 120

def services_from_definitions(sol_services, svc_defs)
  svc_defs.select do |svc|
    # (lb): Sanity checking.
    # Check if service blacklisted/!!whitelisted.
    if service_blacklisted?(svc[:alias])
      sol_services.delete(svc[:alias])
      next(false)
    end
    # Check if service (from Exchange) already in the list we are building.
    sxch = sol_services[svc[:alias]]
    # The /exchange/ and /service/ endpoints do not use same name or description,
    # e.g., Exchange name might be "Websocket Service" and /service/ name might
    # be "WebSocket Gateway Service".
    unless sxch.nil?
      if sxch[:description] != svc[:description]
        if !@abbrev_desc
          sxch[:description] += ' / ' + svc[:description]
        elsif sxch[:description].to_s.empty?
          sxch[:description] = svc[:description]
        end
      end
      sxch[:state] = svc[:status]
      sxch[:state] = add_type(sxch[:state], sxch[:type])
      sxch[:source] = 'Platform/' + sxch[:source]
      next(false)
    end
    true
  end
end