private static IEnumerable<HostnameType> GetHostnamesToDelete(
            PsApiManagementHostnameConfiguration portalHostnameConfiguration,
            PsApiManagementHostnameConfiguration proxyHostnameConfiguration,
            PsApiManagement currentState)
        {
            if (portalHostnameConfiguration == null && currentState.PortalHostnameConfiguration != null)
            {
                yield return HostnameType.Portal;
            }

            if (proxyHostnameConfiguration == null && currentState.ProxyHostnameConfiguration != null)
            {
                yield return HostnameType.Proxy;
            }
        }
        private static IEnumerable<HostnameConfiguration> GetHostnamesToCreateOrUpdate(
            PsApiManagementHostnameConfiguration portalHostnameConfiguration,
            PsApiManagementHostnameConfiguration proxyHostnameConfiguration,
            PsApiManagement currentState)
        {
            if (portalHostnameConfiguration != null)
            {
                yield return new HostnameConfiguration(
                    HostnameType.Portal,
                    portalHostnameConfiguration.Hostname,
                    new CertificateInformation
                    {
                        Thumbprint = portalHostnameConfiguration.HostnameCertificate.Thumbprint,
                        Subject = string.IsNullOrWhiteSpace(portalHostnameConfiguration.HostnameCertificate.Subject) ? "dummy" : portalHostnameConfiguration.HostnameCertificate.Subject
                    });
            }

            if (proxyHostnameConfiguration != null)
            {
                yield return new HostnameConfiguration(
                    HostnameType.Proxy,
                    proxyHostnameConfiguration.Hostname,
                    new CertificateInformation
                    {
                        Thumbprint = proxyHostnameConfiguration.HostnameCertificate.Thumbprint,
                        Subject = string.IsNullOrWhiteSpace(proxyHostnameConfiguration.HostnameCertificate.Subject) ? "dummy" : proxyHostnameConfiguration.HostnameCertificate.Subject
                    });
            }
        }
        public void TestCtor()
        {
            // arrange
            var resource = new ApiServiceResource
            {
                ETag = "etag",
                Id = "id",
                Location = "location",
                Name = "name",
                Type = "resource type",
                Tags = new Dictionary<string, string>()
                {
                    {"tagkey1", "tagvalue1" },
                    {"tagkey2", "tagvalue2" }
                },
                Properties = new ApiServiceProperties
                {
                    AdditionalRegions = new List<AdditionalRegion>()
                    {
                        new AdditionalRegion
                        {
                            Location = "region location",
                            SkuType = SkuType.Premium,
                            SkuUnitCount = 2,
                            StaticIPs = new List<string>() { "192.168.1.1", "192.168.1.1" },
                            VirtualNetworkConfiguration = new VirtualNetworkConfiguration
                            {
                                Location = "region vpn location",
                                SubnetName = "region vpn subnet name",
                                VnetId = Guid.NewGuid()
                            }
                        }
                    },
                    AddresserEmail = "*****@*****.**",
                    CreatedAtUtc = DateTime.UtcNow,
                    CustomProperties = new Dictionary<string, string>()
                    {
                        { "cpkey1", "cpvalue1" },
                        { "cpkey2", "cpvalue2" }
                    },
                    HostnameConfigurations = new List<HostnameConfiguration>()
                    {
                        new HostnameConfiguration
                        {
                            Type = HostnameType.Portal,
                            Certificate = new CertificateInformation
                            {
                                Expiry = DateTime.UtcNow.AddDays(5),
                                Subject = "portal cert subject",
                                Thumbprint = "portal cert thumbprint"
                            },
                            Hostname = "portal hostname"
                        },
                        new HostnameConfiguration
                        {
                            Type = HostnameType.Proxy,
                            Certificate = new CertificateInformation
                            {
                                Expiry = DateTime.UtcNow.AddDays(10),
                                Subject = "proxy cert subject",
                                Thumbprint = "proxy cert thumbprint"
                            },
                            Hostname = "proxy hostname"
                        }
                    },
                    ManagementPortalEndpoint = "http://management.portal.endpoint",
                    ProvisioningState = "Active",
                    ProxyEndpoint = "http://proxy.endpoint",
                    PublisherEmail = "*****@*****.**",
                    PublisherName = "publisher name",
                    SkuProperties = new ApiServiceSkuProperties
                    {
                        Capacity = 3,
                        SkuType = SkuType.Premium
                    },
                    StaticIPs = new[] { "192.168.0.1", "192.168.0.2" },
                    VirtualNetworkConfiguration = new VirtualNetworkConfiguration
                    {
                        Location = "vpn location",
                        SubnetName = "vpn subnet name",
                        VnetId = Guid.NewGuid()
                    }
                }
            };

            // act
            var result = new PsApiManagement(resource);

            // assert
            Assert.Equal(resource.Id, result.Id);
            Assert.Equal(resource.Location, result.Location);
            Assert.Equal(resource.Name, result.Name);
            Assert.Equal(resource.Tags.Count, result.Tags.Count);
            foreach (var tagKey in resource.Tags.Keys)
            {
                Assert.Equal(resource.Tags[tagKey], result.Tags[tagKey]);
            }

            Assert.Equal(resource.Properties.AdditionalRegions.Count, result.AdditionalRegions.Count);
            var resourceRegion = resource.Properties.AdditionalRegions[0];
            var resultRegion = result.AdditionalRegions[0];
            Assert.Equal(resourceRegion.Location, resultRegion.Location);
            Assert.Equal(resourceRegion.SkuType.ToString(), resultRegion.Sku.ToString());
            Assert.Equal(resourceRegion.SkuUnitCount, resultRegion.Capacity);
            for (int i = 0; i < resourceRegion.StaticIPs.Count; i++)
            {
                Assert.Equal(resourceRegion.StaticIPs[i], resultRegion.StaticIPs[i]);
            }
            Assert.Equal(resourceRegion.VirtualNetworkConfiguration.Location, resultRegion.VirtualNetwork.Location);
            Assert.Equal(resourceRegion.VirtualNetworkConfiguration.SubnetName, resultRegion.VirtualNetwork.SubnetName);
            Assert.Equal(resourceRegion.VirtualNetworkConfiguration.VnetId, resultRegion.VirtualNetwork.VnetId);

            var portalHostname = resource.Properties.HostnameConfigurations.Single(h => h.Type == HostnameType.Portal);
            Assert.Equal(portalHostname.Hostname, result.PortalHostnameConfiguration.Hostname);
            Assert.Equal(portalHostname.Certificate.Expiry, result.PortalHostnameConfiguration.HostnameCertificate.Expiry);
            Assert.Equal(portalHostname.Certificate.Subject, result.PortalHostnameConfiguration.HostnameCertificate.Subject);
            Assert.Equal(portalHostname.Certificate.Thumbprint, result.PortalHostnameConfiguration.HostnameCertificate.Thumbprint);

            var proxyHostname = resource.Properties.HostnameConfigurations.Single(h => h.Type == HostnameType.Proxy);
            Assert.Equal(proxyHostname.Hostname, result.ProxyHostnameConfiguration.Hostname);
            Assert.Equal(proxyHostname.Certificate.Expiry, result.ProxyHostnameConfiguration.HostnameCertificate.Expiry);
            Assert.Equal(proxyHostname.Certificate.Subject, result.ProxyHostnameConfiguration.HostnameCertificate.Subject);
            Assert.Equal(proxyHostname.Certificate.Thumbprint, result.ProxyHostnameConfiguration.HostnameCertificate.Thumbprint);

            Assert.Equal(resource.Properties.VirtualNetworkConfiguration.Location, result.VirtualNetwork.Location);
            Assert.Equal(resource.Properties.VirtualNetworkConfiguration.SubnetName, result.VirtualNetwork.SubnetName);
            Assert.Equal(resource.Properties.VirtualNetworkConfiguration.VnetId, result.VirtualNetwork.VnetId);

            Assert.Equal(resource.Properties.ManagementPortalEndpoint, result.PortalUrl);
            Assert.Equal(resource.Properties.ProxyEndpoint, result.RuntimeUrl);
            Assert.Equal(resource.Properties.ProvisioningState, result.ProvisioningState);
            Assert.Equal(resource.Properties.SkuProperties.SkuType.ToString(), result.Sku.ToString());
            Assert.Equal(resource.Properties.SkuProperties.Capacity, result.Capacity);
        }
        public ApiManagementLongRunningOperation BeginSetHostnames(
            string resourceGroupName,
            string serviceName,
            PsApiManagementHostnameConfiguration portalHostnameConfiguration,
            PsApiManagementHostnameConfiguration proxyHostnameConfiguration)
        {
            var currentStateResource = Client.ResourceProvider.Get(resourceGroupName, serviceName);
            var currentState = new PsApiManagement(currentStateResource.Value);

            var parameters = new ApiServiceUpdateHostnameParameters
            {
                HostnamesToDelete = GetHostnamesToDelete(portalHostnameConfiguration, proxyHostnameConfiguration, currentState).ToList(),
                HostnamesToCreateOrUpdate = GetHostnamesToCreateOrUpdate(portalHostnameConfiguration, proxyHostnameConfiguration, currentState).ToList()
            };

            var longrunningResponse = Client.ResourceProvider.BeginUpdatingHostname(resourceGroupName, serviceName, parameters);
            AdjustRetryAfter(longrunningResponse, _client.LongRunningOperationInitialTimeout);
            return ApiManagementLongRunningOperation.CreateLongRunningOperation("Set-AzureRmApiManagementHostnames", longrunningResponse);
        }
        private static IEnumerable<HostnameConfiguration> GetHostnamesToCreateOrUpdate(
            PsApiManagementHostnameConfiguration portalHostnameConfiguration,
            PsApiManagementHostnameConfiguration proxyHostnameConfiguration,
            PsApiManagement currentState)
        {
            if (portalHostnameConfiguration != null && currentState.PortalHostnameConfiguration != null)
            {
                yield return new HostnameConfiguration(
                    HostnameType.Portal,
                    portalHostnameConfiguration.Hostname,
                    new CertificateInformation(
                        portalHostnameConfiguration.HostnameCertificate.Expiry,
                        portalHostnameConfiguration.HostnameCertificate.Thumbprint,
                        portalHostnameConfiguration.HostnameCertificate.Subject));
            }

            if (proxyHostnameConfiguration != null && currentState.ProxyHostnameConfiguration != null)
            {
                yield return new HostnameConfiguration(
                    HostnameType.Proxy,
                    proxyHostnameConfiguration.Hostname,
                    new CertificateInformation(
                        proxyHostnameConfiguration.HostnameCertificate.Expiry,
                        proxyHostnameConfiguration.HostnameCertificate.Thumbprint,
                        proxyHostnameConfiguration.HostnameCertificate.Subject));
            }
        }