Sha256: 420c3177b4eba86be0190c39cfbb52d3c3a8b63e498a57fe5d7917b5d3906154

Contents?: true

Size: 1.79 KB

Versions: 6

Compression:

Stored size: 1.79 KB

Contents

module Ogre
  # Associate user to org while bypassing the association request
  class Associate < Ogre::Base
    include Thor::Actions

    # required
    argument :org, type: :string, desc: DESC_ORG
    argument :user, type: :string, desc: DESC_USER

    # optional
    class_option :admin, aliases: '-a', type: :boolean, desc: DESC_ASSOCIATE_ADMIN

    # Associate user to org while bypassing the association request
    def associate
      begin
        # associate (invite) user
        request_body = { user: user }
        response = chef_rest.post_rest "organizations/#{org}/association_requests", request_body

        # add (force) user to org
        association_id = response['uri'].split('/').last
        chef_rest.put_rest "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_rest "organizations/#{org}/groups/#{groupname}"
        # check if user is in group
        unless group['actors'].include?(user)
          body_hash = {
            groupname: "#{groupname}",
            actors: {
              users: group['actors'].concat([user]),
              groups: group['groups']
            }
          }

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ogre-0.1.5 lib/ogre/associate.rb
ogre-0.1.4 lib/ogre/associate.rb
ogre-0.1.3 lib/ogre/associate.rb
ogre-0.1.2 lib/ogre/associate.rb
ogre-0.1.1 lib/ogre/associate.rb
ogre-0.1.0 lib/ogre/associate.rb