| 1 |  |  | # frozen_string_literal: true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | # rubocop:disable Metrics/LineLength | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | require_relative '../../action/account/public_key/add' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | require_relative '../../action/account/public_key/remove' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | require_relative '../../action/account/public_key/purge' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | module AMA | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |   module Chef | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     module User | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |       class Planner | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         class Account | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |           # This planner creates actions altering account privileges | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |           class PublicKey | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |             # @param [AMA::Chef::User::Model::Account] account | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |             # @param [Hash{Symbol, Hash{Symbol, AMA::Chef::User::Model::PublicKey}}] current_state | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |             # @param [Hash{Symbol, Hash{Symbol, AMA::Chef::User::Model::PublicKey}}] desired_state | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |             def plan(account, current_state, desired_state) | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |               owners = current_state.keys | desired_state.keys | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |               actions = owners.flat_map do |owner| | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |                 current_keys = current_state[owner] || {} | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |                 desired_keys = desired_state[owner] || {} | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |                 (current_keys.keys | desired_keys.keys).map do |key| | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |                   process(account, current_keys[key], desired_keys[key]) | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |                 end | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |               end | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |               actions.push(ns::Purge.new(account)) if desired_state.empty? | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |               actions | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             end | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             # @param [AMA::Chef::User::Model::Account] account | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             # @param [AMA::Chef::User::Model::PublicKey] current_state | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             # @param [AMA::Chef::User::Model::PublicKey] desired_state | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             def process(account, current_state, desired_state) | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |               if desired_state.nil? | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |                 ns::Remove.new(account, current_state) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |               else | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |                 ns::Add.new(account, desired_state) | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |               end | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             end | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             def ns | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |               ::AMA::Chef::User::Action::Account::PublicKey | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             end | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |           end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |       end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |   end | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  | end | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |  |