| 1 |  |  | # frozen_string_literal: true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | # rubocop:disable Metrics/LineLength | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | require_relative 'private_key/remote' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | require_relative '../../action/account/private_key/add' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | require_relative '../../action/account/private_key/remove' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | require_relative '../../action/account/private_key/purge' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | require_relative '../../action/account/public_key/add' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | module AMA | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |   module Chef | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     module User | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |       class Planner | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         class Account | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |           # This planner creates actions altering account private keys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |           class PrivateKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             def initialize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |               @remote = Remote.new | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             # @param [AMA::Chef::User::Model::Account] account | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             # @param [Hash{Symbol, Hash{Symbol, AMA::Chef::User::Model::PrivateKey}}] current_state | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             # @param [Hash{Symbol, Hash{Symbol, AMA::Chef::User::Model::PrivateKey}}] desired_state | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             def plan(account, current_state, desired_state) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |               owners = current_state.keys | desired_state.keys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |               actions = owners.flat_map do |owner| | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 current_keys = current_state[owner] || {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                 desired_keys = desired_state[owner] || {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                 (current_keys.keys | desired_keys.keys).flat_map do |key| | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                   process(account, current_keys[key], desired_keys[key]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |               end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |               actions.push(ns::Purge.new(account)) if desired_state.empty? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |               actions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             # @param [AMA::Chef::User::Model::Account] account | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             # @param [AMA::Chef::User::Model::PrivateKey] current_state | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             # @param [AMA::Chef::User::Model::PrivateKey] desired_state | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             def process(account, current_state, desired_state) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |               current_remotes = current_state ? current_state.remotes : {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |               desired_remotes = desired_state ? desired_state.remotes : {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |               key = desired_state || current_state | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |               actions = @remote.plan( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 account, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 key, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 current_remotes, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 desired_remotes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |               ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |               if desired_state.nil? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 actions.push(ns::Remove.new(account, current_state)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |               else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 actions.unshift(ns::Add.new(account, desired_state)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |               end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |               actions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             def ns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |               ::AMA::Chef::User::Action::Account::PrivateKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             end | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             def pubkey_ns | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |               ::AMA::Chef::User::Action::Account::PublicKey | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |           end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |       end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |   end | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  | end | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  |