class ChefCLI::PolicyfileServices::RmPolicy

def fetch_policy_revision_data

def fetch_policy_revision_data
  @policy_revision_data = http_client.get("/policies/#{policy_name}")
  @policy_exists = true
rescue Net::HTTPServerException => e
  raise unless e.response.code == "404"
  @policy_exists = false
end

def gather_policy_data_for_undo

def gather_policy_data_for_undo
  revisions = policy_revision_data["revisions"].keys
  revisions.each do |revision_id|
    policy_revision_data = http_client.get("/policies/#{policy_name}/revisions/#{revision_id}")
    policy_groups = policy_groups_using_revision(revision_id)
    if policy_groups.empty?
      undo_record.add_policy_revision(policy_name, nil, policy_revision_data)
    else
      policy_groups.each do |policy_group|
        undo_record.add_policy_revision(policy_name, policy_group, policy_revision_data)
      end
    end
  end
end

def http_client

Other tags:
    Api: - private
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

def initialize(config: nil, ui: nil, policy_name: nil)

def initialize(config: nil, ui: nil, policy_name: nil)
  @chef_config = config
  @ui = ui
  @policy_name = policy_name
  @policy_revision_data = nil
  @policy_exists = false
  @policy_group_data = nil
  @undo_record = Policyfile::UndoRecord.new
  @undo_stack = Policyfile::UndoStack.new
end

def policy_exists?

def policy_exists?
  return true if @policy_exists
  fetch_policy_revision_data
  @policy_exists
end

def policy_group_data

def policy_group_data
  @policy_group_data ||= http_client.get("/policy_groups")
end

def policy_groups_using_revision(revision_id)

def policy_groups_using_revision(revision_id)
  groups = []
  policy_group_data.each do |group_name, group_info|
    next unless group_info.key?("policies") && !group_info["policies"].empty?
    next unless group_info["policies"].key?(policy_name)
    next unless group_info["policies"][policy_name]["revision_id"] == revision_id
    groups << group_name if group_info
  end
  groups
end

def policy_has_no_revisions?

def policy_has_no_revisions?
  policy_revision_data.empty? || policy_revision_data["revisions"].empty?
end

def policy_revision_data

def policy_revision_data
  return @policy_revision_data if @policy_exists
  fetch_policy_revision_data
end

def run

def run
  unless policy_exists?
    ui.err("Policy '#{policy_name}' does not exist on the server")
    return false
  end
  undo_record.description = "delete-policy #{policy_name}"
  unless policy_has_no_revisions?
    gather_policy_data_for_undo
  end
  http_client.delete("/policies/#{policy_name}")
  undo_stack.push(undo_record)
  ui.err("Removed policy '#{policy_name}'.")
rescue => e
  raise DeletePolicyError.new("Failed to delete policy '#{policy_name}'", e)
end