module Ey::Core::Mock module Resources # Attempt to determine the identity of the resource. Used to make the mock requests DRYer # # @param params [nil] returns nil # @param params [Integer] id of the resource # @param params [String] id or url of the resource # @param params [Hash] description of resource # @option params [String] url url of the resource # @option params [String] id id of the resource # @return identity of the resource def resource_identity(params) id = nil url = nil case params when Hash id = params["id"] url = params["url"] when String if params.start_with?(@url) url = params else id = params end when Integer id = params when nil return else raise ArgumentError, "Cannot determine resource identity of #{params.inspect}" end id ||= url && URI.parse(url).path.split("/")[2] Integer(id) rescue id end def find(collection, id_or_url) id = resource_identity(id_or_url) # polymorphic associations suck if collection.is_a?(Array) resource = collection.map do |key| data = self.data[key][id] || self.data[key][id.to_i] [key.to_s.tr('_', '-'), data] if data end.compact.first resource || response(status: 404, body: "Couldn't find #{collection.last} with #{id}") else self.data[collection][id] || self.data[collection][id.to_i] || response(status: 404, body: "Couldn't find #{collection} with [#{id}]") end end def load_addon_attachments(account_id, addon_name) found_attachments = [] environments = search(self.data[:environments], account: url_for("/accounts/#{account_id}")) clusters = search(self.data[:clusters], environment: environments.keys.map {|e| url_for("/environments/#{e}") }) cluster_components = self.data[:cluster_components].select {|_,cc| cc["application"] } cluster_components = search(cluster_components, cluster: clusters.keys.map {|c| url_for("/clusters/#{c}") }) cluster_components.each do |id, cc| cluster = find(:clusters, cc["cluster"]) environment = find(:environments, cluster["environment"]) if application = find(:applications, cc["application"]) rescue nil key_found, _ = (cc["configuration"]["vars"] || {}).detect { |k,v| v == "Addon:#{addon_name}" } found_attachments << { "app_id" => application["id"], "environment_id" => environment["id"], "id" => "ClusterComponent:#{id}", "key" => key_found, "suggested_name" => "#{environment["name"]}_#{application["name"]}", } end end found_attachments end def mock_account_setup(resource_id, resource) resource["support_plan"] ||= "standard" account_url = url_for("/accounts/#{resource_id}") resource.merge!({ "id" => resource_id, "addons" => "#{account_url}/addons", "addresses" => "#{account_url}/addressses", "applications" => "#{account_url}/applications", "created_at" => Time.now.to_s, "updated_at" => Time.now.to_s, "cancelled_at" => nil, "environments" => "#{account_url}/environments", "features" => "#{account_url}/features", "memberships" => "#{account_url}/memberships", "notes" => "#{account_url}/notes", "owners" => "#{account_url}/owners", "providers" => "#{account_url}/providers", "referrals" => "#{account_url}/referrals", "requests" => "#{account_url}/requests", "ssl_certificates" => "#{account_url}/ssl-certificates", "resource_url" => account_url, "users" => "#{account_url}/users", "costs" => "#{account_url}/costs", "account_trial" => "#{account_url}/trial", }) resource end def mock_ssh_key { public_key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC05RVR8BF6gm1oevz+y34AY84OJ3JRSK8sGQycW1Ita7PV8H1b/i5zGWUFjhmR4tuQFjtIku9duZzxKES0bHGVng6PbJeUoJVflYf8rXQzcD4gcF+rYHZGv34N59RH2WYcA+4W4JM8m+hmp/dbXvYjqvf5ykOcETkwUcCfCsAD9tk2GqQQpu8z991Lwu36UHN2I8gcuPy+hR1PokbBxSBT+46MbZFomvA7onjqDcvkXIgZ+GyW12yeKMaF8+zvOT1dDxTnzjvWB7zffCDaNfMW7HkdhvlGAKUxk3CdEVO6peQy4a0E5AdElZDEoCvxpKxKZHXfA6penIysk6p/bEBx".freeze, private_key: "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAtOUVUfAReoJtaHr8/st+AGPODidyUUivLBkMnFtSLWuz1fB9\nW/4ucxllBY4ZkeLbkBY7SJLvXbmc8ShEtGxxlZ4Oj2yXlKCVX5WH/K10M3A+IHBf\nq2B2Rr9+DefUR9lmHAPuFuCTPJvoZqf3W172I6r3+cpDnBE5MFHAnwrAA/bZNhqk\nEKbvM/fdS8Lt+lBzdiPIHLj8voUdT6JGwcUgU/uOjG2RaJrwO6J46g3L5FyIGfhs\nltdsnijGhfPs7zk9XQ8U58471ge833wg2jXzFux5HYb5RgClMZNwnRFTuqXkMuGt\nBOQHRJWQxKAr8aSsSmR13wOqXpyMrJOqf2xAcQIDAQABAoIBAACyxbNMRvT/HecC\nWomtRW5A9GGvm0xfCHYWHqtX2uh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8xUgZkSI+hId/dfrZuSqs\nJvms2/3P58fb15rE5fWtn9iPwA6wU2jlJg6KEnCawiEfhTZgy8XRhgQm0XK0HAHz\n4EWoM5VwYbdIs+LyuVQK/yNRJlP3gtODs6ADAZyzVyj8H2VBduzn9w==\n-----END RSA PRIVATE KEY-----\n".freeze, fingerprint: 16.times.map {rand(16**2).to_s(16).rjust(2,'0')}.join(":"), # => "8a:56:9a:b3:90:b1:3e:21:33:8d:a8:ee:61:69:c5:bc" } end end end