Module: Aptible::CLI::Subcommands::Organizations

Included in:
Agent
Defined in:
lib/aptible/cli/subcommands/organizations.rb

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



7
8
9
10
11
12
13
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
# File 'lib/aptible/cli/subcommands/organizations.rb', line 7

def self.included(thor)
  thor.class_eval do
    include Helpers::Token
    include Helpers::Telemetry

    desc 'organizations', 'List all organizations'
    def organizations
      telemetry(__method__, options)

      user_orgs_and_roles = {}
      begin
        roles = whoami.roles_with_organizations
      rescue HyperResource::ClientError => e
        raise Thor::Error, e.message
      end
      roles.each do |role|
        user_orgs_and_roles[role.organization.id] ||= {
          'org' => role.organization,
          'roles' => []
        }
        user_orgs_and_roles[role.organization.id]['roles'] << role
      end
      Formatter.render(Renderer.current) do |root|
        root.list do |list|
          user_orgs_and_roles.each do |org_id, org_and_role|
            org = org_and_role['org']
            roles = org_and_role['roles']
            list.object do |node|
              node.value('id', org_id)
              node.value('name', org.name)
              node.list('roles') do |roles_list|
                roles.each do |role|
                  roles_list.object do |role_node|
                    role_node.value('id', role.id)
                    role_node.value('name', role.name)
                  end
                end
              end
            end
          end
        end
      end
    end
  end
end