Module: Ey::Core::TestHelpers::ResourceHelpers

Included in:
Ey::Core::TestHelpers
Defined in:
lib/ey-core/test_helpers/resource_helpers.rb

Instance Method Summary collapse

Instance Method Details

#create_account_referral(client, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ey-core/test_helpers/resource_helpers.rb', line 63

def (client, options={})
  referred = options.delete(:referred) || (client: client)
  referrer = options.delete(:referrer) || (client: client)

   = SecureRandom.uuid
  referral = client.data[:account_referrals][] = {
    "id" => ,
    "referrer" => client.url_for("accounts/#{referrer.identity}"),
    "referred" => client.url_for("accounts/#{referred.identity}"),
  }

  client..new(referral)
end

#create_application(options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/ey-core/test_helpers/resource_helpers.rb', line 13

def create_application(options={})
   = options.delete(:account) || (options)
  options = Cistern::Hash.stringify_keys(options)

  options["name"]       ||= "application#{SecureRandom.hex(4)}"
  options["repository"] ||= "git://github.com/engineyard/todo.git"
  options["type"]       ||= "rails4"

  .applications.create!(options)
end

#create_cost(client, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ey-core/test_helpers/resource_helpers.rb', line 38

def create_cost(client, options={})
   = options[:account] || (client: client)
  level = options[:level] || "summarized"
  finality = options[:finality] || "estimated"
  related_resource_type = options[:related_resource_type] || "account"
  category = options[:category] || "non-server"
  description = options[:description] || "AWS Other Services"
  value = options[:value] || "1763"
  environment = options[:environment] || nil

  client.data[:costs] << {
    billing_month:         "2015-07",
    data_type:             "cost",
    level:                 level,
    finality:              finality,
    related_resource_type: related_resource_type,
    category:              category,
    units:                 "USD cents",
    description:           description,
    value:                 value,
    account:               client.url_for("accounts/#{.identity}"),
    environment:           environment
  }
end

#create_database_service(options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ey-core/test_helpers/resource_helpers.rb', line 91

def create_database_service(options={})
  provider = options[:provider] || create_provider(options.merge(client: client))

  database_service_params = Hashie::Mash.new(
    :name     => Faker::Name.first_name,
    :provider => provider,
  ).merge(options.fetch(:database_service, {}))

  database_server_params = Hashie::Mash.new(
      :location => "us-west-2c",
      :flavor   => "db.m3.large",
      :engine   => "postgres",
      :version  => "9.3.5",
  ).merge(options.fetch(:database_server, {}))

  client.database_services.create!(database_service_params.merge(database_server: database_server_params)).resource!
end

#create_environment(options = {}) ⇒ Object



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
# File 'lib/ey-core/test_helpers/resource_helpers.rb', line 109

def create_environment(options={})
   = options[:account] || (options)

  unless .providers.first || options[:provider]
    create_provider(account: )
  end

  environment             = options[:environment] || {}
  application             = options[:application] || create_application(account: )
  database_service        = options[:database_service]
  configuration           = Cistern::Hash.stringify_keys(options[:configuration] || {})
  configuration["type"] = "production-cluster" if configuration["type"] == "production"
  configuration["type"] ||= "solo"
  environment[:name]    ||= options.fetch(:name, SecureRandom.hex(3))
  environment[:region]  ||= "us-west-2"

  environment.merge!(application_id: application.id, account: )
  environment.merge!(database_service: database_service) if database_service
  environment = client.environments.create!(environment)

  unless options[:boot] == false
    request = environment.boot(configuration: configuration, application_id: application.id)
    request.ready!
  end
  environment
end

#create_firewall(client, options = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ey-core/test_helpers/resource_helpers.rb', line 77

def create_firewall(client, options={})
  provider = options.fetch(:provider) { create_provider(client: client) }

  firewall_params = options[:firewall] || {}
  name     = firewall_params.delete(:name) || SecureRandom.hex(6)
  location = firewall_params.delete(:location) || "us-west-2"

  client.firewalls.create!(
    :name     => name,
    :location => location,
    :provider => provider,
  ).resource!
end

#create_logical_database(options = {}) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/ey-core/test_helpers/resource_helpers.rb', line 165

def create_logical_database(options={})
  database_service = options.fetch(:database_service) { create_database_service(options) }

  database_service.databases.create!(
    :name     => SecureRandom.hex(6),
    :username => "ey#{SecureRandom.hex(6)}",
    :password => SecureRandom.hex(8),
  ).resource!
end

#create_provider_location(client, attributes = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ey-core/test_helpers/resource_helpers.rb', line 136

def create_provider_location(client, attributes={})
  attributes = Cistern::Hash.stringify_keys(attributes)

  if provider = attributes.delete("provider")
    attributes["provider"] = client.url_for("/providers/#{provider.id}")
  end

  attributes["id"] ||= client.uuid
  client.data[:provider_locations][attributes["id"]] = attributes

  client.provider_locations.new(attributes)
end

#create_server(client, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ey-core/test_helpers/resource_helpers.rb', line 24

def create_server(client, options={})
  options = Cistern::Hash.stringify_keys(options)
  environment = options["environment"]

  request = client.servers.create!({
    :environment => environment,
    :flavor_id   => 'm3.medium',
    :role        => 'util',
    :name        => 'resque',
  }.merge(options["server"] || {}))

  request.resource!
end

#create_server_event(client, attributes = {}) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/ey-core/test_helpers/resource_helpers.rb', line 149

def create_server_event(client, attributes={})
  attributes = Cistern::Hash.stringify_keys(attributes)

  attributes.fetch("type")

  if server = attributes.delete("server")
    attributes["server"] = client.url_for("/servers/#{server.id}")
  end

  event_id = attributes["id"] ||= SecureRandom.uuid

  client.server_events.new(
    client.data[:server_events][event_id] = attributes
  )
end

#create_untracked_server(options = {}) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ey-core/test_helpers/resource_helpers.rb', line 175

def create_untracked_server(options={})
  provider = options.fetch(:provider) { create_provider(options) }

  untracked_server = options[:untracked_server] || {}

  provisioner_id = untracked_server[:provisioner_id] || SecureRandom.uuid
  location       = untracked_server[:location]       || "us-west-2b"
  provisioned_id = untracked_server[:provisioned_id] || "i-#{SecureRandom.hex(4)}"
  state          = untracked_server[:state]          || "found"

  client.untracked_servers.create(
    :location       => location,
    :provider       => provider,
    :provisioned_id => provisioned_id,
    :provisioner_id => provisioner_id,
    :state          => state,
  )
end

#load_blueprint(options = {}) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/ey-core/test_helpers/resource_helpers.rb', line 5

def load_blueprint(options={})
  application      = create_application(account: )
  database_service = create_database_service(provider: .providers.first)
  environment      = create_environment(account: , application: application, database_service: database_service, environment: {name: "environment#{SecureRandom.hex(4)}"})

  [database_service, environment]
end