Class: SimplyGenius::Atmos::Providers::Aws::AccountManager

Inherits:
Object
  • Object
show all
Includes:
FileUtils, GemLogger::LoggerSupport
Defined in:
lib/simplygenius/atmos/providers/aws/account_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(provider) ⇒ AccountManager

Returns a new instance of AccountManager.



13
14
15
# File 'lib/simplygenius/atmos/providers/aws/account_manager.rb', line 13

def initialize(provider)
  @provider = provider
end

Instance Method Details

#create_account(env, name: nil, email: nil) ⇒ Object



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
# File 'lib/simplygenius/atmos/providers/aws/account_manager.rb', line 17

def (env, name: nil, email: nil)
  result = {}
  org = ::Aws::Organizations::Client.new
  resp = nil
  name ||= "Atmos #{env} account"

  begin
    logger.info "Looking up organization"
    resp = org.describe_organization()
    logger.debug "Described organization: #{resp.to_h}"
  rescue ::Aws::Organizations::Errors::AWSOrganizationsNotInUseException
    logger.info "Organization doesn't exist, creating"
    resp = org.create_organization()
    logger.debug "Created organization: #{resp.to_h}"
  end

  if email.blank?
    master_email = resp.organization.
    email = master_email.sub('@', "+#{env}@")
  end
  result[:email] = email
  result[:name] = name


  begin
    logger.info "Creating account named #{name}"
    resp = org.(account_name: name, email: email)
  rescue ::Aws::Organizations::Errors::FinalizingOrganizationException
    logger.info "Waiting to retry account creation as the organization needs to finalize"
    logger.info "This will eventually succeed after receiving a"
    logger.info "'Consolidated Billing verification' email from AWS"
    logger.info "You can leave this running or cancel and restart later."
    sleep 60
    retry
  end

  logger.debug "Created account: #{resp.to_h}"

  status_id = resp..id
  status = resp..state
   = resp..

  while status =~ /in_progress/i
    logger.info "Waiting for account creation to complete, status: #{status}"
    resp = org.(create_account_request_id: status_id)
    logger.debug("Account creation status check: #{resp.to_h}")
    status = resp..state
     = resp..
    sleep 5
  end

  if status =~ /failed/i
    logger.error "Failed to create account: #{resp..failure_reason}"
    exit(1)
  end

  result[:account_id] = 

  return result
end