Update()

in lambda/cfn/lib/lex.js [367:516]


    Update(ID,params,oldparams,reply){
        console.log('Update Lex. ID: ' + ID)
        console.log('Params: ' + JSON.stringify(params,null,2))
        console.log('OldParams: ' + JSON.stringify(oldparams,null,2))
        console.log('Type: ' + this.type)
        delete params.prefix
        var self=this
        if(this.type!=='Alias'){ // The type of Alias should not be updated.
            if(params.childDirected){
                params.childDirected={"false":false,"true":true}[params.childDirected]
            }
            if(params.detectSentiment){
                params.detectSentiment={"false":false,"true":true}[params.detectSentiment]
            }
            if(params.createVersion){
                params.createVersion={"false":false,"true":true}[params.createVersion]
            }
            if (this.type==='Bot') {
                params.name = ID;
                try {
                    /**
                     * Updates are always made against the $LATEST version so find the checksum of this version.
                     */
                    this.checksum(ID,'$LATEST').then( cksum => {
                        params.checksum = cksum;
                        this.mapForIntentVersions(params.intents).then(map => {
                            params.intents.forEach(element=>{
                               element.intentVersion = map.get(element.intentName);
                            });
                            params.processBehavior = "BUILD";
                            console.log('Final params before call to update method: ' + JSON.stringify(params,null,2));
                            run(self.update_method, params)
                                .then(msg => reply(null, msg.name, null))
                                .catch(error => {
                                    console.log('caught', error);
                                    reply(error);
                                })
                                .error(reply).catch(reply)
                        }).catch(error => {
                            console.log('caught', error);
                            reply(error);
                        });
                    }).catch(error => { console.log('caught', error); reply(error); })
                } catch (err) {
                    console.log("Exception detected: " + err);
                    reply(null, ID);
                }
            } else if (this.type==='Intent') {
                /**
                 * Update an Intent
                 */
                params.name = ID;
                try {
                    // find the checksum for the $LATEST version to use for update
                    this.checksumIntentOrSlotType(ID,'$LATEST').then(cksum => {
                        params.checksum = cksum;
                        if (params.slots && params.slots.length > 0) {
                            this.mapForSlotTypeVersions(params.slots).then( slotTypeMap => {
                                params.slots.forEach(element => {
                                    if (slotTypeMap.get(element.slotType)) {
                                        element.slotTypeVersion = slotTypeMap.get(element.slotType);
                                    }
                                });
                                console.log("Intent parameters for update are: " + JSON.stringify(params, null, 2));
                                run(self.update_method, params)
                                    .then(msg => reply(null, msg.name, {}))
                                    .catch(error => {
                                        console.log('caught', error);
                                        reply(error);
                                    })
                                    .error(reply).catch(reply)
                            }).catch(error => {
                                console.log('caught', error);
                                reply(error);
                            });
                        } else {
                            console.log("Intent parameters for update are: " + JSON.stringify(params, null, 2));
                            run(self.update_method, params)
                                .then(msg => reply(null, msg.name, {}))
                                .catch(error => {
                                    console.log('caught', error);
                                    reply(error);
                                })
                                .error(reply).catch(reply)
                        }
                    }).catch(error => { console.log('caught', error); reply(error);})
                } catch (err) {
                    console.log("Exception detected: " + err);
                    reply(null, ID);
                }
            } else if (this.type==='SlotType') {
                /**
                 * Update SlotType. This requires finding the checksum of the more recent version
                 * of the SlotType.
                 */
                params.name = ID;
                try {
                    this.slotTypeVersions(ID).then(versions => {
                        this.checksumIntentOrSlotType(ID,'$LATEST').then(cksum => {
                            params.checksum = cksum;
                            console.log("Slot parameters for update are: " + JSON.stringify(params,null,2));
                            run(self.update_method, params)
                                .then(msg => reply(null, msg.name, null))
                                .catch(error => { console.log('caught', error); reply(error);})
                                .error(reply).catch(reply)
                        }).catch(error => { console.log('caught', error); reply(error);})
                    })
                } catch (err) {
                    console.log("Exception detected: " + err);
                    reply(null, ID);
                }
            } else if (this.type==='BotAlias') {
                /**
                 * Update a BotAlias. This requires obtaining:
                 * - the checksum of the BotAlias
                 * - the latest version of the Bot now existing on the system
                 * With these two pieces of information an update can occur using the "run" method.
                 */
                try {
                    this.checksumBotAlias(params.botName, ID).then(cksum => {
                        params.checksum = cksum;
                        this.latestBotVersion(params.botName).then(version => {
                            params.botVersion = version;
                            console.log("BotAlias parameters for update are: " + JSON.stringify(params,null,2));
                            run(self.update_method, params)
                                .then(msg => reply(null, msg.name, null))
                                .catch(error => { console.log('caught', error); reply(error);})
                                .error(reply).catch(reply)
                        }).catch(error => { console.log('caught', error); reply(error);})
                    }).catch(error => { console.log('caught', error); reply(error);})
                } catch (err) {
                    console.log("Exception detected: " + err);
                    reply(null, ID);
                }
            } else {
                console.log("Parameters for update: " + JSON.stringify(params,null,2));
                try {
                    run(self.update_method, params)
                        .then(msg => reply(null, msg.name, null))
                        .catch(error => { console.log('caught', error); reply(error);})
                        .error(reply).catch(reply)
                } catch (err) {
                    console.log("Exception detected: " + err);
                    reply(null, ID);
                }
            }
        }else{
            reply(null,ID)
        }
    }