def Run()

in bulkprovision/bulkmonitor/__init__.py [0:0]


    def Run(self):
        '''
        Main monitoring process.  This will update status flags based on response from Service Catalog
        '''
        # get the provisioning products and poll them for updates
        prov_items = self.dynamo_query("status",self._status)
        logger.info("Found {} items to Update with status {}".format(len(prov_items), self._status))
        count = 0
        errors = 0
        for drow in prov_items:
            
            ppid= drow["scproductdetails"]["ProvisionedProductId"]   
            dbstatus = drow["status"]
            param_dict = drow["launchparams"]
            guidkey = drow["guidkey"]            
            ppdetails = {
                'ProvisionedProductId':ppid,
                'RecordId':drow["scproductdetails"]['RecordId'],
                'CreatedTime':str(drow["scproductdetails"]['CreatedTime']),
                'Status':drow["scproductdetails"]['Status']
            }            
            failed = False
            errordetails = None
            # keep the error message if we have one
            if "errordetails" in drow:
                errordetails = drow["errordetails"]            
              
            try:
                # update the product and record the important parts of the response
                resp = self.sc_describe_prov_product(ppid)                              
                prod_status = resp['ProvisionedProductDetail']['Status']
                ppdetails['RecordId']=resp['ProvisionedProductDetail']['LastRecordId']
                ppdetails['Status'] = prod_status
                
                # handle the status messages 
                if prod_status == 'AVAILABLE':
                    dbstatus = "AVAILABLE"
                    errordetails = None # if it worked, lets clear out the error message to avoid confusion
                elif prod_status in ['TAINTED','ERROR']:
                    if dbstatus in ["PROVISIONING"]:
                        # if it failed to provision, terminate it in Service Catalog
                        errordetails = resp['ProvisionedProductDetail']['StatusMessage']
                        resp = self.sc_terminate_product(ppid)                        
                        dbstatus = "TERMINATING-FAILURE"
                    else:
                        # failed at some other point - failed termination?? or something else
                        dbstatus = "PRODUCT-ERROR"                    
                elif prod_status in ['UNDER_CHANGE','PLAN_IN_PROGRESS']:
                    # is it terminating?
                    if dbstatus != "TERMINATING":
                        dbstatus = "PROVISIONING"                        
                
            except ClientError as ce:
                msg = ce.response['Error']['Message']
                # tried to find the product details, but it is gone....                
                if msg.startswith("Provisioned product not found: "):
                    #This was a good status termination, lets mark it and we're done with it now.
                    dbstatus = "TERMINATED"
                    ppdetails["Status"] = "TERMINATED"
                    count += 1
                else:
                    # something wrong from the API call. this is where you will see the CFn Errors
                    logger.error("ClientError: {}".format(msg))    
                    errordetails = ce.response['Error']
                    failed = True
            except Exception as e:
                # Something else wrong?
                logger.error(e)                    
                failed = True
            else:
                count += 1
                                    
            if failed:
                errors += 1
                dbstatus = "STATUS-ERROR"
                
            #update the dynamo table
            self.updateItem(guidkey,drow["status"],dbstatus, param_dict, ppdetails, errordetails)
                    
        self.RemoveEntries(["DUPLICATE"])
        if len(prov_items) > 0:
            logger.info("Updated {} of {} products with {} errors using status:{}".format(count, len(prov_items), errors, self._status))        
        return(self.generate_return(count))