Class: Dev::Aws::Account
- Defined in:
- lib/firespring_dev_commands/aws/account.rb,
lib/firespring_dev_commands/aws/account/info.rb
Overview
Class containing useful methods for interacting with the Aws account
Defined Under Namespace
Constant Summary collapse
- CONFIG_FILE =
The name of the file containing the Aws settings
"#{Dev::Aws::CONFIG_DIR}/config".freeze
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#default ⇒ Object
Returns the value of attribute default.
-
#registry ⇒ Object
Returns the value of attribute registry.
-
#root ⇒ Object
Returns the value of attribute root.
Class Method Summary collapse
-
.config {|@config| ... } ⇒ Object
(also: configure)
Instantiates a new top level config object if one hasn't already been created Yields that config object to any given block Returns the resulting config object.
Instance Method Summary collapse
-
#all ⇒ Object
Returns all configured account information objects.
-
#all_accounts ⇒ Object
Returns the id portion of all configured account information objects.
-
#all_names ⇒ Object
Returns the name portion of all configured account information objects.
-
#base_setup! ⇒ Object
Setup base Aws settings.
-
#initialize ⇒ Account
constructor
Instantiate an account object Requires that root account and at least one child account have been configured All accounts must be of type Dev::Aws::Account::Info If a registry is configured then the user will be logged in to ECR when they log in to the account.
-
#name_by_account(account) ⇒ Object
Look up the account name for the given account id.
-
#select ⇒ Object
Menu to select one of the Aws child accounts.
-
#select_default ⇒ Object
Method of determining what the appropriate default account is for the select menu Filters out the account you are currently logged in to if it's no longer an option on the project you are currently trying to login on.
-
#setup!(account) ⇒ Object
Setup Aws account specific settings.
-
#write!(account) ⇒ Object
Write Aws account specific settings to the config file.
Constructor Details
#initialize ⇒ Account
Instantiate an account object Requires that root account and at least one child account have been configured All accounts must be of type Dev::Aws::Account::Info If a registry is configured then the user will be logged in to ECR when they log in to the account
31 32 33 34 35 36 37 38 39 |
# File 'lib/firespring_dev_commands/aws/account.rb', line 31 def initialize raise 'Root account must be configured' unless self.class.config.root.is_a?(Dev::Aws::Account::Info) raise 'Child accounts must be configured' if self.class.config.children.empty? || !self.class.config.children.all?(Dev::Aws::Account::Info) @root = self.class.config.root @children = self.class.config.children @default = self.class.config.default @registry = self.class.config.registry end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
25 26 27 |
# File 'lib/firespring_dev_commands/aws/account.rb', line 25 def children @children end |
#default ⇒ Object
Returns the value of attribute default.
25 26 27 |
# File 'lib/firespring_dev_commands/aws/account.rb', line 25 def default @default end |
#registry ⇒ Object
Returns the value of attribute registry.
25 26 27 |
# File 'lib/firespring_dev_commands/aws/account.rb', line 25 def registry @registry end |
Class Method Details
.config {|@config| ... } ⇒ Object Also known as: configure
Instantiates a new top level config object if one hasn't already been created Yields that config object to any given block Returns the resulting config object
11 12 13 14 15 |
# File 'lib/firespring_dev_commands/aws/account.rb', line 11 def self.config @config ||= Config.new yield(@config) if block_given? @config end |
Instance Method Details
#all ⇒ Object
Returns all configured account information objects
42 43 44 |
# File 'lib/firespring_dev_commands/aws/account.rb', line 42 def all @all ||= ([root] + children).sort_by(&:name) end |
#all_accounts ⇒ Object
Returns the id portion of all configured account information objects
52 53 54 |
# File 'lib/firespring_dev_commands/aws/account.rb', line 52 def all_accounts @all_accounts ||= all.map(&:id) end |
#all_names ⇒ Object
Returns the name portion of all configured account information objects
47 48 49 |
# File 'lib/firespring_dev_commands/aws/account.rb', line 47 def all_names @all_names ||= all.map(&:name) end |
#base_setup! ⇒ Object
Setup base Aws settings
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 |
# File 'lib/firespring_dev_commands/aws/account.rb', line 62 def base_setup! # Make the base config directory FileUtils.mkdir_p(Dev::Aws::CONFIG_DIR) puts puts 'Configuring default login values' # Write region and mfa serial to config file cfgini = IniFile.new(filename: "#{Dev::Aws::CONFIG_DIR}/config", default: 'default') defaultini = cfgini['default'] region_default = defaultini['region'] || ENV['AWS_DEFAULT_REGION'] || Dev::Aws::DEFAULT_REGION defaultini['region'] = Dev::Common.new.ask('Default region name', region_default) mfa_default = defaultini['mfa_serial'] || ENV['AWS_MFA_ARN'] || "arn:aws:iam::#{root}:mfa/#{ENV.fetch('USERNAME', nil)}" defaultini['mfa_serial'] = Dev::Common.new.ask('Default mfa arn', mfa_default) session_name_default = defaultini['role_session_name'] || "#{ENV.fetch('USERNAME', nil)}_cli" defaultini['role_session_name'] = Dev::Common.new.ask('Default session name', session_name_default) duration_default = defaultini['session_duration'] || 36_000 defaultini['session_duration'] = Dev::Common.new.ask('Default session duration in seconds', duration_default) cfgini.write end |
#name_by_account(account) ⇒ Object
Look up the account name for the given account id
57 58 59 |
# File 'lib/firespring_dev_commands/aws/account.rb', line 57 def name_by_account(account) all.find { |it| it.id == account }.name end |
#select ⇒ Object
Menu to select one of the Aws child accounts
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 |
# File 'lib/firespring_dev_commands/aws/account.rb', line 121 def select # If there is only one child account, use that return children.first.id if children.length == 1 # Output a list for the user to select from puts 'Account Selection:' children.each_with_index do |account, i| printf " %2s) %-20s %s\n", i + 1, account.name, account.id end selection = Dev::Common.new.ask('Enter the number of the account you wish to log in to', select_default) number = selection.to_i raise "Invalid selection: #{selection}" if number < 1 # If the selection is 3 characters or more, assume they entered the full account number if selection.length > 3 raise "Invalid selection: #{selection}" unless all_accounts.include?(selection) return selection end # Otherwise they probably entered the number of the account to use # Use the number as the index for lookup in accounts array and then get the value of that number raise "Invalid selection: #{selection}" unless children.length >= number children[number - 1].id end |
#select_default ⇒ Object
Method of determining what the appropriate default account is for the select menu Filters out the account you are currently logged in to if it's no longer an option on the project you are currently trying to login on
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/firespring_dev_commands/aws/account.rb', line 151 def select_default # If we are currently logged in to one of the configured accounts, use it as the default account_id = Dev::Env.new(Dev::Aws::Profile::CONFIG_FILE).get(Dev::Aws::Profile::IDENTIFIER) return account_id if all_accounts.include?(account_id) # Otherwise, if a default is configured, use that return default if default # Otherwise, just return the first account children.first.id end |
#setup!(account) ⇒ Object
Setup Aws account specific settings
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/firespring_dev_commands/aws/account.rb', line 89 def setup!(account) # Run base setup if it doesn't exist Rake::Task['aws:configure:default'].invoke unless File.exist?(CONFIG_FILE) puts puts "Configuring #{account} login values" write!(account) puts end |
#write!(account) ⇒ Object
Write Aws account specific settings to the config file
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/firespring_dev_commands/aws/account.rb', line 101 def write!(account) raise 'Configure default account settings first (rake aws:configure:default)' unless File.exist?(CONFIG_FILE) # Parse the ini file and load values cfgini = IniFile.new(filename: CONFIG_FILE, default: 'default') defaultini = cfgini['default'] profileini = cfgini["profile #{account}"] profileini['source_profile'] = account region_default = profileini['region'] || defaultini['region'] || ENV['AWS_DEFAULT_REGION'] || Dev::Aws::DEFAULT_REGION profileini['region'] = Dev::Common.new.ask('Default region name', region_default) role_default = profileini['role_arn'] || "arn:aws:iam::#{account}:role/ReadonlyAccessRole" profileini['role_arn'] = Dev::Common.new.ask('Default role arn', role_default) cfgini.write end |