Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EYPP-88: added CDN distributions #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/boot_env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
exit
end

env_options = {"blueprint_id": blueprint.id}
env_options = { "blueprint_id" => blueprint.id }
puts "Booting environment using an specific blueprint...."
provision_request = environment.boot(env_options)

Expand Down
50 changes: 50 additions & 0 deletions lib/ey-core/cli/cdn_distributions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'ey-core/cli/subcommand'
require 'ey-core/cli/helpers/stream_printer'

module Ey
module Core
module Cli
class CdnDistributions < Subcommand

include Ey::Core::Cli::Helpers::StreamPrinter

title "cdn_distributions"
summary "Retrieve information about cdn distributions you have configured for your environments or applications."

option :environment,
short: 'e',
long: 'environment',
description: 'Filter by environmeent name or id',
argument: 'Environment'

option :application,
short: 'a',
long: 'application',
description: 'Filter by application name or id',
argument: 'Application'

switch :display_sensitive,
short: 's',
long: 'display_sensitive',
description: 'Determines whether values of sensitive variables should be printed',
argument: 'Display Sensitive'

def handle
cdn_distributions = if option(:application)
core_applications(option(:application)).flat_map(&:cdn_distributions)
elsif option(:environment)
core_environments(option(:environment)).flat_map(&:cdn_distributions)
else
core_cdn_distributions
end

stream_print("ID" => 10, "Domain" => 30, "Origin" => 50, "Aliases" => 50, "Enabled" => 5, "Environment" => 30, "Application" => 30) do |printer|
cdn_distributions.each_entry do |distribution|
printer.print(distribution.id, distribution.domain, distribution.aliases.join(', '), distribution.enabled ? "yes" : "no", distribution.environment.name, distribution.application.name)
end
end
end
end
end
end
end
29 changes: 29 additions & 0 deletions lib/ey-core/cli/helpers/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,35 @@ def core_accounts
end
end

# Fetches a list of environments by given name or ID.
#
# @param env_name_or_id [String] name or ID of environment.
#
# @return [Array<Ey::Core::Client::Environment>] list of environments.
def core_environments(env_name_or_id)
core_client.environments.all(name: env_name_or_id).tap do |result|
result << core_client.environments.get(env_name_or_id) if result.empty?
end.to_a.compact
end

# Fetches a list of applications by given name or ID.
#
# @param app_name_or_id [String] name or ID of application.
#
# @return [Array<Ey::Core::Client::Environment>] list of environments.
def core_applications(app_name_or_id)
core_client.applications.all(name: app_name_or_id).tap do |result|
result << core_client.applications.get(app_name_or_id) if result.empty?
end.to_a.compact
end

# Fetches a list of cdn distributions available for current user.
#
# @return [Array<Ey::Core::Client::CdnDistribution>] list of cdn distributions.
def core_cdn_distributions
core_client.cdn_distributions
end

def write_core_yaml(token=nil)
core_yaml[core_url] = token if token
File.open(self.class.core_file, "w") {|file|
Expand Down
8 changes: 8 additions & 0 deletions lib/ey-core/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Ey::Core::Client < Cistern::Service
collection :auto_scaling_groups
collection :backup_files
collection :blueprints
collection :cdn_distributions
collection :components
collection :contacts
collection :costs
Expand Down Expand Up @@ -75,6 +76,7 @@ class Ey::Core::Client < Cistern::Service
model :backup_file
model :billing
model :blueprint
model :cdn_distribution
model :component
model :contact
model :cost
Expand Down Expand Up @@ -138,6 +140,7 @@ class Ey::Core::Client < Cistern::Service
request :create_application_archive
request :create_auto_scaling_group
request :create_backup_file
request :create_cdn_distribution
request :create_database_server
request :create_database_service
request :create_database_service_snapshot
Expand Down Expand Up @@ -167,6 +170,7 @@ class Ey::Core::Client < Cistern::Service
request :destroy_addon
request :destroy_auto_scaling_group
request :destroy_blueprint
request :destroy_cdn_distribution
request :destroy_database_server
request :destroy_database_server_snapshot
request :destroy_database_service
Expand Down Expand Up @@ -215,6 +219,8 @@ class Ey::Core::Client < Cistern::Service
request :get_billing
request :get_blueprint
request :get_blueprints
request :get_cdn_distribution
request :get_cdn_distributions
request :get_component
request :get_components
request :get_contacts
Expand Down Expand Up @@ -295,6 +301,7 @@ class Ey::Core::Client < Cistern::Service
request :get_user
request :get_users
request :get_volumes
request :invalidate_cdn_distribution
request :reboot_server
request :reconcile_server
request :request_callback
Expand All @@ -315,6 +322,7 @@ class Ey::Core::Client < Cistern::Service
request :update_auto_scaling_group
request :update_billing
request :update_blueprint
request :update_cdn_distribution
request :update_environment
request :update_membership
request :update_provider_location
Expand Down
1 change: 1 addition & 0 deletions lib/ey-core/client/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def self.data
:billing => {},
:blueprints => {},
:cluster_firewalls => [],
:cdn_distributions => {},
:components => components,
:contact_assignments => [],
:contacts => {},
Expand Down
9 changes: 9 additions & 0 deletions lib/ey-core/collections/cdn_distributions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Ey::Core::Client::CdnDistributions < Ey::Core::Collection

model Ey::Core::Client::CdnDistribution

self.model_root = "cdn_distribution"
self.model_request = :get_cdn_distribution
self.collection_root = "cdn_distributions"
self.collection_request = :get_cdn_distributions
end
1 change: 1 addition & 0 deletions lib/ey-core/models/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Ey::Core::Client::Application < Ey::Core::Model
attribute :type

has_one :account
has_one :cdn_distribution

has_many :archives, key: :application_archives
has_many :keypairs
Expand Down
51 changes: 51 additions & 0 deletions lib/ey-core/models/cdn_distribution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Ey::Core::Client::CdnDistribution < Ey::Core::Model
extend Ey::Core::Associations

identity :id

attribute :domain
attribute :aliases, type: :array
attribute :asset_host_name
attribute :enabled, type: :boolean
attribute :origin
attribute :provisioned_id
attribute :created_at, type: :time
attribute :updated_at, type: :time
attribute :deleted_at, type: :time

has_one :provider
has_one :application
has_one :environment

def save!
params = {
"cdn_distribution" => {
"aliases" => self.aliases,
"asset_host_name" => self.asset_host_name,
"enabled" => self.enabled
}
}

if new_record?
requires :provider_id, :environment_id, :application_id
params.merge!("url" => self.collection.url, "provider" => self.provider_id, "environment" => self.environment_id,
"application" => self.application_id)
self.connection.requests.new(self.connection.create_cdn_distribution(params).body["request"])
else
requires :identity
params["id"] = self.identity
params["cdn_distribution"]["origin"] = self.origin
self.connection.requests.new(self.connection.update_cdn_distribution(params).body["request"])
end
end

def invalidate(paths = ["*"])
requires :identity
self.connection.requests.new(self.connection.invalidate_cdn_distribution("id" => self.identity, "paths" => paths).body["request"])
end

def destroy
requires :identity
self.connection.requests.new(self.connection.destroy_cdn_distribution("id" => self.identity).body["request"])
end
end
1 change: 1 addition & 0 deletions lib/ey-core/models/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Ey::Core::Client::Environment < Ey::Core::Model
has_many :applications
has_many :logical_databases
has_many :deployments
has_many :cdn_distributions

attr_accessor :application_id

Expand Down
2 changes: 2 additions & 0 deletions lib/ey-core/models/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def resource
self.connection.addresses.get!(resource_id)
when /auto_scaling_group/
self.connection.auto_scaling_groups.get!(resource_id)
when /cdn_distribution/
self.connection.cdn_distributions.get!(resource_id)
when /cluster_update/
self.connection.cluster_updates.get!(resource_id)
when /database_server_snapshot/
Expand Down
15 changes: 8 additions & 7 deletions lib/ey-core/requests/create_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ def create_application(params={})
end

resource.merge!(
"account" => url_for("/accounts/#{account_id}"),
"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,
"account" => url_for("/accounts/#{account_id}"),
"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,
"cdn_distributions" => url_for("/applications/#{resource_id}/cdn-distributions")
)

key = mock_ssh_key
Expand Down
62 changes: 62 additions & 0 deletions lib/ey-core/requests/create_cdn_distribution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class Ey::Core::Client
class Real
def create_cdn_distribution(params={})
url = params.delete("url")

request(
:method => :post,
:path => "/cdn-distributions",
:url => url,
:body => params,
)
end
end

class Mock
def create_cdn_distribution(params={})
request_id = self.uuid
resource_id = self.serial_id

provider_id = resource_identity(params["provider"])
environment_id = resource_identity(params["environment"])
application_id = resource_identity(params["application"])

find(:providers, provider_id)

resource = params["cdn_distribution"].dup

resource.merge!(
"id" => resource_id,
"provisioned_id" => SecureRandom.hex(10),
"resource_url" => "/cdn-distributions/#{resource_id}",
"provider" => url_for("/providers/#{provider_id}"),
"environment" => url_for("/environments/#{environment_id}"),
"application" => url_for("/applications/#{application_id}"),
"deleted_at" => nil,
"domain" => Faker::Internet.domain_name,
"origin" => Faker::Internet.domain_name
)

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

request = {
"id" => request_id,
"type" => "provision_cdn_distribution",
"successful" => "true",
"started_at" => Time.now,
"finished_at" => nil,
"resource" => [:cdn_distributions, 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
end
end
3 changes: 2 additions & 1 deletion lib/ey-core/requests/create_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def create_environment(params={})
"stack_name" => "nginx_passenger4",
"updated_at" => Time.now,
"username" => "deploy",
"service_level" => "default"
"service_level" => "default",
"cdn_distributions" => url_for("/environments/#{resource_id}/cdn-distributions")
)

self.data[:environments][resource_id] = resource
Expand Down
30 changes: 30 additions & 0 deletions lib/ey-core/requests/destroy_cdn_distribution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Ey::Core::Client
class Real
def destroy_cdn_distribution(params={})
id = params["id"]
url = params.delete("url")

request(
:path => "/cdn-distributions/#{id}",
:url => url,
:method => :delete,
)
end
end

class Mock
def destroy_cdn_distribution(params={})
request_id = self.uuid
url = params.delete("url")

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

self.data[:cdn_distributions][cdn_distribution_id]["deleted_at"] = Time.now

response(
:body => {"request" => {id: request_id}},
:status => 201,
)
end
end
end
21 changes: 21 additions & 0 deletions lib/ey-core/requests/get_cdn_distribution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Ey::Core::Client
class Real
def get_cdn_distribution(params={})
id = params["id"]
url = params["url"]

request(
:path => "/cdn-distributions/#{id}",
:url => url,
)
end
end

class Mock
def get_cdn_distribution(params={})
response(
:body => {"cdn_distribution" => self.find(:cdn_distributions, resource_identity(params))},
)
end
end
end
Loading