Create()

in lambda/cfn/lib/lex.js [243:351]


    Create(params,reply){
        console.log('Create Lex. Params: ' + JSON.stringify(params,null,2))
        console.log('Type: ' + this.type)
        var self=this
        params.name=this.name(params)
        console.log('Create params.name: ' + params.name)
        delete params.prefix
        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]
        }
        var start=Promise.resolve();
        if (this.type==='BotAlias') {
            params.botVersion = '1'; // default version. Should be replaced by call to latestBotVersion.
            this.latestBotVersion(params.botName).then(version => {
                params.botVersion = version
                console.log("BotAlias parameters for Create are: " + JSON.stringify(params,null,2));
                start.then(()=>run(self.create_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);})
        }
        else if(this.type==='Intent') {
            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 create are: " + JSON.stringify(params, null, 2));
                    start.then(()=>run(self.create_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);
                });
            } else {
                start.then(()=>run(self.create_method, params))
                    .then(msg => reply(null, msg.name, null))
                    .catch(error => {
                        console.log('caught', error);
                        reply(error);
                    })
                    .error(reply).catch(reply)
            }
        }
        else if (this.type==='Bot') {
            params.processBehavior = "BUILD";
            start = iam.createServiceLinkedRole({
                AWSServiceName: 'lex.amazonaws.com',
                Description: 'Service linked role for lex'
            }).promise()
                .tap(console.log)
                .catch(console.log)
            if (params.intents) {
                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 create method: ' + JSON.stringify(params,null,2));
                    start.then(() => run(self.create_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);
                    });
            } else {
                start.then(() => run(self.create_method, params)
                    .then(msg => reply(null, msg.name, null))
                    .catch(error => {
                        console.log('caught', error);
                        reply(error);
                    })
                    .error(reply).catch(reply)
                );
            }
        }
        else {
            console.log("Generic create called for: " + JSON.stringify(params,null,2));
            start.then(() => run(self.create_method, params)
                .then(msg => reply(null, msg.name, null))
                .catch(error => {
                    console.log('caught', error);
                    reply(error);
                })
                .error(reply).catch(reply)
            );
        }
    }