Class: Resources::ConfiguredAccount

Inherits:
BaseResource show all
Includes:
ConfiguredAccountPrompter, ServiceDefinitionPrompter, ThirdPartyPrompter
Defined in:
lib/pvdgm-svc-client/resources/configured_account.rb

Instance Attribute Summary

Attributes inherited from BaseResource

#options, #prompter

Instance Method Summary collapse

Methods included from ConfiguredAccountPrompter

#configured_account_id

Methods included from ServiceDefinitionPrompter

#service_definition_id

Methods included from ThirdPartyPrompter

#third_party_id

Methods inherited from BaseResource

#initialize

Constructor Details

This class inherits a constructor from BaseResource

Instance Method Details

#createObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pvdgm-svc-client/resources/configured_account.rb', line 50

def create
  tp_id = third_party_id
  sd_id = service_definition_id
  params = { 
    configured_account: {
      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" },
      username: prompter.ask("\nUser name: ") { |q| q.validate = /\A.{0,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nInvalid user name" },
      enabled: prompter.agree("\nEnabled? (y/n) ", true)  
    }
  }
  puts
  password = prompter.ask("\nPassword: ")
  token = prompter.ask("\nToken: ")
  if (password && password.size > 0) || (token && token.size > 0) 
    params[:credential] = {
    }
    params[:credential][:password] = password if password && password.size > 0
    params[:credential][:token] = token if token && token.size > 0
  end
  result = post("services/third_parties/#{tp_id}/service_definitions/#{sd_id}/configured_accounts", params)
  puts "\nID of new configured account: #{result['id']}"
  puts
end

#destroyObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/pvdgm-svc-client/resources/configured_account.rb', line 101

def destroy
  tp_id = third_party_id
  sd_id = service_definition_id
  ca_id = 
  show
  if prompter.agree("\nAre you sure you want to destroy this configured account? (y/n) ", true)
    puts
    result = delete("services/third_parties/#{tp_id}/service_definitions/#{sd_id}/configured_accounts/#{ca_id}")
    puts "\nID of deleted configured account: #{result['id']}"
  else
    puts "\nCancelled deletion"
  end
  puts
end

#listObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pvdgm-svc-client/resources/configured_account.rb', line 8

def list
  tp_id = third_party_id
  sd_id = service_definition_id
  result = get("services/third_parties/#{tp_id}/service_definitions/#{sd_id}/configured_accounts")
  puts "\nConfigured accounts for third party: #{tp_id}, service definition: #{sd_id}"
  table = Terminal::Table.new headings: [ 'Service Def Id', 'Account ID', 'User name', 'Enabled', 'Password', 'Token' ] do |t|
  result.each do |  |
    t << [ ['service_definition_id'],
           "#{['account_name']} (#{['account_id']})",
           ['username'],
           ['enabled'],
           ['password'],
           ['token'] ]
    end
  end
  prompter.say table.to_s
  puts
end

#showObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pvdgm-svc-client/resources/configured_account.rb', line 27

def show
  tp_id = third_party_id
  sd_id = service_definition_id
  ca_id = 
   = get("services/third_parties/#{tp_id}/service_definitions/#{sd_id}/configured_accounts/#{ca_id}")
  @ca_account_id = ['account_id']
  @ca_username = ['username']
  @ca_enabled = ['enabled']
  @ca_password = ['password']
  @ca_token = ['token']
  puts "\nConfigured account for third party: #{tp_id}/#{options[:id]}"
  table = Terminal::Table.new headings: [ 'Service Def Id', 'Account ID', 'User name', 'Enabled', 'Password', 'Token' ] do |t|
    t << [ ['service_definition_id'],
           "#{['account_name']} (#{['account_id']})",
           ['username'],
           ['enabled'],
           ['password'],
           ['token'] ]
  end
  puts table
  puts
end

#updateObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pvdgm-svc-client/resources/configured_account.rb', line 74

def update
  clear_default = ->(field) { field == "^" ? '' : field }
  tp_id = third_party_id
  sd_id = service_definition_id
  ca_id = 
  show
  params = { 
    configured_account: {
      account_id: prompter.ask("\nAccount ID: ", Integer) { |q| q.default = @ca_account_id; q.validate = lambda { | a | is_valid_object?('Account', a) }; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nNot a valid Account ID" },
      username: prompter.ask("\nUser name (^ to clear): ", clear_default) { |q| q.default = @ca_username; q.validate = /\A.{0,255}\z/; q.responses[:ask_on_error] = :question; q.responses[:not_valid] = "\nInvalid user name" },
      enabled: prompter.agree("\nEnabled? ", true)  { |q| q.default = @ca_enabled }
    }
  }
  puts
  password = prompter.ask("\nPassword (^ to clear): ", clear_default) { |q| q.default = @ca_password }
  token = prompter.ask("\nToken (^ to clear): ", clear_default) { |q| q.default = @ca_token }
  if (password && password.size > 0) || (token && token.size > 0) 
    params[:credential] = {
    }
    params[:credential][:password] = password if password && password.size > 0
    params[:credential][:token] = token if token && token.size > 0
  end
  result = put("services/third_parties/#{tp_id}/service_definitions/#{sd_id}/configured_accounts/#{ca_id}", params)
  puts "\nID of updated configured account: #{result['id']}"
  puts
end