Class: ChefCLI::PolicyfileServices::RmPolicyGroup

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: nil, ui: nil, policy_group: nil) ⇒ RmPolicyGroup

Returns a new instance of RmPolicyGroup.



42
43
44
45
46
47
48
49
# File 'lib/chef-cli/policyfile_services/rm_policy_group.rb', line 42

def initialize(config: nil, ui: nil, policy_group: nil)
  @chef_config = config
  @ui = ui
  @policy_group = policy_group

  @undo_record = Policyfile::UndoRecord.new
  @undo_stack = Policyfile::UndoStack.new
end

Instance Attribute Details

#chef_configObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



31
32
33
# File 'lib/chef-cli/policyfile_services/rm_policy_group.rb', line 31

def chef_config
  @chef_config
end

#policy_groupObject (readonly)

Returns the value of attribute policy_group.



28
29
30
# File 'lib/chef-cli/policyfile_services/rm_policy_group.rb', line 28

def policy_group
  @policy_group
end

#uiObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



34
35
36
# File 'lib/chef-cli/policyfile_services/rm_policy_group.rb', line 34

def ui
  @ui
end

#undo_recordObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



37
38
39
# File 'lib/chef-cli/policyfile_services/rm_policy_group.rb', line 37

def undo_record
  @undo_record
end

#undo_stackObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



40
41
42
# File 'lib/chef-cli/policyfile_services/rm_policy_group.rb', line 40

def undo_stack
  @undo_stack
end

Instance Method Details

#http_clientObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An instance of Chef::ServerAPI configured with the user’s server URL and credentials.



78
79
80
81
82
# File 'lib/chef-cli/policyfile_services/rm_policy_group.rb', line 78

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

#runObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chef-cli/policyfile_services/rm_policy_group.rb', line 51

def run
  undo_record.description = "delete-policy-group #{policy_group}"
  policy_group_list = http_client.get("/policy_groups")

  unless policy_group_list.key?(policy_group)
    ui.err("Policy group '#{policy_group}' does not exist on the server")
    return false
  end
  policy_group_info = policy_group_list[policy_group]

  policies_in_group = policy_group_info["policies"] || []
  policies_in_group.each do |name, revision_info|
    rev_id = revision_info["revision_id"]
    policy_revision_data = http_client.get("/policies/#{name}/revisions/#{rev_id}")
    undo_record.add_policy_revision(name, policy_group, policy_revision_data)
  end
  http_client.delete("/policy_groups/#{policy_group}")
  undo_record.add_policy_group(policy_group)
  ui.err("Removed policy group '#{policy_group}'.")
  undo_stack.push(undo_record)
rescue => e
  raise DeletePolicyGroupError.new("Failed to delete policy group '#{policy_group}'", e)
end