Class: Legion::CLI::Worker

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/worker_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/legion/cli/worker_command.rb', line 8

def self.exit_on_failure?
  true
end

Instance Method Details

#activate(worker_id) ⇒ Object



108
109
110
# File 'lib/legion/cli/worker_command.rb', line 108

def activate(worker_id)
  with_data { transition_worker(worker_id, 'active', nil, authority_verified: true) }
end

#approvalsObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/legion/cli/worker_command.rb', line 127

def approvals
  out = formatter
  with_data do
    require 'legion/digital_worker/registration'
    workers = Legion::DigitalWorker::Registration.pending_approvals

    if options[:json]
      out.json(workers.map(&:to_hash))
    else
      rows = workers.map do |w|
        age = w.created_at ? "#{((Time.now.utc - w.created_at) / 3600).round(1)}h" : '-'
        [w.worker_id[0..7], w.name, w.risk_tier || '-', w.owner_msid, age]
      end
      out.table(%w[ID Name RiskTier Owner PendingFor], rows)
      puts "  #{workers.size} worker(s) pending approval"
    end
  end
end

#approve(worker_id) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/legion/cli/worker_command.rb', line 148

def approve(worker_id)
  out = formatter
  with_data do
    require 'legion/digital_worker/registration'
    worker = Legion::DigitalWorker::Registration.approve(worker_id, approver: 'cli', notes: options[:notes])
    if options[:json]
      out.json({ worker_id: worker.worker_id, lifecycle_state: worker.lifecycle_state, approved: true })
    else
      out.success("Worker #{worker.name} approved and activated")
    end
  rescue ArgumentError => e
    out.error(e.message)
  rescue Legion::DigitalWorker::Lifecycle::InvalidTransition => e
    out.error("Invalid transition: #{e.message}")
  end
end

#costs(worker_id) ⇒ Object



190
191
192
193
194
# File 'lib/legion/cli/worker_command.rb', line 190

def costs(worker_id)
  out = formatter
  out.warn('Cost reporting requires lex-metering extension (coming soon)')
  out.warn("Worker: #{worker_id}, Period: #{options[:period]}")
end

#create(name) ⇒ Object



122
123
124
# File 'lib/legion/cli/worker_command.rb', line 122

def create(name)
  with_data { create_worker(name) }
end

#listObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/legion/cli/worker_command.rb', line 22

def list
  out = formatter
  with_data do
    ds = Legion::Data::Model::DigitalWorker.dataset

    ds = ds.where(team: options[:team])               if options[:team]
    ds = ds.where(owner_msid: options[:owner])        if options[:owner]
    ds = ds.where(lifecycle_state: options[:state])   if options[:state]

    workers = ds.limit(options[:limit]).all

    if options[:json]
      out.json(workers.map(&:to_hash))
    else
      rows = workers.map do |w|
        [w.worker_id[0..7], w.name, out.status(w.lifecycle_state), w.consent_tier, w.owner_msid, w.team || '-']
      end
      out.table(%w[ID Name State Consent Owner Team], rows)
      puts "  #{workers.size} worker(s)"
    end
  end
end

#pause(worker_id) ⇒ Object



84
85
86
# File 'lib/legion/cli/worker_command.rb', line 84

def pause(worker_id)
  with_data { transition_worker(worker_id, 'paused', options[:reason], authority_verified: true) }
end

#reject(worker_id) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/legion/cli/worker_command.rb', line 167

def reject(worker_id)
  out = formatter
  with_data do
    require 'legion/digital_worker/registration'
    unless options[:reason]
      out.error('--reason is required to reject a worker')
      return
    end
    worker = Legion::DigitalWorker::Registration.reject(worker_id, approver: 'cli', reason: options[:reason])
    if options[:json]
      out.json({ worker_id: worker.worker_id, lifecycle_state: worker.lifecycle_state, rejected: true })
    else
      out.success("Worker #{worker.name} rejected")
    end
  rescue ArgumentError => e
    out.error(e.message)
  rescue Legion::DigitalWorker::Lifecycle::InvalidTransition => e
    out.error("Invalid transition: #{e.message}")
  end
end

#retire(worker_id) ⇒ Object



90
91
92
# File 'lib/legion/cli/worker_command.rb', line 90

def retire(worker_id)
  with_data { transition_worker(worker_id, 'retired', options[:reason], authority_verified: true) }
end

#show(worker_id) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/legion/cli/worker_command.rb', line 47

def show(worker_id)
  out = formatter
  with_data do
    worker = find_worker(worker_id)

    unless worker
      out.error("Worker not found: #{worker_id}")
      return
    end

    if options[:json]
      out.json(worker.to_hash)
    else
      out.header("Worker: #{worker.name}")
      out.spacer
      out.detail({
                   'Worker ID'       => worker.worker_id,
                   'Name'            => worker.name,
                   'Extension'       => worker.extension_name,
                   'Entra App ID'    => worker.entra_app_id,
                   'Owner MSID'      => worker.owner_msid,
                   'Owner Name'      => worker.owner_name || '-',
                   'Lifecycle State' => worker.lifecycle_state,
                   'Consent Tier'    => worker.consent_tier,
                   'Trust Score'     => worker.trust_score.to_s,
                   'Risk Tier'       => worker.risk_tier || '-',
                   'Team'            => worker.team || '-',
                   'Manager'         => worker.manager_msid || '-',
                   'Created'         => worker.created_at.to_s,
                   'Updated'         => worker.updated_at&.to_s || '-'
                 })
    end
  end
end

#terminate(worker_id) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/legion/cli/worker_command.rb', line 97

def terminate(worker_id)
  out = formatter
  unless options[:yes]
    out.warn('This action is IRREVERSIBLE.')
    print "Type 'yes' to confirm termination: "
    return unless $stdin.gets&.strip == 'yes'
  end
  with_data { transition_worker(worker_id, 'terminated', options[:reason], governance_override: true) }
end