def process_request(self, request): # custom logic if request.type == 'purchase': for item in request.asset.items: if item.quantity > 100000: raise FulfillmentFail( message='Is Not possible to purchase product') for param in request.asset.params: if param.name == 'email' and not param.value: param.value_error = 'Email address has not been provided, please provide one' raise FulfillmentInquire(params=[param]) # approve by ActivationTile return ActivationTileResponse( tile='\n # Welcome to Fallball!\n\nYes, ' 'you decided to have an account in our amazing service!') # or # return TemplateResource().render(pk='TEMPLATE_ID', request_id=request.id) # aprrove by Template return ActivationTemplateResponse(template_id="TL-497-535-242") # or # return TemplateResource().get(pk='TEMPLATE_ID') elif request.type == 'change': # fail raise FulfillmentFail() else: # skip request raise Skip()
def resume(self, req, automation: FulfillmentAutomation): # Check if the subscription status is Suspended resumed_subscription = self._resume_subscription(automation, req) # Check if the API call was successful and save the response request in Connect # Update fulfilment parameters in Fulfilment request with the request in the response from the Vendor API call. Similar to _check_update_response try: return ActivationTemplateResponse(Globals.ACTIVATION_TEMPLATE) # Returning the Activation Template will update the status of Fulfilment Request object to Approved and Subscription object status to Active. # The statuses will not get updated as Approved/Active if any of the mandatory/required fulfilment parameter in Fulfilment Request remain empty. except SkipRequest as skip: raise skip except FailRequest as err: # The Fulfilment Request was provisioned successfully, but Activation template could not be returned successfully. # Therefore, we log the error message and do not fail the request raise SkipRequest( Message.Shared.ACTIVATING_TEMPLATE_ERROR.format( str(err.message))) except Exception as ex: raise SkipRequest( Message.Shared.ACTIVATING_TEMPLATE_ERROR.format(str(ex)))
def change(self, req, automation: FulfillmentAutomation): # If the business does not support downsize, check if any item quantity is reduced. # If yes, fail the request with proper message. self.check_if_downsize(req) # Process the request to change the subscription self._change_subscription(automation, req) # If none of the SkipRequest, InquireRequst or FailRequest was raised, means that the provisioning was successful. # Approve the Fulfilment Request by sending back the Activation template # Approve is the final status of the Fulfilment Request try: # Returning the Activation Template will update the status of Fulfilment Request object to Approved and Subscription object status remains Active. # The statuses will not get updated as Approved/Active if any of the mandatory/required fulfilment parameter in Fulfilment Request remain empty. return ActivationTemplateResponse(Globals.ACTIVATION_TEMPLATE) # If required, another template can be created and in Vendor Portal. Pass the Template Id to return the template. except SkipRequest as skip: raise skip except FailRequest as err: # The Fulfilment Request was provisioned successfully, but Activation template could not be returned successfully. # Therefore, we log the error message and do not fail the request raise SkipRequest( Message.Shared.ACTIVATING_TEMPLATE_ERROR.format( str(err.message))) except Exception as ex: raise SkipRequest( Message.Shared.ACTIVATING_TEMPLATE_ERROR.format(str(ex)))
def get(self, pk): """ Get an activation template. :param str pk: Primary key of the template to obtain. :return: ActivationTemplateResponse object with template contents. :rtype: ActivationTemplateResponse """ return ActivationTemplateResponse(template_id=pk)
def process_request(self, request): # type: (Fulfillment) -> Union[ActivationTemplateResponse, ActivationTileResponse] if request.needs_migration(): # Skip request if it needs migration (migration is performed by an external service) logger.info( 'Skipping request {} because it needs migration.'.format( request.id)) raise SkipRequest() else: logger.info( 'Processing request {} for contract {}, product {}, marketplace {}' .format(request.id, request.contract.id, request.asset.product.name, request.marketplace.name)) # Custom logic if request.type == 'purchase': for item in request.asset.items: if item.quantity > 100000: raise FailRequest( 'Is not possible to purchase product in such quantities' ) for param in request.asset.params: if param.name == 'email' and not param.value: param.value_error = 'Email address has not been provided, ' \ 'please provide one' raise InquireRequest(params=[param]) # Find a param by its id param = request.asset.get_param_by_id('purchase_id') if param: param.value = '...' # We can assign the id given by the external service here self.update_parameters( request.id, [param]) # Update param on the platform else: raise FailRequest( 'The asset is expected to have a "purchase_id" param.') # Approve by Template return ActivationTemplateResponse('TL-497-535-242') # Or # return TemplateResource().get(pk='TEMPLATE_ID') # Approve by ActivationTile # return ActivationTileResponse('\n # Welcome to Fallball!\n\nYes, you decided ' # 'to have an account in our amazing service!') # Or # return TemplateResource().render(pk='TEMPLATE_ID', request_id=request.id) elif request.type == 'change': # Fail raise FailRequest() else: # Skip request raise SkipRequest()
def get_answer(self, product, answer): """Get template object specified in the Config""" c = Config.get_instance() line = c.templates.get(product, {}).get(answer) if line is None: return line if line.startswith('TL'): return ActivationTemplateResponse(line) return ActivationTileResponse(line)
def process_request(self, req: Fulfillment) -> object: try: logger.info("Processing request " + req.id) partner_config = Utils.get_partner_data(req) setattr(self, 'service', Service(partner_config, req.asset.tiers.customer)) switcher = { "purchase": lambda: __class__.__purchase(self, req), "cancel": lambda: __class__.__cancel(self, req), "change": lambda: __class__.__change(self, req), "suspend": lambda: __class__.__toggle(self, False, req), "resume": lambda: __class__.__toggle(self, True, req) } switcher.get(req.type, "ERROR: Action type is no valid")() if req.type in ['purchase', 'change', 'resume']: result = ActivationTemplateResponse( partner_config['templates']['activation_template']) else: result = ActivationTileResponse('Operation ' + req.type + ' done successfully') logger.info("Finishing request " + req.id) return result except SkipRequest as err: logger.info(err.message) raise SkipRequest(str(err)) except FailRequest as err: logger.error( "Issue while processing Purchase request. Print Error: %s" % str(err)) raise err except InquireRequest as err: logger.error( "Issue while processing Purchase request. Print Error: %s" % str(err)) raise err except Exception as err: logger.error( "Issue while processing Purchase request. Print Error: %s" % str(err)) raise err
def purchase(self, req, automation: FulfillmentAutomation): # Validate Ordering parameters. # Ordering parameters could be used in API payload to create subscription in Vendor system. # self.check_order_parameters(req) # If validation fails, this method can raise Inquire request with appropriate message to get proper information. # If validation is successful then proceed # If the customer creation action or API is separate from subscription creation, then introduce a method for customer creation. # self.create_customer(req) # Create subscription subscription_info = self._create_subscription(automation, req) # If none of the SkipRequest, InquireRequst or FailRequest was raised, means that the provisioning was successful. # Approve the Fulfilment request by returning the Activation template # Approved is the final status of the Fulfilment Request of Subscription in Connect try: return ActivationTemplateResponse(Globals.ACTIVATION_TEMPLATE) # Returning the Activation Template will update the status of Fulfilment Request to Approved and Subscription status to Active. # The statuses will not get updated as Approved/Active if any of the mandatory/required fulfilment parameter in Fulfilment Request remain empty. except SkipRequest as skip: raise skip except FailRequest as err: # The Fulfillment Request was provisioned successfully, but Activation template could not be returned successfully. # Therefore, we log the error message and do not fail the request raise SkipRequest( Message.Shared.ACTIVATING_TEMPLATE_ERROR.format( str(err.message))) except Exception as ex: raise SkipRequest( Message.Shared.ACTIVATING_TEMPLATE_ERROR.format(str(ex)))
def test_process_with_activation_template(): automation = TierConfigAutomationHelper( ActivationTemplateResponse('TL-000-000-000')) automation.process()
def get(self, pk): return ActivationTemplateResponse(template_id=pk)
def process_request(self, req): try: setattr(self, 'tenant', None) if req.type == 'update': raise FailRequest("Update operations are not allowed") logger.info("Begin tier config process:" + req.configuration.id) logger.debug(req) partner_config = Utils.get_partner_data( {'connection': req.configuration.connection.id, 'tier_1': req.configuration.account.external_id, 'tier_2': 'default', 'req_id': req.id}) logger.info( "Validate if req requires Skip according accountExternalIdsConfigRequestSkipPrefix config parameter") if req.configuration.account.external_id.startswith( tuple(partner_config['accountExternalIdsConfigRequestSkipPrefix'])): raise SkipRequest('Skipped by processor configuration') setattr(self, 'service', Service(partner_config, req.configuration.account)) # Check if current logged user exists self.service.check_login(req.get_param_by_id('admin_login_name').value) subs_start = str(time.time()) internal_tag = 'aps-abc-start-prod-date=%s;aps-abc-tier=%s;aps-abc-billing-model=%s' % ( subs_start, '-1', 'per_gb') # Create tenant subscription logger.info("Creating tenant...") tenant_created = self.service.create_tenant(req.id, internal_tag) logger.debug(tenant_created) setattr(self, 'tenant', tenant_created['id']) # Create admin for subscription logger.info("Creating admin...") admin_created = self.service.create_admin(tenant_created, req.get_param_by_id('admin_login_name').value, True) logger.debug(admin_created) # Add access policies for the user logger.info("Adding access policies...") self.service.access_policies(admin_created, tenant_created, True) # Set offerings available for reseller customers logger.info("Set offerings...") self.service.set_offerings(tenant_created) # Send activation email logger.info("Sending email...") self.service.send_email(req.get_param_by_id('admin_login_name').value, admin_created) params = [ Param(id='tenant_id', value=str(tenant_created['id'])), Param(id='admin_id', value=str(admin_created['id'])), Param(id='reseller_customer_id', value=str(internal_tag)), Param(id='billing_date', value=str(subs_start)) ] logger.info("End tier configuration for " + req.configuration.id) self.update_parameters(req.id, params) return ActivationTemplateResponse(partner_config['templates']['t1_template']) except FailRequest as err: self.__rollback() logger.error("Issue while processing Purchase request. Print Error: %s" % err) raise err except SkipRequest as err: logger.info(err) raise err except InquireRequest as err: self.__rollback() if self.tenant: self.service.remove(self.tenant) logger.error("Issue while processing Purchase request. Print Error: %s" % err) raise err except Exception as err: self.__rollback() if self.tenant: self.service.remove(self.tenant) logger.error("Issue while processing Purchase request. Print Error: %s" % err) raise err