Ejemplo n.º 1
0
        public static WCDeveloperOptions Parse(IEnumerable <AddonParameter> _developerParameters, AddonManifest manifest)
        {
            var options = new WCDeveloperOptions();

            foreach (var parameter in manifest.Properties)
            {
                MapToOption(options, parameter.Key.ToLowerInvariant(), parameter.Value);
            }
            foreach (var parameter in _developerParameters)
            {
                MapToOption(options, parameter.Key.ToLowerInvariant(), parameter.Value);
            }
            return(options);
        }
Ejemplo n.º 2
0
        private static void MapToOption(WCDeveloperOptions _options, string _key, string _value)
        {
            var _developerOptions = _options.GetType().GetProperties();

            foreach (var prop in _developerOptions)
            {
                if (prop.Name.Equals(_key))
                {
                    prop.SetValue(_options, _value);
                    return;
                }
            }

            log.Error(string.Format("The developer option '{0}' was not expected.", _key));
            return;
        }
        public override ProvisionAddOnResult Provision(AddonProvisionRequest _request)
        {
            /*The Provision method provisions the instance for the service you are setting up. Within this method, you can access the information
             * requested from the developer (if any) by iterating through the request.DeveloperParameters object.
             * At the end of the provisioning process, simply return the connection information for the service that was provisioned.*/
            //Retrieving developer parameters
            //var parameter = request.DeveloperParameters.First(param => param.Key == "RequiredParameter");

            var provisionResult = new ProvisionAddOnResult(string.Empty)
            {
                IsSuccess = false
            };

            var manifest      = _request.Manifest;
            var devParameters = _request.DeveloperParameters;
            var devOptions    = WCDeveloperOptions.Parse(devParameters, manifest);
            var appURL        = "";

            cloud_url = devOptions.cloudurl;
            appname   = devOptions.name;
            appalias  = devOptions.alias;

            JObject instanceDetails = new JObject();

            try
            {
                var token = authenticate(devOptions.apprendausername, devOptions.apprendapassword, devOptions.apprendatenant);
                createApp(token, appname, appalias, "NodeJS API Server for Watson Conversation");
                var archivePath = createArchive(devOptions.workspace, devOptions.conversationusername, devOptions.conversationpassword);
                setArchive(token, appalias, valias, archivePath);
                promoteApp(token, appalias, valias);
                appURL = getAppURL(token, appalias, valias);
                log.Info("WatsonConversationAddon Provisioned Successfully");
            }
            catch (Exception ex) {
                var errorMessage = string.Format("WatsonConversationAddon Provisioning Error: {0}\n{1}", ex, ex.StackTrace);
                log.Error(errorMessage);
                provisionResult.EndUserMessage = errorMessage;
                return(provisionResult);
            }

            provisionResult.IsSuccess      = true;
            provisionResult.ConnectionData = appURL;
            return(provisionResult);
        }
        public override OperationResult Deprovision(AddonDeprovisionRequest _request)
        {
            /*The Deprovision method allows you to specify the steps taken when a developer deprovisions his/her Add-On.
             * You should use this step to clean up any provisioned artifacts. The connectiondata object inside the request should
             * have all the information needed to clean up the provisioned resource.
             * At the end, you can return whether the operation was successful or not as shown in the sample below*/

            var deprovisionResult = new OperationResult {
                EndUserMessage = string.Empty, IsSuccess = false
            };

            var manifest      = _request.Manifest;
            var devParameters = _request.DeveloperParameters;
            var devOptions    = WCDeveloperOptions.Parse(devParameters, manifest);

            cloud_url = devOptions.cloudurl;

            try
            {
                var token  = authenticate(devOptions.apprendausername, devOptions.apprendapassword, devOptions.apprendatenant);
                int status = deleteApp(token, devOptions.alias);
                if (status == 204)
                {
                    log.Info("WatsonConversationAddon Deprovisioned Successfully");
                }
                else
                {
                    throw new Exception("Failed to deprovision");
                }
            }
            catch (Exception ex)
            {
                var errorMessage = string.Format("WatsonConversationAddon Deprovision Error: {0}\n{1}", ex, ex.StackTrace);
                log.Error(errorMessage);
                deprovisionResult.EndUserMessage = errorMessage;
                return(deprovisionResult);
            }

            deprovisionResult.IsSuccess = true;
            return(deprovisionResult);
        }
        public override OperationResult Test(AddonTestRequest _request)
        {
            /*The test method allows you to test whether the Add-On was developed and configured properly and that any dependent systems are
             * operating normally. During this method, you will want to go through a similar workflow to Provision to ensure proper functioning
             * of the Add-On.*/
            var manifest      = _request.Manifest;
            var devParameters = _request.DeveloperParameters;
            var devOptions    = WCDeveloperOptions.Parse(devParameters, manifest);
            var testResult    = new OperationResult {
                EndUserMessage = string.Empty, IsSuccess = false
            };

            cloud_url = devOptions.cloudurl;
            appalias  = devOptions.alias;

            try
            {
                var token      = authenticate(devOptions.apprendausername, devOptions.apprendapassword, devOptions.apprendatenant);
                int statusCode = getApp(token, appalias);
                if (statusCode != 200)
                {
                    throw new Exception("Watson Conversation API has not successfully been deployed in Apprenda");
                }
            }
            catch (Exception ex)
            {
                var errorMessage = string.Format("WatsonConversationAddon Testing Error: {0}\n{1}", ex, ex.StackTrace);
                log.Error(errorMessage);
                testResult.EndUserMessage = errorMessage;
                return(testResult);
            }

            testResult.EndUserMessage = "WatsonConversationAddon Add-On was tested successfully";
            testResult.IsSuccess      = true;
            return(testResult);
        }