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

Setup ey-core environment_variables CLI functionality #134

Open
wants to merge 18 commits into
base: develop
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
65 changes: 2 additions & 63 deletions lib/ey-core/cli/environment_variables.rb
Original file line number Diff line number Diff line change
@@ -1,70 +1,9 @@
require 'ey-core/cli/subcommand'
require 'ey-core/cli/helpers/stream_printer'
require_relative "environment_variables/main"

module Ey
module Core
module Cli
class EnvironmentVariables < Subcommand
MASK = '****'.freeze
SYMBOLS_TO_DISPLAY = 4
MAX_LENGTH_TO_DISPLAY = 30

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

title "environment_variables"
summary "Retrieve a list of Engine Yard environment variables for environments that you have access to."

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
environment_variables = if option(:application)
core_applications(option(:application)).flat_map(&:environment_variables)
elsif option(:environment)
core_environments(option(:environment)).flat_map(&:environment_variables)
else
core_environment_variables
end

stream_print("ID" => 10, "Name" => 30, "Value" => 50, "Environment" => 30, "Application" => 30) do |printer|
environment_variables.each_entry do |ev|
printer.print(ev.id, ev.name, print_variable_value(ev), ev.environment_name, ev.application_name)
end
end
end

private

def print_variable_value(environment_variable)
if environment_variable.sensitive && !switch_active?(:display_sensitive)
hide_sensitive_data(environment_variable.value)
else
environment_variable.value
end
end

def hide_sensitive_data(value)
if value.length > SYMBOLS_TO_DISPLAY
MASK + value[-SYMBOLS_TO_DISPLAY, SYMBOLS_TO_DISPLAY]
else
MASK
end
end
module EnvironmentVariables
end
end
end
Expand Down
81 changes: 81 additions & 0 deletions lib/ey-core/cli/environment_variables/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'ey-core/cli/subcommand'

module Ey
module Core
module Cli
module EnvironmentVariables
class Create < Ey::Core::Cli::Subcommand
MASK = '****'.freeze
SYMBOLS_TO_DISPLAY = 4
MAX_LENGTH_TO_DISPLAY = 30

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

title "create"
summary "Create Engine Yard environment variable "

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'

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


option :key,
short: 'k',
long: 'key',
description: 'Key',
argument: 'Key'

option :value,
short: 'v',
long: 'value',
description: 'Value',
argument: 'Value'

def handle
application_name = option(:application)
environment_name = option(:environment)
key = option(:key)
value = option(:value)

puts "Create Environment Variable"
env = core_client.environments.filter{ |env| env.name == environment_name }&.first
app = core_client.applications.filter{ |app| app.name == application_name }&.first
puts core_client.environment_variables.create(name:key,value:value,environment_id:env.id, application_id: app.id)
end

private

def print_variable_value(environment_variable)
if environment_variable.sensitive && !switch_active?(:display_sensitive)
hide_sensitive_data(environment_variable.value)
else
environment_variable.value
end
end

def hide_sensitive_data(value)
if value.length > SYMBOLS_TO_DISPLAY
MASK + value[-SYMBOLS_TO_DISPLAY, SYMBOLS_TO_DISPLAY]
else
MASK
end
end
end
end
end
end
end
80 changes: 80 additions & 0 deletions lib/ey-core/cli/environment_variables/destroy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'ey-core/cli/subcommand'

module Ey
module Core
module Cli
module EnvironmentVariables
class Destroy < Ey::Core::Cli::Subcommand
MASK = '****'.freeze
SYMBOLS_TO_DISPLAY = 4
MAX_LENGTH_TO_DISPLAY = 30

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

title "destroy"
summary "Destory a Engine Yard environment variable "

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'

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

option :key,
short: 'k',
long: 'key',
description: 'Key',
argument: 'Key'

option :value,
short: 'v',
long: 'value',
description: 'Value',
argument: 'Value'

def handle
application_name = option(:application)
environment_name = option(:environment)
key = option(:key)
value = option(:value)

puts "Destroy Environment Variable"
environment = core_client.environments.first{|e| e.name == environment_name && e.application.name == application_name }
ev = core_client.environment_variables.first{|ev| ev.name == key && ev.environment_id == environment.id && ev.application_id == environment.application.id }
puts ev.destroy!
end

private

def print_variable_value(environment_variable)
if environment_variable.sensitive && !switch_active?(:display_sensitive)
hide_sensitive_data(environment_variable.value)
else
environment_variable.value
end
end

def hide_sensitive_data(value)
if value.length > SYMBOLS_TO_DISPLAY
MASK + value[-SYMBOLS_TO_DISPLAY, SYMBOLS_TO_DISPLAY]
else
MASK
end
end
end
end
end
end
end
72 changes: 72 additions & 0 deletions lib/ey-core/cli/environment_variables/list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require 'ey-core/cli/subcommand'

module Ey
module Core
module Cli
module EnvironmentVariables
class List < Ey::Core::Cli::Subcommand
MASK = '****'.freeze
SYMBOLS_TO_DISPLAY = 4
MAX_LENGTH_TO_DISPLAY = 30

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

title "list"
summary "Retrieve a list of Engine Yard environment variables for environments that you have access to."

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
environment_variables = if option(:application)
core_applications(option(:application)).flat_map(&:environment_variables)
elsif option(:environment)
core_environments(option(:environment)).flat_map(&:environment_variables)
else
core_environment_variables
end
stream_print("ID" => 10, "Name" => 30, "Value" => 50, "Environment" => 30, "Application" => 30) do |printer|
environment_variables.each_entry do |ev|
printer.print(ev.id, ev.name, ev.value, ev.environment_name, ev.application_name)
end
end
end

private

def print_variable_value(environment_variable)
puts "in print_variable_value"
if environment_variable.sensitive && !switch_active?(:display_sensitive)
hide_sensitive_data(environment_variable.value)
else
environment_variable.value
end
end

def hide_sensitive_data(value)
if value.length > SYMBOLS_TO_DISPLAY
MASK + value[-SYMBOLS_TO_DISPLAY, SYMBOLS_TO_DISPLAY]
else
MASK
end
end
end
end
end
end
end
23 changes: 23 additions & 0 deletions lib/ey-core/cli/environment_variables/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'ey-core/cli/subcommand'
require 'ey-core/cli/environment_variables/list'
require 'ey-core/cli/environment_variables/create'
require 'ey-core/cli/environment_variables/update'
require 'ey-core/cli/environment_variables/destroy'

module Ey
module Core
module Cli
module EnvironmentVariables
class Main < Ey::Core::Cli::Subcommand
title "environment_variables"
summary "Environment variables specific commands"

mount Ey::Core::Cli::EnvironmentVariables::List
mount Ey::Core::Cli::EnvironmentVariables::Create
mount Ey::Core::Cli::EnvironmentVariables::Update
mount Ey::Core::Cli::EnvironmentVariables::Destroy
end
end
end
end
end
Loading