private PSIntegrationRuntime CreateSelfHostedIntegrationRuntime(
            IntegrationRuntimeResource integrationRuntime,
            string resourceGroupName,
            string dataFactoryName)
        {
            PSIntegrationRuntime psIntegrationRuntime = null;
            var selfHosted = integrationRuntime.Properties as SelfHostedIntegrationRuntime;

            if (selfHosted != null)
            {
                if (selfHosted.LinkedInfo != null)
                {
                    psIntegrationRuntime = new PSLinkedIntegrationRuntime(integrationRuntime,
                                                                          resourceGroupName,
                                                                          dataFactoryName)
                    {
                        AuthorizationType = selfHosted.LinkedInfo is LinkedIntegrationRuntimeKey
                            ? Constants.LinkedIntegrationRuntimeKeyAuth
                            : Constants.LinkedIntegrationRuntimeRbacAuth
                    };
                }
                else
                {
                    psIntegrationRuntime = new PSSelfHostedIntegrationRuntime(integrationRuntime,
                                                                              resourceGroupName,
                                                                              dataFactoryName);
                }
            }

            return(psIntegrationRuntime);
        }
        internal async Task <bool> CheckIntegrationRuntimeExistsAsync(
            string resourceGroupName,
            string dataFactoryName,
            string integrationRuntimeName)
        {
            try
            {
                PSIntegrationRuntime integrationRuntime = await this.GetIntegrationRuntimeAsync(
                    resourceGroupName,
                    dataFactoryName,
                    integrationRuntimeName);

                return(integrationRuntime != null);
            }
            catch (ErrorResponseException e)
            {
                //Get throws Exception message with NotFound Status
                if (e.Response.StatusCode == HttpStatusCode.NotFound)
                {
                    return(false);
                }

                throw;
            }
        }
        private PSIntegrationRuntime GenerateIntegraionRuntimeObject(
            IntegrationRuntimeResource integrationRuntime,
            IntegrationRuntimeStatusResponse status,
            string resourceGroupName,
            string dataFactoryName)
        {
            var managed = integrationRuntime.Properties as ManagedIntegrationRuntime;

            if (status == null)
            {
                PSIntegrationRuntime ir = (managed != null
                    ? new PSManagedIntegrationRuntime(integrationRuntime, resourceGroupName, dataFactoryName)
                    : CreateSelfHostedIntegrationRuntime(integrationRuntime, resourceGroupName, dataFactoryName))
                                          ?? new PSIntegrationRuntime(integrationRuntime, resourceGroupName, dataFactoryName);

                return(ir);
            }

            if (managed != null)
            {
                return(new PSManagedIntegrationRuntimeStatus(
                           integrationRuntime,
                           (ManagedIntegrationRuntimeStatus)status.Properties,
                           resourceGroupName,
                           dataFactoryName));
            }
            else
            {
                var selfHosted = integrationRuntime.Properties as SelfHostedIntegrationRuntime;
                if (selfHosted != null)
                {
                    if (selfHosted.LinkedInfo != null)
                    {
                        return(new PSLinkedIntegrationRuntimeStatus(
                                   integrationRuntime,
                                   (SelfHostedIntegrationRuntimeStatus)status.Properties,
                                   resourceGroupName,
                                   dataFactoryName,
                                   DataFactoryManagementClient.DeserializationSettings,
                                   selfHosted.LinkedInfo is LinkedIntegrationRuntimeKey
                                ? Constants.LinkedIntegrationRuntimeKeyAuth
                                : Constants.LinkedIntegrationRuntimeRbacAuth,
                                   status.Name,
                                   status.Properties.DataFactoryName));
                    }

                    return(new PSSelfHostedIntegrationRuntimeStatus(
                               integrationRuntime,
                               (SelfHostedIntegrationRuntimeStatus)status.Properties,
                               resourceGroupName,
                               dataFactoryName,
                               DataFactoryManagementClient.DeserializationSettings));
                }
            }

            // Don't support get status for legacy integraiton runtime.
            throw new PSInvalidOperationException("This type of integration runtime is not supported by this version powershell cmdlets.");
        }
        public virtual PSIntegrationRuntime CreateOrUpdateIntegrationRuntime(CreatePSIntegrationRuntimeParameters parameters)
        {
            PSIntegrationRuntime psIntegrationRuntime = null;

            Action createOrUpdateIntegrationRuntime = () =>
            {
                var integrationRuntime = this.CreateOrUpdateIntegrationRuntimeAsync(
                    parameters.ResourceGroupName,
                    parameters.DataFactoryName,
                    parameters.Name,
                    parameters.IntegrationRuntimeResource).ConfigureAwait(true).GetAwaiter().GetResult();

                var managed = integrationRuntime.Properties as ManagedIntegrationRuntime;
                if (managed != null)
                {
                    psIntegrationRuntime = new PSManagedIntegrationRuntime(integrationRuntime,
                                                                           parameters.ResourceGroupName,
                                                                           parameters.DataFactoryName);
                }
                else
                {
                    var selfHosted = integrationRuntime.Properties as SelfHostedIntegrationRuntime;
                    if (selfHosted != null)
                    {
                        psIntegrationRuntime = new PSSelfHostedIntegrationRuntime(integrationRuntime,
                                                                                  parameters.ResourceGroupName,
                                                                                  parameters.DataFactoryName);
                    }
                }
            };

            parameters.ConfirmAction(
                parameters.Force,      // prompt only if the integration runtime exists
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.IntegrationRuntimeExists,
                    parameters.Name,
                    parameters.DataFactoryName),
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.IntegrationRuntimeUpdating,
                    parameters.Name,
                    parameters.DataFactoryName),
                parameters.Name,
                createOrUpdateIntegrationRuntime,
                () => parameters.IsUpdate);

            return(psIntegrationRuntime);
        }