Module: Aptible::CLI::Helpers::AwsAccount

Includes:
Token
Defined in:
lib/aptible/cli/helpers/aws_account.rb

Constant Summary

Constants included from Token

Token::TOKEN_ENV_VAR

Instance Method Summary collapse

Methods included from Token

#current_token, #current_token_hash, #decode_token, #fetch_token, #save_token, #token_file, #whoami

Methods included from ConfigPath

#aptible_config_path

Instance Method Details

#aws_account_from_id(id) ⇒ Object



26
27
28
# File 'lib/aptible/cli/helpers/aws_account.rb', line 26

def (id)
  Aptible::Api::ExternalAwsAccount.find(id.to_s, token: fetch_token)
end

#aws_accounts_allObject



19
20
21
22
23
24
# File 'lib/aptible/cli/helpers/aws_account.rb', line 19

def aws_accounts_all
  Aptible::Api::ExternalAwsAccount.all(
    token: fetch_token,
    href: aws_accounts_href
  )
end

#aws_accounts_hrefObject



11
12
13
14
15
16
17
# File 'lib/aptible/cli/helpers/aws_account.rb', line 11

def aws_accounts_href
  if Renderer.format == 'json'
    '/external_aws_accounts'
  else
    '/external_aws_accounts?per_page=5000&no_embed=true'
  end
end

#build_external_aws_account_attrs(options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/aptible/cli/helpers/aws_account.rb', line 60

def (options)
  discovery_role_arn = if options[:remove_discovery_role_arn]
                         ''
                       else
                         options[:discovery_role_arn]
                       end
  discovery_enabled = if options.key?(:discovery_enabled)
                        options[:discovery_enabled]
                      end
  attrs = {
    account_name: options[:account_name] || options[:name],
    aws_account_id: options[:aws_account_id],
    aws_region_primary: options[:aws_region_primary],
    status: options[:status],
    discovery_enabled: discovery_enabled,
    discovery_role_arn: discovery_role_arn,
    discovery_frequency: options[:discovery_frequency]
  }
  attrs.reject { |_, v| v.nil? }
end

#check_external_aws_account!(id) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/aptible/cli/helpers/aws_account.rb', line 134

def check_external_aws_account!(id)
  ext = (id)
  begin
    ext.check!
  rescue HyperResource::ClientError => e
    raise Thor::Error, e.message
  end
end

#create_external_aws_account!(options) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/aptible/cli/helpers/aws_account.rb', line 81

def create_external_aws_account!(options)
  attrs = (options)
  attrs[:organization_id] = organization_id_from_opts_or_auth(options)
  begin
    resource = Aptible::Api::ExternalAwsAccount.create(
      token: fetch_token,
      **attrs
    )
    if resource.errors.any?
      raise Thor::Error, resource.errors.full_messages.first
    end
    resource
  rescue HyperResource::ClientError => e
    raise Thor::Error, e.message
  end
end

#delete_external_aws_account!(id) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/aptible/cli/helpers/aws_account.rb', line 114

def delete_external_aws_account!(id)
  ext = (id)
  begin
    if ext.respond_to?(:destroy!)
      ext.destroy!
    elsif ext.respond_to?(:destroy)
      ext.destroy
    elsif ext.respond_to?(:delete!)
      ext.delete!
    elsif ext.respond_to?(:delete)
      ext.delete
    else
      raise Thor::Error, 'Delete is not supported for this resource'
    end
  rescue HyperResource::ClientError => e
    raise Thor::Error, e.message
  end
  true
end

#ensure_external_aws_account(id) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/aptible/cli/helpers/aws_account.rb', line 30

def (id)
  acct = (id)
  if acct.nil?
    raise Thor::Error, "External AWS account not found: #{id}"
  end

  acct
end

#fetch_organization_idObject

Raises:

  • (Thor::Error)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/aptible/cli/helpers/aws_account.rb', line 39

def fetch_organization_id
  orgs = Aptible::Auth::Organization.all(token: fetch_token)
  raise Thor::Error, 'No organizations found, specify one with ' \
                     '--organization-id=ORG_ID' if orgs.empty?
  raise Thor::Error, 'Multiple organizations found, indicate which ' \
                     'one to use with --organization-id=ORG_ID ' \
                     "\n\tFound organization ids:" \
                     "\n\t\t#{orgs.map do |o|
                       "#{o.id} (#{o.name})"
                     end.join("\n\t\t")}" \
                     if orgs.count > 1

  orgs.first.id
end

#format_check_state(state) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/aptible/cli/helpers/aws_account.rb', line 143

def format_check_state(state)
  case state
  when 'success'
    '✅ success'
  when 'failed'
    '❌ failed'
  when 'not_run'
    '⏭️  not_run'
  else
    state
  end
end

#organization_id_from_opts_or_auth(options) ⇒ Object



54
55
56
57
58
# File 'lib/aptible/cli/helpers/aws_account.rb', line 54

def organization_id_from_opts_or_auth(options)
  return options[:organization_id] if options.key? :organization_id

  fetch_organization_id
end

#update_external_aws_account!(id, options) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/aptible/cli/helpers/aws_account.rb', line 98

def update_external_aws_account!(id, options)
  ext = (id)
  attrs = (options)
  begin
    unless attrs.empty?
      ext.update!(**attrs)
      if ext.errors.any?
        raise Thor::Error, ext.errors.full_messages.first
      end
    end
    ext
  rescue HyperResource::ClientError => e
    raise Thor::Error, e.message
  end
end