Class: OpscodeAcl::GroupRemoveActor

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/group_remove_actor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actor_nameObject (readonly)

Returns the value of attribute actor_name.



23
24
25
# File 'lib/chef/knife/group_remove_actor.rb', line 23

def actor_name
  @actor_name
end

#clientsObject (readonly)

Returns the value of attribute clients.



23
24
25
# File 'lib/chef/knife/group_remove_actor.rb', line 23

def clients
  @clients
end

#group_nameObject (readonly)

Returns the value of attribute group_name.



23
24
25
# File 'lib/chef/knife/group_remove_actor.rb', line 23

def group_name
  @group_name
end

#user_mapObject (readonly)

Returns the value of attribute user_map.



23
24
25
# File 'lib/chef/knife/group_remove_actor.rb', line 23

def user_map
  @user_map
end

Instance Method Details

#find_actor_in_mapObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chef/knife/group_remove_actor.rb', line 73

def find_actor_in_map
  @actor_type, @actor_id = if user_map[:users][actor_name]
                             [:user, user_map[:users][actor_name]]
                           else
                             [:client, clients[actor_name]]
                           end
  if @actor_id.nil?
    ui.error("no user or client named '#{actor_name}' in actor-map.yaml")
    exit 1
  end
  true
end

#make_group_for_put(existing_group) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chef/knife/group_remove_actor.rb', line 61

def make_group_for_put(existing_group)
  new_group = {
    "groupname" => existing_group["groupname"],
    "orgname" => existing_group["orgname"],
    "actors" => {
      "clients" => existing_group["clients"],
      "groups" => existing_group["groups"],
      "users" => existing_group["users"]
    }
  }
end

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef/knife/group_remove_actor.rb', line 28

def run
  if !File.exists?("actor-map.yaml")
    ui.error "unable to find 'actor-map.yaml'. Run 'knife actor map' and try again."
    exit 1
  end
  actor_map = YAML.load(IO.read("actor-map.yaml"))
  @user_map = actor_map[:user_map]
  @clients = actor_map[:clients]
  @group_name = name_args[0]
  @actor_name = name_args[1]

  if !group_name || !actor_name
    ui.error "must specify GROUP and ACTOR"
    exit 1
  end
  find_actor_in_map
  @chef_rest = Chef::REST.new(Chef::Config[:chef_server_url])
  group = @chef_rest.get_rest("groups/#{group_name}")
  case @actor_type
  when :user
    group["groups"].delete(@actor_id)
    group["users"].delete(actor_name)
  when :client
    group["clients"].delete(@actor_id)
  end
  save_group(group)
end

#save_group(group) ⇒ Object



56
57
58
59
# File 'lib/chef/knife/group_remove_actor.rb', line 56

def save_group(group)
  new_group = make_group_for_put(group)
  @chef_rest.put_rest("groups/#{new_group["groupname"]}", new_group)
end