zhoujiexiong commented on issue #11534:
URL: https://github.com/apache/apisix/issues/11534#issuecomment-2315415407

   @tanujjjj 
   
   You can access consumer info/config in serverless-pre-function@`access 
phase` in ways like `ctx.consumer.labels.version`.
   
   ```
   use t::APISIX 'no_plan';
   
   add_block_preprocessor(sub {
       my ($block) = @_;
   
       if (!$block->request) {
           $block->set_value("request", "GET /t");
       }
   });
   
   run_tests();
   
   __DATA__
   
   === TEST 1: access consumer detail in serverless-pre-function
   --- config
       location = /t {
           content_by_lua_block {
               local t = require("lib.test_admin").test
   
               local code, body = t('/apisix/admin/upstreams/11534',
                   ngx.HTTP_PUT,
                   [[{
                       "nodes": {
                           "127.0.0.1:1980": 1
                       },
                       "type": "roundrobin"
                   }]]
               )
   
               if code >= 300 then
                   ngx.status = code
                   ngx.say(body)
                   return
               end
   
               local code, body = t('/apisix/admin/consumers/tanujjjj',
                   ngx.HTTP_PUT,
                   [[{
                       "username": "tanujjjj",
                       "desc": "consumer_desc",
                       "labels": 
{"version":"v2","build":"16","env":"production"},
                       "plugins": {
                           "key-auth": {
                               "key": "11534"
                           }
                       }
                   }]]
               )
   
               if code >= 300 then
                   ngx.status = code
                   ngx.say(body)
                   return
               end
   
               local code, body = t('/apisix/admin/routes/demo',
                    ngx.HTTP_PUT,
                    [[{
                       "uri": "/demo",
                       "upstream_id": "11534",
                       "plugins": {
                           "key-auth": {},
                           "serverless-pre-function": {
                               "phase": "access",
                               "functions" : ["return function(conf, ctx) 
ngx.log(ngx.WARN, \"consumer.username:\", ctx.consumer.username, \",desc:\", 
ctx.consumer.desc, \"labels.version:\", ctx.consumer.labels.version); 
ngx.exit(200); end"]
                           }
                       }
                   }]]
               )
   
               if code >= 300 then
                   ngx.status = code
                   ngx.say(body)
                   return
               end
   
               ngx.sleep(3)
   
               local code, body = t("/demo?apikey=11534", ngx.HTTP_GET)
   
               if code >= 300 then
                   ngx.status = code
                   ngx.say(body)
                   return
               end
   
               ngx.say(body)
           }
       }
   --- response_body
   passed
   --- timeout: 10
   --- grep_error_log eval
   qr/consumer.username:tanujjjj,desc:consumer_desclabels.version:v2/
   --- grep_error_log_out
   consumer.username:tanujjjj,desc:consumer_desclabels.version:v2
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to