Class: Ey::Core::Client::Mock

Inherits:
Object
  • Object
show all
Extended by:
Mock::Util
Includes:
Shared, Mock::Helper, Mock::Params, Mock::Resources, Mock::Searching, Mock::Util
Defined in:
lib/ey-core/requests/signup.rb,
lib/ey-core/client/mock.rb,
lib/ey-core/requests/get_gem.rb,
lib/ey-core/requests/get_log.rb,
lib/ey-core/requests/get_logs.rb,
lib/ey-core/requests/get_task.rb,
lib/ey-core/requests/get_user.rb,
lib/ey-core/requests/get_addon.rb,
lib/ey-core/requests/get_alert.rb,
lib/ey-core/requests/get_costs.rb,
lib/ey-core/requests/get_tasks.rb,
lib/ey-core/requests/get_token.rb,
lib/ey-core/requests/get_users.rb,
lib/ey-core/requests/create_log.rb,
lib/ey-core/requests/get_addons.rb,
lib/ey-core/requests/get_alerts.rb,
lib/ey-core/requests/get_server.rb,
lib/ey-core/requests/get_tokens.rb,
lib/ey-core/requests/get_volume.rb,
lib/ey-core/requests/create_task.rb,
lib/ey-core/requests/create_user.rb,
lib/ey-core/requests/get_account.rb,
lib/ey-core/requests/get_address.rb,
lib/ey-core/requests/get_billing.rb,
lib/ey-core/requests/get_feature.rb,
lib/ey-core/requests/get_keypair.rb,
lib/ey-core/requests/get_message.rb,
lib/ey-core/requests/get_request.rb,
lib/ey-core/requests/get_servers.rb,
lib/ey-core/requests/get_storage.rb,
lib/ey-core/requests/get_volumes.rb,
lib/ey-core/requests/stop_server.rb,
lib/ey-core/requests/upload_file.rb,
lib/ey-core/requests/create_addon.rb,
lib/ey-core/requests/create_alert.rb,
lib/ey-core/requests/create_token.rb,
lib/ey-core/requests/destroy_user.rb,
lib/ey-core/requests/get_accounts.rb,
lib/ey-core/requests/get_contacts.rb,
lib/ey-core/requests/get_features.rb,
lib/ey-core/requests/get_firewall.rb,
lib/ey-core/requests/get_keypairs.rb,
lib/ey-core/requests/get_messages.rb,
lib/ey-core/requests/get_metadata.rb,
lib/ey-core/requests/get_provider.rb,
lib/ey-core/requests/get_requests.rb,
lib/ey-core/requests/get_storages.rb,
lib/ey-core/requests/start_server.rb,
lib/ey-core/requests/update_addon.rb,
lib/ey-core/requests/update_alert.rb,
lib/ey-core/requests/create_server.rb,
lib/ey-core/requests/destroy_addon.rb,
lib/ey-core/requests/download_file.rb,
lib/ey-core/requests/get_addresses.rb,
lib/ey-core/requests/get_api_token.rb,
lib/ey-core/requests/get_blueprint.rb,
lib/ey-core/requests/get_component.rb,
lib/ey-core/requests/get_firewalls.rb,
lib/ey-core/requests/get_providers.rb,
lib/ey-core/requests/reboot_server.rb,
lib/ey-core/requests/update_server.rb,
lib/ey-core/requests/attach_address.rb,
lib/ey-core/requests/cancel_account.rb,
lib/ey-core/requests/create_account.rb,
lib/ey-core/requests/create_address.rb,
lib/ey-core/requests/create_keypair.rb,
lib/ey-core/requests/create_message.rb,
lib/ey-core/requests/create_storage.rb,
lib/ey-core/requests/destroy_server.rb,
lib/ey-core/requests/detach_address.rb,
lib/ey-core/requests/enable_feature.rb,
lib/ey-core/requests/get_blueprints.rb,
lib/ey-core/requests/get_components.rb,
lib/ey-core/requests/get_deployment.rb,
lib/ey-core/requests/get_membership.rb,
lib/ey-core/requests/reset_password.rb,
lib/ey-core/requests/update_address.rb,
lib/ey-core/requests/update_billing.rb,
lib/ey-core/requests/create_firewall.rb,
lib/ey-core/requests/create_provider.rb,
lib/ey-core/requests/destroy_storage.rb,
lib/ey-core/requests/disable_feature.rb,
lib/ey-core/requests/discover_server.rb,
lib/ey-core/requests/get_application.rb,
lib/ey-core/requests/get_backup_file.rb,
lib/ey-core/requests/get_deployments.rb,
lib/ey-core/requests/get_environment.rb,
lib/ey-core/requests/get_memberships.rb,
lib/ey-core/requests/get_plan_usages.rb,
lib/ey-core/requests/boot_environment.rb,
lib/ey-core/requests/destroy_firewall.rb,
lib/ey-core/requests/destroy_provider.rb,
lib/ey-core/requests/get_applications.rb,
lib/ey-core/requests/get_backup_files.rb,
lib/ey-core/requests/get_current_user.rb,
lib/ey-core/requests/get_environments.rb,
lib/ey-core/requests/get_legacy_alert.rb,
lib/ey-core/requests/get_server_event.rb,
lib/ey-core/requests/get_storage_user.rb,
lib/ey-core/requests/reconcile_server.rb,
lib/ey-core/requests/request_callback.rb,
lib/ey-core/requests/update_blueprint.rb,
lib/ey-core/requests/update_connector.rb,
lib/ey-core/requests/acknowledge_alert.rb,
lib/ey-core/requests/create_membership.rb,
lib/ey-core/requests/destroy_blueprint.rb,
lib/ey-core/requests/get_account_trial.rb,
lib/ey-core/requests/get_firewall_rule.rb,
lib/ey-core/requests/get_legacy_alerts.rb,
lib/ey-core/requests/get_load_balancer.rb,
lib/ey-core/requests/get_server_events.rb,
lib/ey-core/requests/get_server_usages.rb,
lib/ey-core/requests/get_storage_users.rb,
lib/ey-core/requests/get_support_trial.rb,
lib/ey-core/requests/update_membership.rb,
lib/ey-core/requests/authorized_channel.rb,
lib/ey-core/requests/create_application.rb,
lib/ey-core/requests/create_backup_file.rb,
lib/ey-core/requests/create_environment.rb,
lib/ey-core/requests/get_firewall_rules.rb,
lib/ey-core/requests/get_load_balancers.rb,
lib/ey-core/requests/get_token_by_login.rb,
lib/ey-core/requests/reset_server_state.rb,
lib/ey-core/requests/timeout_deployment.rb,
lib/ey-core/requests/update_environment.rb,
lib/ey-core/requests/create_storage_user.rb,
lib/ey-core/requests/destroy_environment.rb,
lib/ey-core/requests/get_database_server.rb,
lib/ey-core/requests/get_ssl_certificate.rb,
lib/ey-core/requests/apply_server_updates.rb,
lib/ey-core/requests/create_firewall_rule.rb,
lib/ey-core/requests/create_load_balancer.rb,
lib/ey-core/requests/destroy_storage_user.rb,
lib/ey-core/requests/get_database_servers.rb,
lib/ey-core/requests/get_database_service.rb,
lib/ey-core/requests/get_logical_database.rb,
lib/ey-core/requests/get_ssl_certificates.rb,
lib/ey-core/requests/get_untracked_server.rb,
lib/ey-core/requests/unassign_environment.rb,
lib/ey-core/requests/blueprint_environment.rb,
lib/ey-core/requests/create_password_reset.rb,
lib/ey-core/requests/destroy_firewall_rule.rb,
lib/ey-core/requests/destroy_load_balancer.rb,
lib/ey-core/requests/get_account_referrals.rb,
lib/ey-core/requests/get_database_services.rb,
lib/ey-core/requests/get_logical_databases.rb,
lib/ey-core/requests/get_provider_location.rb,
lib/ey-core/requests/get_untracked_servers.rb,
lib/ey-core/requests/create_database_server.rb,
lib/ey-core/requests/create_ssl_certificate.rb,
lib/ey-core/requests/get_auto_scaling_alarm.rb,
lib/ey-core/requests/get_auto_scaling_group.rb,
lib/ey-core/requests/get_keypair_deployment.rb,
lib/ey-core/requests/get_load_balancer_node.rb,
lib/ey-core/requests/get_provider_locations.rb,
lib/ey-core/requests/update_ssl_certificate.rb,
lib/ey-core/requests/create_database_service.rb,
lib/ey-core/requests/create_logical_database.rb,
lib/ey-core/requests/create_untracked_server.rb,
lib/ey-core/requests/deprovision_environment.rb,
lib/ey-core/requests/destroy_database_server.rb,
lib/ey-core/requests/destroy_ssl_certificate.rb,
lib/ey-core/requests/get_application_archive.rb,
lib/ey-core/requests/get_auto_scaling_alarms.rb,
lib/ey-core/requests/get_auto_scaling_groups.rb,
lib/ey-core/requests/get_auto_scaling_policy.rb,
lib/ey-core/requests/get_keypair_deployments.rb,
lib/ey-core/requests/get_load_balancer_nodes.rb,
lib/ey-core/requests/get_operational_contact.rb,
lib/ey-core/requests/update_untracked_server.rb,
lib/ey-core/requests/create_untracked_address.rb,
lib/ey-core/requests/destroy_database_service.rb,
lib/ey-core/requests/destroy_logical_database.rb,
lib/ey-core/requests/discover_database_server.rb,
lib/ey-core/requests/get_account_cancellation.rb,
lib/ey-core/requests/get_application_archives.rb,
lib/ey-core/requests/get_database_plan_usages.rb,
lib/ey-core/requests/get_environment_variable.rb,
lib/ey-core/requests/get_operational_contacts.rb,
lib/ey-core/requests/update_provider_location.rb,
lib/ey-core/requests/apply_environment_updates.rb,
lib/ey-core/requests/create_auto_scaling_alarm.rb,
lib/ey-core/requests/create_auto_scaling_group.rb,
lib/ey-core/requests/create_keypair_deployment.rb,
lib/ey-core/requests/get_alerting_environments.rb,
lib/ey-core/requests/get_auto_scaling_policies.rb,
lib/ey-core/requests/get_environment_variables.rb,
lib/ey-core/requests/get_load_balancer_service.rb,
lib/ey-core/requests/update_auto_scaling_alarm.rb,
lib/ey-core/requests/update_auto_scaling_group.rb,
lib/ey-core/requests/bootstrap_logical_database.rb,
lib/ey-core/requests/create_application_archive.rb,
lib/ey-core/requests/create_auto_scaling_policy.rb,
lib/ey-core/requests/destroy_auto_scaling_alarm.rb,
lib/ey-core/requests/destroy_auto_scaling_group.rb,
lib/ey-core/requests/discover_provider_location.rb,
lib/ey-core/requests/get_application_deployment.rb,
lib/ey-core/requests/get_database_server_usages.rb,
lib/ey-core/requests/get_load_balancer_services.rb,
lib/ey-core/requests/update_application_archive.rb,
lib/ey-core/requests/update_auto_scaling_policy.rb,
lib/ey-core/requests/create_environment_variable.rb,
lib/ey-core/requests/destroy_auto_scaling_policy.rb,
lib/ey-core/requests/get_application_deployments.rb,
lib/ey-core/requests/get_environment_plan_usages.rb,
lib/ey-core/requests/update_environment_variable.rb,
lib/ey-core/requests/get_database_server_snapshot.rb,
lib/ey-core/requests/get_database_server_revisions.rb,
lib/ey-core/requests/get_database_server_snapshots.rb,
lib/ey-core/requests/change_environment_maintenance.rb,
lib/ey-core/requests/deploy_environment_application.rb,
lib/ey-core/requests/get_database_servers_firewalls.rb,
lib/ey-core/requests/run_cluster_application_action.rb,
lib/ey-core/requests/upload_recipes_for_environment.rb,
lib/ey-core/requests/get_possible_provider_locations.rb,
lib/ey-core/requests/restart_environment_app_servers.rb,
lib/ey-core/requests/create_database_service_snapshot.rb,
lib/ey-core/requests/destroy_database_server_snapshot.rb,
lib/ey-core/requests/get_environment_database_services.rb,
lib/ey-core/requests/get_environment_logical_databases.rb,
lib/ey-core/requests/discover_database_server_snapshots.rb,
lib/ey-core/requests/run_environment_application_action.rb,
lib/ey-core/requests/retrieve_docker_registry_credentials.rb,
lib/ey-core/requests/discover_container_service_deployments.rb

Overview

Real

Constant Summary collapse

APP_TYPE_LANGUAGES =
{
  "java" => "java",
  "merb" => "ruby",
  "nodejs" => "nodejs",
  "php" => "php",
  "rack" => "ruby",
  "rails2" => "ruby",
  "rails3" => "ruby",
  "rails4" => "ruby",
  "sinatra" => "ruby",
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Shared

#authentication, #cache, #url

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mock::Util

api_token, deep_dup, ip_address, normalize_hash, uuid

Methods included from Mock::Helper

#create_database_service_resource, #create_feature

Methods included from Mock::Searching

#page, #pluralize, #search, #search_and_page, #url_for_page

Methods included from Mock::Resources

#find, #mock_account_setup, #mock_ssh_key, #resource_identity

Methods included from Mock::Params

#extract_url_params!, #path_params, #url_params

Methods included from Shared

#metadata, #require_argument, #require_argument!, #require_arguments, #require_parameters, #setup, #url_for

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



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
# File 'lib/ey-core/client/mock.rb', line 201

def initialize(options={})
  setup(options)
  @url = options[:url] || "https://api.engineyard.com"

  if authentication == :token
    if server = self.data[:servers].values.find { |s| s["token"] == options[:token] }
      @current_server = server
    else
      @current_user ||= begin
                          _,found = self.data[:users].detect{|k,v| v["token"] == options[:token]}
                          found
                        end

      @current_user ||= begin
                          _,token = self.data[:tokens].detect{|k,v| v["auth_id"] == options[:token]}
                          if token
                            user_id = token["on_behalf_of"].split("/").last
                            self.data[:users][user_id]
                          end
                        end

      # FIXME(rs) get rid of this and the implicit Josh Lane creation entirely
      @current_user ||= begin
                          _,found = self.data[:users].detect{|k,v| v["email"] == "[email protected]"}
                          found
                        end

      unless @current_user
        user = Ey::Core::Client.new({
          :url      => @url,
          :auth_id  => SecureRandom.hex(8),
          :auth_key => SecureRandom.hex(16),
        }).users.create!({
            :email => "[email protected]",
            :name  => "Joshua Lane",
          })

        if options[:token]
          self.data[:users][user.id]["token"] = options[:token]
        end

        @current_user = self.data[:users][user.id]
      end
    end
  end
end

Class Attribute Details

.delayObject

Returns the value of attribute delay.



182
183
184
# File 'lib/ey-core/client/mock.rb', line 182

def delay
  @delay
end

Instance Attribute Details

#current_serverObject (readonly)

Returns the value of attribute current_server.



199
200
201
# File 'lib/ey-core/client/mock.rb', line 199

def current_server
  @current_server
end

Class Method Details

.dataObject



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
# File 'lib/ey-core/client/mock.rb', line 39

def self.data
  @data ||= Hash.new do |h,k|
    h[k] = begin
             cc_id     = self.uuid
             custom_id = self.uuid
             dc_id     = self.uuid
             app_id    = self.uuid
             deis_id   = self.uuid
             components = {
               cc_id => {
                 "created_at" => Time.now.to_s,
                 "deleted_at" => nil,
                 "id"         => cc_id,
                 "name"       => "cluster_cookbooks",
                 "updated_at" => Time.now.to_s,
                 "uri"        => "git://github.com/engineyard/cluster_cookbooks.git",
               },
               custom_id => {
                 "created_at" => Time.now.to_s,
                 "deleted_at" => nil,
                 "id"         => custom_id,
                 "name"       => "custom_cookbooks",
                 "updated_at" => Time.now.to_s,
                 "uri"        => "git://github.com/engineyard/cluster_cookbooks.git",
               },
               dc_id => {
                 "created_at" => Time.now.to_s,
                 "deleted_at" => nil,
                 "id"         => dc_id,
                 "name"       => "default_cookbooks",
                 "updated_at" => Time.now.to_s,
                 "uri"        => "git://github.com/engineyard/cloud_cookbooks.git",
               },
               app_id => {
                 "created_at" => Time.now.to_s,
                 "deleted_at" => nil,
                 "id"         => app_id,
                 "name"       => "default_deployer",
                 "updated_at" => Time.now.to_s,
                 "uri"        => "git://github.com/engineyard/engineyard-serverside.git",
               },
               deis_id => {
                 "created_at" => Time.now.to_s,
                 "deleted_at" => nil,
                 "id"         => deis_id,
                 "name"       => "deis",
                 "updated_at" => Time.now.to_s,
                 "uri"        => "git://github.com/deis/deis.git",
               }
             }
             possible_provider_locations = {
               "azure" => [
                 { "id" => "East US",      "name" => "East US" },
                 { "id" => "North Europe", "name" => "North Europe" },
                 { "id" => "West Europe",  "name" => "West Europe" },
                 { "id" => "West US",      "name" => "West US" },
                 { "id" => "Japan East",   "name" => "Japan East" }
               ],
               "aws" => [
                 { "id" => "us-east-1",      "name" => "Eastern United States" },
                 { "id" => "us-west-1",      "name" => "Western US (Northern CA)" },
                 { "id" => "us-west-2",      "name" => "Western US (Oregon)" },
                 { "id" => "sa-east-1",      "name" => "South America" },
                 { "id" => "eu-west-1",      "name" => "Europe" },
                 { "id" => "ap-southeast-1", "name" => "Singapore" },
                 { "id" => "ap-southeast-2", "name" => "Australia" },
                 { "id" => "ap-northeast-1", "name" => "Japan" }
               ]
             }
             {
               :account_referrals           => {},
               :accounts                    => {},
               :account_trials              => {},
               :addons                      => {},
               :addresses                   => {},
               :agents                      => {},
               :alerts                      => {},
               :application_archives        => {},
               :application_deployments     => {},
               :applications                => {},
               :auto_scaling_alarms         => {},
               :auto_scaling_groups         => {},
               :auto_scaling_policies       => {},
               :backup_files                => {},
               :backups                     => {},
               :billing                     => {},
               :blueprints                  => {},
               :cluster_firewalls           => [],
               :components                  => components,
               :contact_assignments         => [],
               :contacts                    => {},
               :container_service_deployments => {},
               :costs                       => [],
               :database_plan_usages        => Hash.new { |h1,k1| h1[k1] = {} },
               :database_server_firewalls   => [],
               :database_server_revisions   => {},
               :database_server_snapshots   => {},
               :database_server_usages      => Hash.new { |h1,k1| h1[k1] = {} },
               :database_servers            => {},
               :database_services           => {},
               :deleted                     => Hash.new {|x,y| x[y] = {}},
               :deployments                 => {},
               :environment_plan_usages     => Hash.new { |h1,k1| h1[k1] = {} },
               :environments                => {},
               :environment_variables       => {},
               :features                    => {},
               :firewall_rules              => {},
               :firewalls                   => {},
               :instance_updates            => {},
               :keypair_deployments         => {},
               :keypairs                    => {},
               :legacy_alerts               => {},
               :load_balancer_nodes         => {},
               :load_balancer_services      => {},
               :load_balancers              => {},
               :logical_databases           => {},
               :logs                        => {},
               :memberships                 => {},
               :messages                    => {},
               :plan_usages                 => Hash.new { |h1,k1| h1[k1] = {} },
               :possible_provider_locations => possible_provider_locations,
               :provider_locations          => {},
               :providers                   => {},
               :requests                    => {},
               :server_events               => {},
               :server_usages               => Hash.new { |h1,k1| h1[k1] = {} },
               :servers                     => {},
               :ssl_certificates            => {},
               :storage_users               => {},
               :storages                    => {},
               :support_trials              => {},
               :tasks                       => {},
               :temp_files                  => {},
               :tokens                      => {},
               :untracked_servers           => {},
               :users                       => {},
               :volumes                     => {},
             }
           end
  end
end

.for(type, options = {}) ⇒ Object



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
# File 'lib/ey-core/client/mock.rb', line 11

def self.for(type, options={})
  case type
  when :server
    client = Ey::Core::Client::Mock.for(:user)
    server_options = options[:server] || {}

         = server_options.fetch(:account_name, SecureRandom.hex(4))
    location         = server_options.fetch(:location, "us-west-2")
    environment_name = server_options.fetch(:environment_name, SecureRandom.hex(4))

     = client.accounts.create!(name: )

    provider = .providers.create!(type: :aws)
    environment = create_environment(account: , name: environment_name)
    environment.servers.first
  when :user
    user = Ey::Core::Client::Mock.for(:consumer).users.create!(
      {
        :name  => "#{SecureRandom.hex(3)} #{SecureRandom.hex(3)}",
        :email => "#{SecureRandom.hex(8)}@example.org",
      }.merge(options[:user] || {})
    )
    Ey::Core::Client.new(token: user.token)
  when :consumer
    Ey::Core::Client.new({auth_id: SecureRandom.hex(8), auth_key: SecureRandom.hex(16)}.merge(options[:consumer] || {}))
  end
end

.reset!Object



185
186
187
188
189
# File 'lib/ey-core/client/mock.rb', line 185

def self.reset!
  @data = nil
  @serial_id = 1
  @delay = 1
end

.support_trial_active(client, account_id, started_at = nil, expires_at = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/ey-core/requests/get_support_trial.rb', line 41

def self.support_trial_active(client, , started_at = nil, expires_at = nil)
  support_trial_elibigle(client, )
  started_at ||= Time.now
  expires_at ||= started_at + 60 * 60 * 24 * 30 #30 days
  client.data[:support_trials][].merge!(
    "started_at" => started_at,
    "expires_at" => expires_at,
  )
end

.support_trial_elibigle(client, account_id) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/ey-core/requests/get_support_trial.rb', line 31

def self.support_trial_elibigle(client, )
   = client.url_for("/accounts/#{}")
  client.data[:accounts][]["support_trial"] = "#{}/support_trial"
  client.data[:support_trials][] ||= {
    "id"         => Cistern::Mock.random_numbers(4),
    "started_at" => nil,
    "expires_at" => nil,
  }
end

Instance Method Details

#acknowledge_alert(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/acknowledge_alert.rb', line 14

def acknowledge_alert(params={})
  id = params.delete("id")

  if alert = self.data[:alerts][id]
    alert["acknowledged"] = true

    response(
      :body => {"legacy_alert" => alert},
      :status => 200
    )
  else
    response(status: 404)
  end
end

#add_agent(cluster, options = {}) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/ey-core/client/mock.rb', line 288

def add_agent(cluster, options={})
  deis_cluster = find(:deis_clusters, cluster.identity)

  host = options[:host] || self.ip_address

  agent_id = SecureRandom.uuid
  agent = self.data[:agents][agent_id] = {
    "id"           => agent_id,
    "alerts"       => url_for("agents", agent_id, "alerts"),
    "cluster"      => url_for("deis-clusters", deis_cluster["id"]),
    "created_at"   => Time.now.to_s,
    "host"         => host,
    "last_seen_at" => options.fetch(:last_seen_at, Time.now.to_s),
    "updated_at"   => Time.now.to_s,
  }

  self.agents.new(agent)
end

#apply_environment_updates(options = {}) ⇒ 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
# File 'lib/ey-core/requests/apply_environment_updates.rb', line 17

def apply_environment_updates(options={})
  id          = options.delete("id")
  servers     = get_servers("environment" => id).body["servers"]
  request_id  = self.uuid

  servers.each do |server|
    update_id = self.uuid
    self.data[:instance_updates][update_id] = {
      "id"          => update_id,
      "instance_id" => server["id"],
      "data"        => {
        "type" => options["type"] || "main",
      }
    }
  end

  request = {
    "id"           => request_id,
    "type"         => "configure_environment",
    "successful"   => true,
    "started_at"   => Time.now,
    "finished_at"  => Time.now,
    "resource_url" => nil,
    "resource"     => nil,
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body   => {"request" => response_hash},
    :status => 201,
  )
end

#apply_server_updates(options = {}) ⇒ 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
# File 'lib/ey-core/requests/apply_server_updates.rb', line 17

def apply_server_updates(options={})
  id         = options.delete("id")
  server     = self.data[:servers][id]
  request_id = self.uuid
  update_id  = self.uuid

  self.data[:instance_updates][update_id] = {
    "id"          => update_id,
    "instance_id" => server["id"],
    "data"        => {
      "type" => options["type"] || "main",
    }
  }

  request = {
    "id"           => request_id,
    "type"         => "instance_update",
    "successful"   => true,
    "started_at"   => Time.now,
    "finished_at"  => Time.now,
    "resource_url" => nil,
    "resource"     => nil,
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body   => {"request" => response_hash},
    :status => 201,
  )
end

#attach_address(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/attach_address.rb', line 15

def attach_address(params={})
  resource_id = params.delete("id")
  server_id   = params.delete("server").to_i
  request_id  = self.uuid

  server = self.find(:servers, server_id)
  server.merge!(
    "address_url" => "/addresses/#{resource_id}",
  )

  resource = self.data[:addresses][resource_id]
  resource.merge!(
    "server_url" => "/servers/#{server_id}",
  )

  request = {
    "id"          => request_id,
    "type"        => "attach_address",
    "successful"  => "true",
    "started_at"  => Time.now,
    "finished_at" => nil,
    "resource"    => [:addresses, resource_id, resource],
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 200,
    :headers => {
      "Content-Type" => "application/json; charset=utf8"
    }
  )
end

#authorized_channel(path) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/ey-core/requests/authorized_channel.rb', line 12

def authorized_channel(path)
  if task = self.data[:tasks].values.find { |t| Addressable::URI.parse(t["read_channel"]).query_values["subscription"] == path }
    response(
      :body => {"task" => task},
    )
  else
    response(status: 404, :body => {"errors" => ["Couldn't find Awsm::Task with [channel_path: #{path}]"]})
  end
end

#block_device_mapObject



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
# File 'lib/ey-core/requests/create_server.rb', line 91

def block_device_map(params={})
  [
    {
      "mount"                 => "/",
      "file_system"           => "ext4",
      "device"                => "/dev/sda",
      "size"                  => 100,
      "delete_on_termination" => true,
      "volume_type"           => "gp2",
    },
    {
      "mount"                 => "/mnt",
      "file_system"           => "ext4",
      "device"                => "/dev/sdo",
      "delete_on_termination" => true,
      "size"                  => params["mnt_volume_size"] || 100,
      "volume_type"           => "gp2",
    },
    {
      "mount"                 => nil,
      "file_system"           => "swap",
      "device"                => "/dev/sdp",
      "delete_on_termination" => true,
      "size"                  => 8,
      "volume_type"           => "gp2",
    },
    { "device" => "/dev/sdb", "name"   => "ephemeral0", },
  ]
end

#blueprint_environment(params = {}) ⇒ 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
# File 'lib/ey-core/requests/blueprint_environment.rb', line 17

def blueprint_environment(params={})
  params        = Cistern::Hash.stringify_keys(params)
  id            = params.delete("id")
  environment   = find(:environments, id)
  blueprint_id  = self.uuid
  servers       = self.data[:servers].values.select { |s| s["environment"] == url_for("/environments/#{id}") && s["deleted_at"].nil? }
  volumes       = servers.inject({}) do |hash,server|
    hash.merge!(server["id"] => self.data[:volumes].values.select { |v| v["server"] == url_for("/servers/#{server["id"]}") })
    hash
  end
  instances = {}
  instances["apps"]      = servers.select { |s| %w(app_master app solo).include?(s["role"]) }
  instances["db_master"] = servers.select { |s| s["role"] == "db_master" }
  instances["db_slaves"] = servers.select { |s| s["role"] == "db_slave" }
  instances["utils"]     = servers.select { |s| s["role"] == "util" }

  %w(apps db_master db_slaves utils).each do |type|
    mapped_types = instances[type].map do |server|
      volume     = volumes[server["id"]].first
      mnt_volume = server["devices"].detect { |d| d["mount"] == "/mnt" }
      {
        "encrypted"       => volume && volume["encrypted"],
        "flavor"          => server["flavor"]["id"],
        "mnt_volume_size" => mnt_volume["size"],
        "name"            => server["name"],
        "volume_iops"     => volume["iops"],
        "volume_size"     => volume["size"]
      }
    end
    instances["blueprint_#{type}"] = mapped_types
  end

  blueprint = {
    "id"          => blueprint_id,
    "account"     => environment["account"],
    "environment" => url_for("/environments/#{id}"),
    "created_at"  => Time.now,
    "updated_at"  => Time.now,
    "name"        => params["name"],
    "data"        => {
      "app_instances" => instances["blueprint_apps"],
      "db_master"     => instances["blueprint_db_master"],
      "db_slaves"     => instances["blueprint_db_slaves"],
      "utils"         => instances["blueprint_utils"],
    }
  }

  self.data[:blueprints][blueprint_id] = blueprint

  response(
    :body   => {"blueprint" => blueprint},
    :status => 200,
  )
end

#boot_environment(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/boot_environment.rb', line 15

def boot_environment(params={})
  params        = Cistern::Hash.stringify_keys(params)
  id            = params.delete("id")
  configuration = params["cluster_configuration"]["configuration"]
  request_id    = self.uuid
  environment   = find(:environments, id)

  servers = {}

  if blueprint_id = params["cluster_configuration"]["blueprint_id"]
    blueprint = find(:blueprints, blueprint_id)

    %w(app_instances db_master db_slaves utils).each do |type|
      blueprint_data = blueprint["data"][type]
      role           = case type
                       when "app_instances" then "app"
                       when "db_master"     then "db_master"
                       when "db_slaves"     then "db_slave"
                       when "utils"         then "util"
                       end
      blueprint_data.each_with_index do |server_data,index|
        role_to_use = (type == 'app_instances' && index == 0) ? "app_master" : role
        server = server_hash(role: role_to_use, environment: environment, flavor: server_data["flavor"]["id"], name: server_data["name"])
        if role_to_use == "app_master"
          if ip_id = params["cluster_configuration"]["configuration"]["ip_id"]
            if find(:addresses, ip_id)
              server[:address] = url_for("/addresses/#{ip_id}")
            end
          end
        end
        create_volume(server: server, size: server_data["volume_size"], iops: server_data["volume_iops"])
        servers[server["id"]] = server
      end
    end
  else
    case configuration["type"]
    when "solo"
      server = server_hash(environment: environment)
      servers[server["id"]] = server
    when "cluster"
      %w(app_master app db_master).each do |role|
        server = server_hash(role: role, environment: environment)
        servers[server["id"]] = server
        volume_size, iops = if %w(app_master app).include?(role)
                              [configuration["apps"] ? configuration["apps"]["volume_size"] : 25, nil]
                            elsif %w(db_master db_slave).include?(role)
                              [configuration["db_master"] ? configuration["db_master"]["volume_size"] : 25, configuration["db_master"] ? configuration["db_master"]["iops"] : nil]
                            end
        create_volume(server: server, volume_size: volume_size, iops: iops)
      end
    when "production-cluster"
      %w(app_master app app db_master db_slave).each do |role|
        server = server_hash(role: role, environment: environment)
        servers[server["id"]] = server
        volume_size, iops = if %w(app_master app).include?(role)
                              [configuration["apps"] ? configuration["apps"]["volume_size"] : 25, nil]
                            elsif %w(db_master db_slave).include?(role)
                              [configuration["db_master"] ? configuration["db_master"]["volume_size"] : 25, configuration["db_master"] ? configuration["db_master"]["iops"] : nil]
                            end
        create_volume(server: server, size: volume_size, iops: iops)
      end
    when "custom"
      raise "not implemented"
    end
  end

  self.data[:servers].merge!(servers)

  request = {
    "id"          => request_id,
    "type"        => "boot_environment",
    "successful"  => true,
    "started_at"  => Time.now,
    "finished_at" => nil,
    "resource"    => [:environments, id, environment],
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 200,
    :headers => {
      "Content-Type" => "application/json; charset=utf8"
    }
  )
end

#bootstrap_logical_database(_params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/bootstrap_logical_database.rb', line 14

def bootstrap_logical_database(_params={})
  params = Cistern::Hash.stringify_keys(_params)
  provider_id = resource_identity(params["provider"])
  find(:providers, provider_id)

  logical_database_name = require_parameters(params["logical_database"], "name")
  request_id  = self.uuid
  resource_id = self.uuid

  database_service = params["database_service"]

  if database_service.is_a?(Hash)
    require_parameters(params["database_service"], "name")
    database_service["id"] = self.uuid
  else
    database_service = find(:database_services, database_service)
    existing_database_service = true
  end

  resource = {
    "username"     => "eyuser#{SecureRandom.hex(4)}",
    "password"     => SecureRandom.hex(16),
    "name"         => logical_database_name,
    "id"           => resource_id,
    "resource_url" => "/logical-databases/#{resource_id}",
    "service"      => url_for("/database-services/#{database_service.fetch("id")}"),
    "deleted_at"   => nil,
  }

  request_resource = if existing_database_service
                       { "resource" => [:logical_databases, resource_id, resource] }
                     else
                       {
                         "resource" => [
                           :logical_databases,
                           request_id,
                           create_database_service_resource(
                             "logical_database" => resource,
                             "database_service" => database_service,
                             "database_server"  => params["database_server"].dup,
                             "provider_id"      => provider_id,
                           )
                         ],
                       }
                     end

  request = {
    "id"           => request_id,
    "type"         => "bootstrap_logical_database",
    "successful"   => "true",
    "started_at"   => Time.now,
    "finished_at"  => nil,
    "resource_url" => "/logical-databases/#{resource_id}",
  }.merge(request_resource)

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body   => {"request" => response_hash},
    :status => 201,
  )
end

#cancel_account(resource_id, requested_by_id) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ey-core/requests/cancel_account.rb', line 13

def (resource_id, requested_by_id)
  self.data[:accounts][resource_id]['cancelled_at'] = Time.now
   = self.uuid
  self.data[:accounts][resource_id]['cancellation'] = url_for("/account_cancellations/#{}")
  cancellation = {
    "id" => ,
    "created_at" => Time.now,
    "kind" => "self",
  }
  self.data[:account_cancellations]||= {}
  self.data[:account_cancellations][] = cancellation
  response(
    :body    => {"cancellation" => cancellation},
    :status  => 200,
    :headers => {
      "Content-Type" => "application/json; charset=utf8"
    }
  )
end

#change_environment_maintenance(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ey-core/requests/change_environment_maintenance.rb', line 17

def change_environment_maintenance(options={})
  find(:environments, resource_identity(params))
  request_id = self.uuid

  request = {
    "id"           => request_id,
    "type"         => "app_deployment_maintenance",
    "successful"   => "true",
    "started_at"   => Time.now,
    "finished_at"  => nil,
    "resource_url" => nil
  }

  self.data[:requests][request_id] = request

  response(
    :body   => {"request" => request},
    :status => 200,
  )
end

#create_account(params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ey-core/requests/create_account.rb', line 13

def (params={})
  resource_id = self.uuid
  owner_id    = params["owner"] || @current_user && @current_user["id"]

  find(:users, owner_id)

  if name_prefix = params["account"].delete("name_prefix")
    params["account"]["name"] = "#{name_prefix}-#{resource_id[0,4]}"
  end

  resource = (resource_id, params["account"].dup)

  self.data[:accounts][resource_id] = resource.merge(:account_users => [owner_id], :account_owners => [owner_id])

  resource.merge!(
    :environments_url => url_for("/accounts/#{resource_id}/environments"),
    :applications_url => url_for("/accounts/#{resource_id}/applications")
  )

  response(
    :body    => {"account" => resource},
    :status  => 201,
  )
end

#create_addon(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ey-core/requests/create_addon.rb', line 14

def create_addon(params={})
  url        = params.delete("url") or raise "URL needed"
   = path_params(url)["accounts"] or raise "Need account id, not parsed from #{url}"

  resource_id = self.serial_id

  resource = params["addon"].merge(
    "id"                 => resource_id,
    "addon_attachments"  => url_for("/accounts/#{}/addons/#{resource_id}/attachments"),
    "account"            => url_for("/accounts/#{}"),
    "vars"               => normalize_hash(params["addon"]["vars"] || {}),
  )

  self.data[:addons][resource_id] = resource

  response(
    :body    => {"addon" => resource},
    :status  => 201,
  )
end

#create_address(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_address.rb', line 13

def create_address(params={})
  request_id  = self.uuid
  resource_id = self.serial_id
  provider_id = params["provider"]
  scope       = params["scope"] || "standard"

  self.find(:providers, provider_id)

  resource = params["address"].dup
  ip_address = resource["provisioned_id"] || self.ip_address
  resource.merge!(
    "id"             => resource_id,
    "provisioned_id" => ip_address,
    "ip_address"     => ip_address,
    "resource_url"   => "/addresses/#{resource_id}",
    "provider"       => url_for("/providers/#{provider_id}")
  )

  request = {
    "id"          => request_id,
    "type"        => "provision_address",
    "successful"  => "true",
    "started_at"  => Time.now,
    "finished_at" => nil,
    "resource"    => [:addresses, resource_id, resource],
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 201,
    :headers => {
      "Content-Type" => "application/json; charset=utf8"
    }
  )
end

#create_alert(_params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_alert.rb', line 16

def create_alert(_params={})
  params = Cistern::Hash.stringify_keys(_params)
  extract_url_params!(params)

  database_server_id = resource_identity(params["database_server"] || params["database_server_id"])
  server_id          = resource_identity(params["server"]          || params["server_id"])
  agent_id           = resource_identity(params["agent"]           || params["agent_id"])

  alert_params = require_parameters(params, "alert")
  name, external_id, message, severity = require_parameters(alert_params, *%w[name external_id message severity])
  resource_id = self.uuid

  alert = {
    "id"           => resource_id,
    "created_at"   => Time.now,
    "updated_at"   => Time.now,
    "deleted_at"   => nil,
    "severity"     => severity,
    "acknowledged" => alert_params.fetch("acknowledged", false),
    "ignored"      => alert_params.fetch("ignored", false),
    "message"      => message,
    "description"  => alert_params["description"],
    "external_id"  => external_id,
    "name"         => name,
    "finished_at"  => alert_params["finished_at"],
    "started_at"   => alert_params["started_at"],
  }

  alert.merge!("resource" =>
               if database_server_id
                 url_for("/database-servers/#{database_server_id}")
               elsif server_id
                 url_for("/servers/#{server_id}")
               elsif agent_id
                 url_for("/agents/#{agent_id}")
               else
                 raise response(status: 422, body: "Requires a resource")
               end
              )

  self.data[:alerts][resource_id] = alert
  response(
    :body   => {"alert" => alert},
    :status => 201,
  )
end

#create_application(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_application.rb', line 28

def create_application(params={})
  resource_id  = self.serial_id
  url          = params.delete("url")

   = params["account"] || url && path_params(url)["accounts"]

  find(:accounts, )

  resource = params["application"].dup

  unless language = APP_TYPE_LANGUAGES[resource["type"].to_s]
    raise response(status: 422, body: "Unknown app type: #{resource["type"]}")
  end

  resource.merge!(
    "account"               => url_for("/accounts/#{}"),
    "archives"              => url_for("/applications/#{resource_id}/archives"),
    "keypairs"              => url_for("/applications/#{resource_id}/keypairs"),
    "language"              => language,
    "created_at"            => Time.now,
    "updated_at"            => Time.now,
    "id"                    => resource_id,
    "environment_variables" => url_for("/applications/#{resource_id}/environment_variables")
  )

  key = mock_ssh_key

  keypair = {
    "id"          => self.serial_id,
    "application" => url_for("/applications/#{resource_id}"),
    "user"        => nil,
    "fingerprint" => key[:fingerprint],
    "public_key"  => key[:public_key],
    "private_key" => key[:private_key],
  }

  self.data[:keypairs][keypair['id']] = keypair
  self.data[:applications][resource_id] = resource

  response(
    :body   => {"application" => resource},
    :status => 201,
  )
end

#create_application_archive(params = {}) ⇒ 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
# File 'lib/ey-core/requests/create_application_archive.rb', line 17

def create_application_archive(params={})
  resource_id    = self.uuid
  url            = params.delete("url")
  application_id = Integer(params["application"] || (url && path_params(url)["applications"]))

  find(:applications, application_id)

  resource = params["application_archive"].dup

  resource.merge!(
    "id"                => resource_id,
    "application"       => url_for("/applications/#{application_id}"),
    "created_at"        => Time.now,
    "updated_at"        => Time.now,
    "upload_successful" => false,
    "upload_url"        => "http://example.org/#{resource_id}",
    "url"               => "http://example.org/#{resource_id}",
  )

  self.data[:application_archives][resource_id] = resource

  response(
    :body   => {"application_archive" => resource},
    :status => 201,
  )
end

#create_auto_scaling_alarm(options = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_auto_scaling_alarm.rb', line 24

def create_auto_scaling_alarm(options = {})
  params = Cistern::Hash.stringify_keys(options)

  request_id  = self.serial_id
  resource_id = self.uuid

  auto_scaling_policy_id = params.delete("auto_scaling_policy_id")
  auto_scaling_policy = find(:auto_scaling_policies, auto_scaling_policy_id)

  resource = params["auto_scaling_alarm"].dup
  arn = "arn:aws:cloudwatch:eu-west-1:000000000000:metricAlarm:00000000-0000-0000-0000-000000000000:alarmName/#{resource["name"]}"

  now = Time.now

  resource.merge!(
    "id"                  => resource_id,
    "created_at"          => now,
    "auto_scaling_policy" => auto_scaling_policy["id"],
    "provisioned_id"      => arn,
    "resource_url"        => url_for("/auto_scaling_alarms/#{resource_id}")
  )

  request = {
    "id"           => request_id,
    "created_at"   => now - 2,
    "updated_at"   => now,
    "started_at"   => now - 1,
    "finished_at"  => now,
    "message"      => nil,
    "read_channel" => nil,
    "resource"     => [:auto_scaling_alarms, resource_id, resource],
    "resource_url" => url_for("/auto_scaling_alarms/#{resource_id}"),
    "type"         => "provision_auto_scaling_alarm",
    "successful"   => true,
  }

  self.data[:auto_scaling_alarms][resource_id] = resource
  self.data[:requests][request_id] = request

  response(
    :body   => { "request" => request },
    :status => 200
  )
end

#create_auto_scaling_group(options = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_auto_scaling_group.rb', line 16

def create_auto_scaling_group(options={})
  url = options.delete("url")

  environment_id = options.delete("environment")
  environment = find(:environments, environment_id)
  resource_id = self.uuid
  resource = options["auto_scaling_group"].dup

  now = Time.now

  resource.merge!(
    "created_at"       => now,
    "environment"      => url_for("/environments/#{environment_id}"),
    "id"               => resource_id,
    "location_id"      => environment["region"],
    "provisioned_id"   => "#{environment["name"]}#{SecureRandom.uuid}",
    "desired_capacity" => 1,
    "resource_url"     => url_for("/auto_scaling_groups/#{resource_id}"),
  )

  self.data[:auto_scaling_groups][resource_id] = resource

  environment.merge!("auto_scaling_group" => url_for("/auto_scaling_groups/#{resource_id}"))

  request = {
    "created_at"   => now - 2,
    "finished_at"  => now,
    "id"           => self.uuid,
    "message"      => nil,
    "read_channel" => nil,
    "resource"     => [:auto_scaling_groups, resource_id, resource],
    "resource_url" => url_for("/auto_scaling_groups/#{resource_id}"),
    "started_at"   => now - 1,
    "successful"   => true,
    "type"         => "provision_auto_scaling_group",
    "updated_at"   => now,
  }

  self.data[:requests][request["id"]] = request

  response(
    :body   => {"request" => request},
    :status => 200
  )
end

#create_auto_scaling_policy(options = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_auto_scaling_policy.rb', line 22

def create_auto_scaling_policy(options = {})
  params = Cistern::Hash.stringify_keys(options)

  request_id  = self.serial_id
  resource_id = self.uuid

  auto_scaling_group_id = params.delete("auto_scaling_group_id")
  auto_scaling_group = find(:auto_scaling_groups, auto_scaling_group_id)

  resource = params["auto_scaling_policy"].dup
  arn = "scalingPolicy:00000000-0000-0000-0000-000000000000:autoScalingGroupName/#{auto_scaling_group["name"]}:policyName/#{resource["name"]}"

  now = Time.now

  resource.merge!(
    "id"                  => resource_id,
    "created_at"          => now,
    "auto_scaling_group"  => auto_scaling_group["id"],
    "arn"                 => arn,
    "resource_url"        => url_for("/auto_scaling_policies/#{resource_id}"),
    "auto_scaling_alarms" => url_for("/auto_scaling_alarms?auto_scaling_policy=#{resource_id}")
  )

  request = {
    "id"           => request_id,
    "created_at"   => now - 2,
    "updated_at"   => now,
    "started_at"   => now - 1,
    "finished_at"  => now,
    "message"      => nil,
    "read_channel" => nil,
    "resource"     => [:auto_scaling_policies, resource_id, resource],
    "resource_url" => url_for("/auto_scaling_policies/#{resource_id}"),
    "type"         => "provision_auto_scaling_policy",
    "successful"   => true,
  }

  self.data[:auto_scaling_policies][resource_id] = resource
  self.data[:requests][request_id] = request

  response(
    :body   => { "request" => request },
    :status => 200
  )
end

#create_backup_file(params = {}) ⇒ 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
# File 'lib/ey-core/requests/create_backup_file.rb', line 17

def create_backup_file(params={})
  resource_id = self.uuid

  unless resource = params["backup_file"].dup
    response(status: 422)
  end

  unless filename = resource["filename"]
    response(status: 422, body: {"error" => ["Missing required attribute: filename"]})
  end

  backup_id = params["url"].split("/")[-2]

  uuid = self.uuid

  resource.merge!(
    "id"           => resource_id,
    "files"        => url_for("/backup_files/#{resource_id}/files"),
    "mime_type"    => resource["mime_type"] || MIME::Types.type_for(filename).first.to_s,
    # these need to be the same for mocking purposes
    "download_url" => "http://example.org/#{uuid}",
    "upload_url"   => "http://example.org/#{uuid}",
    "backup"       => url_for("/backups/#{backup_id}"),
    "created_at"   => Time.now,
    "updated_at"   => Time.now,
  )

  self.data[:backup_files][resource_id] = resource

  response(
    :body   => {"backup_file" => resource},
    :status => 201,
  )
end

#create_database_server(_params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_database_server.rb', line 14

def create_database_server(_params={})
  params = Cistern::Hash.stringify_keys(_params)

  require_parameters(params, "provider")
  require_parameters(params["database_server"], "replication_source")

  request_id  = self.uuid
  resource_id = self.uuid

  provider_id = resource_identity(params["provider"])
  find(:providers, provider_id)

  replication_source_id = resource_identity(params["database_server"]["replication_source"])
  replication_source    = find(:database_servers, replication_source_id)

  resource = {
    "id"                 => resource_id,
    "resource_url"       => url_for("/database-servers/#{resource_id}"),
    "provisioned_id"     => "replica-#{SecureRandom.hex(3)}",
    "engine"             => replication_source["engine"],
    "version"            => replication_source["version"],
    "storage"            => replication_source["storage"],
    "modifiers"          => replication_source["modifiers"],
    "endpoint"           => replication_source["endpoint"],
    "flavor"             => replication_source["flavor"],
    "location"           => replication_source["location"],
    "deleted_at"         => nil,
    "database_service"   => replication_source["database_service"],
    "replication_source" => url_for("/database-servers/#{replication_source_id}"),
    "alerts"             => url_for("/database-servers/#{resource_id}/alerts"),
    "firewalls"          => url_for("/database-servers/#{resource_id}/firewalls"),
    "provider"           => url_for("/providers/#{provider_id}"),
    "messages"           => url_for("/database-servers/#{resource_id}/messages"),
    "snapshots"          => url_for("/database-servers/#{resource_id}/snapshots"),
    "revisions"          => url_for("/database-servers/#{resource_id}/revisions"),
  }

  request = {
    "id"           => request_id,
    "type"         => "provision_database_server",
    "successful"   => "true",
    "started_at"   => Time.now,
    "finished_at"  => nil,
    "resource_url" => url_for("/database-servers/#{resource_id}"),
    "resource"     => [:database_servers, resource_id, resource],
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 201,
  )
end

#create_database_service(_params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_database_service.rb', line 14

def create_database_service(_params={})
  params = Cistern::Hash.stringify_keys(_params)

  require_parameters(params, "provider")
  provider_id = resource_identity(params["provider"])
  find(:providers, provider_id)

  require_parameters(params["database_service"], "name")
  request_id  = self.uuid
  resource_id = self.uuid

  database_service = params["database_service"].dup
  database_service["id"] = resource_id
  database_server = params["database_server"].dup
  database_server["storage"] ||= 50

  request_block = create_database_service_resource(
    "database_service" => database_service,
    "database_server"  => database_server,
    "provider_id"      => provider_id,
  )

  request = {
    "id"           => request_id,
    "type"         => "provision_database_service",
    "successful"   => "true",
    "started_at"   => Time.now,
    "finished_at"  => nil,
    "resource_url" => database_service["resource_url"],
    "resource"     => [:database_services, request_id, request_block],
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 201,
  )
end

#create_database_service_snapshot(_params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_database_service_snapshot.rb', line 16

def create_database_service_snapshot(_params={})
  params = Cistern::Hash.stringify_keys(_params)

  server_or_service_id = resource_identity(params["url"]) || require_parameters(params, "database_service")

  database_service = if (database_server = self.data[:database_servers][server_or_service_id])
                       find(:database_services, resource_identity(database_server["database_service"]))
                     else
                       find(:database_services, database_service_id)
                     end

  database_server ||= self.data[:database_servers].values.find { |ds| url_for("/database-services/#{database_service["id"]}") == ds["database_service"] }
  database_service_id = database_service["id"]

  find(:database_services, database_service_id)

  request_id  = self.uuid
  resource_id = self.uuid

  resource = {
    "id"                => resource_id,
    "created_at"        => Time.now.to_s,
    "updated_at"        => Time.now.to_s,
    "resource_url"      => url_for("/database-server-snapshots/#{resource_id}"),
    "provisioned_id"    => params["name"] || "rds:#{database_server["provisioned_id"]}-#{Time.now.strftime("%Y-%M-%D-%H")}",
    "database_server"   => url_for("/database-servers/#{database_server["id"]}"),
    "database_service"  => url_for("/database-services/#{database_service_id}"),
    "provider"          => database_service["provider"],
    "provisioned_at"    => Time.now.to_s,
    "logical_databases" => url_for("database-server-snapshots/#{resource_id}/logical-databases"),
  }.merge(Cistern::Hash.slice(database_server, "storage", "engine", "engine_version"))

  request = {
    "id"           => request_id,
    "type"         => "provision_database_server_snapshot",
    "successful"   => "true",
    "started_at"   => Time.now,
    "finished_at"  => nil,
    "resource_url" => url_for("/database-server-snapshots/#{resource_id}"),
    "resource"     => [:database_server_snapshots, resource_id, resource],
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 201,
  )
end

#create_environment(params = {}) ⇒ 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
# File 'lib/ey-core/requests/create_environment.rb', line 17

def create_environment(params={})
  resource_id       = self.serial_id
  app_deployment_id = self.serial_id
  application_id    = params["environment"].delete("application_id")
  url               = params.delete("url")
  application       = find(:applications, application_id)

   ||= params["account"] || url && path_params(url)["accounts"]

  find(:accounts, )

  resource = params["environment"].dup

  internal_key = mock_ssh_key

  resource.merge!(
    "account"               => url_for("/accounts/#{}"),
    "applications"          => url_for("/environments/#{resource_id}/applications"),
    "classic"               => true,
    "clusters"              => url_for("/environments/#{resource_id}/clusters"),
    "created_at"            => Time.now,
    "custom_recipes"        => nil,
    "database_stack"        => "postgres9_4",
    "deployments_url"       => url_for("/environments/#{resource_id}/deployments"),
    "id"                    => resource_id,
    "internal_private_key"  => internal_key[:private_key],
    "internal_public_key"   => internal_key[:public_key],
    "keypairs"              => url_for("/environments/#{resource_id}/keypairs"),
    "logical_databases_url" => url_for("/environments/#{resource_id}/logical-databases"),
    "release_label"         => "stable-v4-2.0.101",
    "servers"               => url_for("/environments/#{resource_id}/servers"),
    "stack_name"            => "nginx_passenger4",
    "updated_at"            => Time.now,
    "username"              => "deploy",
    "service_level"         => "default",
    "environment_variables" => url_for("/environments/#{resource_id}/environment_variables")
  )

  self.data[:environments][resource_id] = resource

  if service_id = params["environment"]["database_service"]
    self.requests.new(create_logical_database(
      "database_service" => service_id,
      "logical_database" => {
        "name" => "#{resource["name"]}_#{application["name"]}"
      }
    ).body["request"]).ready!
  end

  self.data[:application_deployments][app_deployment_id] = {
    :environment_id => resource_id,
    :application_id => application_id,
  }

  response(
    :body   => {"environment" => resource},
    :status => 201,
  )
end

#create_environment_variable(_params = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ey-core/requests/create_environment_variable.rb', line 18

def create_environment_variable(_params={})
  params = Cistern::Hash.stringify_keys(_params)

  resource_id = serial_id
  environment = find(:environments, params["environment_id"])
  application = find(:applications, params["application_id"])

  params["id"] = resource_id
  params["environment_name"] = environment["name"]
  params["environment"] = url_for("/environments/#{environment['id']}")
  params["application_name"] = application["name"]
  params["application"] = url_for("/applications/#{application['id']}")

  self.data[:environment_variables][resource_id] = params

  response(
    :body    => {"environment_variable" => params},
    :status  => 201,
  )
end

#create_firewall(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_firewall.rb', line 16

def create_firewall(params={})
  request_id  = self.uuid
  resource_id = self.serial_id

  provider_id = resource_identity(params["provider"])
  cluster_id  = resource_identity(params["cluster"])

  find(:providers, provider_id)

  cluster  = find(:clusters, cluster_id) if cluster_id

  resource = params["firewall"].dup

  resource.merge!(
    "id"             => resource_id,
    "provisioned_id" => SecureRandom.hex(10),
    "resource_url"   => "/firewalls/#{resource_id}",
    "provider"       => url_for("/providers/#{provider_id}"),
    "clusters"       => url_for("/firewalls/#{resource_id}/clusters"),
    "location"       => resource["location"] || cluster["location"],
    "rules"          => url_for("/firewalls/#{resource_id}/rules"),
    "deleted_at"     => nil,
  )

  self.data[:cluster_firewalls] << [cluster_id, resource_id] if cluster_id

  request = {
    "id"          => request_id,
    "type"        => "provision_firewall",
    "successful"  => "true",
    "started_at"  => Time.now,
    "finished_at" => nil,
    "resource"    => [:firewalls, resource_id, resource],
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 201,
  )
end

#create_firewall_rule(params = {}) ⇒ 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
# File 'lib/ey-core/requests/create_firewall_rule.rb', line 17

def create_firewall_rule(params={})
  request_id = self.uuid
  resource_id = self.serial_id

  firewall_id = resource_identity(params["firewall"])

  find(:firewalls, firewall_id)

  source = params["firewall_rule"]["source"] or return response(status: 422, body: { "errors" => ["missing firewall rule source"] })
  range  = params["firewall_rule"]["port_range"] or return response(status: 422, body: { "errors" => ["missing firewall rule port_range"] })

  resource = {
    "id"           => resource_id,
    "source"       => source.to_s,
    "port_range"   => range.to_s,
    "firewall"     => url_for("/firewalls/#{firewall_id}"),
    "resource_url" => "/firewall-rules/#{resource_id}",
  }

  request = {
    "id"          => request_id,
    "type"        => "provision_firewall_rule",
    "successful"  => "true",
    "started_at"  => Time.now,
    "finished_at" => nil,
    "resource"    => [:firewall_rules, resource_id, resource],
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup

  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 201,
    :headers => {
      "Content-Type" => "application/json; charset=utf8"
    }
  )
end

#create_keypair(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_keypair.rb', line 16

def create_keypair(params={})
  unless current_user
    return response(status: 403)
  end

  unless params["keypair"]["public_key"]
    return response(status: 422)
  end

  resource_id = self.serial_id

  resource = {
    "id"          => resource_id,
    "application" => nil,
    "fingerprint" => mock_ssh_key[:fingerprint],
    "user"        => url_for("/users/#{current_user["id"]}"),
  }.merge!(params["keypair"])

  self.data[:keypairs][resource_id] = resource

  response(
    :body   => {"keypair" => resource},
    :status => 201,
  )
end

#create_keypair_deployment(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ey-core/requests/create_keypair_deployment.rb', line 17

def create_keypair_deployment(params={})
  resource_id    = self.serial_id
  keypair_id     = params["keypair"]
  environment_id = params["environment"]

  resource  =  {
    "id"      => resource_id,
    "keypair" => url_for("/keypairs/#{keypair_id}"),
    "target"  => url_for("/environments/#{environment_id}"),
  }

  self.data[:keypair_deployments][resource_id] = resource

  response(
    :body   => {"keypair_deployment" => resource},
    :status => 201,
  )
end

#create_load_balancer(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_load_balancer.rb', line 16

def create_load_balancer(params={})
  request_id  = self.uuid
  resource_id = self.serial_id

  provider_id    = params.delete("provider")
  firewall_id    = params.delete("firewall")

  find(:providers, provider_id)
  find(:firewalls, firewall_id) if firewall_id

  resource = params["load_balancer"].dup

  resource.merge!(
    "id"             => resource_id,
    "provisioned_id" => SecureRandom.hex(10),
    "resource_url"   => "/load_balancers/#{resource_id}",
    "provider"       => url_for("/providers/#{provider_id}"),
    "location"       => resource["location"],
    "name"           => resource["name"],
  )
  resource.merge!("firewall" => url_for("/firewalls/#{firewall_id}")) if firewall_id

  request = {
    "id"          => request_id,
    "type"        => "provision_load_balancer",
    "successful"  => true,
    "started_at"  => Time.now,
    "finished_at" => nil,
    "resource"    => [:load_balancers, resource_id, resource],
  }

  self.data[:requests][request_id] = request
  self.data[:load_balancers][resource_id] = resource

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body => {"request" => response_hash},
    :status => 201,
    :headers => {
      "Content-Type" => "application/json; charset=utf8",
    }
  )
end

#create_log(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_log.rb', line 27

def create_log(params={})
  log_id = self.uuid
  log_params = params["log"]

  filename = log_params["filename"]

  log = {
    "id"               => log_id,
    "filename"         => filename,
    "download_url"     => "http://s3.amazon.com/#{filename}",
    "uplaod_url"       => nil,
    "created_at"       => Time.now,
    "updated_at"       => Time.now,
    "deleted_at"       => nil,
    "mime_type"        => params[:mime_type] || "application/json",
    "component_action" => nil, # @fixme support genuine component actions
    "server"           => params["server"],
  }

  self.data[:logs][log_id] = log
  response(
    :body => {"log" => log},
    :status => 201
  )
end

#create_logical_database(_params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_logical_database.rb', line 14

def create_logical_database(_params={})
  params = Cistern::Hash.stringify_keys(_params)
  database_service_id = resource_identity(params["url"]) || require_parameters(params, "database_service")
  logical_database_name = require_parameters(params["logical_database"], "name")

  request_id  = self.uuid
  resource_id = self.serial_id

  find(:database_services, database_service_id)

  resource = {
    "name" => logical_database_name,
    "username" => "eyuser#{SecureRandom.hex(4)}",
    "password" => SecureRandom.hex(16),
  }.merge(params["logical_database"]).merge(
    "id"           => resource_id,
    "resource_url" => "/logical-databases/#{resource_id}",
    "service"      => url_for("/database-services/#{database_service_id}"),
    "deleted_at"   => nil,
    )

  request = {
    "id"           => request_id,
    "type"         => "provision_logical_database",
    "successful"   => "true",
    "started_at"   => Time.now,
    "finished_at"  => nil,
    "resource_url" => resource["resource_url"],
    "resource"     => [:logical_databases, resource_id, resource],
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 201,
  )
end

#create_membership(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_membership.rb', line 16

def create_membership(params={})
  id = self.uuid

  requester_id = @current_user && @current_user["id"]

  membership = {
    "id"           => id,
    "account"      => url_for("/accounts/"+params["membership"]["account"]),
    "requester"    => url_for("/users/"+requester_id),
    "role"         => params["membership"]["role"],
    "email"        => params["membership"]["email"],
    "redirect_url" => params["membership"]["redirect_url"]
    #NOTE missing attributes:, created_at, updated_at, deleted_at, requester_url
    #also "implied" attribute: accepted = false
  }

  if params["membership"]["user"]
    user = find(:users, params["membership"]["user"])
    membership.merge!({
      "user"  => url_for("/users/"+user["id"]),
      "email" => user["email"],
    })
  end

  self.data[:memberships][id] = membership
  response(
    :body => {"membership" => membership},
    :status => 201
  )
end

#create_message(_params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ey-core/requests/create_message.rb', line 15

def create_message(_params={})
  params, _ = require_arguments(_params, "url")

  extract_url_params!(params)

  message_id = self.uuid

  params.merge!(
    "id"         => message_id,
    "created_at" => Time.now,
  )

  self.data[:messages][message_id] = params

  response(
    :body   => {"message" => params},
    :status => 201,
  )
end

#create_password_reset(_params) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ey-core/requests/create_password_reset.rb', line 15

def create_password_reset(_params)
  params = Cistern::Hash.stringify_keys(_params)

  unless self.data[:users].map{ |_, user| user["email"] }.include?(params["email"])
    response(
      :body => {
        :errors => ["Could not find User with email \"#{params["email"]}\""]
      },
      :status => 404
    )
  end

  response(
    :body => {
      "password_reset" => {
        "sent" => true
      },
    },
    :status  => 201,
  )
end

#create_provider(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_provider.rb', line 23

def create_provider(params={})
  resource_id    = self.serial_id
  request_id     = self.uuid
       = params["account"]
  url            = params.delete("url")

   ||= url && path_params(url)["accounts"]

  find(:accounts, )

  provider_params = params["provider"]

  provider = {
    "account"            => url_for("/accounts/#{}"),
    "id"                 => resource_id,
    "resource_url"       => url_for("/providers/#{resource_id}"),
    "provider_locations" => url_for("/providers/#{resource_id}/locations"),
    "storages"           => url_for("/providers/#{resource_id}/storages"),
    "servers"            => url_for("/providers/#{resource_id}/servers"),
    "untracked_servers"  => url_for("/providers/#{resource_id}/untracked-servers"),
    "provisioned_id"     => provider_params["provisioned_id"] || SecureRandom.uuid,
    "created_at"         => Time.now,
    "updated_at"         => Time.now,
    "type"               => provider_params["type"].to_s,
    "credentials"        => provider_params["credentials"],
    "shared"             => provider_params.fetch(:shared, false),
  }

  unless ["aws", "azure"].include?(provider["type"])
    response(status: 422, body: {"errors" => ["Unknown provider type: #{provider["type"]}"]})
  end

  self.data[:possible_provider_locations][provider["type"].to_s].each do |location|
    provider_location_id = self.uuid
    provider_location = {
      "id"          => provider_location_id,
      "location_id" => location["id"],
      "provider"    => url_for("/providers/#{resource_id}"),
      "name"        => location["name"],
      "data"        => {},
    }
    self.data[:provider_locations][provider_location_id] = provider_location
  end

  request = provider.merge(
    "resource"    => [:providers, resource_id, provider],
    "type"        => "provider",
    "started_at"  => Time.now,
    "finished_at" => nil,
    "successful"  => "true",
    "id"          => request_id,
  )

  self.data[:requests][request_id]   = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 201,
    :headers => {
      "Content-Type" => "application/json; charset=utf8"
    }
  )
end

#create_server(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_server.rb', line 14

def create_server(params={})
  params = Cistern::Hash.stringify_keys(params)
  server = params["server"]

  require_parameters(params, "environment")
  require_parameters(params["server"], "role", "location", "flavor")
  if server["role"] == "util"
    require_parameters(params["server"], "name")
  end

  request_id     = self.uuid
  resource_id    = self.serial_id
  provisioned_id = "i-#{SecureRandom.hex(4)}"
  environment_id = resource_identity(params["environment"])
  environment    = find(:environments, environment_id)
          = resource_identity(environment["account"])
  provider       = self.data[:providers].values.detect { |p| p["account"] == url_for("/accounts/#{}") }

  servers = self.data[:servers].values.select { |s| s["environment"] == url_for("/environments/#{environment_id}") }

  if servers.count == 1 && servers.first["role"] == "solo" && server["role"] == "db_slave"
    response(
      :status => 422,
      :body   => {"errors" => ["Cannot add a DB Slave"]},
    )
  end

  resource = {
    "account"          => url_for("/accounts/#{}"),
    "alerts"           => url_for("/servers/#{resource_id}/alerts"),
    "created_at"       => Time.now.to_s,
    "dedicated"        => !!server["dedicated"],
    "deprovisioned_at" => nil,
    "devices"          => block_device_map(server),
    "enabled"          => true,
    "environment"      => url_for("/environments/#{environment["id"]}"),
    "flavor"           => { "id" => server["flavor"] },
    "id"               => resource_id,
    "location"         => environment["region"],
    "logs"             => url_for("/servers/#{resource_id}/logs"),
    "name"             => server["name"],
    "private_hostname" => "#{provisioned_id}.private.example.org",
    "provider"         => url_for("/providers/#{provider["id"]}"),
    "provisioned_at"   => Time.now.to_s,
    "provisioned_id"   => provisioned_id,
    "public_hostname"  => "#{provisioned_id}.public.example.org",
    "public_key"       => mock_ssh_key[:fingerprint],
    "ssh_port"         => 22,
    "state"            => "running",
    "token"            => SecureRandom.hex(16),
    "updated_at"       => Time.now.to_s,
    "volumes"          => url_for("/servers/#{resource_id}/volumes"),
    "role"             => server["role"],
    "resource_url"     => url_for("/servers/#{resource_id}"),
  }

  request = {
    "id"           => request_id,
    "type"         => "provision_server",
    "successful"   => "true",
    "started_at"   => Time.now,
    "finished_at"  => nil,
    "resource_url" => url_for("/servers/#{resource_id}"),
    "resource"     => [:servers, resource_id, resource],
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body   => {"request" => response_hash},
    :status => 201,
  )
end

#create_ssl_certificate(params = {}) ⇒ 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
77
78
79
80
81
# File 'lib/ey-core/requests/create_ssl_certificate.rb', line 17

def create_ssl_certificate(params={})
  url = params.delete("url")
   = params["account"] || url && path_params(url)["accounts"]

  find(:accounts, )

  resource_id = self.uuid

  resource = params["ssl_certificate"].dup

  if resource.delete("self_sign")
    key  = OpenSSL::PKey::RSA.new(1024)
    name = OpenSSL::X509::Name.parse("/CN=example.org")

    # NB: the order in which these attributes are set seems to be
    # important in making it self-signed and not just a certificate
    # with a mystery issuer. It's not clear which attributes have
    # the ordering requirement.
    cert = OpenSSL::X509::Certificate.new
    cert.version    = 2
    cert.serial     = Time.now.tv_sec            # monotonically increasing
    cert.not_before = Time.now - (7 * 24 * 60 * 60) # allow for plenty of clock skew
    cert.not_after  = Time.now + (10 * 356 * 24 * 60 * 60) # allow for plenty of clock skew
    cert.subject    = name
    cert.public_key = key.public_key
    cert.issuer     = name

    cert.sign(key, OpenSSL::Digest::SHA1.new)

    resource["public_certificate"] = cert.to_pem
    resource["private_key"]        = key.to_pem
    resource["self_signed"]        = true
  elsif ! resource['public_certificate'] or ! resource['private_key']
    return response(status: 422, body: {"errors" => ["public_certificate and private_key must not be blank."]})
  end

  resource.merge!(
    "account"      => url_for("/accounts/#{}"),
    "created_at"   => Time.now,
    "id"           => resource_id,
    "updated_at"   => Time.now,
    "resource_url" => url_for("/ssl_certificates/#{resource_id}"),
  )

  request = {
    "account"      => url_for("/accounts/#{}"),
    "created_at"   => Time.now,
    "finished_at"  => nil,
    "id"           => self.uuid,
    "message"      => nil,
    "read_channel" => nil,
    "resource"     => [:ssl_certificates, resource_id, resource],
    "started_at"   => Time.now,
    "successful"   => true,
    "type"         => "provision_ssl_certificate",
    "updated_at"   => Time.now,
  }

  self.data[:requests][request["id"]] = request

  response(
    :body    => {"request" => request},
    :status  => 201
  )
end

#create_storage(params = {}) ⇒ 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
# File 'lib/ey-core/requests/create_storage.rb', line 17

def create_storage(params={})
  extract_url_params!(params)

  request_id  = self.uuid
  resource_id = self.uuid

  provider_id  = resource_identity(params["provider"])
  provider_url = url_for("/providers/#{provider_id}")

  provider = find(:providers, provider_id)

  resource = params["storage"].dup

  resource.merge!({
    "id"                => resource_id,
    "name"              => resource["name"],
    "location"          => resource["location"],
    "provisioned_id"    => self.uuid,
    "provider"          => provider_url,
    "storage_users_url" => url_for("/storages/#{resource_id}"),
    "resource_url"      => "/storages/#{resource_id}"
  })

  request = {
    "id"          => request_id,
    "type"        => "provision_provider_storage",
    "successful"  => true,
    "started_at"  => Time.now,
    "finished_at" => nil,
    "resource"    => [:storages, resource_id, resource],
  }

  self.data[:requests][request_id]  = request
  self.data[:storages][resource_id] = resource

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 201,
    :headers => {
      "Content-Type" => "application/json; charset=utf8",
    }
  )
end

#create_storage_user(params = {}) ⇒ 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
# File 'lib/ey-core/requests/create_storage_user.rb', line 17

def create_storage_user(params={})
  extract_url_params!(params)

  request_id  = self.uuid
  resource_id = self.uuid

  storage_id  = resource_identity(params["storage"])
  storage_url = url_for("/storages/#{storage_id}")

  storage = find(:storages, storage_id)

  resource = params["storage_user"].dup

  resource.merge!({
    "id"             => resource_id,
    "username"       => resource["username"],
    "provisioned_id" => self.uuid,
    "access_id"      => SecureRandom.hex(16),
    "secret_key"     => SecureRandom.hex(32),
    "storage"        => storage_url,
    "resource_url"   => "/storages/#{storage_id}/storage-users/#{resource_id}"
  })

  request = {
    "id"          => request_id,
    "type"        => "provision_provider_storage_credential",
    "successful"  => true,
    "started_at"  => Time.now,
    "finished_at" => nil,
    "resource"    => [:storage_users, resource_id, resource],
  }

  self.data[:requests][request_id]  = request
  self.data[:storage_users][resource_id] = resource

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 201,
    :headers => {
      "Content-Type" => "application/json; charset=utf8",
    }
  )
end

#create_task(params = {}) ⇒ 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
# File 'lib/ey-core/requests/create_task.rb', line 17

def create_task(params={})
  request_id = self.uuid
  resource_id = self.uuid
  url = params["url"]

  cluster_component_id = url && path_params(url)["cluster_components"]
  cluster_component_id ||= params.delete("cluster_component")

  self.find(:cluster_components, cluster_component_id)

  task = {
    "finished_at"       => nil,
    "id"                => request_id,
    "started_at"        => Time.now,
    "successful"        => true,
    "read_channel"      => "https://messages.engineyard.com/stream?subscription=/tasks/#{request_id[0..7]}&token=#{SecureRandom.hex(6)}",
    "cluster_component" => url_for("/cluster-components/#{cluster_component_id}"),
  }

  request = task.merge(
    "resource"    => [:tasks, resource_id, task],
    "type"        => "task",
    "finished_at" => nil,
    "successful"  => nil,
  )

  self.data[:requests][request_id] = request
  self.data[:tasks][request_id]    = task

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body   => {"request" => response_hash},
    :status => 201,
  )
end

#create_token(params = {}) ⇒ Object



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

def create_token(params={})
  response(
    :body    => {"token" => {"auth_id" => self.uuid}},
    :status  => 201,
    :headers => {
      "Content-Type" => "application/json; charset=utf8"
    }
  )
end

#create_untracked_address(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_untracked_address.rb', line 14

def create_untracked_address(params={})
  url         = params["url"]

  provider_id ||= params["provider"] || (url && path_params(url)["providers"])
  find(:providers, provider_id)

  resource = params["untracked_address"].dup

  require_parameters(resource, "location", "provisioned_id", "provisioner_id")
  existing_address = self.data[:addresses].find {|id, a| a['provisioned_id'] == resource["provisioned_id"] }

  if existing_address
    existing_id, existing_address = existing_address
    response(
      :body => {"errors" => ["Address '#{resource['provisioned_id']}' is tracked."],
                "address" => url_for("/addresses/#{existing_id}")},
      :status => 409,
    )
  else
    resource_id = self.serial_id
    self.data[:addresses][resource_id] = resource.merge(
      "provider" => provider_id, "id" => resource_id, "resource_url" => "/addresses/#{resource_id}")

    resource.merge!(
      "provider" => url_for("/providers/#{provider_id}"),
      "location" => resource['location'],
      "address" => url_for("/addresses/#{resource_id}"),
    )

    response(
      :body   => { "untracked_address" => resource },
      :status => 201,
    )
  end
end

#create_untracked_server(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ey-core/requests/create_untracked_server.rb', line 14

def create_untracked_server(params={})
  resource_id = self.serial_id
  url         = params["url"]

  provider_id ||= params["provider"] || (url && path_params(url)["providers"])

  find(:providers, provider_id)

  resource = params["untracked_server"].dup

  require_parameters(resource, "location", "provisioned_id", "provisioner_id")

  resource.merge!(
    "provider" => url_for("/providers/#{provider_id}"),
    "id"       => resource_id,
  )

  self.data[:untracked_servers][resource_id] = resource

  response(
    :body   => { "untracked_server" => resource },
    :status => 201,
  )
end

#create_user(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/create_user.rb', line 13

def create_user(params={})
  if self.data[:users].map{ |_, user| user["email"] }.include?(params["user"]["email"])
    response(
      :status => 422,
      :body => {
        :errors => ["Email has already been taken"]
      }
    )
  end

  resource_id   = self.uuid
  token_id      = self.uuid
  token_auth_id = self.uuid

  resource = params["user"].dup

  resource.merge!({
    "id"            => resource_id,
    "accounts"      => url_for("/users/#{resource_id}/accounts"),
    "memberships"   => url_for("/users/#{resource_id}/memberships"),
    "keypairs"      => url_for("/users/#{resource_id}/keypairs"),
    "tokens"        => url_for("/users/#{resource_id}/tokens"),
    "environments"  => url_for("/accounts/#{resource_id}/environments"),
    "applications"  => url_for("/accounts/#{resource_id}/applications"),
    "api_token"     => SecureRandom.hex(16),
  })

  self.data[:tokens][token_id] = {
    "id"           => token_id,
    "auth_id"      => token_auth_id,
    "on_behalf_of" => url_for("/users/#{resource_id}"),
  }

  self.data[:users][resource_id] = resource

  response(
    :body    => {"user" => resource},
    :status  => 201,
  )
end

#create_volume(params = {}) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/ey-core/requests/boot_environment.rb', line 173

def create_volume(params={})
  server      = params[:server]
  volume_size = params[:size]
  volume_iops = params[:iops]
  volume_id   = self.serial_id

  self.data[:volumes][volume_id] = {
    "id"     => volume_id,
    "size"   => volume_size,
    "iops"   => volume_iops,
    "server" => url_for("/servers/#{server["id"]}")
  }
end

#current_userHash

Lazily re-seeds data after reset

Returns:

  • (Hash)

    current user response



250
251
252
253
254
255
256
# File 'lib/ey-core/client/mock.rb', line 250

def current_user
  if @current_user
    self.data[:users][@current_user["id"]] ||= @current_user

    @current_user
  end
end

#dataObject



195
196
197
# File 'lib/ey-core/client/mock.rb', line 195

def data
  self.class.data[self.url]
end

#delayObject



191
192
193
# File 'lib/ey-core/client/mock.rb', line 191

def delay
  self.class.delay
end

#deploy_environment_application(options = {}) ⇒ Object



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
# File 'lib/ey-core/requests/deploy_environment_application.rb', line 15

def deploy_environment_application(options={})
  environment_id = options.delete("id")
  application_id = options.delete("application_id")
  request_id     = self.uuid
  deployment_id  = self.serial_id

  response(status: 422) unless self.data[:application_deployments].values.detect { |ad| ad[:environment_id] == environment_id && ad[:application_id] == application_id }

  deployment = {
    "account"         => find(:environments, environment_id)["account"],
    "application"     => url_for("/applications/#{application_id}"),
    "commit"          => options["deploy"]["ref"],
    "environment"     => url_for("/environments/#{environment_id}"),
    "finished_at"     => Time.now,
    "id"              => deployment_id,
    "migrate_command" => options["deploy"]["migrate"] ? (options["deploy"]["migrate_command"] || "rake db:migrate") : nil,
    "migrate"         => options["deploy"]["migrate"] || false,
    "resolved_ref"    => options["deploy"]["ref"],
    "started_at"      => Time.now,
    "successful"      => true
  }

  self.data[:deployments][deployment_id] = deployment

  request = {
    "id"           => request_id,
    "type"         => "app_deployment",
    "successful"   => true,
    "started_at"   => Time.now,
    "finished_at"  => nil,
    "resource_url" => url_for("/environments/#{environment_id}"),
    "resource"     => [:environments, environment_id, find(:environments, environment_id)]
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body   => {"request" => response_hash},
    :status => 201,
  )
end

#deprovision_environment(options = {}) ⇒ Object



14
15
# File 'lib/ey-core/requests/deprovision_environment.rb', line 14

def deprovision_environment(options={})
end

#destroy_addon(params = {}) ⇒ Object



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

def destroy_addon(params={})
  extract_url_params!(params)

  find(:accounts, params["account"])

  addon = find(:addons, params["addon"])
  addon["deleted_at"] = Time.now

  response(status: 204)
end

#destroy_auto_scaling_alarm(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/destroy_auto_scaling_alarm.rb', line 16

def destroy_auto_scaling_alarm(params = {})
  request_id = self.uuid
  url        = params.delete("url")

  auto_scaling_alarm_id = params["id"] || url && url.split('/').last

  auto_scaling_alarm = self.data[:auto_scaling_alarms][auto_scaling_alarm_id]
  auto_scaling_alarm.merge!(
    "deleted_at" => Time.now,
    "resource_url" => url_for("/auto_scaling_alarms/#{auto_scaling_alarm_id}")
  )

  auto_scaling_policy = find(
    :auto_scaling_policies,
    resource_identity(auto_scaling_alarm["auto_scaling_policy"])
  )
  auto_scaling_policy.delete("auto_scaling_alarm")

  self.data[:requests][request_id] = {
    "id"          => request_id,
    "finished_at" => nil,
    "successful"  => "true",
    "started_at"  => Time.now,
    "resource"    => [:auto_scaling_alarms, auto_scaling_alarm_id, auto_scaling_alarm],
    "type"        => "deprovision_auto_scaling_alarm",
  }

  response(
    body:   { "request" => self.data[:requests][request_id] },
    status: 201
  )
end

#destroy_auto_scaling_group(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/destroy_auto_scaling_group.rb', line 16

def destroy_auto_scaling_group(params={})
  request_id = self.uuid
  url        = params.delete("url")

  auto_scaling_group_id = params["id"] || url && url.split('/').last

  auto_scaling_group = self.data[:auto_scaling_groups][auto_scaling_group_id].dup
  auto_scaling_group["deleted_at"] = Time.now
  auto_scaling_group["resource_url"] = url_for("/auto_scaling_groups/#{auto_scaling_group_id}")

  environment = find(:environments, resource_identity(auto_scaling_group["environment"]))
  environment.delete("auto_scaling_group")

  self.data[:requests][request_id] = {
    "id"          => request_id,
    "finished_at" => nil,
    "successful"  => "true",
    "started_at"  => Time.now,
    "resource"    => [:auto_scaling_groups, auto_scaling_group_id, auto_scaling_group],
    "type"        => "deprovision_auto_scaling_group",
  }

  response(
    :body   => {"request" => {id: request_id}},
    :status => 201,
  )
end

#destroy_auto_scaling_policy(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/destroy_auto_scaling_policy.rb', line 16

def destroy_auto_scaling_policy(params = {})
  request_id = self.uuid
  url        = params.delete("url")

  auto_scaling_policy_id = params["id"] || url && url.split('/').last

  auto_scaling_policy = self.data[:auto_scaling_policies][auto_scaling_policy_id]
  auto_scaling_policy.merge!(
    "deleted_at" => Time.now,
    "resource_url" => url_for("/auto_scaling_policies/#{auto_scaling_policy_id}")
  )

  auto_scaling_group = find(
    :auto_scaling_groups,
    resource_identity(auto_scaling_policy["auto_scaling_group"])
  )
  auto_scaling_group.delete("auto_scaling_policy")

  self.data[:requests][request_id] = {
    "id"          => request_id,
    "finished_at" => nil,
    "successful"  => "true",
    "started_at"  => Time.now,
    "resource"    => [:auto_scaling_policies, auto_scaling_policy_id, auto_scaling_policy],
    "type"        => "deprovision_auto_scaling_policy",
  }

  response(
    body:   { "request" => self.data[:requests][request_id] },
    status: 201
  )
end

#destroy_blueprint(params = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/ey-core/requests/destroy_blueprint.rb', line 14

def destroy_blueprint(params={})
  blueprint = find(:blueprints, params["id"])

  self.data[:blueprints].delete(blueprint["id"])

  response(status: 204)
end

#destroy_database_server(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ey-core/requests/destroy_database_server.rb', line 16

def destroy_database_server(params={})
  extract_url_params!(params)
  request_id = self.uuid

  database_server_id = params["id"] || params["database_server"]

  database_server = self.find(:database_servers, database_server_id)

  request = {
    "resource"    => [:database_servers, database_server_id, database_server.merge("deleted_at" => Time.now)],
    "type"        => "deprovision_database_server",
    "started_at"  => Time.now,
    "finished_at" => nil,
    "successful"  => "true",
    "id"          => request_id,
  }

  self.data[:requests][request_id] = request

  response(
    :body   => {"request" => {id: request_id}},
    :status => 201,
  )
end

#destroy_database_server_snapshot(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/destroy_database_server_snapshot.rb', line 15

def destroy_database_server_snapshot(params={})
  request_id = self.uuid
  url        = params.delete("url")

  database_server_snapshot_id = resource_identity(url) || params.fetch("id")

  database_server_snapshot = self.data[:database_server_snapshots][database_server_snapshot_id].dup
  database_server_snapshot["deleted_at"] = Time.now
  database_server_snapshot["resource_url"] = url_for("/database_server_snapshots/#{database_server_snapshot_id}")

  # @todo use a procedure to deprovision clusters as well

  self.data[:requests][request_id] = {
    "id"          => request_id,
    "finished_at" => nil,
    "successful"  => "true",
    "started_at"  => Time.now,
    "resource"    => [:database_server_snapshots, database_server_snapshot_id, database_server_snapshot],
    "type"        => "deprovision_database_server_snapshot",
  }

  response(
    :body   => {"request" => {id: request_id}},
    :status => 201,
  )
end

#destroy_database_service(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/destroy_database_service.rb', line 16

def destroy_database_service(params={})
  extract_url_params!(params)
  request_id = self.uuid

  database_service_id = params["id"] || params["database_service"]

  database_service = self.find(:database_services, database_service_id)

  request = {
    "finished_at" => nil,
    "id"          => request_id,
    "started_at"  => Time.now,
    "successful"  => "true",
    "type"        => "deprovision_database_service",
    "resource"    => [:database_services, database_service_id, lambda do |r|
      database_service.merge!("deleted_at" => Time.now)

      self.data[:database_servers].values.
        select { |ds| ds["database_service"] == url_for("/database-services/#{database_service["id"]}") }.
        each { |ds|
          self.data[:database_server_firewalls].select { |ds_id, _| ds["id"] == ds_id }.
            each { |_, f_id| self.data[:firewalls][f_id].merge!("deleted_at" => Time.now) }
        }.
        each { |ds| ds["deleted_at"] = Time.now }

      self.data[:logical_databases].values.
        select { |ds| ds["service"] == url_for("/database-services/#{database_service["id"]}") }.
        each   { |ds| ds["deleted_at"] = Time.now }

      r.delete("resource_url")
    end],
  }

  self.data[:requests][request_id] = request

  response(
    :body   => {"request" => {id: request_id}},
    :status => 201,
  )
end

#destroy_environment(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/destroy_environment.rb', line 16

def destroy_environment(params={})
  request_id = self.uuid
  url         = params.delete("url")

  environment_id = params["id"] || url && url.split('/').last

  environment = self.data[:environments][environment_id].dup
  environment["deleted_at"] = Time.now
  environment["resource_url"] = url_for("/environments/#{environment_id}")

  # @todo use a procedure to deprovision clusters as well

  self.data[:requests][request_id] = {
    "id"          => request_id,
    "finished_at" => nil,
    "successful"  => "true",
    "started_at"  => Time.now,
    "resource"    => [:environments, environment_id, environment],
    "type"        => "deprovision_environment",
  }

  response(
    :body   => {"request" => {id: request_id}},
    :status => 201,
  )
end

#destroy_firewall(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ey-core/requests/destroy_firewall.rb', line 16

def destroy_firewall(params={})
  extract_url_params!(params)
  request_id = self.uuid

  firewall_id = params["id"] || params["firewall"]

  firewall = self.find(:firewalls, firewall_id)

  request = {
    "type"        => "deprovision_firewall",
    "started_at"  => Time.now,
    "finished_at" => nil,
    "successful"  => "true",
    "id"          => request_id,
  }

  self.data[:requests][request_id] = request.merge(
    "resource"    => [:firewalls, firewall_id, firewall.merge("deleted_at" => Time.now)],
  )

  response(
    :body => {"request" => request},
  )
end

#destroy_firewall_rule(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ey-core/requests/destroy_firewall_rule.rb', line 16

def destroy_firewall_rule(params={})
  extract_url_params!(params)
  request_id = self.uuid

  firewall_rule_id = params["id"] || params["firewall_rule"]

  firewall_rule = self.find(:firewall_rules, firewall_rule_id)

  request = {
    "type"        => "deprovision_firewall_rule",
    "started_at"  => Time.now,
    "finished_at" => nil,
    "successful"  => "true",
    "id"          => request_id,
  }

  self.data[:requests][request_id] = request.merge(
    "resource" => [:firewall_rules, firewall_rule_id, firewall_rule.merge("deleted_at" => Time.now)],
  )

  response(
    :body => {"request" => request},
  )
end

#destroy_load_balancer(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ey-core/requests/destroy_load_balancer.rb', line 16

def destroy_load_balancer(params={})
  request_id = self.uuid
  url = params.delete("url")

  load_balancer_id = params["id"] || url && url.split('/').last

  self.data[:load_balancers][load_balancer_id]["deleted_at"] = Time.now

  response(
    :body   => {"request" => {id: request_id}},
    :status => 201,
  )
end

#destroy_logical_database(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ey-core/requests/destroy_logical_database.rb', line 16

def destroy_logical_database(params={})
  extract_url_params!(params)
  request_id = self.uuid

  logical_database_id = params["id"] || params["logical_database"]

  logical_database = self.find(:logical_databases, logical_database_id)

  request = {
    "resource"    => [:logical_databases, logical_database_id, logical_database.merge("deleted_at" => Time.now)],
    "type"        => "deprovision_logical_database",
    "started_at"  => Time.now,
    "finished_at" => nil,
    "successful"  => "true",
    "id"          => request_id,
  }

  self.data[:requests][request_id] = request

  response(
    :body   => {"request" => {id: request_id}},
    :status => 201,
  )
end

#destroy_provider(id) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ey-core/requests/destroy_provider.rb', line 12

def destroy_provider(id)
  provider   = self.data[:providers][id].dup
  request_id = self.uuid

  provider["cancelled_at"] = Time.now
  provider["resource_url"] = url_for("/providers/#{id}")

  self.data[:requests][request_id] = {
    "id"          => request_id,
    "finished_at" => nil,
    "successful"  => "true",
    "started_at"  => Time.now,
    "resource"    => [:providers, id, provider],
    "type"        => "cancel_provider",
  }

  response(
    :body   => {"request" => {id: request_id}},
    :status => 201,
  )
end

#destroy_server(id, options = {}) ⇒ Object



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
# File 'lib/ey-core/requests/destroy_server.rb', line 14

def destroy_server(id,options={})
  deprovision_procedure = lambda do |_|
    server = self.data[:servers][id]

    server.merge!("deleted_at" => Time.now, "deprovisioned_at" => Time.now)

    if slot_id = resource_identity(server["slot"])
      self.data[:slots][slot_id]["server"] = nil
    end

    self.data[:volumes].values.select {|v| v["server"] == url_for("/servers/#{id}") }.each do |volume|
      volume.merge!("deleted_at" => Time.now)
    end

    nil
  end

  request_id = self.uuid

  self.data[:requests][request_id] = {
    "id"          => request_id,
    "finished_at" => nil,
    "successful"  => "true",
    "started_at"  => Time.now,
    "resource"    => [:servers, id, deprovision_procedure],
    "type"        => "deprovision_server",
  }

  response(
    :body   => {"request" => {id: request_id}},
    :status => 201,
  )
end

#destroy_ssl_certificate(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/destroy_ssl_certificate.rb', line 16

def destroy_ssl_certificate(params={})
  url = params.delete("url")

  ssl_certificate_id = params["id"] || url_params(url)["ssl_certificates"]
  self.data[:ssl_certificates][ssl_certificate_id]["deleted_at"] = Time.now

  response(
    :body   => nil,
    :status => 204,
  )
end

#destroy_storage(id) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ey-core/requests/destroy_storage.rb', line 12

def destroy_storage(id)
  request_id  = self.uuid

  self.data[:requests][request_id] = {
    "id"          => request_id,
    "type"        => "deprovision_provider_storage",
    "successful"  => true,
    "started_at"  => Time.now,
    "finished_at" => nil,
    "resource"    => [:storages, id, self.data[:storages][id].merge("deleted_at" => Time.now)],
  }

  response(
    :body    => {"request" => {id: request_id}},
    :status  => 201,
    :headers => {
      "Content-Type" => "application/json; charset=utf8",
    }
  )
end

#destroy_storage_user(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ey-core/requests/destroy_storage_user.rb', line 14

def destroy_storage_user(params={})
  storage_id = params.delete("storage")
  id         = params.delete("id")

  request_id  = self.uuid

  self.data[:requests][request_id] = {
    "id"          => request_id,
    "type"        => "deprovision_provider_storage_user",
    "successful"  => true,
    "started_at"  => Time.now,
    "finished_at" => nil,
    "resource"    => [:storage_users, id, self.data[:storage_users][id].merge("deleted_at" => Time.now)],
  }

  response(
    :body    => {"request" => {id: request_id}},
    :status  => 201,
    :headers => {
      "Content-Type" => "application/json; charset=utf8",
    }
  )
end

#destroy_user(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/destroy_user.rb', line 16

def destroy_user(params={})
  url = params.delete("url")

  user_id = params["id"] || url && url.split('/').last

  self.data[:users][user_id]["deleted_at"] = Time.now

  response(
    :body   => nil,
    :status => 204,
  )
end

#detach_address(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/detach_address.rb', line 14

def detach_address(params={})
  resource_id = params.delete("id")
  request_id  = self.uuid

  resource = self.data[:addresses][resource_id]
  server_id = resource["server_url"].split("/").last.to_i
  server = self.data[:servers][server_id]

  server["address_url"] = nil
  resource["server_url"] = nil

  request = {
    "id"          => request_id,
    "type"        => "detach_address",
    "successful"  => "true",
    "started_at"  => Time.now,
    "finished_at" => nil,
    "resource"    => [:addresses, resource_id, resource],
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 200,
    :headers => {
      "Content-Type" => "application/json; charset=utf8"
    }
  )
end

#disable_feature(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ey-core/requests/disable_feature.rb', line 15

def disable_feature(params={})
   = params["account"]
  feature_id = params["feature"]["id"]

   = find(:accounts, )
  feature = find(:features, feature_id)

  feature["account"] = nil

  response(
    :body   => nil,
    :status => 200
  )
end

#discover_container_service_deployments(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/discover_container_service_deployments.rb', line 16

def discover_container_service_deployments(params={})
  extract_url_params!(params)

  callback_url  = params.delete("callback_url")
  deployment_id = self.uuid
  resource_id   = self.uuid

  existing_deploymens = self.data[:container_service_deployments].values.select do |deployment|
    deployment["container_service_id"] == container_service["id"]
  end
  existing_deploymens = nil if existing_deploymens.empty?

  resources = existing_deploymens || [{
    "id"                    => deployment_id,
    "container_service_id"  => params["container_service_id"],
    "public_ip_address"     => "123.456.789.123",
    "private_ip_address"    => "10.0.1.2",
    "container_instance_id" => SecureRandom.uuid,
    "port_mappings"         => [{
      "container_port"      => "80",
      "host_port"           => "80",
      "protocol"            => "tcp"
    }],
    "provisioned_id"        => resource_id,
    "health_status"         => "HEALTHY",
    "task_arn"              => SecureRandom.uuid,
    "container_health_status" => [{
      "container_arn"     => SecureRandom.uuid,
      "health_status"     => SecureRandom.uuid
    }]
  }]

  request = {
    "id" => request_id,
    "type" => "discover_container_service_deployments",
    "successful" => true,
    "started_at" => Time.now,
    "finished_at" => nil,
    "resource" => [:container_service_deployments, resource_id, resources, "/container_service_deployments"]
  }

  self.data[:container_service_deployments][deployment_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 200,
    :headers => {
      "Content-Type" => "application/json; charset=utf8"
    }
  )
end

#discover_database_server(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/discover_database_server.rb', line 14

def discover_database_server(params={})
  request_id  = self.uuid
  resource_id = self.uuid
  id          = params["id"]

  database_server = self.data[:database_servers][id]
  old_flavor      = database_server["flavor"]
  new_flavor      = %w(db.m3.medium db.m3.large db.m3.xlarge db.m3.2xlarge) - [old_flavor]
  revisions       = self.data[:database_server_revisions][id]

  unless revisions
    database_server["flavor"]        = new_flavor
    self.data[:database_servers][id] = database_server

    revision = {
      "id"              => resource_id,
      "database_server" => url_for("/database-servers/#{id}"),
      "revision_time"   => Time.now,
      "data"            => {
        "flavor" => {
          "old" => old_flavor,
          "new" => new_flavor,
        },
      },
    }

    self.data[:database_server_revisions][id] = revision
  end

  request = {
    "id"          => request_id,
    "type"        => "discover_database_server",
    "successful"  => true,
    "started_at"  => Time.now,
    "finished_at" => nil,
    "resource"    => [:database_servers, id, database_server],
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body => {"request" => response_hash},
    :status => 200,
    :headers => {
      "Content-Type" => "application/json; charset=utf8"
    }
  )
end

#discover_database_server_snapshots(params = {}) ⇒ 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
# File 'lib/ey-core/requests/discover_database_server_snapshots.rb', line 17

def discover_database_server_snapshots(params={})
  extract_url_params!(params)

  provider     = params.delete('provider')
  resource_id  = self.uuid
  request_id   = self.uuid
  provider_url = url_for("/providers/#{provider}")

  existing_snapshots = self.data[:database_server_snapshots].values.select { |dss| dss["provider"] == provider_url }
  database_server    = self.data[:database_servers].values.detect { |ds| ds["provider"] == provider_url }

  resources = if existing_snapshots.empty?
                if database_server
                  [{
                    "id"              => resource_id,
                    "location"        => database_server["location"],
                    "engine"          => database_server["engine"],
                    "engine_version"  => database_server["version"],
                    "storage"         => database_server["storage"],
                    "provisioned_id"  => resource_id,
                    "provisioned_at"  => Time.now - 3600,
                    "provider"        => provider_url,
                    "database_server" => url_for("/database-servers/#{database_server["id"]}")
                  }]
                else []
                end
              else existing_snapshots
              end

  request = {
    "id" => request_id,
    "type" => "discover_database_server_snapshots",
    "successful" => true,
    "started_at" => Time.now,
    "finished_at" => nil,
    "resource" => [:database_server_snapshots, resource_id, resources, "providers/#{provider}/database-server-snapshots"]
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 200,
    :headers => {
      "Content-Type" => "application/json; charset=utf8"
    }
  )
end

#discover_provider_location(url, location_id) ⇒ Object



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
# File 'lib/ey-core/requests/discover_provider_location.rb', line 15

def discover_provider_location(url, location_id)
  resource_id = self.uuid
  request_id  = self.uuid
  provider_id = url.split("/")[-2]
  provider    = self.data[:providers][provider_id.to_i]
  type        = provider["type"]

  possible_location = self.data[:possible_provider_locations][type].detect { |l| l["id"] == location_id }

  resource = {
    "id"            => resource_id,
    "provider"      => url_for("/providers/#{provider_id}"),
    "location_id"   => location_id,
    "location_name" => possible_location["name"],
    "resource_url"  => "/provider_locations/#{resource_id}"
  }

  self.data[:provider_locations][resource_id] = resource

  request = {
    "id"          => request_id,
    "type"        => "discover_provider_location",
    "successful"  => "true",
    "started_at"  => Time.now,
    "finished_at" => nil,
    "resource"    => [:provider_locations, resource_id, resource],
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body    => {"request" => response_hash},
    :status  => 200,
    :headers => {
      "Content-Type" => "application/json; charset=utf8"
    }
  )
end

#discover_server(options = {}) ⇒ Object



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
# File 'lib/ey-core/requests/discover_server.rb', line 15

def discover_server(options={})
  options = Cistern::Hash.stringify_keys(options)

  provider_id = options.delete("provider")
  request_id = self.uuid

  options["server"]["role"] ||= "app"
  options["server"]["flavor"] ||= "m4.large"

  server = self.data[:servers].values.detect do |s|
    !s["deleted_at"] && s["provider"] == url_for("/providers/#{provider_id}") && s["provisioned_id"] == options["server"]["provisioned_id"]
  end

  resource = if server
               server
             else
               if auto_scaling_group_id = options["auto_scaling_group"]
                 find(:auto_scaling_groups, auto_scaling_group_id)
               end

               s = self.requests.new(create_server(options).body["request"]).resource! # cheating
               self.data[:servers][s.id]
             end

  request = {
    "id"           => request_id,
    "type"         => "discover_server",
    "successful"   => true,
    "started_at"   => Time.now,
    "finished_at"  => nil,
    "resource_url" => url_for("/servers/#{resource["id"]}"),
    "resource"     => [:servers, resource["id"], resource],
  }

  self.data[:requests][request_id] = request

  response_hash = request.dup
  response_hash.delete("resource")

  response(
    :body   => {"request" => response_hash},
    :status => 201,
  )
end

#download_file(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/ey-core/requests/download_file.rb', line 14

def download_file(params={})
  url = params["url"]

  body = File.read(self.data[:temp_files][url])

  response(
    :body   => body,
    :status => 200,
  )
end

#enable_feature(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ey-core/requests/enable_feature.rb', line 15

def enable_feature(params={})
    = params["account"]
  resource_id = params["feature"]["id"]

   = self.data[:accounts][]
  feature = self.data[:features][resource_id]

   = url_for("/accounts/#{}")
  feature["account"] = 

  response(
    :body   => {"feature" => {
                  "id" => feature[:id],
                  "name" => feature[:name],
                  "description" => feature[:description]}
                },
    :status => 200
  )
end

#get_account(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/get_account.rb', line 15

def (params={})
  identity = resource_identity(params)

   = self.find(:accounts, identity)
  resource = .dup

  resource.delete(:users)
  resource.delete(:owners)

  response(
    :body => {"account" => resource},
  )
end

#get_account_cancellation(params = {}) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/ey-core/requests/get_account_cancellation.rb', line 12

def (params={})
  identity = resource_identity(params)

  cancellation = self.find(:account_cancellations, identity)
  response(
    :body => {"cancellation" => cancellation},
  )
end

#get_account_referrals(params = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/get_account_referrals.rb', line 12

def (params={})
  extract_url_params!(params)

  if params["account"]
    params["referrer"] = params.delete("account")
  end

  headers,  = search_and_page(params, :account_referrals, search_keys: %w[referrer])

  response(
    :body    => {"account_referrals" => },
    :status  => 200,
    :headers => headers
  )
end

#get_account_trial(params = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ey-core/requests/get_account_trial.rb', line 12

def (params={})
   = resource_identity(params)

  find(:accounts, )

  resource = self.data[:account_trials][]
  resource ||= {
    "id"         => Cistern::Mock.random_numbers(4),
    "duration"   => 500,
    "used"       => 30,
    "created_at" => Time.now.to_s,
  }
  resource["account"] = url_for("/accounts/#{}")

  response(
    :body   => {"account_trial" => resource},
    :status => 200,
  )
end

#get_accounts(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/get_accounts.rb', line 16

def get_accounts(params={})
  extract_url_params!(params)

  user_id = if user_url = params.delete("user")
              user_url.split('/').last
            end

  resources = if user_id
                find(:users, user_id)
                self.data[:accounts].select{|k,v| v[:account_users] && v[:account_users].include?(user_id)}
              else
                self.data[:accounts]
              end

  #No need for mock to try and replicate the behavior of active_in_month, but does need to allow it and not get in the way
  params.delete("active_in_month")

  headers, accounts_page = search_and_page(params, :accounts, search_keys: %w[name id legacy_id], resources: resources)

  response(
    :body    => {"accounts" => accounts_page},
    :status  => 200,
    :headers => headers
  )
end

#get_addon(params = {}) ⇒ Object



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

def get_addon(params={})
  extract_url_params!(params)

  self.find(:accounts, params["account"])

  addon = self.find(:addons, params["addon"])

  response(
    :body => {"addon" => addon},
  )
end

#get_addons(params = {}) ⇒ Object



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

def get_addons(params={})
  extract_url_params!(params)

  headers, addons_page = search_and_page(params, :addons, search_keys: %w[account id name])

  response(
    :body    => {"addons" => addons_page},
    :headers => headers
  )
end

#get_address(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/ey-core/requests/get_address.rb', line 15

def get_address(params={})
  identity = resource_identity(params)

  address = self.data[:addresses][identity]

  response(
    :body => {"address" => address},
  )
end

#get_addresses(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/ey-core/requests/get_addresses.rb', line 16

def get_addresses(params={})
  extract_url_params!(params)

  headers, addresses_page = search_and_page(params, :addresses, search_keys: %w[provisioned_id ip_address server location provider])

  response(
    :body    => {"addresses" => addresses_page},
    :headers => headers
  )
end

#get_alert(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_alert.rb', line 15

def get_alert(params={})
  response(
    :body => { "alert" => self.find(:alerts, resource_identity(params)) }
  )
end

#get_alerting_environments(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ey-core/requests/get_alerting_environments.rb', line 17

def get_alerting_environments(params={})
  extract_url_params!(params)

  resources = self.data[:legacy_alerts].map do |id, alert|
    server_id = resource_identity(alert["server"])
    self.data[:servers][server_id]
  end.compact.map do |server|
    environment_id = resource_identity(server["environment"])
    self.data[:environments][environment_id]
  end.compact.uniq.inject({}) {|hash, env| hash[env["id"]] = env; hash}

  if params.delete("exclude_ignored")
    resources.reject! { |id,hash| hash["permanently_ignored"] }
  end

  headers, environments_page = search_and_page(params, :environments, search_keys: %w[account project name assignee_id], resources: resources)

  response(
    :body    => {"environments" => environments_page},
    :status  => 200,
    :headers => headers
  )
end

#get_alerts(params = {}) ⇒ 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
# File 'lib/ey-core/requests/get_alerts.rb', line 17

def get_alerts(params={})
  extract_url_params!(params)

  if params["server"] && params["database_server"]
    return response(status: 422, body: "Cannot search for both server & database_server")
  end

  if database_server_id = resource_identity(params.delete("database_server"))
    params["resource"] = url_for("/database-servers/#{database_server_id}")
  end

  if server_id = resource_identity(params.delete("server"))
    params["resource"] = url_for("/servers/#{server_id}")
  end

  if agent_id = resource_identity(params.delete("agent"))
    params["resource"] = url_for("/agents/#{agent_id}")
  end

  headers, alerts_page = search_and_page(params, :alerts, search_keys: %w[resource external_id name severity agent finished_at started_at], deleted_key: "finished_at")

  response(
    :body    => {"alerts" => alerts_page},
    :headers => headers
  )
end

#get_api_token(username, password) ⇒ Object



13
14
15
16
17
# File 'lib/ey-core/requests/get_api_token.rb', line 13

def get_api_token(username, password)
  response(
    :body => {"api_token" => self.api_token}
  )
end

#get_application(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_application.rb', line 15

def get_application(params={})
  response(
    :body => {"application" => self.find(:applications, resource_identity(params))},
  )
end

#get_application_archive(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ey-core/requests/get_application_archive.rb', line 15

def get_application_archive(params={})
  if params.is_a? String
    params = {"url" => params}
  end

  url            = params.delete("url")
  application_id = params["application"] || (url && path_params(url)["applications"])
  resource_id    = params["id"] || (url && path_params(url)["archives"])

  find(:applications, application_id)

  response(
    :body   => {"application_archive" => find(:application_archives, resource_id)},
    :status => 200,
  )
end

#get_application_archives(params = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/get_application_archives.rb', line 18

def get_application_archives(params={})
  extract_url_params!(params)

  headers, archives_pages = search_and_page(params, :application_archives, search_keys: %w[application])

  response(
    :body    => {"application_archives" => archives_pages},
    :headers => headers
  )
end

#get_application_deployment(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_application_deployment.rb', line 15

def get_application_deployment(params={})
  response(
    :body => {"application_deployment" => self.find(:application_deployments, resource_identity(params))},
  )
end

#get_application_deployments(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/get_application_deployments.rb', line 17

def get_application_deployments(params={})
  extract_url_params!(params)

  headers, application_deployments_page = search_and_page(params, :application_deployments, search_keys: %w[application cluster environment])

  response(
    :body    => {"application_deployments" => application_deployments_page},
    :headers => headers
  )
end

#get_applications(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/get_applications.rb', line 17

def get_applications(params={})
  extract_url_params!(params)

  headers, applications_page = search_and_page(params, :applications, search_keys: %w[id type repository account name environment])

  response(
    :body    => {"applications" => applications_page},
    :headers => headers
  )
end

#get_auto_scaling_alarm(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/ey-core/requests/get_auto_scaling_alarm.rb', line 16

def get_auto_scaling_alarm(params = {})
  response(
    :body => {
      "auto_scaling_alarm" => self.find(
        :auto_scaling_alarms,
        resource_identity(params)
      )
    },
  )
end

#get_auto_scaling_alarms(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ey-core/requests/get_auto_scaling_alarms.rb', line 17

def get_auto_scaling_alarms(params = {})
  extract_url_params!(params)

  headers, auto_scaling_alarms_page = search_and_page(
    params,
    :auto_scaling_alarms,
    search_keys: %w{ id auto_scaling_policy },
    resources: data[:auto_scaling_alarms]
  )

  response(
    :body    => { "auto_scaling_alarms" => auto_scaling_alarms_page },
    :status  => 200,
    :headers => headers
  )
end

#get_auto_scaling_group(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_auto_scaling_group.rb', line 15

def get_auto_scaling_group(params={})
  response(
    :body => {"auto_scaling_group" => self.find(:auto_scaling_groups, resource_identity(params))},
  )
end

#get_auto_scaling_groups(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/get_auto_scaling_groups.rb', line 17

def get_auto_scaling_groups(options={})
  extract_url_params!(options)

  headers, auto_scaling_groups_page = search_and_page(options, :auto_scaling_groups, search_keys: %w[environment environment_id account account_id provider provider_id provisioned_id])

  response(
    :body    => {"auto_scaling_groups" => auto_scaling_groups_page},
    :status  => 200,
    :headers => headers
  )
end

#get_auto_scaling_policies(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ey-core/requests/get_auto_scaling_policies.rb', line 17

def get_auto_scaling_policies(params = {})
  extract_url_params!(params)

  resources = data[:auto_scaling_policies].select { |_key, policy| filter_policy(params, policy) }
  params.delete("types")

  headers, auto_scaling_policies_page = search_and_page(
    params,
    :auto_scaling_policies,
    search_keys: %w{ id auto_scaling_group },
    resources: resources
  )

  response(
    :body    => { "auto_scaling_policies" => auto_scaling_policies_page },
    :status  => 200,
    :headers => headers
  )
end

#get_auto_scaling_policy(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/ey-core/requests/get_auto_scaling_policy.rb', line 16

def get_auto_scaling_policy(params = {})
  response(
    :body => {
      "auto_scaling_policy" => self.find(
        :auto_scaling_policies,
        resource_identity(params)
      )
    },
  )
end

#get_backup_file(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_backup_file.rb', line 15

def get_backup_file(params={})
  response(
    :body   => {"backup_file" => self.find(:backup_files, resource_identity(params))},
  )
end

#get_backup_files(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/get_backup_files.rb', line 16

def get_backup_files(params={})
  extract_url_params!(params)

  headers, backup_files_page = search_and_page(params, :backup_files, search_keys: %w[backup])

  response(
    :body    => {"backup_files" => backup_files_page},
    :status  => 200,
    :headers => headers
  )
end

#get_billing(params = {}) ⇒ Object



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

def get_billing(params={})
  identity = resource_identity(params)

  self.find(:accounts, identity)

  state = self.data[:billing][identity] || "requested"
  response(
    :body   => {"billing" => {"id" => identity, "state" => state}},
    :status => 200,
  )
end

#get_blueprint(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_blueprint.rb', line 15

def get_blueprint(params={})
  response(
    :body => {"blueprint" => self.find(:blueprints, resource_identity(params))}
  )
end

#get_blueprints(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/get_blueprints.rb', line 17

def get_blueprints(params={})
  extract_url_params!(params)

  headers, blueprints_page = search_and_page(params, :blueprints, search_keys: %w[account environment environment_id name])

  response(
    :body    => {"blueprints" => blueprints_page},
    :status  => 200,
    :headers => headers,
  )
end

#get_component(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_component.rb', line 15

def get_component(params={})
  response(
    :body => {"component" => self.find(:components, resource_identity(params))},
  )
end

#get_components(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/get_components.rb', line 17

def get_components(params={})
  extract_url_params!(params)

  headers, components_page = search_and_page(params, :components, search_keys: %w[name id uri])

  response(
    :body    => {"components" => components_page},
    :status  => 200,
    :headers => headers
  )
end

#get_contacts(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ey-core/requests/get_contacts.rb', line 14

def get_contacts(params={})
  extract_url_params!(params)

  resources = if database_service_url = params.delete("database_service")
                database_service_id = resource_identity(database_service_url)
                contact_ids = self.data[:contact_assignments].inject([]) { |r, (contact_id, resource_id)| (resource_id == database_service_id) ? r << contact_id : r }
                self.data[:contacts].select { |id, _| contact_ids.include?(id) }
              else
                self.data[:contacts]
              end

  headers, contacts_page = search_and_page(params, :contacts, search_keys: %w[name email], resources: resources)

  response(
    :body    => {"contacts" => contacts_page},
    :status  => 200,
    :headers => headers
  )
end

#get_costs(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/ey-core/requests/get_costs.rb', line 16

def get_costs(params={})
  extract_url_params!(params)

  response(
    body:   {"costs" => self.data[:costs]},
    status: 200
  )
end

#get_current_user(params = {}) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/ey-core/requests/get_current_user.rb', line 11

def get_current_user(params={})
  if current_user
    get_user("id" => current_user["id"])
  else
    response(status: 404)
  end
end

#get_database_plan_usages(params = {}) ⇒ Object



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

def get_database_plan_usages(params={})
      = params["account_id"]
  billing_month = params.fetch("billing_month", Date.today.strftime("%Y-%m"))

  response(
    :body   => { "database_plan_usages" => self.find(:database_plan_usages, )[billing_month] || [] },
    :status => 200,
  )
end

#get_database_server(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_database_server.rb', line 15

def get_database_server(params={})
  response(
    :body => { "database_server" => self.find(:database_servers, resource_identity(params)) },
  )
end

#get_database_server_revisions(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/ey-core/requests/get_database_server_revisions.rb', line 16

def get_database_server_revisions(params={})
  extract_url_params!(params)

  headers, page = search_and_page(params, :database_server_revisions, search_keys: %w[database_server])

  response(
    :body    => {"database_server_revisions" => page},
    :headers => headers,
  )
end

#get_database_server_snapshot(params = {}) ⇒ Object



14
15
16
17
18
# File 'lib/ey-core/requests/get_database_server_snapshot.rb', line 14

def get_database_server_snapshot(params={})
  response(
    :body => {"database_server_snapshot" => self.find(:database_server_snapshots, resource_identity(params))},
  )
end

#get_database_server_snapshots(params = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/ey-core/requests/get_database_server_snapshots.rb', line 28

def get_database_server_snapshots(params={})
  extract_url_params!(params)

  headers, database_server_snapshots_page = search_and_page(params, :database_server_snapshots, search_keys: %w[database_server database_service provider provisioned_id])

  response(
    :body    => {"database_server_snapshots" => database_server_snapshots_page},
    :headers => headers,
  )
end

#get_database_server_usages(params = {}) ⇒ Object



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

def get_database_server_usages(params={})
      = params["account_id"]
  billing_month = params.fetch("billing_month", Date.today.strftime("%Y-%m"))

  response(
    :body   => { "database_server_usages" => self.find(:database_server_usages, )[billing_month] || [] },
    :status => 200,
  )
end

#get_database_servers(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ey-core/requests/get_database_servers.rb', line 17

def get_database_servers(params={})
  extract_url_params!(params)
  extract_url_params!(params)

  headers, database_servers_page = search_and_page(params, :database_servers, search_keys: %w[provisioned_id provider database_service flavor location])

  response(
    :body    => {"database_servers" => database_servers_page},
    :status  => 200,
    :headers => headers
  )
end

#get_database_servers_firewalls(params = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ey-core/requests/get_database_servers_firewalls.rb', line 18

def get_database_servers_firewalls(params={})
  extract_url_params!(params)

  headers, firewalls_page = search_and_page(params, :firewalls, search_keys: %w[cluster name])

  response(
    :body    => {"firewalls" => firewalls_page},
    :status  => 200,
    :headers => headers
  )
end

#get_database_service(params = {}) ⇒ Object



14
15
16
17
18
# File 'lib/ey-core/requests/get_database_service.rb', line 14

def get_database_service(params={})
  response(
    :body => {"database_service" => self.find(:database_services, resource_identity(params))},
  )
end

#get_database_services(_params = {}) ⇒ 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
# File 'lib/ey-core/requests/get_database_services.rb', line 17

def get_database_services(_params={})
  params = Cistern::Hash.stringify_keys(_params)
  extract_url_params!(params)

  if provider_id = resource_identity(params.delete("provider"))
    params["provider"] = url_for("/providers/#{provider_id}")
  end

  if  = resource_identity(params.delete("account"))
    if provider = self.data[:providers].values.detect{|p| p["account"].index()}
      params["provider"] = url_for("/providers/#{provider["id"]}")
    end
  end

  resources = if environment_id = resource_identity(params.delete("environment") || params.delete("environment_id"))
                self.data[:connectors].
                  select { |_,c| c["_environment"] == url_for("/environments/#{environment_id}") }.
                  select { |_,c| c["source"].match("logical-databases") }.
                  inject({}) { |r, (_, connector)|
                    logical_database = self.find(:logical_databases,
                                                 resource_identity(connector["source"]))

                    source_id = resource_identity(logical_database["service"])
                    database_service = self.find(:database_services, source_id)

                    r.merge(source_id => database_service)
                }
              end

  headers, database_services_page = search_and_page(params, :database_services, search_keys: %w[provider name environment], resources: resources)

  response(
    :body    => {"database_services" => database_services_page},
    :status  => 200,
    :headers => headers
  )
end

#get_deployment(params = {}) ⇒ Object



15
16
17
# File 'lib/ey-core/requests/get_deployment.rb', line 15

def get_deployment(params={})
  response(body: {"deployment" => find(:deployments, resource_identity(params))})
end

#get_deployments(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/get_deployments.rb', line 17

def get_deployments(params={})
  extract_url_params!(params)

  headers, deployments_page = search_and_page(params, :deployments, search_keys: %w(account environment application))

  response(
    :body    => {"deployments" => deployments_page},
    :status  => 200,
    :headers => headers
  )
end

#get_environment(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_environment.rb', line 15

def get_environment(params={})
  response(
    :body => {"environment" => self.find(:environments, resource_identity(params))},
  )
end

#get_environment_database_services(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_environment_database_services.rb', line 15

def get_environment_database_services(params={})
  require_parameters(params, "environment_id")

  get_database_services(params)
end

#get_environment_logical_databases(params = {}) ⇒ Object



14
15
16
17
18
# File 'lib/ey-core/requests/get_environment_logical_databases.rb', line 14

def get_environment_logical_databases(params={})
  require_parameters(params, "environment_id")

  get_logical_databases(params)
end

#get_environment_plan_usages(params = {}) ⇒ Object



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

def get_environment_plan_usages(params={})
      = params["account_id"]
  billing_month = params.fetch("billing_month", Date.today.strftime("%Y-%m"))

  response(
    :body   => { "environment_plan_usages" => self.find(:environment_plan_usages, )[billing_month] || [] },
    :status => 200,
  )
end

#get_environment_variable(params = {}) ⇒ Object



15
16
17
# File 'lib/ey-core/requests/get_environment_variable.rb', line 15

def get_environment_variable(params={})
  response(body: {"environment_variable" => find(:environment_variables, resource_identity(params))})
end

#get_environment_variables(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/get_environment_variables.rb', line 17

def get_environment_variables(params={})
  extract_url_params!(params)

  headers, environment_variables_page = search_and_page(params, :environment_variables, search_keys: %w(environment application))

  response(
    :body    => {"environment_variables" => environment_variables_page},
    :status  => 200,
    :headers => headers
  )
end

#get_environments(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/get_environments.rb', line 17

def get_environments(params={})
  extract_url_params!(params)

  headers, environments_page = search_and_page(params, :environments, search_keys: %w[id account name])

  response(
    :body    => {"environments" => environments_page},
    :status  => 200,
    :headers => headers
  )
end

#get_feature(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_feature.rb', line 15

def get_feature(params={})
  response(
    :body => {"feature" => self.find(:features, resource_identity(params))},
  )
end

#get_features(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/get_features.rb', line 17

def get_features(params={})
  extract_url_params!(params)

  headers, features_page = search_and_page(params, :features, search_keys: %w[account id privacy])

  response(
    :body    => {"features" => features_page},
    :headers => headers
  )
end

#get_firewall(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_firewall.rb', line 15

def get_firewall(params={})
  response(
    :body => {"firewall" => self.find(:firewalls, resource_identity(params))},
  )
end

#get_firewall_rule(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_firewall_rule.rb', line 15

def get_firewall_rule(params={})
  response(
    :body => {"firewall_rule" => self.find(:firewall_rules, resource_identity(params))},
  )
end

#get_firewall_rules(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/get_firewall_rules.rb', line 17

def get_firewall_rules(params={})
  extract_url_params!(params)

  headers, firewall_rules_page = search_and_page(params, :firewall_rules, search_keys: %w[firewall port_range source])

  response(
    :body    => {"firewall_rules" => firewall_rules_page},
    :status  => 200,
    :headers => headers
  )
end

#get_firewalls(params = {}) ⇒ 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
# File 'lib/ey-core/requests/get_firewalls.rb', line 17

def get_firewalls(params={})
  extract_url_params!(params)

  if server_url = params.delete("server")
    params.merge!("cluster" => resource_identity(self.find(:servers, server_url)["cluster"]))
  end

  resources = if database_server_url = params.delete("database_server")
                database_server_id = resource_identity(database_server_url)
                firewall_ids = self.data[:database_server_firewalls].inject([]) { |r, (ds, f)| (ds == database_server_id) ? r << f : r }
                firewall_ids.inject({}) { |r, id| r.merge(id => self.data[:firewalls][id]) }
              elsif cluster_url = params.delete("cluster")
                cluster_id = resource_identity(cluster_url)
                firewall_ids = self.data[:cluster_firewalls].inject([]) { |r, (c, f)| (c == cluster_id) ? r << f : r }
                firewall_ids.inject({}) { |r, id| r.merge(id => self.data[:firewalls][id]) }
              else
                self.data[:firewalls]
              end

  headers, firewalls_page = search_and_page(params, :firewalls, search_keys: %w[provisioned_id name], resources: resources)

  response(
    :body    => {"firewalls" => firewalls_page},
    :status  => 200,
    :headers => headers
  )
end

#get_gem(params = {}) ⇒ Object



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

def get_gem(params={})
  response(
    :body => {
      "gem" => {
        "name" => params["id"],
        "current_version" => Ey::Core::VERSION,
      }
    }
  )
end

#get_keypair(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_keypair.rb', line 15

def get_keypair(params={})
  response(
    :body => {"keypair" => self.find(:keypairs, resource_identity(params))},
  )
end

#get_keypair_deployment(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_keypair_deployment.rb', line 15

def get_keypair_deployment(params={})
  response(
    :body => {"keypair_deployment" => self.find(:keypair_deployments, resource_identity(params))},
  )
end

#get_keypair_deployments(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/get_keypair_deployments.rb', line 17

def get_keypair_deployments(params={})
  extract_url_params!(params)

  headers, keypair_deployments_page = search_and_page(params, :keypair_deployments, search_keys: %w[keypair name fingerprint target])

  response(
    :body    => {"keypair_deployments" => keypair_deployments_page},
    :status  => 200,
    :headers => headers
  )
end

#get_keypairs(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ey-core/requests/get_keypairs.rb', line 17

def get_keypairs(params={})
  extract_url_params!(params)

  if environment = params.delete("environment")
    deployments = get_keypair_deployments("target" => environment).body["keypair_deployments"]
    params["id"] = deployments.map {|d| resource_identity(d["keypair"]) }
  end

  headers, keypairs_page = search_and_page(params, :keypairs, search_keys: %w[application user name fingerprint id])

  response(
    :body    => {"keypairs" => keypairs_page},
    :status  => 200,
    :headers => headers
  )
end

#get_legacy_alert(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_legacy_alert.rb', line 15

def get_legacy_alert(params={})
  response(
    :body   => {"legacy_alert" => self.find(:legacy_alerts, resource_identity(params))},
  )
end

#get_legacy_alerts(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/get_legacy_alerts.rb', line 17

def get_legacy_alerts(params={})
  extract_url_params!(params)

  headers, alerts_page = search_and_page(params, :legacy_alerts, search_keys: %w[environment server severity assigned acknowledged])

  response(
    :body    => {"legacy_alerts" => alerts_page},
    :status  => 200,
    :headers => headers
  )
end

#get_load_balancer(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_load_balancer.rb', line 15

def get_load_balancer(params={})
  response(
    :body => {"load_balancer" => self.find(:load_balancers, resource_identity(params))},
  )
end

#get_load_balancer_nodes(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_load_balancer_node.rb', line 15

def get_load_balancer_nodes(params={})
  response(
    :body => {"load_balancer_node" => self.find(:load_balancer_nodes, resource_identity(params))},
  )
end

#get_load_balancer_service(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_load_balancer_service.rb', line 15

def get_load_balancer_service(params={})
  response(
    :body => {"load_balancer_service" => self.find(:load_balancer_services, resource_identity(params))},
  )
end

#get_load_balancer_services(params = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ey-core/requests/get_load_balancer_services.rb', line 18

def get_load_balancer_services(params={})
  extract_url_params!(params)

  headers, page = search_and_page(params, :load_balancer_services, search_keys: %w[load_balancer provider_ssl_certificate protocol external_port internal_port])

  response(
    :body    => {"load_balancer_services" => page},
    :status  => 200,
    :headers => headers
  )
end

#get_load_balancers(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/get_load_balancers.rb', line 17

def get_load_balancers(params={})
  extract_url_params!(params)

  headers, load_balancers_page = search_and_page(params, :load_balancers, search_keys: %w[name])

  response(
    :body    => {"load_balancers" => load_balancers_page},
    :status  => 200,
    :headers => headers
  )
end

#get_log(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_log.rb', line 15

def get_log(params={})
  response(
    :body => {"log" => self.find(:logs, resource_identity(params))},
  )
end

#get_logical_database(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/ey-core/requests/get_logical_database.rb', line 14

def get_logical_database(params={})
  logical_database = self.find(:logical_databases, resource_identity(params))

  response(
    :body   => {"logical_database" => logical_database},
    :status => 200,
  )
end

#get_logical_databases(_params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ey-core/requests/get_logical_databases.rb', line 17

def get_logical_databases(_params={})
  params = Cistern::Hash.stringify_keys(_params)

  extract_url_params!(params)

  params["service"] = params.delete("database_service") if params["database_service"]
  if environment_id = resource_identity(params.delete("environment") || params.delete("environment_id"))
    params["service"] = url_for("/database-services/#{find(:environments, environment_id)["database_service"]}")
  end

  headers, logical_databases_page = search_and_page(params, :logical_databases, search_keys: %w[name username service ])

  response(
    :body    => {"logical_databases" => logical_databases_page},
    :status  => 200,
    :headers => headers
  )
end

#get_logs(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ey-core/requests/get_logs.rb', line 17

def get_logs(params={})
  extract_url_params!(params)

  if cluster = params.delete("cluster")
    params["_cluster"] = cluster
  end

  if cluster_update = params.delete("cluster_update")
    params["_cluster_update"] = cluster_update
  end

  headers, logs_page = search_and_page(params, :logs, search_keys: %w[component_action _cluster _cluster_update])

  response(
    :body    => {"logs" => logs_page},
    :status  => 200,
    :headers => headers
  )
end

#get_membership(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_membership.rb', line 15

def get_membership(params={})
  response(
    :body => {"membership" => self.find(:memberships, resource_identity(params))},
  )
end

#get_memberships(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/get_memberships.rb', line 17

def get_memberships(params={})
  extract_url_params!(params)

  headers, memberships_page = search_and_page(params, :memberships, search_keys: %w[account email])

  response(
    :body    => {"memberships" => memberships_page},
    :status  => 200,
    :headers => headers
  )
end

#get_message(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/get_message.rb', line 15

def get_message(params={})
  identity = resource_identity(params)

  if message = self.data[:messages].values.detect { |m| m["id"] == identity }
    response(
      :body   => {"message" => message},
      :status => 200,
    )
  else
    response(status: 404)
  end
end

#get_messages(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ey-core/requests/get_messages.rb', line 15

def get_messages(params={})
  extract_url_params!(params)

  headers, messages_page = search_and_page(params, :messages, search_keys: %w[cluster slot database_server database_service request])

  response(
    :body    => {"messages" => messages_page},
    :status  => 200,
    :headers => headers
  )
end

#get_metadata(params = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ey-core/requests/get_metadata.rb', line 9

def (params={})
  response(
    :body => {
      "core" => {
        "environment" => "mock",
        "revision"    => "mock",
        "version"     => "mock",
        "now"         => Time.now,
      }
    },
  )
end

#get_operational_contact(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_operational_contact.rb', line 15

def get_operational_contact(params={})
  #response(
    #:body => { "contact" => self.find(:operational_contacts, resource_identity(params)) },
  #)
end

#get_operational_contacts(params = {}) ⇒ Object



17
18
# File 'lib/ey-core/requests/get_operational_contacts.rb', line 17

def get_operational_contacts(params={})
end

#get_plan_usages(params = {}) ⇒ Object



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

def get_plan_usages(params={})
      = params["account_id"]
  billing_month = params.fetch("billing_month", Date.today.strftime("%Y-%m"))

  response(
    :body   => { "plan_usages" => self.find(:plan_usages, )[billing_month] || [] },
    :status => 200,
  )
end

#get_possible_provider_locations(type) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/ey-core/requests/get_possible_provider_locations.rb', line 11

def get_possible_provider_locations(type)
  response(
    :body    => {"locations" => self.data[:possible_provider_locations][type]},
    :status  => 200,
    :headers => {},
  )
end

#get_provider(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_provider.rb', line 15

def get_provider(params={})
  response(
    :body => {"provider" => self.find(:providers, resource_identity(params))},
  )
end

#get_provider_location(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/get_provider_location.rb', line 15

def get_provider_location(params={})
  extract_url_params!(params)

  identity = params["id"] || params["provider_location"]

  provider_location = self.find(:provider_locations, identity)
  body = provider_location.dup
  body.delete("name")

  response(
    :body => {"provider_location" => body},
  )
end

#get_provider_locations(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/get_provider_locations.rb', line 16

def get_provider_locations(params={})
  extract_url_params!(params)

  headers, provider_locations_page = search_and_page(params, :provider_locations, search_keys: %w[provider name location_id])

  response(
    :body    => {"provider_locations" => provider_locations_page},
    :status  => 200,
    :headers => headers
  )
end

#get_providers(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/get_providers.rb', line 16

def get_providers(params={})
  extract_url_params!(params)

  headers, providers_page = search_and_page(params, :providers, search_keys: %w[name account provisioned_id type])

  response(
    :body    => {"providers" => providers_page},
    :status  => 200,
    :headers => headers
  )
end

#get_request(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/get_request.rb', line 15

def get_request(params={})
  id = params["id"]

  request = self.find(:requests, id)
  request["callback_url"] = "#{self.url}/requests/#{id}/callback"

  request["poll_count"] ||= 0
  request["poll_count"] += 1

  response_hash = if request["finished_at"].nil?
                    if request["poll_count"] >= delay
                      # finish a previously unfinished request
                      resource_key, key, resource, resource_url = request.delete("resource")

                      if resource # provisioning
                        if resource.is_a?(Array)
                          resource.each { |res| self.data[resource_key][res["id"]] = res }
                        else
                          resource_url ||= if resource.respond_to?(:call)
                                             resource.call(request)
                                           else
                                             self.data[resource_key][key] = resource
                                             resource.delete("resource_url")
                                           end
                        end
                        request["finished_at"] = Time.now
                        request["resource"] = resource_url
                      else
                        resource = self.data[resource_key].delete(key) # deprovisioning
                        self.data[:deleted][resource_key][key] ||= resource
                      end
                      request
                    else
                      request
                    end
                  elsif request["finished_at"] # already finished
                    request
                  else
                    request.dup.tap do |r|
                      collection, id, resource = r.delete("resource")
                      r["progress"] = resource["progress"] if resource && resource.key?("progress")
                      if resource && self.data[collection] && self.data[collection][id]
                        r["resource_url"] = resource["resource_url"]
                      end
                    end
                  end

  response_hash = response_hash.dup
  response_hash.delete("poll_count")

  response(
    :body   => {"request" => response_hash},
    :status => 200,
  )
end

#get_requests(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/get_requests.rb', line 17

def get_requests(params={})
  extract_url_params!(params)

  headers, requests_page = search_and_page(params, :requests, search_keys: %w[type finished_at requester_id])

  response(
    :body    => {"requests" => requests_page},
    :status  => 200,
    :headers => headers
  )
end

#get_server(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ey-core/requests/get_server.rb', line 15

def get_server(params={})
  url = params.delete("url")
  id  = params["id"] && Integer(params["id"])

  identity = if slot_id = url && path_params(url)["slots"]
               find(:slots, slot_id)

               server = self.data[:servers].values.find {|s| s["slot"] == url_for("/slots/#{slot_id}") }
               server && server["id"]
             else
               id || path_params(url)["servers"].to_i
             end

  response(
    :body   => {"server" => self.find(:servers, identity)},
    :status => 200,
  )
end

#get_server_event(params = {}) ⇒ Object



14
15
16
17
18
# File 'lib/ey-core/requests/get_server_event.rb', line 14

def get_server_event(params={})
  response(
    :body => {"server_event" => self.find(:server_events, resource_identity(params))},
  )
end

#get_server_events(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/get_server_events.rb', line 16

def get_server_events(params={})
  extract_url_params!(params)

  headers, server_events_page = search_and_page(params, :server_events, search_keys: %w[slot server environment])

  response(
    :body    => {"server_events" => server_events_page},
    :status  => 200,
    :headers => headers
  )
end

#get_server_usages(params = {}) ⇒ Object



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

def get_server_usages(params={})
      = params["account_id"]
  billing_month = params.fetch("billing_month", Date.today.strftime("%Y-%m"))

  response(
    :body   => { "server_usages" => self.find(:server_usages, )[billing_month] || [] },
    :status => 200,
  )
end

#get_servers(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/ey-core/requests/get_servers.rb', line 14

def get_servers(params={})
  extract_url_params!(params)

  headers, servers_page = search_and_page(params, :servers, search_keys: %w[account environment provider state private_hostname public_hostname provisioned_id role])

  response(
    :body    => {"servers" => servers_page},
    :headers => headers
  )
end

#get_ssl_certificate(params = {}) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/ey-core/requests/get_ssl_certificate.rb', line 15

def get_ssl_certificate(params={})
  identity = resource_identity(params)

  response(
    :body => {"ssl_certificate" => self.find(:ssl_certificates, identity)},
  )
end

#get_ssl_certificates(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/get_ssl_certificates.rb', line 16

def get_ssl_certificates(params={})
  extract_url_params!(params)

  headers, ssl_certificates_page = search_and_page(params, :ssl_certificates, search_keys: %w[account name])

  response(
    :body    => {"ssl_certificates" => ssl_certificates_page},
    :status  => 200,
    :headers => headers
  )
end

#get_storage(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_storage.rb', line 15

def get_storage(params={})
  response(
    :body => {"storage" => self.find(:storages, resource_identity(params))},
  )
end

#get_storage_user(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_storage_user.rb', line 15

def get_storage_user(params={})
  response(
    :body => {"storage_user" => self.find(:storage_users, resource_identity(params))},
  )
end

#get_storage_users(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/get_storage_users.rb', line 16

def get_storage_users(params={})
  extract_url_params!(params)

  headers, storage_users_page = search_and_page(params, :storage_users, search_keys: %w[username storage])

  response(
    :body    => {"storage_users" => storage_users_page},
    :status  => 200,
    :headers => headers
  )
end

#get_storages(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/get_storages.rb', line 16

def get_storages(params={})
  extract_url_params!(params)

  headers, storages_page = search_and_page(params, :storages, search_keys: %w[name id provider])

  response(
    :body    => {"storages" => storages_page},
    :status  => 200,
    :headers => headers
  )
end

#get_support_trial(params = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ey-core/requests/get_support_trial.rb', line 12

def get_support_trial(params={})
   = resource_identity(params)

  find(:accounts, )
  support_trial = data[:support_trials][]
  unless support_trial
    response(status: 404)
  end

  resource = support_trial.merge(
    "account"    => url_for("/accounts/#{}")
  )

  response(
    :body   => {"support_trial" => resource},
    :status => 200,
  )
end

#get_task(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_task.rb', line 15

def get_task(params={})
  response(
    :body => {"task" => self.find(:tasks, resource_identity(params))},
  )
end

#get_tasks(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/get_tasks.rb', line 16

def get_tasks(params={})
  extract_url_params!(params)

  headers, tasks_page = search_and_page(params, :tasks, search_keys: %w[cluster_component])

  response(
    :body    => {"tasks" => tasks_page},
    :status  => 200,
    :headers => headers
  )
end

#get_token(options = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_token.rb', line 15

def get_token(options={})
  response(
    :body => {"token" => self.find(:tokens, resource_identity(options))},
  )
end

#get_token_by_login(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ey-core/requests/get_token_by_login.rb', line 17

def (params={})
  user = self.data[:users].values.detect { |u| u["username"] == params[:username] && u["password"] == params[:password] }

  if user
    response(
      :status => 200,
      :body   => {"api_token" => user["token"]},
    )
  else
    response(status: 401)
  end
end

#get_tokens(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ey-core/requests/get_tokens.rb', line 17

def get_tokens(params={})
  extract_url_params!(params)
  find(:users, resource_identity(params["user"]))

  params["on_behalf_of"] = params.delete("user")

  headers, tokens_page = search_and_page(params, :tokens, search_keys: %w(on_behalf_of))

  response(
    :body    => {"tokens" => tokens_page},
    :status  => 200,
    :headers => headers,
  )
end

#get_untracked_server(params = {}) ⇒ Object



12
13
14
15
16
# File 'lib/ey-core/requests/get_untracked_server.rb', line 12

def get_untracked_server(params={})
  response(
    :body => {"untracked_server" => self.find(:untracked_servers, resource_identity(params))},
  )
end

#get_untracked_servers(params = {}) ⇒ Object



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

def get_untracked_servers(params={})
  extract_url_params!(params)

  headers, servers_page = search_and_page(params, :untracked_servers, search_keys: %w[provider provisioned_id provisioner_id location])

  response(
    :body    => {"untracked_servers" => servers_page},
    :headers => headers
  )
end

#get_user(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_user.rb', line 15

def get_user(params={})
  response(
    :body => {"user" => self.find(:users, resource_identity(params))},
  )
end

#get_users(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/get_users.rb', line 16

def get_users(params={})
  resources = if url = params.delete("url")
                if  = path_params(url)["accounts"]
                   = self.find(:accounts, )

                  if url.index("/owners")
                    [:account_owners].inject({}){|r,id| r.merge(id => self.data[:users][id])}
                  elsif url.index("/users")
                    [:account_users].inject({}){|r,id| r.merge(id => self.data[:users][id])}
                  else
                    raise "Mock doesn't know how to handle url: #{url}"
                  end
                else []
                end
              else
                self.data[:users]
              end

  headers, users_page = search_and_page(params, :users, search_keys: %w[name email first_name last_name], resources: resources)

  response(
    :body    => {"users" => users_page},
    :headers => headers
  )
end

#get_volume(params = {}) ⇒ Object



15
16
17
18
19
# File 'lib/ey-core/requests/get_volume.rb', line 15

def get_volume(params={})
  response(
    :body => {"volume" => self.find(:volumes, resource_identity(params))},
  )
end

#get_volumes(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/get_volumes.rb', line 16

def get_volumes(params={})
  extract_url_params!(params)

  headers, volumes_page = search_and_page(params, :volumes, search_keys: %w[server name size iops mount device])

  response(
    :body    => {"volumes" => volumes_page},
    :status  => 200,
    :headers => headers
  )
end

#reboot_server(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ey-core/requests/reboot_server.rb', line 16

def reboot_server(params={})
  request_id  = self.uuid
  identity = resource_identity(params)

  find(:servers, identity)

  reboot_request = {
    "finished_at" => Time.now,
    "id"          => request_id,
    "started_at"  => Time.now,
    "successful"  => "true",
    "server"      => url_for("/servers/#{identity}"),
    "type"        => "reboot_server_request",
  }

  self.data[:requests][request_id] = reboot_request

  response(
    :body   => {"request" => reboot_request},
    :status => 201,
  )
end

#reconcile_server(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ey-core/requests/reconcile_server.rb', line 16

def reconcile_server(params={})
  request_id  = self.uuid
  identity = resource_identity(params)

  find(:servers, identity)

  reconcile_request = {
    "finished_at" => Time.now,
    "id"          => request_id,
    "started_at"  => Time.now,
    "successful"  => "true",
    "server"      => url_for("/servers/#{identity}"),
    "type"        => "reconcile_server_request",
  }

  self.data[:requests][request_id] = reconcile_request

  response(
    :body   => {"request" => reconcile_request},
    :status => 201,
  )
end

#request_callback(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/ey-core/requests/request_callback.rb', line 15

def request_callback(options={})
  request_id = resource_identity(options)

  request = find(:requests, request_id).dup
  request.delete("resource")

  response(
    :body => {"request" => request},
  )
end

#reset_passwordObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/ey-core/requests/reset_password.rb', line 19

def reset_password(*)
  response(
    :body => {
      "password_reset" => {
        "accepted" => true
      },
    },
    :status  => 201,
  )
end

#reset_server_state(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ey-core/requests/reset_server_state.rb', line 17

def reset_server_state(params={})
  id    = params["id"]
  state = params["state"]

  if server = self.data[:servers][id]
    server[:state] = state
    response(
      :body   => {"server" => server},
      :status => 200,
    )
  else
    response(status: 404)
  end
end

#response(options = {}) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/ey-core/client/mock.rb', line 258

def response(options={})
  status  = options[:status] || 200
  body    = options[:body]
  headers = {
    "Content-Type"  => "application/json; charset=utf-8"
  }.merge(options[:headers] || {})


  @logger.debug "MOCKED RESPONSE: #{status}"
  @logger.debug('response') { headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n") }
  @logger.debug caller[0]
  @logger.debug('response.body') { body }
  @logger.debug ''

  Ey::Core::Response.new(
    :status  => status,
    :headers => headers,
    :body    => body,
    :request => {
      :method  => :mocked,
      :url     => caller[1],
    }
  ).raise!
end

#restart_environment_app_servers(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ey-core/requests/restart_environment_app_servers.rb', line 17

def restart_environment_app_servers(params={})
  find(:environments, resource_identity(params))
  request_id = self.uuid

  request = {
    "id"           => request_id,
    "type"         => "app_deployment_maintenance",
    "successful"   => "true",
    "started_at"   => Time.now,
    "finished_at"  => nil,
    "resource_url" => nil
  }

  self.data[:requests][request_id] = request

  response(
    :body   => {"request" => request},
    :status => 200,
  )
end

#retrieve_docker_registry_credentials(account_id, location_id) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ey-core/requests/retrieve_docker_registry_credentials.rb', line 11

def retrieve_docker_registry_credentials(, location_id)
  response(
    :body => {
      'docker_registry_credentials' => {
        'username'          => 'foo',
        'password'          => 'bar',
        'registry_endpoint' => "https://012345678901.dkr.ecr.#{location_id}.amazonaws.com",
        'expires_at'        => (Time.now + 8 * 3600).to_i
      }
    }
  )
end

#run_cluster_application_action(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/run_cluster_application_action.rb', line 17

def run_cluster_application_action(params={})
  cluster_id     = params.delete("cluster")
  application_id = params.delete("application")

  cluster_component = self.data[:cluster_components].values.find { |cc| cc["cluster"] == url_for("/clusters/#{cluster_id}") && cc["configuration"]["application"] == application_id }

  response(status: 404) unless cluster_component

  return run_cluster_component_action(params.merge("cluster_component" => cluster_component["id"]))
end

#run_environment_application_action(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/run_environment_application_action.rb', line 17

def run_environment_application_action(params={})
  environment_id = params.delete('environment')
  application_id = params.delete('application')

  clusters = self.data[:clusters].select { |k,c| c['environment'] == url_for("/environments/#{environment_id}") }
  cluster_urls = clusters.keys.map { |k| url_for("/clusters/#{k}") }
  cluster_components = self.data[:cluster_components].values.select { |cc| cluster_urls.include?(cc["cluster"]) && cc["configuration"]["application"] == application_id }

  response(status: 404) unless cluster_components
  cluster_components.map { |cc| run_cluster_component_action(params.merge("cluster_component" => cc["id"])) }.first
end

#serial_idObject



283
284
285
286
# File 'lib/ey-core/client/mock.rb', line 283

def serial_id
  @@serial_id ||= 0
  @@serial_id += 1
end

#server_hash(options = {}) ⇒ Object



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
# File 'lib/ey-core/requests/boot_environment.rb', line 106

def server_hash(options={})
  id             = self.serial_id
  flavor         = options.delete(:flavor) || "m3_medium"
  role           = options.delete(:role)
  name           = options.delete(:name)
  environment    = options.delete(:environment)
  provisioned_id = "i-#{SecureRandom.hex(4)}"
  provider       = get_providers("account" => environment["account"]).body["providers"].first
  {
    "account"          => url_for("/accounts/#{resource_identity(environment["account"])}"),
    "alerts"           => url_for("/servers/#{id}/alerts"),
    "created_at"       => Time.now.to_s,
    "dedicated"        => !!options[:dedicated],
    "deprovisioned_at" => Time.now.to_s,
    "devices"          => block_device_map,
    "enabled"          => true,
    "environment"      => url_for("/environments/#{environment["id"]}"),
    "flavor"           => { "id" => flavor },
    "id"               => id,
    "location"         => environment["region"],
    "logs"             => url_for("/servers/#{id}/logs"),
    "name"             => name,
    "private_hostname" => "#{provisioned_id}.private.example.org",
    "provider"         => url_for("/providers/#{provider["id"]}"),
    "provisioned_at"   => Time.now.to_s,
    "provisioned_id"   => provisioned_id,
    "public_hostname"  => "#{provisioned_id}.public.example.org",
    "public_key"       => mock_ssh_key[:fingerprint],
    "ssh_port"         => 22,
    "state"            => "running",
    "token"            => SecureRandom.hex(16),
    "updated_at"       => Time.now.to_s,
    "volumes"          => url_for("/servers/#{id}/volumes"),
    "role"             => role || "solo",
  }
end

#signup(_params) ⇒ Object



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
# File 'lib/ey-core/requests/signup.rb', line 15

def (_params)
  params = Cistern::Hash.stringify_keys(_params)

  user_id = self.uuid

  user = params["user"].dup
  user.merge!({
    "id"          => user_id,
    "accounts"    => url_for("/users/#{user_id}/accounts"),
    "memberships" => url_for("/users/#{user_id}/memberships"),
    "keypairs"    => url_for("/users/#{user_id}/keypairs"),
    "token"       => SecureRandom.hex(20)
  })

  self.data[:users][user_id] = user

   = self.uuid

   = (, params["account"].dup)

  self.data[:accounts][] = .merge(:account_users => [user_id], :account_owners => [user_id])

  (params["features"] || []).each do |resource_id|
    feature = self.data[:features][resource_id]

     = url_for("/accounts/#{}")
    feature["account"] = 
  end

  response(
    :body => {
      "signup" => {
        "user_id"     => user_id,
        "account_id"  => ,
        "upgrade_url" => "http://login.localdev.engineyard.com:9292/session-tokens/#{self.uuid}/upgrade",
      },
    },
    :status  => 201,
  )
end

#start_server(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ey-core/requests/start_server.rb', line 16

def start_server(params={})
  request_id  = self.uuid
  identity = resource_identity(params)

  find(:servers, identity)

  start_request = {
    "finished_at" => Time.now,
    "id"          => request_id,
    "started_at"  => Time.now,
    "successful"  => "true",
    "server"      => url_for("/servers/#{identity}"),
    "type"        => "start_server_request",
  }

  self.data[:requests][request_id] = start_request

  response(
    :body   => {"request" => start_request},
    :status => 201,
  )
end

#stop_server(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ey-core/requests/stop_server.rb', line 16

def stop_server(params={})
  request_id  = self.uuid
  identity = resource_identity(params)

  find(:servers, identity)

  stop_request = {
    "finished_at" => Time.now,
    "id"          => request_id,
    "started_at"  => Time.now,
    "successful"  => "true",
    "server"      => url_for("/servers/#{identity}"),
    "type"        => "stop_server_request",
  }

  self.data[:requests][request_id] = stop_request

  response(
    :body   => {"request" => stop_request},
    :status => 201,
  )
end

#timeout_deployment(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/ey-core/requests/timeout_deployment.rb', line 17

def timeout_deployment(params={})
  deployment = find(:deployments, resource_identity(params))

  deployment["successful"] = false

  self.data[:deployments][deployment["id"]] = deployment

  response(body: {"deployment" => deployment})
end

#unassign_environment(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ey-core/requests/unassign_environment.rb', line 16

def unassign_environment(params={})
  request_id = self.uuid
  url         = params.delete("url")

  environment_id = params["id"] || url && url.split('/').last

  environment = self.data[:environments][environment_id]
  environment[:assignee] = nil

  response(
    :body   => {"environment" => environment},
    :status => 200,
  )
end

#update_addon(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ey-core/requests/update_addon.rb', line 14

def update_addon(params={})
  addon = params["addon"]

  extract_url_params!(params)

  find(:accounts, params["account"])

  resource = self.find(:addons, params["addon"])

  response(
    :body => {"addon" => resource.merge!(addon) },
  )
end

#update_address(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/update_address.rb', line 15

def update_address(params={})
  identity = resource_identity(params)
  address  = find(:addresses, identity)

  address_params = Cistern::Hash.slice(Cistern::Hash.stringify_keys(params["address"]), "disappeared_at")

  address.merge! address_params

  response(
    :body => { "address" => address },
    :status => 200
  )
end

#update_alert(_params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ey-core/requests/update_alert.rb', line 17

def update_alert(_params={})
  params = Cistern::Hash.stringify_keys(_params)
  identity = params["id"]
  alert  = find(:alerts, identity)

  update_params = Cistern::Hash.slice(params["alert"],
                                      "acknowledged", "ignored", "severity", "finished_at", "external_id", "started_at", "message")

  alert.merge!(update_params.merge!("updated_at" => Time.now))

  response(
    :body => { "alert" => alert },
  )
end

#update_application_archive(params = {}) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/ey-core/requests/update_application_archive.rb', line 15

def update_application_archive(params={})
  archive = self.find(:application_archives, resource_identity(params))

  response(
    :body => {"application_archive" => archive.merge(params["application_archive"])},
  )
end

#update_auto_scaling_alarm(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/update_auto_scaling_alarm.rb', line 15

def update_auto_scaling_alarm(params = {})
  resource_id = params.delete("id")
  now = Time.now
  resource = find(:auto_scaling_alarms, resource_id)
    .merge(params["auto_scaling_alarm"])
    .merge("updated_at" => now)
  resource.merge!(params)

  request = {
    "id"           => self.uuid,
    "type"         => "update_auto_scaling_alarm",
    "successful"   => true,
    "created_at"   => now - 5,
    "started_at"   => now - 3,
    "finished_at"  => now,
    "updated_at"   => now,
    "message"      => nil,
    "read_channel" => nil,
    "resource"     => [:auto_scaling_alarms, resource_id, resource],
    "resource_url" => url_for("/auto_scaling_alarms/#{resource_id}")
  }

  self.data[:requests][request["id"]] = request

  response(
    body:   { "request" => request },
    status: 200
  )
end

#update_auto_scaling_group(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/update_auto_scaling_group.rb', line 15

def update_auto_scaling_group(params = {})
  url = params.delete("url")

  resource_id = params.delete("id")
  resource = find(:auto_scaling_groups, resource_id).merge(params["auto_scaling_group"])

  now = Time.now

  request = {
    "id"           => self.uuid,
    "type"         => "update_auto_scaling_group",
    "successful"   => true,
    "created_at"   => now - 5,
    "updated_at"   => now,
    "started_at"   => now - 3,
    "finished_at"  => now,
    "message"      => nil,
    "read_channel" => nil,
    "resource"     => [:auto_scaling_groups, resource_id, resource],
    "resource_url" => url_for("/auto_scaling_groups/#{resource_id}")
  }

  self.data[:requests][request["id"]] = request

  response(
    :body   => {"request" => request},
    :status => 200
  )
end

#update_auto_scaling_policy(params = {}) ⇒ Object



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
# File 'lib/ey-core/requests/update_auto_scaling_policy.rb', line 15

def update_auto_scaling_policy(params = {})
  resource_id = params.delete("id")
  now = Time.now

  resource = find(:auto_scaling_policies, resource_id)
    .merge(params["auto_scaling_policy"])
    .merge("updated_at" => now)
  resource.merge!(params)

  request = {
    "id"           => self.uuid,
    "type"         => "update_auto_scaling_policy",
    "successful"   => true,
    "created_at"   => now - 5,
    "updated_at"   => now,
    "started_at"   => now - 3,
    "finished_at"  => now,
    "message"      => nil,
    "read_channel" => nil,
    "resource"     => [:auto_scaling_policies, resource_id, resource],
    "resource_url" => url_for("/auto_scaling_policies/#{resource_id}")
  }

  self.data[:requests][request["id"]] = request

  response(
    body:   { "request" => request },
    status: 200
  )
end

#update_billing(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ey-core/requests/update_billing.rb', line 14

def update_billing(params={})
  id = params["id"]
  state = params["state"]
  self.find(:accounts, id)

  self.data[:billing][id] = state

  response(
    :body => {"billing" => {"id" => id, "state" => state}},
  )
end

#update_blueprint(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/update_blueprint.rb', line 15

def update_blueprint(params={})
  params           = Cistern::Hash.stringify_keys(params)
  blueprint_params = params["blueprint"]
  blueprint        = find(:blueprints, params["id"])

  blueprint["name"] = blueprint_params["name"]
  self.data[:blueprints][blueprint["id"]] = blueprint

  response(
    :body   => {"blueprint" => blueprint},
    :status => 200,
  )
end

#update_connector(params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ey-core/requests/update_connector.rb', line 13

def update_connector(params={})
  identity = resource_identity(params)
  connector  = find(:connectors, identity)

  update_params = Cistern::Hash.slice(params["connector"], "configuration")

  # core api doesn't actually do this yet
  if config = update_params.delete("configuration")
    update_params["configuration"] = normalize_hash(config)
  end

  connector.merge!(update_params)

  response(
    :body => { "connector" => connector },
    :status => 200
  )
end

#update_environment(params = {}) ⇒ Object

Raises:

  • (NotImplementedError)


13
14
15
# File 'lib/ey-core/requests/update_environment.rb', line 13

def update_environment(params={})
  raise NotImplementedError
end

#update_environment_variable(_params = {}) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/ey-core/requests/update_environment_variable.rb', line 17

def update_environment_variable(_params={})
  params = Cistern::Hash.stringify_keys(_params).merge!("updated_at" => Time.now)
  environment_variable  = find(:environment_variables, params.delete("id"))
  environment_variable.merge!(params)

  response(:body => { "environment_variable" => environment_variable })
end

#update_membership(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/update_membership.rb', line 14

def update_membership(params={})
  resource_id = resource_identity(params)

  membership = self.find(:memberships, resource_id)
  if params["membership"] && params["membership"]["accepted"]
    self.find(:accounts, resource_identity(membership["account"]))[:account_users] << resource_identity(membership["user"])
  end

  #NOTE: doesn't currently support updating role
  response(
    :body   => {"membership" => membership},
    :status => 200,
  )
end

#update_provider_location(params = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/update_provider_location.rb', line 14

def update_provider_location(params={})
  resource_id = params.delete("id")

  if provider_location = self.data[:provider_locations][resource_id]
    provider_location[:limits] = params["provider_location"]["limits"]

    response(
      :body   => {"provider_location" => provider_location},
      :status => 200,
    )
  else
    response(status:404)
  end
end

#update_server(params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ey-core/requests/update_server.rb', line 13

def update_server(params={})
  server = find(:servers, params.fetch("id"))

  server_params = Cistern::Hash.slice(
    Cistern::Hash.stringify_keys(params["server"]),
    "provisioned_at", "deprovisioned_at", "disappeared_at"
  )

  server.merge!("state" => params["server"]["status"]) if params["server"]["status"]
  server.merge!(server_params.merge("updated_at" => Time.now))

  response(
    :body => { "server" => server },
  )
end

#update_ssl_certificate(params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/ey-core/requests/update_ssl_certificate.rb', line 15

def update_ssl_certificate(params={})
  ssl_certificate = self.find(:ssl_certificates, resource_identity(params))

  ssl_certificate.merge!(params["ssl_certificate"])

  response(
    :body => {"ssl_certificate" => ssl_certificate}
  )
end

#update_untracked_server(params = {}) ⇒ Object



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

def update_untracked_server(params={})
  identity = resource_identity(params) || require_parameters(params["untracked_server"], "id")
  server  = find(:untracked_servers, identity)

  update_params = Cistern::Hash.slice(params["untracked_server"], "provisioner_id", "location", "state")

  response(
    :body => { "untracked_server" => server.merge!(update_params) },
  )
end

#upload_file(_params = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ey-core/requests/upload_file.rb', line 28

def upload_file(_params={})
  params = Cistern::Hash.stringify_keys(_params)
  upload_url = params["upload_url"]

  temp_file = Tempfile.new("ey-core")
  temp_file.write(params["body"] || File.read(params.fetch("file")))
  temp_file.close

  self.data[:temp_files][upload_url] = temp_file.path

  response(
    :body   => "",
    :status => 200,
  )
end

#upload_recipes_for_environment(params = {}) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/ey-core/requests/upload_recipes_for_environment.rb', line 20

def upload_recipes_for_environment(params={})
  environment = find(:environments, resource_identity(params))
  environment["custom_recipes"] = "https://#{self.uuid}.s3.amazonaws.com/#{self.uuid}?X-Amz-Expires=1800"
  self.data[:environments][environment["id"]] = environment

  response(status: 204)
end