Class: ChefCLI::PolicyfileServices::ShowPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-cli/policyfile_services/show_policy.rb

Defined Under Namespace

Classes: ReportPrinter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: nil, ui: nil, policy_name: nil, policy_group: nil, show_orphans: false, summary_diff: false, pager: false) ⇒ ShowPolicy

Returns a new instance of ShowPolicy.



69
70
71
72
73
74
75
76
77
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 69

def initialize(config: nil, ui: nil, policy_name: nil, policy_group: nil, show_orphans: false, summary_diff: false, pager: false)
  @chef_config = config
  @ui = ui
  @policy_name = policy_name
  @policy_group = policy_group
  @show_orphans = show_orphans
  @summary_diff = summary_diff
  @enable_pager = pager
end

Instance Attribute Details

#chef_configObject (readonly)

Returns the value of attribute chef_config.



65
66
67
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 65

def chef_config
  @chef_config
end

#policy_groupObject (readonly)

Returns the value of attribute policy_group.



67
68
69
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 67

def policy_group
  @policy_group
end

#policy_nameObject (readonly)

Returns the value of attribute policy_name.



63
64
65
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 63

def policy_name
  @policy_name
end

#uiObject (readonly)

Returns the value of attribute ui.



61
62
63
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 61

def ui
  @ui
end

Instance Method Details

#display_all_policiesObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 128

def display_all_policies
  if policy_lister.empty?
    ui.err("No policies or policy groups exist on the server")
    return
  end
  if policy_lister.policies_by_name.empty?
    ui.err("No policies exist on the server")
    return
  end
  policy_lister.revision_ids_by_group_for_each_policy do |policy_name, rev_id_by_group|
    report.h1(policy_name)

    if rev_id_by_group.empty?
      ui.err("Policy #{policy_name} is not assigned to any groups")
      ui.err("")
    else
      rev_ids_for_report = format_rev_ids_for_report(rev_id_by_group)
      report.table_list(rev_ids_for_report)
    end

    if show_orphans?
      orphans = policy_lister.orphaned_revisions(policy_name)

      unless orphans.empty?
        report.h2("Orphaned:")
        formatted_orphans = orphans.map { |id| shorten_rev_id(id) }
        report.list(formatted_orphans)
      end
    end
  end
end

#display_policy_revisionObject



122
123
124
125
126
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 122

def display_policy_revision
  lock = Policyfile::ComparisonBase::PolicyGroup.new(policy_group, policy_name, http_client).lock
  pager = Pager.new(enable_pager: enable_pager?)
  pager.with_pager { |p| p.ui.msg(FFI_Yajl::Encoder.encode(lock, pretty: true)) }
end

#display_single_policyObject



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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 160

def display_single_policy
  report.h1(policy_name)
  rev_id_by_group = policy_lister.revision_ids_by_group_for(policy_name)

  if rev_id_by_group.empty? || rev_id_by_group.all? { |_k, rev| rev.nil? }
    ui.err("No policies named '#{policy_name}' are associated with a policy group")
    ui.err("")
  elsif show_summary_diff?
    unique_rev_ids = rev_id_by_group.unique_revision_ids
    revision_info = policy_lister.revision_info_for(policy_name, unique_rev_ids)

    ljust_size = rev_id_by_group.max_group_name_length + 2

    cbs_with_differing_ids = revision_info.cbs_with_differing_ids

    rev_id_by_group.each do |group_name, rev_id|
      heading = "#{group_name}:".ljust(ljust_size) + shorten_rev_id(rev_id)
      report.h2(heading)

      differing_cbs_version_info = cbs_with_differing_ids.inject({}) do |cb_version_info, cb_name|

        version, identifier = revision_info.cb_info_for(rev_id, cb_name)

        cb_info_for_report =
          if !version.nil?
            "#{version} (#{shorten_rev_id(identifier)})"
          else
            "*NONE*"
          end

        cb_version_info[cb_name] = cb_info_for_report

        cb_version_info
      end

      report.table_list(differing_cbs_version_info)
    end

  else
    rev_ids_for_report = format_rev_ids_for_report(rev_id_by_group)
    report.table_list(rev_ids_for_report)
  end

  if show_orphans?
    orphans = policy_lister.orphaned_revisions(policy_name)

    unless orphans.empty?
      report.h2("Orphaned:")
      formatted_orphans = orphans.map { |id| shorten_rev_id(id) }
      report.list(formatted_orphans)
    end
  end
end

#enable_pager?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 110

def enable_pager?
  @enable_pager
end

#http_clientObject



218
219
220
221
222
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 218

def http_client
  @http_client ||= Chef::ServerAPI.new(chef_config.chef_server_url,
    signing_key_filename: chef_config.client_key,
    client_name: chef_config.node_name)
end

#policy_listerObject



118
119
120
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 118

def policy_lister
  @policy_info_fetcher ||= Policyfile::Lister.new(config: chef_config)
end

#reportObject



114
115
116
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 114

def report
  @report ||= ReportPrinter.new(ui)
end

#runObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 79

def run
  if show_policy_revision?
    display_policy_revision
  elsif show_all_policies?
    display_all_policies
  else
    display_single_policy
  end
  true
rescue PolicyfileNestedException
  raise
rescue => e
  raise PolicyfileListError.new("Failed to list policyfile data from the server", e)
end

#shorten_rev_id(revision_id) ⇒ Object



214
215
216
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 214

def shorten_rev_id(revision_id)
  revision_id[0, 10]
end

#show_all_policies?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 98

def show_all_policies?
  !policy_name
end

#show_orphans?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 102

def show_orphans?
  @show_orphans
end

#show_policy_revision?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 94

def show_policy_revision?
  !!policy_group
end

#show_summary_diff?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/chef-cli/policyfile_services/show_policy.rb', line 106

def show_summary_diff?
  @summary_diff
end