Question:

I want to run some code when limits for my application's resources are changed in a subscription. For instance, as soon as the customer purchases additional amount of a certain resource, I want to send a notification about it to some external system. How can I do this?

Answer:

In most cases, if your application needs to keep track of some limits in external system (disk space, traffic, etc.) you should implement counters in customer's resource. Then, if limits for counter change, your resource gets a PUT request with new limits (or, if you use PHP runtime, configure function is executed, more about this in documentation, you may also want to check out the resource counters sample package).

Starting with POA 5.5.7 it is possible to subscribe to a custom event that is triggered when resources limits is changed in POA subscription.

More information about this event is available in documentation.

If you use the PHP runtime library, you can subscribe to these events like this:

  • You need to declare a relation with POA subscription in customer's resource (typically the one that is automatically provided automatically):

    /**
     * @link("http://aps-standard.org/types/core/subscription/1.0")
     * @required
     */
    public $subscription;
    
  • Then, from any function in your code, e.g. from provision(), you can subscribe to limits change event for this subscription:

    $sub = new \APS\EventSubscription("http://parallels.com/aps/events/pa/subscription/limits/changed", "onLimitChange");
    $sub->source->id=$this->subscription->aps->id;
    $apsc = \APS\Request::getController();
    $apsc->subscribe($this, $sub);
    
  • Finally, declare the actual event handler with the logic you need executed when limits change. This example will add a log entry:

    /**
     * @verb(POST)
     * @path("/onLimitChange")
     * @param("http://aps-standard.org/types/core/resource/1.0#Notification",body)
     */
    public function onLimitChange($event) {
        APS\Logger::get()->debug("Limits changed, and we got a notification about it!");
    }
    

After subscription limits get changed, onLimitChange function for your resource will be called by APS controller. Note that you will not get limits with the event object, you can fetch the limits using the SubscriptionResource structure in the event handler.

Internal content