Class: AdminModule::CLI

Inherits:
Thor
  • Object
show all
Includes:
Pages
Defined in:
lib/admin_module/cli.rb,
lib/admin_module/cli_old.rb,
lib/admin_module/cli/cli_lock.rb,
lib/admin_module/cli/cli_task.rb,
lib/admin_module/cli/cli_guideline.rb,
lib/admin_module/cli/cli_parameter.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pages

#browser

Constructor Details

#initializeCLI

Returns a new instance of CLI.



22
23
24
# File 'lib/admin_module/cli.rb', line 22

def initialize(*args)
  super
end

Class Method Details

.startObject



16
17
18
19
20
# File 'lib/admin_module/cli.rb', line 16

def self.start(*)
  super
rescue Exception => e
  raise e
end

Instance Method Details

#alias_to_name(gdl_name_or_alias) ⇒ Object

Retrieve a guideline name from the configured aliases



60
61
62
63
64
65
66
67
# File 'lib/admin_module/cli/cli_guideline.rb', line 60

def alias_to_name gdl_name_or_alias
  aliases = AdminModule.configuration.aliases

  gdl_name = aliases[gdl_name_or_alias]
  gdl_name = gdl_name_or_alias if gdl_name.nil?

  gdl_name
end

#all_locksObject

Return configuration data for all locks in the current environment



44
45
46
47
48
49
50
51
52
# File 'lib/admin_module/cli/cli_lock.rb', line 44

def all_locks
  locks = {}

  current_lock_names.each do |name|
    locks[name] = get_lock(name)
  end

  locks
end

#all_tasksObject

Return configuration data for all tasks in the current environment



44
45
46
47
48
49
50
51
52
# File 'lib/admin_module/cli/cli_task.rb', line 44

def all_tasks
  tasks = {}

  current_task_names.each do |name|
    tasks[name] = get_task(name)
  end

  tasks
end

#base_urlObject

Return the base url for the current environment



57
58
59
# File 'lib/admin_module/cli_old.rb', line 57

def base_url
  return AdminModule.configuration.base_urls[environment]
end

#create_lock(lock_data) ⇒ Object

Create a lock in the current environment

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/admin_module/cli/cli_lock.rb', line 82

def create_lock lock_data
  raise ArgumentError, "Invalid lock data: #{lock_data.inspect}" unless valid_lock_data?(lock_data)
  raise ArgumentError, "Missing lock name: #{lock_data.inspect}" unless lock_data_has_name?(lock_data)

  

  lock_def_url = LockDefinitionsPage.new(browser, base_url).
    create_lock(lock_data)

  LockDefinitionPage.new(browser, lock_def_url).set_lock_data lock_data
end

#create_task(task_data) ⇒ Object

Create a task in the current environment

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/admin_module/cli/cli_task.rb', line 82

def create_task task_data
  raise ArgumentError, "Invalid task data: #{task_data.inspect}" unless valid_task_data?(task_data)
  raise ArgumentError, "Missing task name: #{task_data.inspect}" unless task_data_has_name?(task_data)

  

  task_def_url = TaskDefinitionsPage.new(browser, base_url).
    create_task(task_data)

  WorkflowTasksPage.new(browser, task_def_url).set_task_data task_data
end

#credentialsObject

Return the credentials for the current environment



50
51
52
# File 'lib/admin_module/cli_old.rb', line 50

def credentials
  return AdminModule.configuration.credentials[environment]
end

#current_lock_namesObject

Return a list of lock names in the current environment



36
37
38
39
# File 'lib/admin_module/cli/cli_lock.rb', line 36

def current_lock_names
  
  LockDefinitionsPage.new(browser, base_url).locks_options
end

#current_task_namesObject

Return a list of task names in the current environment



36
37
38
39
# File 'lib/admin_module/cli/cli_task.rb', line 36

def current_task_names
  
  TaskDefinitionsPage.new(browser, base_url).tasks_options
end

#deploy(source_file, gdl_name_or_alias, comments = nil) ⇒ Object

Deploy a source file to a guideline in the current environment.

source_file full path to xml file to upload gdl_name_or_alias guideline name (or alias) to version comments to be added to Version Notes area. Defaults to ‘auto upload’

Raises:

  • (IOError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/admin_module/cli/cli_guideline.rb', line 36

def deploy source_file, gdl_name_or_alias, comments = nil
  source_file = Array(source_file)[0]
  raise IOError.new("Missing source file [#{source_file}]") unless File.exists? source_file
  source_file = File.expand_path(source_file)

  gdl_name_or_alias = File.basename(source_file, '.xml') if gdl_name_or_alias.nil?

  

  gdl_name = alias_to_name(gdl_name_or_alias)

  gdl_page_url = GuidelinesPage.new(browser, base_url).
    open_guideline(gdl_name)

  version_gdl_url = GuidelinePage.new(browser, gdl_page_url).
    add_version()

  GuidelineVersionPage.new(browser, version_gdl_url).
    upload(source_file, comments)
end

#deploy_files(source_files, comments = nil) ⇒ Object

Deploy an array of source files to the current environment.

source_files array of files, each file’s basename must be in the configured aliases. comments to be added to Version Notes area. Defaults to ‘auto upload’



23
24
25
26
27
# File 'lib/admin_module/cli/cli_guideline.rb', line 23

def deploy_files source_files, comments = nil
  source_files.each do |src|
    deploy src, File.basename(src, '.xml'), comments
  end
end

#environmentObject

Return the current environment



42
43
44
45
# File 'lib/admin_module/cli_old.rb', line 42

def environment
  @env ||= AdminModule.configuration.default_environment
  @env
end

#environment=(env) ⇒ Object

Set the current environment



31
32
33
34
35
36
37
# File 'lib/admin_module/cli_old.rb', line 31

def environment=(env)
  raise "Unknown environment [#{env}]" unless AdminModule.configuration.credentials.key?(env)
  @env = env
  AdminModule.configure do |config|
    config.default_environment = env
  end
end

#export_locks(file_name) ⇒ Object

Export all lock configurations in the current environment to a file.



119
120
121
122
123
124
125
# File 'lib/admin_module/cli/cli_lock.rb', line 119

def export_locks file_name
  FileUtils.mkdir_p File.dirname(file_name)
  File.open(file_name, 'w') do |f|
    # Write array of lock hashes.
    f << YAML.dump(all_locks)
  end
end

#export_tasks(file_name) ⇒ Object

Export all task configurations in the current environment to a file.



119
120
121
122
123
124
125
# File 'lib/admin_module/cli/cli_task.rb', line 119

def export_tasks file_name
  FileUtils.mkdir_p File.dirname(file_name)
  File.open(file_name, 'w') do |f|
    # Write array of task hashes.
    f << YAML.dump(all_tasks)
  end
end

#get_lock(lock_name) ⇒ Object

Retrieve lock configuration data from the current environment



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/admin_module/cli/cli_lock.rb', line 20

def get_lock lock_name
  

  begin
    lock_def_url = LockDefinitionsPage.new(browser, base_url).
      modify_lock(lock_name)
  rescue Watir::Exception::NoValueFoundException => e
    raise ArgumentError, "Lock [#{lock_name}] not found.\n\n#{e.message}"
  end

  lock_data = LockDefinitionPage.new(browser, lock_def_url).get_lock_data
end

#get_parametersObject



16
17
18
19
20
21
# File 'lib/admin_module/cli/cli_parameter.rb', line 16

def get_parameters
  

  parameters_page = ParametersPage.new(browser, base_url)
  variables = parameters_page.get_parameters
end

#get_task(task_name) ⇒ Object

Retrieve task configuration data from the current environment



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/admin_module/cli/cli_task.rb', line 20

def get_task task_name
  

  begin
    task_def_url = TaskDefinitionsPage.new(browser, base_url).
      modify_task(task_name)
  rescue Watir::Exception::NoValueFoundException => e
    raise ArgumentError, "Task [#{task_name}] not found.\n\n#{e.message}"
  end

  task_data = WorkflowTasksPage.new(browser, task_def_url).get_task_data
end

#import_locks(file_name) ⇒ Object

Import lock configurations into the current environment from a file.

Raises:

  • (IOError)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/admin_module/cli/cli_lock.rb', line 130

def import_locks file_name
  raise IOError, "File not found: #{file_name}" unless File.exists?(file_name)

  locks = {}
  File.open(file_name, 'r') do |f|
    # Write array of lock hashes.
    locks = YAML.load(f)
  end

  existing_locks = current_lock_names

  locks.each do |name, data|
    if existing_locks.include?(name)
      modify_lock(data, name)
    else
      create_lock(data)
    end
  end
end

#import_tasks(file_name) ⇒ Object

Import task configurations into the current environment from a file.

Raises:

  • (IOError)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/admin_module/cli/cli_task.rb', line 130

def import_tasks file_name
  raise IOError, "File not found: #{file_name}" unless File.exists?(file_name)

  tasks = {}
  File.open(file_name, 'r') do |f|
    # Write array of task hashes.
    tasks = YAML.load(f)
  end

  existing_tasks = current_task_names

  tasks.each do |name, data|
    if existing_tasks.include?(name)
      modify_task(data, name)
    else
      create_task(data)
    end
  end
end

#lock_data_has_name?(lock_data) ⇒ Boolean

Test lock data structure for valid name

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/admin_module/cli/cli_lock.rb', line 73

def lock_data_has_name? lock_data
  return false unless lock_data.key?(:name)
  return false if lock_data[:name].empty?
  true
end

#login(force = false) ⇒ Object

Login to the Admin Module

If we’re already logged in, do nothing unless the force flag is true.

force force a re-login if we’ve already logged in



68
69
70
71
72
73
74
75
# File 'lib/admin_module/cli_old.rb', line 68

def (force = false)
  if force || @login_page.nil?
    @login_page = LoginPage.new(browser, base_url)
    @login_page.(*credentials)
  end

  @login_page
end

#logoutObject



77
78
79
80
# File 'lib/admin_module/cli_old.rb', line 77

def logout
  @login_page.logout
  @login_page = nil
end

#modify_lock(lock_data, lock_name = nil) ⇒ Object

Modify an existing lock in the current environment

Raises:

  • (ArgumentError)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/admin_module/cli/cli_lock.rb', line 97

def modify_lock lock_data, lock_name = nil
  lock_name ||= lock_data[:name]
  raise ArgumentError, "Invalid lock data: #{lock_data.inspect}" unless valid_lock_data?(lock_data)
  raise ArgumentError, "Missing lock name" if (lock_name.nil? || lock_name.empty?)

  # Make sure we populate the data's name param if empty so we don't try to write
  # and save an empty name.
  if lock_data[:name].nil? || lock_data[:name].empty?
    lock_data[:name] = lock_name
  end

  

  lock_def_url = LockDefinitionsPage.new(browser, base_url).
    modify_lock(lock_name)

  LockDefinitionPage.new(browser, lock_def_url).set_lock_data lock_data
end

#modify_task(task_data, task_name = nil) ⇒ Object

Modify an existing task in the current environment

Raises:

  • (ArgumentError)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/admin_module/cli/cli_task.rb', line 97

def modify_task task_data, task_name = nil
  task_name ||= task_data[:name]
  raise ArgumentError, "Invalid task data: #{task_data.inspect}" unless valid_task_data?(task_data)
  raise ArgumentError, "Missing task name" if (task_name.nil? || task_name.empty?)

  # Make sure we populate the data's name param if empty so we don't try to write
  # and save an empty name.
  if task_data[:name].nil? || task_data[:name].empty?
    task_data[:name] = task_name
  end

  

  task_def_url = TaskDefinitionsPage.new(browser, base_url).
    modify_task(task_name)

  WorkflowTasksPage.new(browser, task_def_url).set_task_data task_data
end

#quitObject

Close the browser



85
86
87
88
89
90
91
# File 'lib/admin_module/cli_old.rb', line 85

def quit
  unless @browser.nil?
    logout
    @browser.close
    @browser = nil
  end
end

#task_data_has_name?(task_data) ⇒ Boolean

Test task data structure for valid name

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/admin_module/cli/cli_task.rb', line 73

def task_data_has_name? task_data
  return false unless task_data.key?(:name)
  return false if task_data[:name].empty?
  true
end

#update_parameter(var_name, params) ⇒ Object

Update a parameter’s config values.

  • var_name name of parameter to update

  • params hash of values to update where:

    • :name is name of parameter

    • :type is Boolean, Date, DateTime, Money, Numeric, Percentage, or Text (case sensitive)

    • :decision is DSM when true, DPM when false

    • :order Fixnum value

    • :precision Only valid if :type is Numeric

    • :include include in app XML if true

Raises:

  • (ArgumentError)


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
# File 'lib/admin_module/cli/cli_parameter.rb', line 34

def update_parameter(var_name, params)
  raise "Missing params" unless params.size > 0

  

  parameters_page_url = ParametersPage.new(browser, base_url).edit_parameter(var_name)
  page = ParameterPage.new(browser, false)

  page.parameter_name = params.delete(:name) if params.key?(:name)
  page.parameter_type = params.delete(:type) if params.key?(:type)

  if ! params.key?(:decision).nil?
    page.decision_parameter = params[:decision] ? 'Yes' : 'No'
    params.delete(:decision)
  end

  page.parameter_order = params.delete(:order) if params.key?(:order)
  page.precision = params.delete(:precision) if params.key?(:precision)

  if ! params.key?(:include).nil?

    if params[:include]
      page.check_include_in_application_xml
    else
      page.uncheck_include_in_application_xml
    end
  end
  params.delete(:include)
  raise ArgumentError, "Unexpected params: #{params.inspect}" unless params.size == 0

  page.save
end

#valid_lock_data?(lock_data) ⇒ Boolean

Test lock data structure for validity

Required:

at least 1 parameter OR dts

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
# File 'lib/admin_module/cli/cli_lock.rb', line 60

def valid_lock_data? lock_data
  if !lock_data.key?(:parameters) || lock_data[:parameters].empty?
    if !lock_data.key?(:dts) || lock_data[:dts].empty?
      return false
    end
  end
  true
end

#valid_task_data?(task_data) ⇒ Boolean

Test task data structure for validity

Required:

at least 1 parameter OR dts

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
# File 'lib/admin_module/cli/cli_task.rb', line 60

def valid_task_data? task_data
  if !task_data.key?(:parameters) || task_data[:parameters].empty?
    if !task_data.key?(:dts) || task_data[:dts].empty?
      return false
    end
  end
  true
end

#version_all(gdl_names, comments = nil) ⇒ Object

Version all guidelines



72
73
74
75
76
77
78
79
# File 'lib/admin_module/cli/cli_guideline.rb', line 72

def version_all gdl_names, comments = nil
  

  version_all_page_url = GuidelinesPage.new(browser, base_url).version_all
  page = GuidelinesVersionAllPage.new(browser, version_all_page_url)

  page.version gdl_names, comments
end