Module: Aptible::CLI::Subcommands::AwsAccounts

Included in:
Agent
Defined in:
lib/aptible/cli/subcommands/aws_accounts.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/aptible/cli/subcommands/aws_accounts.rb', line 7

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

    desc 'aws_accounts', 'List external AWS accounts', hide: true
    option :organization_id, aliases: '--org-id',
                             type: :string,
                             default: nil,
                             desc: 'Organization ID'
    def aws_accounts
      telemetry(__method__, options)

      accounts = aws_accounts_all

      Formatter.render(Renderer.current) do |root|
        root.list do |list|
          accounts.each do |ext|
            list.object do |node|
              node.value('id', ext.id) if ext.respond_to?(:id)
              attrs = ext.respond_to?(:attributes) ? ext.attributes : {}
              %w(
                aws_account_id
                account_name
                aws_region_primary
                status
                discovery_enabled
                discovery_role_arn
                discovery_frequency
                account_id
                created_at
                updated_at
              ).each do |k|
                v = attrs[k]
                node.value(k, v) unless v.nil?
              end
            end
          end
        end
      end
    end

    desc 'aws_accounts:add ' \
         '[--account-name ACCOUNT_NAME] ' \
         '[--aws-account-id AWS_ACCOUNT_ID] ' \
         '[--org-id ORGANIZATION_ID] '\
         '[--aws-region-primary AWS_REGION] ' \
         '[--discovery-enabled|--no-discovery-enabled] ' \
         '[--discovery-role-arn DISCOVERY_ROLE_ARN] ' \
         '[--discovery-frequency FREQ]', \
         'Add a new external AWS account', hide: true
    option :account_name, type: :string, desc: 'Display name'
    option :aws_account_id, type: :string, desc: 'AWS Account ID'
    option :organization_id, aliases: '--org-id',
                             type: :string,
                             default: nil,
                             desc: 'Organization ID'
    option :aws_region_primary, type: :string,
                                desc: 'Primary AWS region'
    option :discovery_enabled, type: :boolean,
                               desc: 'Enable resource discovery'
    option :discovery_role_arn, type: :string,
                                desc: 'IAM Role ARN that Aptible ' \
                                      'will assume to discover ' \
                                      'resources in your AWS account'
    option :discovery_frequency,
           type: :string,
           desc: 'Discovery frequency (e.g., daily)'
    define_method 'aws_accounts:add' do
      telemetry(__method__, options)

      resource = create_external_aws_account!(options)

      Formatter.render(Renderer.current) do |root|
        root.object do |node|
          node.value('id', resource.id) if resource.respond_to?(:id)
          rattrs =
            if resource.respond_to?(:attributes)
              resource.attributes
            else
              {}
            end
          %w(
            aws_account_id
            account_name
            aws_region_primary
            discovery_enabled
            discovery_role_arn
            discovery_frequency
            account_id
            created_at
            updated_at
          ).each do |k|
            v = rattrs[k]
            node.value(k, v) unless v.nil?
          end
        end
      end
    end

    desc 'aws_accounts:show ID',
         'Show an external AWS account', \
         hide: true
    define_method 'aws_accounts:show' do |id|
      telemetry(__method__, options.merge(id: id))
      ext = (id)
      Formatter.render(Renderer.current) do |root|
        root.object do |node|
          node.value('id', ext.id)
          rattrs =
            if ext.respond_to?(:attributes)
              ext.attributes
            else
              {}
            end
          %w(
            aws_account_id
            account_name
            aws_region_primary
            discovery_enabled
            discovery_role_arn
            discovery_frequency
            account_id
            created_at
            updated_at
          ).each do |k|
            v = rattrs[k]
            node.value(k, v) unless v.nil?
          end
        end
      end
    end

    desc 'aws_accounts:delete ID',
         'Delete an external AWS account', \
         hide: true
    define_method 'aws_accounts:delete' do |id|
      telemetry(__method__, options.merge(id: id))

      delete_external_aws_account!(id)

      Formatter.render(Renderer.current) do |root|
        root.object do |node|
          node.value('id', id)
          node.value('deleted', true)
        end
      end
    end

    desc 'aws_accounts:update ID ' \
         '[--account-name ACCOUNT_NAME] ' \
         '[--aws-account-id AWS_ACCOUNT_ID] ' \
         '[--aws-region-primary AWS_REGION] ' \
         '[--discovery-enabled|--no-discovery-enabled] ' \
         '[--discovery-role-arn DISCOVERY_ROLE_ARN] ' \
         '[--remove-discovery-role-arn] ' \
         '[--discovery-frequency FREQ]', \
         'Update an external AWS account', hide: true
    option :account_name, type: :string, desc: 'New display name'
    option :aws_account_id, type: :string, desc: 'AWS Account ID'
    option :aws_region_primary, type: :string,
                                desc: 'Primary AWS region'
    option :discovery_enabled, type: :boolean,
                               desc: 'Enable resource discovery'
    option :discovery_role_arn, type: :string,
                                desc: 'IAM Role ARN that Aptible ' \
                                      'will assume to discover ' \
                                      'resources in your AWS account'
    option :remove_discovery_role_arn, type: :boolean,
                                       desc: 'Remove the discovery ' \
                                             'role ARN from this ' \
                                             'account'
    option :discovery_frequency,
           type: :string,
           desc: 'Discovery frequency (e.g., daily)'
    define_method 'aws_accounts:update' do |id|
      telemetry(__method__, options.merge(id: id))

      ext = update_external_aws_account!(id, options)

      Formatter.render(Renderer.current) do |root|
        root.object do |node|
          node.value('id', ext.id)
          rattrs =
            if ext.respond_to?(:attributes)
              ext.attributes
            else
              {}
            end
          %w(
            aws_account_id
            account_name
            aws_region_primary
            discovery_enabled
            discovery_role_arn
            discovery_frequency
            account_id
            created_at
            updated_at
          ).each do |k|
            v = rattrs[k]
            node.value(k, v) unless v.nil?
          end
        end
      end
    end

    desc 'aws_accounts:check ID',
         'Check the connection for an external AWS account', \
         hide: true
    define_method 'aws_accounts:check' do |id|
      telemetry(__method__, options.merge(id: id))

      response = check_external_aws_account!(id)

      fmt_state = lambda do |state|
        Renderer.format == 'json' ? state : format_check_state(state)
      end

      Formatter.render(Renderer.current) do |root|
        root.object do |node|
          node.value('state', fmt_state.call(response.state))
          node.list('checks') do |check_list|
            response.checks.each do |check|
              check_list.object do |check_node|
                check_node.value('name', check.check_name)
                check_node.value('state', fmt_state.call(check.state))
                check_node.value('details', check.details) \
                  unless check.details.nil?
              end
            end
          end
        end
      end

      unless response.state == 'success'
        raise Thor::Error, 'AWS account check failed'
      end
    end
  end
end