Class: Cli::AccountCli

Inherits:
Base
  • Object
show all
Defined in:
lib/cli/account.rb

Instance Method Summary collapse

Instance Method Details

#addObject

method_option :verbose, :type => :boolean, :aliases => “-v”, :desc => “Print feedback on what is happening.”



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cli/account.rb', line 52

def add
  #Check if the data we got is valid
  if options[:name] != nil && options[:name].length > 255
    raise "Account name can not be longer than 255 characters."
  end
  if options[:description] != nil && options[:description].length > 100
    raise "Account description can not be longer than 100 chracters."
  end
  
  #Put them in the backend
  #fields = {:description => options[:description], :enabled => M::Account::ENABLED}
  #fields.merge!({:uuid => options[:uuid]}) unless options[:uuid].nil?
  
  #new_uuid = super(M::Account,fields)

  ##This should never happen as long as the databases remain synchronized.
  #begin
    #raise "A uuid collision occurred. This means the account databases are not synchronized." if Account[new_uuid] != nil
  #rescue
    #M::Account[new_uuid].delete
    #raise 
  #end
  
  #Put them in the frontend
  #super(Account,{:uuid => new_uuid,:name => options[:name],:description => options[:description]})
  
  #puts new_uuid
  fields = {:name => options[:name],:description => options[:description], :enable => Account::ENABLED}
  fields.merge!({:uuid => options[:uuid]}) unless options[:uuid].nil?
  puts super(Account,fields)
end

#associate(uuid) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/cli/account.rb', line 205

def associate(uuid)      
   = Account[uuid] || UnknownUUIDError.raise(uuid)
  
  options[:users].each { |u|        
    if User[u].nil?
      puts "Unknown user UUID: #{u}" if options[:verbose]
    elsif !.users.index(User[u]).nil?
      puts "Account #{uuid} is already associated with user #{u}." if options[:verbose]
    else
      user = User[u]
      .add_user(user)
      if user..nil?
        user. = .uuid
        user.save
      end          
      
      puts "Account #{uuid} successfully associated with user #{u}." if options[:verbose]
    end
  }
end

#del(uuid) ⇒ Object



151
152
153
154
155
156
# File 'lib/cli/account.rb', line 151

def del(uuid)
  super(Account,uuid)
  #super(M::Account,uuid)
  
  puts "Account #{uuid} has been deleted." if options[:verbose]
end

#disable(uuid) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/cli/account.rb', line 182

def disable(uuid)
  to_disable = Account[uuid]
  UnknownUUIDError.raise(uuid) if to_disable == nil or to_disable.is_deleted
  #to_disable_back = M::Account[uuid] || Error.raise("Unknown backend account UUID: #{uuid}", 100)
  
  if to_disable.disable? #&& to_disable_back.disable?
    puts "Account #{id} is already disabled." if options[:verbose]
  else
    to_disable.enable = Account::DISABLED
    to_disable.updated_at = Time.now.utc.iso8601
    to_disable.save
    
    #to_disable_back.enabled = M::Account::DISABLED
    #to_disable_back.updated_at = Time.now.utc.iso8601
    #to_disable_back.save
    
    puts "Account #{uuid} has been disabled." if options[:verbose]
  end
end

#dissociate(uuid) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/cli/account.rb', line 229

def dissociate(uuid)
   = Account[uuid] || UnknownUUIDError.raise(uuid)
  
  options[:users].each { |u|
    user = User[u]
    if user.nil?
      puts "Unknown user UUID: #{u}" if options[:verbose]
    elsif .users.index(User[u]).nil?
      puts "Account #{uuid} is not associated with user #{u}." if options[:verbose]
    else
      .remove_user(user)
      
      puts "Account #{uuid} successfully dissociated from user #{u}." if options[:verbose]
      
      if .uuid == user.
        user. = nil
        user.save
        puts "This was user #{u}'s primary account. Has been set to Null now." if options[:verbose]
      end
    end
  }
end

#enable(uuid) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/cli/account.rb', line 160

def enable(uuid)
  to_enable = Account[uuid]
  Error.raise("Unknown frontend account UUID: #{uuid}", 100) if to_enable == nil or to_enable.is_deleted
  #to_enable_back = M::Account[uuid] || Error.raise("Unknown backend account UUID: #{uuid}", 100)
  
  if to_enable.enable?# && to_enable_back.enable?
    puts "Account #{uuid} is already enabled." if options[:verbose]
  else      
    to_enable.enable = Account::ENABLED
    to_enable.updated_at = Time.now.utc.iso8601
    to_enable.save   
     
    #to_enable_back.enabled = Dcmgr::Models::Account::ENABLED
    #to_enable_back.updated_at = Time.now.utc.iso8601
    #to_enable_back.save
	
    puts "Account #{uuid} has been enabled." if options[:verbose]
  end
end

#modify(uuid) ⇒ Object

method_option :verbose, :type => :boolean, :aliases => “-v”, :desc => “Print feedback on what is happening.”



141
142
143
144
145
146
# File 'lib/cli/account.rb', line 141

def modify(uuid)
  raise "Account name can not be longer than 255 characters." if options[:name] != nil && options[:name].length > 255
  raise "Description can not be longer than 100 characters." if options[:description] != nil && options[:description].length > 100
  super(Account,uuid,{:name => options[:name],:description => options[:description]})
  #super(M::Account,uuid,{:description => options[:description]})
end

#show(uuid = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cli/account.rb', line 86

def show(uuid = nil)
  if uuid
    #back_acc = M::Account[uuid] || raise(Thor::Error, "Unknown Account UUID: #{uuid}")
    acc = Account[uuid] || raise(Thor::Error, "Unknown Account UUID: #{uuid}")
    puts ERB.new(<<__END, nil, '-').result(binding)
Account UUID:
<%- if acc.class == Account -%>
  <%= acc.canonical_uuid %>
<%- else -%>
  <%= Account.uuid_prefix%>-<%= acc.uuid %>
<%- end -%>
Enabled:
<%- if acc.enable? -%>
  Yes
<%- else -%>
  No
<%- end -%>
<%- if acc.name -%>
Name:
  <%= acc.name %>
<%- end -%>
<%- if acc.description -%>
Description:
  <%= acc.description %>
<%- end -%>
<%- if acc.is_deleted -%>
Deleted at:
  <%= acc.deleted_at %>
<%- end -%>
<%- unless acc.users.empty? -%>
Associated users:
<%- acc.users.each { |row| -%>
  <%= row.canonical_uuid %>\t<%= row.name %>
<%- } -%>
<%- end -%>
__END
  else
    #This needs an "|| false" because options[:deleted] is usually nil which isn't the same as false
    acc = Account.filter(:is_deleted => (options[:deleted] || false )).all
    puts ERB.new(<<__END, nil, '-').result(binding)
<%- acc.each { |row| -%>
<%- if row.class == Account -%>
<%= row.canonical_uuid %>\t<%= row.name %>
<%- else -%>
<%= Account.uuid_prefix%>-<%= row.uuid %>\t<%= row.name %>
<%- end -%>
<%- } -%>
__END
  end
end