Class: JanRandom

Inherits:
Thor
  • Object
show all
Defined in:
lib/janrandom.rb

Instance Method Summary collapse

Instance Method Details

#appsObject



54
55
56
57
58
59
# File 'lib/janrandom.rb', line 54

def apps()
  read_options

  puts 'Apps available to janrandom:'
  @apps.each_key { |key| puts key }
end

#create(app, number = 10) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/janrandom.rb', line 112

def create(app, number = 10)
  read_options

  unless @apps.has_key?(app)
    puts "#{app} has not been initialized to JRUG yet. Try running `janrandom init #{app}` first."
    exit
  end

  puts "Creating #{number} randomized users for #{app}:"

  randomUsers = [ ]
  number.to_i.times do
    user = UserAttrs.new()
    record = { }
    @apps[app][:schema].each do |attr,meth|
      if meth.is_a?(String) && UserAttrs.method_defined?(meth)
        record[attr] = user.send(meth)
      end
    end

    randomUsers << record
    user = nil
  end

  appoptions = @apps[app]

  createdUsers = RestClient.post( "https://#{appoptions[:server]}/entity.bulkCreate",
    {
      :client_id      => appoptions[:clientid],
      :client_secret  => appoptions[:clientsecret],
      :type_name      => 'user',
      :all_attributes => JSON.dump(randomUsers)
    }
  )

  apiResponse = JSON.parse( createdUsers )

  tableRows = apiResponse['uuid_results'].zip(
    randomUsers.collect { |n| n['displayName'] },
    randomUsers.collect { |n| n['email'] },
    randomUsers.collect { |n| n['currentLocation'] }
  )

  table = Terminal::Table.new :rows => tableRows
  puts table
end

#init(app) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
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
100
101
102
103
104
105
106
107
108
# File 'lib/janrandom.rb', line 63

def init(app)
  read_options

  if @apps && @apps.has_key?(app)
    puts 'Using credentials from janrandom.appconfig file...'
    appoptions = @apps[app]
  else
    puts "Enter credentials for #{app}...\n"
    appoptions = Hash.new
    appoptions[:server]       = ask("Capture Server?  ") { |q| q.default = app }
    appoptions[:clientid]     = ask("Client ID?       ") { |q| q.default = "none" }
    appoptions[:clientsecret] = ask("Client Secret?   ") { |q| q.echo    = "x" }
    @apps[app] = appoptions
    File.open(@config_file, 'w') { |f| YAML.dump(@apps, f) }
  end

  entityType = RestClient.post( "https://#{appoptions[:server]}/entityType",
    {
      :client_id     => appoptions[:clientid],
      :client_secret => appoptions[:clientsecret],
      :type_name     => 'user'
    }
  )

  attrDefs = JSON.parse( entityType )

  @schema = { }
  parse_attr_defs( attrDefs['schema']['attr_defs'] )

  # If there's no "janRandomGenerated" attribute in the schema, add it now
  unless @schema.has_key?('janRandomGenerated')
    RestClient.post( "https://#{appoptions[:server]}/entityType.addAttribute",
      {
        :client_id     => appoptions[:clientid],
        :client_secret => appoptions[:clientsecret],
        :type_name     => 'user',
        :attr_def      => '{"name":"janRandomGenerated","type":"string","length":1}'
      }
    )
  end

  @apps[app][:schema] = @schema
  File.open(@config_file, 'w') { |f| YAML.dump(@apps, f) }

  puts "Successfully prepared app #{app} for random user generation."
end

#test(app) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/janrandom.rb', line 217

def test(app)
  read_options
  unless @apps.has_key?(app)
    puts 'Invalid app name.'
    exit
  end

  user = UserAttrs.new()
  record = { }
  @apps[app][:schema].each do |attr,meth|
    if meth.is_a?(String) && UserAttrs.method_defined?(meth)
      record[attr] = user.send(meth)
    end
  end

  table = Terminal::Table.new :rows => record
  puts table
end

#uncreate(app) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/janrandom.rb', line 161

def uncreate(app)

  read_options
  unless @apps.has_key?(app)
    puts "#{app} has not been initialized to JRUG yet. Try running `janrandom init #{app}` first."
    exit
  end

  appoptions = @apps[app]

  deletedUsers = RestClient.post( "https://#{appoptions[:server]}/entity.bulkDelete",
    {
      :client_id     => appoptions[:clientid],
      :client_secret => appoptions[:clientsecret],
      :type_name     => 'user',
      :filter        => 'janRandomGenerated is not null',
      :commit        => true
    }
  )

  apiResponse = JSON.parse( deletedUsers )
  if apiResponse.has_key?('delete_count')
    puts ( apiResponse['delete_count'] > 0 ) ?
      "Successfully deleted #{apiResponse['delete_count']} random people from #{app}." :
      "No random users left to delete from this app!"
  else
    puts "Error deleting users."
    puts apiResponse
  end

end

#uninit(app) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/janrandom.rb', line 194

def uninit(app)

  read_options
  unless @apps.has_key?(app)
    puts 'Invalid app name.'
    exit
  end

  # If there is a "janRandomGenerated" attribute in the schema, remove it now
  if @apps[app].has_key?('janRandomGenerated')
    apiResponse = RestClient.post( "https://#{appoptions[:server]}/entityType.removeAttribute",
      {
        :client_id     => appoptions[:clientid],
        :client_secret => appoptions[:clientsecret],
        :type_name     => 'user',
        :attr_name     => 'janRandomGenerated'
      }
    )
    puts apiResponse
  end
end