class Ogre::Associate

Associate user to org while bypassing the association request

Public Instance Methods

associate() click to toggle source

Associate user to org while bypassing the association request

# 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