Class: Cli::AccountCli

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

Instance Method Summary collapse

Instance Method Details

#addObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cli/account.rb', line 15

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
  
  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



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/cli/account.rb', line 139

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



93
94
95
96
97
98
99
100
# File 'lib/cli/account.rb', line 93

def del(uuid)
  to_do = Account[uuid]
  Error.raise("Unknown frontend account UUID: #{uuid}", 100) if to_do == nil or to_do.is_deleted

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

#disable(uuid) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/cli/account.rb', line 121

def disable(uuid)
  to_disable = Account[uuid]
  UnknownUUIDError.raise(uuid) if to_disable == nil or to_disable.is_deleted
  
  if to_disable.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
    
    puts "Account #{uuid} has been disabled." if options[:verbose]
  end
end

#dissociate(uuid) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/cli/account.rb', line 163

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cli/account.rb', line 104

def enable(uuid)
  to_enable = Account[uuid]
  Error.raise("Unknown frontend account UUID: #{uuid}", 100) if to_enable == nil or to_enable.is_deleted
  
  if to_enable.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

    puts "Account #{uuid} has been enabled." if options[:verbose]
  end
end

#modify(uuid) ⇒ Object



84
85
86
87
88
# File 'lib/cli/account.rb', line 84

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]})
end

#oauth(uuid) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/cli/account.rb', line 188

def oauth(uuid)
  require 'oauth'
  acc = Account[uuid]
  Error.raise("Unknown frontend account UUID: #{uuid}", 100) if acc == nil or acc.is_deleted

  oauth_token = OauthToken.new
  oauth_token.generate_keys
  oauth_consumer = OauthConsumer.find(:account_id => acc.id)
  if oauth_consumer.nil?
    oauth_consumer = OauthConsumer.create(
                                          :key => oauth_token.token,
                                          :secret => oauth_token.secret,
                                          :account_id => acc.id
                                          )
  end

  puts ERB.new(<<__END, nil, '-').result(binding)
Account UUID:
<%- if acc.class == Account -%>
  <%= acc.canonical_uuid %>
<%- else -%>
  <%= Account.uuid_prefix%>-<%= acc.uuid %>
<%- end -%>

Consumer Key:
  <%= oauth_consumer.key %>
Consumer Secret:
  <%= oauth_consumer.secret %>
__END
end

#show(uuid = nil) ⇒ Object



31
32
33
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cli/account.rb', line 31

def show(uuid = nil)
  if uuid
    acc = Account[uuid] || UnknownUUIDError.raise(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