protected static string GenerateCscfgWithNetworkConfiguration(string serviceName,
                                                                      Dictionary <string, RoleConfiguration> roleNameToPropertiesMapping,
                                                                      string vNetName,
                                                                      string subnetName,
                                                                      ServiceConfigurationNetworkConfigurationAddressAssignmentsReservedIPs reservedIPs = null,
                                                                      List <ServiceConfigurationRoleCertificate> cscfgCerts = null,
                                                                      ServiceConfigurationRoleSecurityConfigurations securityConfigurations = null,
                                                                      int osFamily = 5,
                                                                      Setting[] serviceSettings = null)
        {
            string cscfgPlainText = ServiceConfigurationHelpers.GenerateServiceConfiguration(
                serviceName: serviceName,
                osFamily: osFamily,
                osVersion: "*",
                roleNameToPropertiesMapping: roleNameToPropertiesMapping,
                schemaVersion: "2015-04.2.6",
                vNetName: vNetName,
                subnetName: subnetName,
                reservedIPs: reservedIPs,
                certificates: cscfgCerts,
                securityConfigurations: securityConfigurations,
                serviceSettings: serviceSettings
                );

            return(cscfgPlainText);
        }
        protected static CloudService GenerateCloudService(string serviceName,
                                                           string cspkgSasUri,
                                                           string vnetName,
                                                           string subnetName,
                                                           Dictionary <string, RoleConfiguration> roleNameToPropertiesMapping,
                                                           CloudServiceVaultSecretGroup vaultGroup = null,
                                                           List <ServiceConfigurationRoleCertificate> cscfgCerts = null,
                                                           ServiceConfigurationRoleSecurityConfigurations securityConfigurations = null,
                                                           List <Extension> extensions = null)
        {
            CloudService cloudService = new CloudService
            {
                Properties = new CloudServiceProperties
                {
                    RoleProfile = new CloudServiceRoleProfile()
                    {
                        Roles = GenerateRoles(roleNameToPropertiesMapping)
                    },
                    Configuration = GenerateCscfgWithNetworkConfiguration(serviceName, roleNameToPropertiesMapping, vnetName, subnetName, null, cscfgCerts, securityConfigurations),
                    PackageUrl    = cspkgSasUri
                },
                Location = m_location
            };

            if (vaultGroup != null)
            {
                cloudService.Properties.OsProfile =
                    new CloudServiceOsProfile
                {
                    Secrets = new List <CloudServiceVaultSecretGroup>
                    {
                        vaultGroup
                    }
                };
            }

            if (extensions != null)
            {
                cloudService.Properties.ExtensionProfile = new CloudServiceExtensionProfile
                {
                    Extensions = extensions
                };
            }
            return(cloudService);
        }
        private static void AddRoleConfigurations(
            Dictionary <string, Utilities.RoleConfiguration> roleNameToPropertiesMapping,
            List <ServiceConfigurationRole> roles,
            List <ServiceConfigurationRoleCertificate> certificates,
            ServiceConfigurationRoleSecurityConfigurations securityConfigurations,
            List <ServiceConfigurationNetworkConfigurationAddressAssignmentsInstanceAddress> roleInstanceAddresses,
            string subnetName)
        {
            foreach (string roleName in roleNameToPropertiesMapping.Keys)
            {
                Utilities.RoleConfiguration       roleConfiguration = roleNameToPropertiesMapping[roleName];
                ServiceConfigurationRoleSetting[] settingsArray     = roleConfiguration.Settings.Keys.Select(key => new ServiceConfigurationRoleSetting {
                    name = key, value = roleConfiguration.Settings[key]
                }).ToArray();

                // Add Configuration for each role
                roles.Add(new ServiceConfigurationRole()
                {
                    name      = roleName,
                    Instances = new ServiceConfigurationRoleInstances()
                    {
                        count = roleConfiguration.InstanceCount.ToString()
                    },
                    // Note: For now these settings and certificates are same for all roles.
                    // They can be handled later as we add support for other scenarios.
                    ConfigurationSettings  = settingsArray,
                    Certificates           = certificates?.ToArray(),
                    SecurityConfigurations = securityConfigurations
                });

                // Add the required subnet for each role
                roleInstanceAddresses.Add(new ServiceConfigurationNetworkConfigurationAddressAssignmentsInstanceAddress()
                {
                    roleName = roleName,
                    Subnets  = new ServiceConfigurationNetworkConfigurationAddressAssignmentsInstanceAddressSubnets()
                    {
                        Subnet = new ServiceConfigurationNetworkConfigurationAddressAssignmentsInstanceAddressSubnetsSubnet()
                        {
                            name = subnetName
                        }
                    }
                });
            }
        }
        public static string GenerateServiceConfiguration(
            string serviceName,
            int osFamily,
            string osVersion,
            string schemaVersion,
            Dictionary <string, Utilities.RoleConfiguration> roleNameToPropertiesMapping,
            string vNetName,
            string subnetName,
            ServiceConfigurationNetworkConfigurationAddressAssignmentsReservedIPs reservedIPs = null,
            List <ServiceConfigurationRoleCertificate> certificates = null,
            ServiceConfigurationRoleSecurityConfigurations securityConfigurations = null,
            Setting[] serviceSettings = null)
        {
            ConfigureRoleSettings(roleNameToPropertiesMapping, certificates);

            List <ServiceConfigurationRole> roles = new List <ServiceConfigurationRole>();
            List <ServiceConfigurationNetworkConfigurationAddressAssignmentsInstanceAddress> roleInstanceAddresses = new List <ServiceConfigurationNetworkConfigurationAddressAssignmentsInstanceAddress>();

            AddRoleConfigurations(roleNameToPropertiesMapping, roles, certificates, securityConfigurations, roleInstanceAddresses, subnetName);

            ServiceConfiguration serviceConfiguration = GenerateServiceConfiguration(serviceName, osFamily, osVersion, schemaVersion, roles, vNetName, roleInstanceAddresses, serviceSettings, reservedIPs);

            return(SerializeToXML(serviceConfiguration));
        }