public static async Task <CertificateGroupConfigurationModel> UpdateCertificateGroupConfiguration(
            KeyVaultServiceClient keyVaultServiceClient,
            string id,
            CertificateGroupConfigurationModel config)
        {
            if (id.ToLower() != config.Id.ToLower())
            {
                throw new ArgumentException("groupid doesn't match config id");
            }
            string json = await keyVaultServiceClient.GetCertificateConfigurationGroupsAsync().ConfigureAwait(false);

            List <CertificateGroupConfigurationModel> certificateGroupCollection = JsonConvert.DeserializeObject <List <CertificateGroupConfigurationModel> >(json);

            var original = certificateGroupCollection.SingleOrDefault(cg => String.Equals(cg.Id, id, StringComparison.OrdinalIgnoreCase));

            if (original == null)
            {
                throw new ArgumentException("invalid groupid");
            }

            ValidateConfiguration(config);

            var index = certificateGroupCollection.IndexOf(original);

            certificateGroupCollection[index] = config;

            json = JsonConvert.SerializeObject(certificateGroupCollection);

            // update config
            json = await keyVaultServiceClient.PutCertificateConfigurationGroupsAsync(json).ConfigureAwait(false);

            // read it back to verify
            certificateGroupCollection = JsonConvert.DeserializeObject <List <CertificateGroupConfigurationModel> >(json);
            return(certificateGroupCollection.SingleOrDefault(cg => String.Equals(cg.Id, id, StringComparison.OrdinalIgnoreCase)));
        }
        public static async Task <string[]> GetCertificateGroupIds(
            KeyVaultServiceClient keyVaultServiceClient)
        {
            string json = await keyVaultServiceClient.GetCertificateConfigurationGroupsAsync().ConfigureAwait(false);

            List <CertificateGroupConfigurationModel> certificateGroupCollection =
                JsonConvert.DeserializeObject <List <CertificateGroupConfigurationModel> >(json);
            List <string> groups = certificateGroupCollection.Select(cg => cg.Id).ToList();

            return(groups.ToArray());
        }
        public static async Task <CertificateGroupConfigurationModel> GetCertificateGroupConfiguration(
            KeyVaultServiceClient keyVaultServiceClient,
            string id)
        {
            string json = await keyVaultServiceClient.GetCertificateConfigurationGroupsAsync().ConfigureAwait(false);

            List <CertificateGroupConfigurationModel> certificateGroupCollection =
                JsonConvert.DeserializeObject <List <CertificateGroupConfigurationModel> >(json);

            return(certificateGroupCollection.SingleOrDefault(cg => String.Equals(cg.Id, id, StringComparison.OrdinalIgnoreCase)));
        }
        public static async Task <CertificateGroupConfigurationModel> CreateCertificateGroupConfiguration(
            KeyVaultServiceClient keyVaultServiceClient,
            string id,
            string subject,
            string certType)
        {
            var config = DefaultConfiguration(id, subject, certType);

            if (id.ToLower() != config.Id.ToLower())
            {
                throw new ArgumentException("groupid doesn't match config id");
            }
            string json;
            IList <CertificateGroupConfigurationModel> certificateGroupCollection = new List <CertificateGroupConfigurationModel>();

            try
            {
                json = await keyVaultServiceClient.GetCertificateConfigurationGroupsAsync().ConfigureAwait(false);

                certificateGroupCollection =
                    JsonConvert.DeserializeObject <List <CertificateGroupConfigurationModel> >(json);
            }
            catch (KeyVaultErrorException kex)
            {
                if (kex.Response.StatusCode != HttpStatusCode.NotFound)
                {
                    throw kex;
                }
            }

            var original = certificateGroupCollection.SingleOrDefault(cg => String.Equals(cg.Id, id, StringComparison.OrdinalIgnoreCase));

            if (original != null)
            {
                throw new ArgumentException("groupid already exists");
            }

            ValidateConfiguration(config);

            certificateGroupCollection.Add(config);

            json = JsonConvert.SerializeObject(certificateGroupCollection);

            // update config
            json = await keyVaultServiceClient.PutCertificateConfigurationGroupsAsync(json).ConfigureAwait(false);

            // read it back to verify
            certificateGroupCollection = JsonConvert.DeserializeObject <List <CertificateGroupConfigurationModel> >(json);
            return(certificateGroupCollection.SingleOrDefault(cg => String.Equals(cg.Id, id, StringComparison.OrdinalIgnoreCase)));
        }