pvdgm-svc-client 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/Gemfile +17 -0
  4. data/Gemfile.lock +75 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +21 -0
  7. data/Rakefile +50 -0
  8. data/VERSION +1 -0
  9. data/bin/service_mgr +8 -0
  10. data/bin/service_mgr_completion +86 -0
  11. data/bin/valid_commands +16 -0
  12. data/bin/valid_resources +11 -0
  13. data/lib/pvdgm-svc-client/base_resource.rb +118 -0
  14. data/lib/pvdgm-svc-client/prompters/account_mapping_prompter.rb +26 -0
  15. data/lib/pvdgm-svc-client/prompters/configured_account_prompter.rb +30 -0
  16. data/lib/pvdgm-svc-client/prompters/configured_facility_prompter.rb +30 -0
  17. data/lib/pvdgm-svc-client/prompters/facility_mapping_prompter.rb +26 -0
  18. data/lib/pvdgm-svc-client/prompters/public_key_prompter.rb +27 -0
  19. data/lib/pvdgm-svc-client/prompters/service_definition_prompter.rb +30 -0
  20. data/lib/pvdgm-svc-client/prompters/service_prompter.rb +26 -0
  21. data/lib/pvdgm-svc-client/prompters/sltc_provider_prompter.rb +24 -0
  22. data/lib/pvdgm-svc-client/prompters/sltc_registration_prompter.rb +26 -0
  23. data/lib/pvdgm-svc-client/prompters/third_party_prompter.rb +26 -0
  24. data/lib/pvdgm-svc-client/resources/account_mapping.rb +83 -0
  25. data/lib/pvdgm-svc-client/resources/assessment_request.rb +61 -0
  26. data/lib/pvdgm-svc-client/resources/available_file.rb +48 -0
  27. data/lib/pvdgm-svc-client/resources/configured_account.rb +118 -0
  28. data/lib/pvdgm-svc-client/resources/configured_facility.rb +118 -0
  29. data/lib/pvdgm-svc-client/resources/facility_mapping.rb +90 -0
  30. data/lib/pvdgm-svc-client/resources/mds_pull_account.rb +55 -0
  31. data/lib/pvdgm-svc-client/resources/provider.rb +28 -0
  32. data/lib/pvdgm-svc-client/resources/public_key.rb +119 -0
  33. data/lib/pvdgm-svc-client/resources/service.rb +67 -0
  34. data/lib/pvdgm-svc-client/resources/service_definition.rb +147 -0
  35. data/lib/pvdgm-svc-client/resources/sltc_registration.rb +51 -0
  36. data/lib/pvdgm-svc-client/resources/third_party.rb +61 -0
  37. data/lib/pvdgm-svc-client/service_mgr_options.rb +157 -0
  38. data/lib/pvdgm-svc-client.rb +16 -0
  39. data/spec/base_resource_spec.rb +208 -0
  40. data/spec/service_mgr_options_spec.rb +266 -0
  41. data/spec/spec_helper.rb +22 -0
  42. metadata +202 -0
@@ -0,0 +1,119 @@
1
+ module Resources
2
+
3
+ class PublicKey < BaseResource
4
+ include PublicKeyPrompter
5
+
6
+ def list
7
+ result = get("services/public_keys")
8
+ puts "\nPublic Keys:"
9
+ table = Terminal::Table.new headings: [ 'Name', 'Valid Until', 'Created At', 'Updated At' ] do |t|
10
+ result.each do | pk |
11
+ t << [ pk['name'],
12
+ pk['valid_until'],
13
+ pk['created_at'],
14
+ pk['updated_at']
15
+ ]
16
+ end
17
+ end
18
+
19
+ prompter.say table.to_s
20
+ puts
21
+ end
22
+
23
+ def show
24
+ pk_id = public_key_id
25
+ pk = get("services/public_keys/#{pk_id}")
26
+ puts "\nPublic Key:"
27
+ table = Terminal::Table.new headings: [ 'Name', 'Valid Until', 'Created At', 'Updated At' ] do |t|
28
+ t << [ pk['name'],
29
+ pk['valid_until'],
30
+ pk['created_at'],
31
+ pk['updated_at']
32
+ ]
33
+ end
34
+
35
+ puts table
36
+ puts
37
+ end
38
+
39
+ def create
40
+ params = {
41
+ public_key: {
42
+ name: prompter.ask("\nName for new public key: "),
43
+
44
+ key_file: prompter.ask("\nFile containing public key (PEM format): ", ->(field) { File.new(field) }) do |q|
45
+ q.glob = "*.pem"
46
+ q.validate = ->(file_name) { File.exists?(file_name) }
47
+ q.responses[:ask_on_error] = :question
48
+ q.responses[:not_valid] = "\nFile does not exist"
49
+ end,
50
+
51
+ valid_until: prompter.ask("\nDate at which key becomes invalid: ") do |q|
52
+ q.validate = ->(valid_until) {
53
+ return true if valid_until.nil? || valid_until.length == 0
54
+ begin
55
+ Date.parse(valid_until)
56
+ true
57
+ rescue ArgumentError
58
+ false
59
+ end
60
+ }
61
+ q.responses[:ask_on_error] = :question
62
+ q.responses[:not_valid] = "\nInvalid date"
63
+ end
64
+ }
65
+ }
66
+ result = post("services/public_keys", params)
67
+ puts "\nID of new Public Key: #{result['id']}"
68
+ puts
69
+ end
70
+
71
+ def update
72
+ pk_id = public_key_id
73
+ show
74
+ params = {
75
+ public_key: {
76
+ name: prompter.ask("\nName for new public key: "),
77
+
78
+ key_file: prompter.ask("\nFile containing public key (PEM format): ", ->(field) { File.new(field) }) do |q|
79
+ q.validate = ->(file_name) { File.exists?(file_name) }
80
+ q.responses[:ask_on_error] = :question
81
+ q.responses[:not_valid] = "\nFile does not exist"
82
+ end,
83
+
84
+ valid_until: prompter.ask("\nDate at which key becomes invalid: ") do |q|
85
+ q.validate = ->(valid_until) {
86
+ return true if valid_until.nil? || valid_until.length == 0
87
+ begin
88
+ Date.parse(valid_until)
89
+ true
90
+ rescue ArgumentError
91
+ false
92
+ end
93
+ }
94
+ q.responses[:ask_on_error] = :question
95
+ q.responses[:not_valid] = "\nInvalid date"
96
+ end
97
+ }
98
+ }
99
+ result = put("services/public_keys/#{pk_id}", params)
100
+ puts "\nID of updated Public Key: #{result['id']}"
101
+ puts
102
+ end
103
+
104
+ def destroy
105
+ pk_id = public_key_id
106
+ show
107
+ if prompter.agree("\nAre you sure you want to destroy this public key? (y/n) ", true)
108
+ puts
109
+ result = delete("services/public_keys/#{pk_id}")
110
+ puts "\nID of deleted public key: #{result['id']}"
111
+ else
112
+ puts "\nCancelled deletion"
113
+ end
114
+ puts
115
+ end
116
+
117
+ end
118
+
119
+ end
@@ -0,0 +1,67 @@
1
+ module Resources
2
+
3
+ class Service < BaseResource
4
+ include ServicePrompter
5
+
6
+ def list
7
+ result = get("services/services")
8
+ puts "\nServices:"
9
+ table = Terminal::Table.new headings: [ 'Name' ] do |t|
10
+ result.each do | service |
11
+ # puts format "(%4d) %s", service['id'], service['name']
12
+ t << [ service['name'] ]
13
+ end
14
+ end
15
+ prompter.say table.to_s
16
+ puts
17
+ end
18
+
19
+ def show
20
+ sv_id = service_id
21
+ result = get("services/services/#{sv_id}")
22
+ puts "\nService:"
23
+ table = Terminal::Table.new headings: [ 'Name' ] do |t|
24
+ t << [ result['name'] ]
25
+ end
26
+ @service_name = result['name']
27
+ puts table
28
+ puts
29
+ end
30
+
31
+ def create
32
+ params = {
33
+ service: {
34
+ name: prompter.ask("\nName for new service: ") { |q| q.validate = /\A.{1,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid service name" }
35
+ }
36
+ }
37
+ result = post("services/services", params)
38
+ puts "\nID of new service: #{result['id']}"
39
+ puts
40
+ end
41
+
42
+ def update
43
+ sv_id = service_id
44
+ show
45
+ params = {
46
+ service: {
47
+ name: prompter.ask("\nNew name for service: ") { |q| q.default = @service_name; q.validate = /\A.{1,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid service name" }
48
+ }
49
+ }
50
+ result = put("services/services/#{sv_id}", params)
51
+ puts "\nID of updated service: #{result['id']}"
52
+ puts
53
+ end
54
+
55
+ def invoke
56
+ sv_id = service_id
57
+ result = put("services/services/#{sv_id}/invoke", {})
58
+ if result.has_key?('message')
59
+ puts result['message']
60
+ else
61
+ puts result['error']
62
+ end
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -0,0 +1,147 @@
1
+ module Resources
2
+
3
+ class ServiceDefinition < BaseResource
4
+ include ThirdPartyPrompter
5
+ include ServicePrompter
6
+ include ServiceDefinitionPrompter
7
+
8
+ VALIDATE_PORT = ->(answer) do
9
+ return true if answer.is_a?(Fixnum)
10
+ return true if answer.nil? || (answer.is_a?(String) && (answer.empty? || answer == '^'))
11
+ answer =~ /\A[0-9]+\z/
12
+ end
13
+
14
+ def list
15
+ tp_id = third_party_id
16
+ result = get("services/third_parties/#{tp_id}/service_definitions")
17
+ puts "\nService definitions for third party: #{tp_id}"
18
+ table = Terminal::Table.new headings: [ 'Third Party', 'Service', 'Hostname', 'Port', 'Base URI', 'User Name', 'Service Class', 'Password', 'Token' ] do |t|
19
+ result.each do | service_definition |
20
+ t << [ "#{service_definition['third_party_name']} (#{service_definition['third_party_id']})",
21
+ "#{service_definition['service_name']} (#{service_definition['service_id']})",
22
+ service_definition['hostname'],
23
+ service_definition['port'],
24
+ service_definition['base_uri'],
25
+ service_definition['username'],
26
+ service_definition['service_class'],
27
+ service_definition['password'],
28
+ service_definition['token'] ]
29
+ end
30
+ end
31
+ puts table
32
+ puts
33
+ end
34
+
35
+ def show
36
+ tp_id = third_party_id
37
+ sd_id = service_definition_id
38
+ service_definition = get("services/third_parties/#{tp_id}/service_definitions/#{sd_id}")
39
+ @sd_third_party_id = service_definition['third_party_id']
40
+ @sd_service_id = service_definition['service_id']
41
+ @sd_hostname = service_definition['hostname']
42
+ @sd_port = service_definition['port']
43
+ @sd_base_uri = service_definition['base_uri']
44
+ @sd_username = service_definition['username']
45
+ @sd_service_class = service_definition['service_class']
46
+ @sd_password = service_definition['password']
47
+ @sd_token = service_definition['token']
48
+ puts "\nService Definition for third party: #{tp_id}/#{sd_id}"
49
+ table = Terminal::Table.new headings: [ 'Third Party', 'Service', 'Hostname', 'Port', 'Base URI', 'User Name', 'Service Class', 'Password', 'Token' ] do |t|
50
+ t << [ "#{service_definition['third_party_name']} (#{service_definition['third_party_id']})",
51
+ "#{service_definition['service_name']} (#{service_definition['service_id']})",
52
+ service_definition['hostname'],
53
+ service_definition['port'],
54
+ service_definition['base_uri'],
55
+ service_definition['username'],
56
+ service_definition['service_class'],
57
+ service_definition['password'],
58
+ service_definition['token'] ]
59
+ end
60
+ puts table
61
+ puts
62
+ end
63
+
64
+ def get_port_number(prev_value=nil)
65
+ clear_default = ->(field) { field == "^" ? nil : field }
66
+ prompt = "\nPort"
67
+ prompt << " (^ to clear)" unless prev_value.nil?
68
+ prompt << ": "
69
+ v = prompter.ask(prompt, clear_default) do |q|
70
+ q.default = prev_value.to_s unless prev_value.nil?
71
+ q.validate = VALIDATE_PORT
72
+ q.responses[:ask_on_error] = :question
73
+ q.responses[:not_valid] = "\nNot a valid port number"
74
+ end
75
+ v == "0" ? nil : v
76
+ end
77
+
78
+ def create
79
+ tp_id = third_party_id
80
+ params = {
81
+ service_definition: {
82
+ service_id: service_id,
83
+ hostname: prompter.ask("\nHost name: ") { |q| q.validate = /\A.{1,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid host name" },
84
+ port: get_port_number,
85
+ base_uri: prompter.ask("\nBase URI: ") { |q| q.validate = /\A.{1,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid base URI" },
86
+ username: prompter.ask("\nUser name: ") { |q| q.validate = /\A.{0,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid user name" },
87
+ service_class: prompter.ask("\nService Class: ") { |q| q.validate = /\A.{1,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid service class" }
88
+ }
89
+ }
90
+ password = prompter.ask("\nPassword: ")
91
+ token = prompter.ask("\nToken: ")
92
+ if (password && password.size > 0) || (token && token.size > 0)
93
+ params[:credential] = {
94
+ }
95
+ params[:credential][:password] = password if password && password.size > 0
96
+ params[:credential][:token] = token if token && token.size > 0
97
+ end
98
+ result = post("services/third_parties/#{tp_id}/service_definitions", params)
99
+ puts "\nID of new service definition: #{result['id']}"
100
+ puts
101
+ end
102
+
103
+ def update
104
+ clear_default = ->(field) { field == "^" ? '' : field }
105
+ tp_id = third_party_id
106
+ sd_id = service_definition_id
107
+ show
108
+ params = {
109
+ service_definition: {
110
+ service_id: service_id,
111
+ hostname: prompter.ask("\nHost name: ") { |q| q.default = @sd_hostname; q.validate = /\A.{1,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid host name" },
112
+ port: get_port_number(@sd_port),
113
+ base_uri: prompter.ask("\nBase URI: ", String) { |q| q.default = @sd_base_uri.gsub("<%=", "<%%="); q.validate = /\A.{1,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid base URI" }.gsub("<%%=", "<%="),
114
+ username: prompter.ask("\nUser name (^ to clear): ", clear_default) { |q| q.default = @sd_username; q.validate = /\A.{0,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid user name" },
115
+ service_class: prompter.ask("\nService Class: ") { |q| q.default = @sd_service_class; q.validate = /\A.{1,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid service class" }
116
+ }
117
+ }
118
+ password = prompter.ask("\nPassword (^ to clear): ", clear_default) { |q| q.default = @sd_password }
119
+ token = prompter.ask("\nToken (^ to clear): ", clear_default) { |q| q.default = @sd_token }
120
+ if (password && password.size > 0) || (token && token.size > 0)
121
+ params[:credential] = {
122
+ }
123
+ params[:credential][:password] = password if password && password.size > 0
124
+ params[:credential][:token] = token if token && token.size > 0
125
+ end
126
+ result = put("services/third_parties/#{tp_id}/service_definitions/#{sd_id}", params)
127
+ puts "\nID of updated service definition: #{result['id']}"
128
+ puts
129
+ end
130
+
131
+ def destroy
132
+ tp_id = third_party_id
133
+ sd_id = service_definition_id
134
+ show
135
+ if prompter.agree("\nAre you sure you want to destroy this service definition? (y/n) ", true)
136
+ puts
137
+ result = delete("services/third_parties/#{tp_id}/service_definitions/#{sd_id}")
138
+ puts "\nID of deleted service_definition: #{result['id']}"
139
+ else
140
+ puts "\nCancelled deletion"
141
+ end
142
+ puts
143
+ end
144
+
145
+ end
146
+
147
+ end
@@ -0,0 +1,51 @@
1
+ module Resources
2
+
3
+ class SltcRegistration < BaseResource
4
+ include ThirdPartyPrompter
5
+ include ServiceDefinitionPrompter
6
+ include SltcRegistrationPrompter
7
+
8
+ def list
9
+ status = prompter.choose do | menu |
10
+ menu.prompt = "\nSelect the status to filter with: "
11
+
12
+ menu.choice('All', 'Specify no status') { -1 }
13
+ menu.choice('New', 'New registrations') { 0 }
14
+ menu.choice('Completed', 'Completed registrations') { 1 }
15
+ end
16
+
17
+ filter = status >= 0 ? "?status=#{status}" : ''
18
+ result = get("services/sltc_registrations#{filter}")
19
+
20
+ puts "\nSimpleLTC Registrations"
21
+ table = Terminal::Table.new headings: [ 'Status', 'Company Name', 'Initiated By', 'Created At' ] do |t|
22
+ result.each do | registration |
23
+ t << [ registration['status'],
24
+ registration['company_name'],
25
+ registration['initiated_by'],
26
+ registration['created_at'] ]
27
+ end
28
+ end
29
+ prompter.say table.to_s
30
+ puts
31
+ end
32
+
33
+ def complete
34
+ tp_id = third_party_id
35
+ sd_id = service_definition_id
36
+ reg_id = sltc_registration_id
37
+
38
+ params = {
39
+ third_party_id: tp_id,
40
+ service_definition_id: sd_id,
41
+ account_id: prompter.ask("\nAccount ID: ", Integer) { |q| q.validate = lambda { | a | is_valid_object?('Account', a) }; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid Account ID" },
42
+ baseline_days: prompter.ask("\nNumber of days for baseline: ", Integer) { |q| q.default = 180; q.responses[:not_valid] = "\nNot a valid number of days" }
43
+ }
44
+ result = post("services/sltc_registrations/#{reg_id}/complete", params)
45
+ puts result.inspect
46
+ puts
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,61 @@
1
+ module Resources
2
+
3
+ class ThirdParty < BaseResource
4
+ include ThirdPartyPrompter
5
+
6
+ EMAIL_REGEX = /\A[\w\.%\+\-\']+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|jobs|museum)\z/i
7
+
8
+ def list
9
+ result = get("services/third_parties")
10
+ puts "\nThird Parties:"
11
+ table = Terminal::Table.new headings: [ 'Name', 'Contact Email' ] do |t|
12
+ result.each do | third_party |
13
+ t << [ third_party['name'], third_party['contact_email'] ]
14
+ end
15
+ end
16
+
17
+ prompter.say table.to_s
18
+ puts
19
+ end
20
+
21
+ def show
22
+ tp_id = third_party_id
23
+ result = get("services/third_parties/#{tp_id}")
24
+ @third_party_name = result['name']
25
+ @third_party_contact_email = result['contact_email']
26
+ puts "\nThird Party"
27
+ table = Terminal::Table.new headings: [ 'Name', 'Contact Email' ] do |t|
28
+ t << [ result['name'], result['contact_email'] ]
29
+ end
30
+ puts table
31
+ puts
32
+ end
33
+
34
+ def create
35
+ params = {
36
+ third_party: {
37
+ name: prompter.ask("\nName for new third party: ") { |q| q.validate = /\A.{1,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid Third Party Name" },
38
+ contact_email: prompter.ask("\nContact email address for new third party: ") { |q| q.validate = EMAIL_REGEX; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid email address" }
39
+ }
40
+ }
41
+ result = post("services/third_parties", params)
42
+ puts "\nID of new third party: #{result['id']}"
43
+ puts
44
+ end
45
+
46
+ def update
47
+ show
48
+ params = {
49
+ third_party: {
50
+ name: prompter.ask("\nNew name for third party: ") { |q| q.default = @third_party_name; q.validate = /\A.{1,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid Third Party Name" },
51
+ contact_email: prompter.ask("\nNew contact email address for third party: ") { |q| q.default = @third_party_contact_email; q.validate = EMAIL_REGEX; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid email address" }
52
+ }
53
+ }
54
+ result = put("services/third_parties/#{third_party_id}", params)
55
+ puts "\nID of updated third party: #{result['id']}"
56
+ puts
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,157 @@
1
+ class ServiceMgrOptions
2
+
3
+ DEFAULT_SERVER = 'www.abaqis.com'
4
+ LOCAL_SERVER = 'localhost:3000'
5
+ UAT_SERVER = 'uat.abaqis.com'
6
+ DEFAULT_USE_SSL = true
7
+ DEFAULT_RESOURCE = 'service'
8
+ DEFAULT_COMMAND = 'list'
9
+ DEFAULT_TOKEN = ENV['API_TOKEN']
10
+
11
+ def initialize
12
+ @options = {}
13
+
14
+ @optparse = OptionParser.new do | opts |
15
+ # Set a banner, displayed at the top of the help screen
16
+ opts.banner = "Usage: service_mgr [ options ] [ resource [command] ]"
17
+
18
+ opts.on '-h', '--help', 'Display the help message' do
19
+ puts opts
20
+ puts
21
+ print_valid_resources
22
+ puts
23
+ exit
24
+ end
25
+
26
+ @options[:server] = DEFAULT_SERVER
27
+ opts.on '-s', '--server SERVER', "Specify the abaqis server to hit (default #{DEFAULT_SERVER})" do | server |
28
+ @options[:server] = server
29
+ end
30
+
31
+ @options[:use_ssl] = DEFAULT_USE_SSL
32
+ opts.on '-n', '--no-ssl', "If specified, SSL will NOT be used for the call to the server" do
33
+ @options[:use_ssl] = false
34
+ end
35
+
36
+ opts.on '-l', '--list', 'List the valid commands' do
37
+ print_valid_resources
38
+ exit
39
+ end
40
+
41
+ @options[:api_token] = DEFAULT_TOKEN
42
+ opts.on '-t', '--token TOKEN', 'The API security token to use (defaults to ENV["API_TOKEN"])' do | token |
43
+ @options[:api_token] = token
44
+ end
45
+
46
+ opts.on '--local', 'Set the host to the localhost and non-ssl as a testing convenience' do
47
+ @options[:server] = LOCAL_SERVER
48
+ @options[:use_ssl] = false
49
+ end
50
+
51
+ opts.on '--uat', 'Set the host to the uat machine and ssl as a testing convenience' do
52
+ @options[:server] = UAT_SERVER
53
+ @options[:use_ssl] = true
54
+ end
55
+
56
+ @options[:trace] = false
57
+ opts.on '--trace', 'Set the output to verbose logging (including stack trace)' do
58
+ @options[:trace] = true
59
+ end
60
+
61
+ opts.on '--account-mapping-id ID', 'The ID of the account mapping object' do | id |
62
+ @options[:account_mapping_id] = id
63
+ end
64
+
65
+ opts.on '--configured-account-id ID', 'The ID of the configured account' do | id |
66
+ @options[:configured_account_id] = id
67
+ end
68
+
69
+ opts.on '--facility-mapping-id ID', 'The ID of the facility mapping' do | id |
70
+ @options[:facility_mapping_id] = id
71
+ end
72
+
73
+ opts.on '--service-definition-id ID', 'The ID of the service definition' do | id |
74
+ @options[:service_definition_id] = id
75
+ end
76
+
77
+ opts.on '--service-id ID', 'The ID of the service' do | id |
78
+ @options[:service_id] = id
79
+ end
80
+
81
+ opts.on '--third-party-id ID', 'The ID of the third party' do | id |
82
+ @options[:third_party_id] = id
83
+ end
84
+
85
+ end
86
+ end
87
+
88
+ def parse_options!(argv=ARGV)
89
+ @optparse.parse!(argv)
90
+
91
+ resource = argv.shift || DEFAULT_RESOURCE
92
+ command = argv.shift || DEFAULT_COMMAND
93
+
94
+ # Check to make sure we have a valid resource
95
+ unless valid_resource?(resource)
96
+ puts @optparse
97
+ puts
98
+ puts "Invalid resource: #{resource}"
99
+ puts
100
+ print_valid_resources
101
+ exit
102
+ end
103
+
104
+ # Must have been a valid resource; do we have a valid command
105
+ unless valid_command?(resource, command)
106
+ puts @optparse
107
+ puts
108
+ puts "Invalid command (#{command}) for resource (#{resource})"
109
+ puts
110
+ print_valid_resources
111
+ exit
112
+ end
113
+
114
+ # Check the API token
115
+ if @options[:api_token].nil?
116
+ puts @optparse
117
+ puts
118
+ puts "Must specify an API token either via the environment variable 'API_TOKEN' or via the -t option"
119
+ puts
120
+ print_valid_resources
121
+ exit
122
+ end
123
+
124
+ [ @options, resource, command ]
125
+ end
126
+
127
+ def invoke_command
128
+ options, resource, command = parse_options!
129
+
130
+ eval("Resources::#{ActiveSupport::Inflector.camelize(resource)}").new(options).send(command.to_sym)
131
+ end
132
+
133
+ private
134
+
135
+ def valid_command?(resource, command)
136
+ return false if resource.blank? || command.blank?
137
+ eval("Resources::#{ActiveSupport::Inflector.camelize(resource)}").instance_methods(false).include?(command.to_sym)
138
+ end
139
+
140
+ def valid_resource?(resource)
141
+ Resources.constants.map { | const | ActiveSupport::Inflector.underscore(const.to_s) }.include?(resource)
142
+ end
143
+
144
+ def print_valid_resources
145
+ puts "Valid Resources:"
146
+ Resources.constants.each do | const |
147
+ resource_string = ActiveSupport::Inflector.underscore(const.to_s)
148
+ puts " - #{resource_string}"
149
+ puts " Commands: #{print_valid_commands(resource_string)} Default: list"
150
+ end
151
+ end
152
+
153
+ def print_valid_commands(resource)
154
+ eval("Resources::#{ActiveSupport::Inflector.camelize(resource)}").instance_methods(false).map { | command | command.to_s }.join(", ")
155
+ end
156
+ end
157
+
@@ -0,0 +1,16 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext/object'
3
+ require 'pvdgm-svc-client/service_mgr_options'
4
+ require 'pvdgm-svc-client/base_resource'
5
+
6
+ # Load all the prompters
7
+ Dir.glob(File.join(File.dirname(__FILE__), 'pvdgm-svc-client/prompters/*.rb')).each do | prompter_path |
8
+ require File.join('pvdgm-svc-client/prompters', File.basename(prompter_path))
9
+ end
10
+
11
+ # Load all the resources
12
+ Dir.glob(File.join(File.dirname(__FILE__), 'pvdgm-svc-client/resources/*.rb')).each do | resource_path |
13
+ require File.join('pvdgm-svc-client/resources', File.basename(resource_path))
14
+ end
15
+
16
+