public void CanNewGateway()
        {
            var expectedOutput = new PSDataFactoryGateway
            {
                Name = GatewayName,
                Status = GatewayStatus.Online,
                Description = "New gateway description for test",
            };

            var fakeRequest = new HttpRequestMessage(HttpMethod.Get, "https://www.microsoft.com");
            var fakeResponse = new HttpResponseMessage(HttpStatusCode.NotFound);

            dataFactoriesClientMock.Setup(f => f.GetGateway(ResourceGroupName, DataFactoryName, GatewayName))
                                   .Returns(() =>
                                   {
                                       throw CloudException.Create(fakeRequest, String.Empty, fakeResponse, String.Empty);
                                   })
                                   .Verifiable();

            dataFactoriesClientMock.Setup(
                f => f.CreateOrUpdateGateway(ResourceGroupName, DataFactoryName, It.IsAny<PSDataFactoryGateway>()))
                                   .Returns(expectedOutput)
                                   .Verifiable();

            _cmdlet.Name = GatewayName;
            _cmdlet.DataFactoryName = DataFactoryName;

            _cmdlet.ExecuteCmdlet();

            dataFactoriesClientMock.VerifyAll();
            commandRuntimeMock.Verify(f => f.WriteObject(expectedOutput), Times.Once());
        }
        public void CanSetGateway()
        {
            const string description = "New gateway description for test";

            var expectedOutput = new PSDataFactoryGateway
            {
                Name = GatewayName,
                Status = GatewayStatus.NeedRegistration,
                Description = description
            };

            dataFactoriesClientMock.Setup(
                f => f.PatchGateway(ResourceGroupName, DataFactoryName,
                    It.Is<PSDataFactoryGateway>
                    (parameters => parameters.Name == GatewayName && parameters.Description == description)))
                    .Returns(expectedOutput).Verifiable();

            _cmdlet.Name = GatewayName;
            _cmdlet.DataFactoryName = DataFactoryName;
            _cmdlet.Description = expectedOutput.Description;

            _cmdlet.ExecuteCmdlet();

            dataFactoriesClientMock.VerifyAll();
            commandRuntimeMock.Verify(f => f.WriteObject(expectedOutput), Times.Once());
        }
        public void CanNewGatewayKey()
        {
            var gatewaykey = new PSDataFactoryGatewayKey("FakedKey");

            var expectedOutput = new PSDataFactoryGateway
            {
                Name = GatewayName,
                Status = GatewayStatus.Online,
                Description = "New gateway description for test"
            };

            var fakeRequest = new HttpRequestMessage(HttpMethod.Get, "https://www.microsoft.com");

            dataFactoriesClientMock.Setup(
                f => f.RegenerateGatewayKey(ResourceGroupName, DataFactoryName, GatewayName))
                                   .Returns(gatewaykey)
                                   .Verifiable();

            _cmdlet.GatewayName = GatewayName;
            _cmdlet.DataFactoryName = DataFactoryName;

            _cmdlet.ExecuteCmdlet();

            dataFactoriesClientMock.VerifyAll();
            commandRuntimeMock.Verify(f => f.WriteObject(gatewaykey), Times.Once());
        }
        public virtual PSDataFactoryGateway PatchGateway(string resourceGroupName, string dataFactoryName, PSDataFactoryGateway gateway)
        {
            if (gateway == null)
            {
                throw new ArgumentNullException("gateway");
            }

            var response = DataPipelineManagementClient.Gateways.Update(
                resourceGroupName, dataFactoryName, new GatewayCreateOrUpdateParameters { Gateway = gateway.ToGatewayDefinition() });

            return new PSDataFactoryGateway(response.Gateway);
        }
        public virtual PSDataFactoryGateway CreateOrUpdateGateway(string resourceGroupName, string dataFactoryName, PSDataFactoryGateway gateway)
        {
            if (gateway == null)
            {
                throw new ArgumentNullException("gateway");
            }

            var response = DataPipelineManagementClient.Gateways.CreateOrUpdate(
                resourceGroupName, dataFactoryName, new GatewayCreateOrUpdateParameters { Gateway = gateway.ToGatewayDefinition() });

            Gateway createdGateway = response.Gateway;
            if (createdGateway.Properties != null &&
                !DataFactoryCommonUtilities.IsSucceededProvisioningState(createdGateway.Properties.ProvisioningState))
            {
                // ToDo: service side should set the error message for provisioning failures.
                throw new ProvisioningFailedException(Resources.GatewayProvisioningFailed);
            }

            return new PSDataFactoryGateway(createdGateway);
        }
        public void CanGetGateway()
        {
            var expectedOutput = new PSDataFactoryGateway
            {
                Name = GatewayName,
                Status = GatewayStatus.NeedRegistration
            };

            dataFactoriesClientMock.Setup(f => f.GetGateway(ResourceGroupName, DataFactoryName, GatewayName))
                                   .Returns(expectedOutput).Verifiable();

            _cmdlet.Name = GatewayName;
            _cmdlet.DataFactoryName = DataFactoryName;

            _cmdlet.ExecuteCmdlet();

            // Assert
            dataFactoriesClientMock.VerifyAll();

            commandRuntimeMock.Verify(f => f.WriteObject(expectedOutput), Times.Once());
        }
        protected override void ProcessRecord()
        {
            if (ParameterSetName == ByFactoryObject)
            {
                if (DataFactory == null)
                {
                    throw new PSArgumentNullException(string.Format(CultureInfo.InvariantCulture, Resources.DataFactoryArgumentInvalid));
                }

                DataFactoryName = DataFactory.DataFactoryName;
                ResourceGroupName = DataFactory.ResourceGroupName;
            }

            PSDataFactoryGateway gateway = new PSDataFactoryGateway
            {
                Description = Description,
                Name = Name
            };

            PSDataFactoryGateway response = DataFactoryClient.PatchGateway(ResourceGroupName, DataFactoryName, gateway);
            WriteObject(response);
        }
        public override void ExecuteCmdlet()
        {
            if (ParameterSetName == ByFactoryObject)
            {
                if (DataFactory == null)
                {
                    throw new PSArgumentNullException(string.Format(CultureInfo.InvariantCulture, Resources.DataFactoryArgumentInvalid));
                }

                DataFactoryName = DataFactory.DataFactoryName;
                ResourceGroupName = DataFactory.ResourceGroupName;
            }

            PSDataFactoryGateway gateway = null;
            try
            {
                gateway = DataFactoryClient.GetGateway(ResourceGroupName, DataFactoryName, Name);
            }
            catch (CloudException ex)
            {
                if (ex.Response.StatusCode != HttpStatusCode.NotFound) throw;
            }

            if (gateway != null)
            {
                throw new PSInvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.DataFactoryGatewayExists, Name, DataFactoryName));
            }

            var request = new PSDataFactoryGateway
            {
                Name = Name,
                Location = NormalizeLocation(Location),
                Description = Description
            };

            PSDataFactoryGateway response = DataFactoryClient.CreateOrUpdateGateway(ResourceGroupName, DataFactoryName, request);
            WriteObject(response);
        }
        public void CanThrowWhenCreateExistingGateway()
        {
            var expectedOutput = new PSDataFactoryGateway
            {
                Name = GatewayName,
                Status = GatewayStatus.Online,
                Description = "New gateway description for test"
            };

            dataFactoriesClientMock.Setup(f => f.GetGateway(ResourceGroupName, DataFactoryName, GatewayName))
                                   .Returns(expectedOutput)
                                   .Verifiable();

            _cmdlet.Name = GatewayName;
            _cmdlet.DataFactoryName = DataFactoryName;

            Assert.Throws<PSInvalidOperationException>(() => _cmdlet.ExecuteCmdlet());

            dataFactoriesClientMock.Verify(f => f.CreateOrUpdateGateway(ResourceGroupName, DataFactoryName, It.IsAny<PSDataFactoryGateway>()),
                                           Times.Never());
        }