<commit_before>﻿// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Rest.Azure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.Common.Strategies
{
    public static class ResourceConfigExtensions
    {
        public static ResourceConfig<TModel> CreateResourceConfig<TModel>(
            this ResourceStrategy<TModel> strategy,
            IResourceConfig resourceGroup,
            string name,
            Func<IEngine, TModel> createModel = null)
            where TModel : class, new()
        {
            // update dependencies
            createModel = createModel ?? (_ => new TModel());
            var engine = new DependencyEngine();
            createModel(engine);
            //
            return new ResourceConfig<TModel>(
                strategy,
                resourceGroup,
                name,
                createModel,
                engine
                    .Dependencies
                    .Values
                    .Concat(new[] { resourceGroup })
                    .Where(v => v != null));
        }

        public static async Task<TModel> GetAsync<TModel>(
            this ResourceConfig<TModel> config,
            IClient client,
            CancellationToken cancellationToken)
            where TModel : class
        {
            try
            {
                return await config.Strategy.GetAsync(
                    client,
                    new GetAsyncParams(config.GetResourceGroupName(), config.Name, cancellationToken));
            }
            catch (CloudException e)
                when (e.Response.StatusCode == HttpStatusCode.NotFound)
            {
                return null;
            }
        }

        public static Task<TModel> CreateOrUpdateAsync<TModel>(
            this ResourceConfig<TModel> config,
            IClient client,
            TModel model,
            CancellationToken cancellationToken)
            where TModel : class
            => config.Strategy.CreateOrUpdateAsync(
                client,
                CreateOrUpdateAsyncParams.Create(
                    config.GetResourceGroupName(),
                    config.Name,
                    model,
                    cancellationToken));

        public static IEnumerable<IResourceConfig> GetResourceDependencies(
            this IEntityConfig config)
            => config
                .Dependencies
                .Select(d => d.Resource)
                .Concat(config.NestedResources.SelectMany(GetResourceDependencies));

        public static string GetFullName(this IResourceConfig config)
            => config.Strategy.Type.Provider + "/" + config.Name; 
    }
}<commit_msg>Fix circular dependencies.<commit_after>﻿// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Rest.Azure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.Common.Strategies
{
    public static class ResourceConfigExtensions
    {
        public static ResourceConfig<TModel> CreateResourceConfig<TModel>(
            this ResourceStrategy<TModel> strategy,
            IResourceConfig resourceGroup,
            string name,
            Func<IEngine, TModel> createModel = null)
            where TModel : class, new()
        {
            // update dependencies
            createModel = createModel ?? (_ => new TModel());
            var engine = new DependencyEngine();
            createModel(engine);
            //
            return new ResourceConfig<TModel>(
                strategy,
                resourceGroup,
                name,
                createModel,
                engine
                    .Dependencies
                    .Values
                    .Concat(new[] { resourceGroup })
                    .Where(v => v != null));
        }

        public static async Task<TModel> GetAsync<TModel>(
            this ResourceConfig<TModel> config,
            IClient client,
            CancellationToken cancellationToken)
            where TModel : class
        {
            try
            {
                return await config.Strategy.GetAsync(
                    client,
                    new GetAsyncParams(config.GetResourceGroupName(), config.Name, cancellationToken));
            }
            catch (CloudException e)
                when (e.Response.StatusCode == HttpStatusCode.NotFound)
            {
                return null;
            }
        }

        public static Task<TModel> CreateOrUpdateAsync<TModel>(
            this ResourceConfig<TModel> config,
            IClient client,
            TModel model,
            CancellationToken cancellationToken)
            where TModel : class
            => config.Strategy.CreateOrUpdateAsync(
                client,
                CreateOrUpdateAsyncParams.Create(
                    config.GetResourceGroupName(),
                    config.Name,
                    model,
                    cancellationToken));

        public static IEnumerable<IResourceConfig> GetResourceDependencies(
            this IEntityConfig config)
            => config
                .Dependencies
                .Select(d => d.Resource)
                .Where(r => r != config)
                .Concat(config.NestedResources.SelectMany(GetResourceDependencies));

        public static string GetFullName(this IResourceConfig config)
            => config.Strategy.Type.Provider + "/" + config.Name; 
    }
}
{'fraction_non_alphanumeric': 0.11866326819597847, 'fraction_numerical': 0.0011328235627301047, 'mean_word_length': 1.544308357348703, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6819765', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1492, 'n_words': 449}