Merge branch '16319-set-test-db-collation'
[arvados.git] / services / api / script / get_anonymous_user_token.rb
index 7b146fc8e35f55be8bad6cb012d4fbd93678f10a..4bb91e244635d7c6e10dcdff32b72264141ff9de 100755 (executable)
@@ -7,9 +7,9 @@
 # If get option is used, an existing anonymous user token is returned. If none exist, one is created.
 # If the get option is omitted, a new token is created and returned.
 
-require 'trollop'
+require 'optimist'
 
-opts = Trollop::options do
+opts = Optimist::options do
   banner ''
   banner "Usage: get_anonymous_user_token "
   banner ''
@@ -17,23 +17,40 @@ opts = Trollop::options do
 Get an existing anonymous user token. If no such token exists \
 or if this option is omitted, a new token is created and returned.
   eos
+  opt :token, "token to create (optional)", :type => :string
 end
 
 get_existing = opts[:get]
+supplied_token = opts[:token]
 
 require File.dirname(__FILE__) + '/../config/environment'
 
 include ApplicationHelper
 act_as_system_user
 
-def create_api_client_auth
+def create_api_client_auth(supplied_token=nil)
+
+  # If token is supplied, see if it exists
+  if supplied_token
+    api_client_auth = ApiClientAuthorization.
+      where(api_token: supplied_token).
+      first
+    if !api_client_auth
+      # fall through to create a token
+    else
+      raise "Token exists, aborting!"
+    end
+  end
+
   api_client_auth = ApiClientAuthorization.
     new(user: anonymous_user,
         api_client_id: 0,
         expires_at: Time.now + 100.years,
-        scopes: ['GET /'])
+        scopes: ['GET /'],
+        api_token: supplied_token)
   api_client_auth.save!
   api_client_auth.reload
+  api_client_auth
 end
 
 if get_existing
@@ -46,7 +63,7 @@ end
 
 # either not a get or no api_client_auth was found
 if !api_client_auth
-  api_client_auth = create_api_client_auth
+  api_client_auth = create_api_client_auth(supplied_token)
 end
 
 # print it to the console