Class: Ogre::Associate

Inherits:
Base
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/ogre/associate.rb

Overview

Associate user to org while bypassing the association request

Instance Method Summary collapse

Methods inherited from Base

#chef_rest

Instance Method Details

#associateObject

Associate user to org while bypassing the association request



14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
55
# File 'lib/ogre/associate.rb', line 14

def associate
  begin
    # associate (invite) user
    request_body = { user: user }
    response = chef_rest.post "organizations/#{org}/association_requests", request_body

    # add (force) user to org
    association_id = response['uri'].split('/').last
    chef_rest.put "users/#{user}/association_requests/#{association_id}", response: 'accept'
  rescue Net::HTTPServerException => e
    # already exists -- i will allow it
    if e.response.code == '409'
      puts "User '#{user}' already associated with organization '#{org}'"
    else
      raise e
    end
  end

  # add to admin?
  groups = ['users']
  groups << 'admins' if options[:admin]

  # add user to group(s)
  groups.each do |groupname|
    group = chef_rest.get "organizations/#{org}/groups/#{groupname}"
    # check if user is in group
    unless group['actors'].include?(user)
      body_hash = {
        groupname: groupname.to_s,
        actors: {
          users: group['actors'].concat([user]),
          groups: group['groups']
        }
      }

      # associate user
      chef_rest.put "organizations/#{org}/groups/#{groupname}", body_hash
      puts "Successfully added '#{user}' to '#{groupname}' in the #{org} org"
    end
    next
  end
end