Skip to content

Commit 846d6af

Browse files
authored
Abstraction for config framework (#313)
* abstractions and models for config API * revert the changes to Authentication.Abstractions.csproj
1 parent 660c188 commit 846d6af

File tree

8 files changed

+405
-0
lines changed

8 files changed

+405
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
18+
namespace Microsoft.Azure.PowerShell.Common.Config
19+
{
20+
public interface IConfigManager
21+
{
22+
/// <summary>
23+
/// Path of the config file.
24+
/// </summary>
25+
string ConfigFilePath { get; }
26+
27+
/// <summary>
28+
/// Register a config.
29+
/// </summary>
30+
/// <remarks>Register all the configs before <see cref="BuildConfig"/></remarks>
31+
void RegisterConfig(ConfigDefinition config);
32+
33+
/// <summary>
34+
/// Retrieve data from all the providers and build config values.
35+
/// </summary>
36+
void BuildConfig();
37+
38+
/// <summary>
39+
/// Get the value of a config by key.
40+
/// </summary>
41+
/// <typeparam name="T">Type of the value.</typeparam>
42+
/// <param name="key">Key of the config.</param>
43+
/// <param name="invocation">PowerShell cmdlet invocation info. If not null, the config that matches the module or cmdlet name will be returned.</param>
44+
/// <remarks>For the list of available keys, see <see cref="ConfigKeys"/>, for those used in service projects, or see <see cref="ConfigKeysForCommon"/> for those used in common projects.
45+
/// The `invocation` parameter is typed `object` because we don't want Authentication.Abstractions project to take dependency on PowerShell SDK.
46+
/// However at runtime it needs to be of type `InvocationInfo`.</remarks>
47+
/// <returns>Value of the config, or the default value if never set.</returns>
48+
T GetConfigValue<T>(string key, object invocation = null);
49+
50+
/// <summary>
51+
/// List all configs with values.
52+
/// </summary>
53+
/// <param name="filter">Filter the result by config key or level etc.</param>
54+
IEnumerable<ConfigData> ListConfigs(ConfigFilter filter = null);
55+
56+
/// <summary>
57+
/// List all the definitions of all the registered configs.
58+
/// </summary>
59+
IEnumerable<ConfigDefinition> ListConfigDefinitions();
60+
61+
/// <summary>
62+
/// Update the value of a config.
63+
/// </summary>
64+
/// <param name="key">Key of the config.</param>
65+
/// <param name="value">Value to update.</param>
66+
/// <param name="scope">Scope of the config to update.</param>
67+
/// <remarks>This is a simple version of <see cref="UpdateConfig(UpdateConfigOptions)"/>.</remarks>
68+
/// <returns>The updated config, both definition and value.</returns>
69+
ConfigData UpdateConfig(string key, object value, ConfigScope scope);
70+
71+
/// <summary>
72+
/// Update the value of a config.
73+
/// </summary>
74+
/// <param name="options">Specify the key, value, and optionally scope and appliesTo etc. to update.</param>
75+
/// <returns>The updated config, both definition and value.</returns>
76+
ConfigData UpdateConfig(UpdateConfigOptions options);
77+
78+
/// <summary>
79+
/// Clear a config set previously.
80+
/// </summary>
81+
/// <remarks>This is a simple version of <see cref="ClearConfig(ClearConfigOptions)"/>.</remarks>
82+
/// <param name="key">Key of the config to clear.</param>
83+
/// <param name="scope">Scope of the config to update.</param>
84+
void ClearConfig(string key, ConfigScope scope);
85+
86+
/// <summary>
87+
/// Clear a config set previously.
88+
/// </summary>
89+
/// <param name="options">Specify the key, and optionally scope and appliesTo etc. to update.</param>
90+
void ClearConfig(ClearConfigOptions options);
91+
}
92+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.PowerShell.Common.Config
16+
{
17+
/// <summary>
18+
/// General categories of levels that a config applies to.
19+
/// </summary>
20+
public enum AppliesTo
21+
{
22+
/// <summary>
23+
/// The config can apply to whole Azure PowerShell.
24+
/// </summary>
25+
Az,
26+
27+
/// <summary>
28+
/// The config can apply to a certain module.
29+
/// </summary>
30+
Module,
31+
32+
/// <summary>
33+
/// The config can apply to a certain cmdlet.
34+
/// </summary>
35+
Cmdlet
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
16+
namespace Microsoft.Azure.PowerShell.Common.Config
17+
{
18+
public class ClearConfigOptions
19+
{
20+
public ClearConfigOptions(string key, ConfigScope scope)
21+
{
22+
Key = key;
23+
Scope = scope;
24+
}
25+
26+
public string Key { get; }
27+
28+
public ConfigScope Scope { get; set; }
29+
30+
/// <summary>
31+
/// Specifies a module or cmdlet that the config applies to.
32+
/// If null, it applies to all.
33+
/// </summary>
34+
public string AppliesTo { get; set; } = null;
35+
}
36+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
17+
namespace Microsoft.Azure.PowerShell.Common.Config
18+
{
19+
/// <summary>
20+
/// Wrapper for both definition and value of a config. Used as output of some API of <see cref="IConfigManager"/>.
21+
/// </summary>
22+
public class ConfigData
23+
{
24+
public ConfigData(ConfigDefinition config, object value, ConfigScope scope, string appliesTo)
25+
{
26+
Definition = config ?? throw new ArgumentNullException(nameof(config));
27+
Value = value;
28+
Scope = scope;
29+
AppliesTo = appliesTo;
30+
}
31+
32+
public ConfigDefinition Definition { get; }
33+
34+
public object Value { get; }
35+
36+
/// <summary>
37+
/// Specifies a module or cmdlet that the config applies to.
38+
/// If null, it applies to all.
39+
/// </summary>
40+
public string AppliesTo { get; }
41+
42+
public ConfigScope Scope { get; }
43+
}
44+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Collections.Generic;
17+
18+
namespace Microsoft.Azure.PowerShell.Common.Config
19+
{
20+
/// <summary>
21+
/// Represents the definition of a config of Azure PowerShell.
22+
/// </summary>
23+
public abstract class ConfigDefinition
24+
{
25+
/// <summary>
26+
/// Gets the default value of this config.
27+
/// </summary>
28+
public abstract object DefaultValue { get; }
29+
30+
/// <summary>
31+
/// Gets the unique key of this config.
32+
/// </summary>
33+
/// <remarks>It is also used as the name of the PowerShell parameter which maps to this config, so the key must follow the design guideline and conventions. See <see href="https://github.com/Azure/azure-powershell/blob/main/documentation/development-docs/design-guidelines/parameter-best-practices.md#parameter-best-practices">Parameter Best Practices</see>.</remarks>
34+
/// <seealso cref=""/>
35+
public abstract string Key { get; }
36+
37+
/// <summary>
38+
/// Gets the help message or description of the config.
39+
/// It is also used as the help message of the PowerShell parameter which maps to this config.
40+
/// </summary>
41+
public abstract string HelpMessage { get; }
42+
43+
/// <summary>
44+
/// Gets the name of the environment variable that can control this config.
45+
/// </summary>
46+
public virtual string EnvironmentVariableName { get; } = null;
47+
48+
/// <summary>
49+
/// Gets how the config can be applied to.
50+
/// </summary>
51+
public virtual IReadOnlyCollection<AppliesTo> CanApplyTo => new AppliesTo[] { AppliesTo.Az, AppliesTo.Module, AppliesTo.Cmdlet };
52+
53+
/// <summary>
54+
/// Gets the type of the value of this config.
55+
/// </summary>
56+
public abstract Type ValueType { get; }
57+
58+
/// <summary>
59+
/// Override in derived classes to validate the input value. Throws an exception if not.
60+
/// </summary>
61+
/// <param name="value">The value to check.</param>
62+
public virtual void Validate(object value) { }
63+
64+
/// <summary>
65+
/// Override in derived classes to perform side effects of applying the config value.
66+
/// If a exception is thrown, the config will not be updated.
67+
/// </summary>
68+
/// <param name="value">Value of the config to apply.</param>
69+
public virtual void Apply(object value) { }
70+
}
71+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System.Collections.Generic;
16+
17+
namespace Microsoft.Azure.PowerShell.Common.Config
18+
{
19+
/// <summary>
20+
/// Filter options for listing configurations. Used as input of some API of <see cref="IConfigManager" />
21+
/// </summary>
22+
public class ConfigFilter
23+
{
24+
/// <summary>
25+
/// Represents the global config "applies to" - the config applies to all cmdlets of Azure PowerShell.
26+
/// </summary>
27+
public const string GlobalAppliesTo = "Az";
28+
29+
/// <summary>
30+
/// Keys of the configs to filter. When omitted, all the keys will be used.
31+
/// </summary>
32+
public IEnumerable<string> Keys { get; set; } = null;
33+
34+
/// <summary>
35+
/// Specifies what part of Azure PowerShell the config applies to.
36+
/// </summary>
37+
/// <remarks>
38+
/// Possible values are:
39+
/// - null: the config applies to any of above.
40+
/// - <see cref="GlobalAppliesTo"/> ("Az"): the config applies to all modules and cmdlets of Azure PowerShell.
41+
/// - Name of a module: the config applies to a certain module of Azure PowerShell. For example, "Az.Storage".
42+
/// - Name of a cmdlet: the config applies to a certain cmdlet of Azure PowerShell. For example, "Get-AzKeyVault".
43+
/// </remarks>
44+
public string AppliesTo { get; set; } = null;
45+
}
46+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.PowerShell.Common.Config
16+
{
17+
/// <summary>
18+
/// Scope for the config.
19+
/// </summary>
20+
public enum ConfigScope
21+
{
22+
/// <summary>
23+
/// Config will be persitent on the disk, available for all the PowerShell sessions initiated by the current user.
24+
/// </summary>
25+
CurrentUser,
26+
27+
/// <summary>
28+
/// Config is effective in current PowerShell process.
29+
/// </summary>
30+
Process,
31+
32+
/// <summary>
33+
/// Config is never set.
34+
/// </summary>
35+
/// <remarks>This option is not available when updating or clearing a config.</remarks>
36+
Default
37+
}
38+
}

0 commit comments

Comments
 (0)