Class: ChefDK::PolicyfileServices::ShowPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-dk/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.



71
72
73
74
75
76
77
78
79
# File 'lib/chef-dk/policyfile_services/show_policy.rb', line 71

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.



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

def chef_config
  @chef_config
end

#policy_groupObject (readonly)

Returns the value of attribute policy_group.



69
70
71
# File 'lib/chef-dk/policyfile_services/show_policy.rb', line 69

def policy_group
  @policy_group
end

#policy_listerObject (readonly)

Returns the value of attribute policy_lister.



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

def policy_lister
  @policy_lister
end

#policy_nameObject (readonly)

Returns the value of attribute policy_name.



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

def policy_name
  @policy_name
end

#uiObject (readonly)

Returns the value of attribute ui.



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

def ui
  @ui
end

Instance Method Details

#display_all_policiesObject



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
159
160
# File 'lib/chef-dk/policyfile_services/show_policy.rb', line 130

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



124
125
126
127
128
# File 'lib/chef-dk/policyfile_services/show_policy.rb', line 124

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



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
213
214
# File 'lib/chef-dk/policyfile_services/show_policy.rb', line 162

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?
    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)


112
113
114
# File 'lib/chef-dk/policyfile_services/show_policy.rb', line 112

def enable_pager?
  @enable_pager
end

#http_clientObject



220
221
222
223
224
# File 'lib/chef-dk/policyfile_services/show_policy.rb', line 220

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

#reportObject



116
117
118
# File 'lib/chef-dk/policyfile_services/show_policy.rb', line 116

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

#runObject



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

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



216
217
218
# File 'lib/chef-dk/policyfile_services/show_policy.rb', line 216

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

#show_all_policies?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/chef-dk/policyfile_services/show_policy.rb', line 100

def show_all_policies?
  !policy_name
end

#show_orphans?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/chef-dk/policyfile_services/show_policy.rb', line 104

def show_orphans?
  @show_orphans
end

#show_policy_revision?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/chef-dk/policyfile_services/show_policy.rb', line 96

def show_policy_revision?
  !!policy_group
end

#show_summary_diff?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/chef-dk/policyfile_services/show_policy.rb', line 108

def show_summary_diff?
  @summary_diff
end