Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit f4d5f3a

Browse files
f-alizadaFarhad Alizada
and
Farhad Alizada
authored
Exclude builtIn groups from groupTemplate via config parameter (#788)
* Add parameter to exclude built-in groups for goups template * Add excludeBuildInGroups to documentation Co-authored-by: Farhad Alizada <[email protected]>
1 parent 4ef967b commit f4d5f3a

File tree

8 files changed

+83
-14
lines changed

8 files changed

+83
-14
lines changed

src/ArmTemplates/Commands/Configurations/ExtractorConsoleAppConfiguration.cs

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ public class ExtractorConsoleAppConfiguration
109109
[Option(longName: "parametersOutputDirectoryName ", HelpText = "Parameters output directory name, by default it is \"parameters\"")]
110110
public string ParametersOutputDirectoryName { get; set; }
111111

112+
[Option(longName: "excludeBuildInGroups ", HelpText = "Excludes built-in groups from generated template if set to \"true\"")]
113+
public string ExcludeBuildInGroups { get; set; }
114+
112115
/// <summary>
113116
/// Api parameter properties for overriding Api OAuth2 scope or/and Service urloverride. Available via extractor-config file only.
114117
/// </summary>

src/ArmTemplates/Common/API/Clients/Groups/GroupsClient.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task<List<GroupTemplateResource>> GetAllAsync(ExtractorParameters e
3636
this.BaseUrl, azSubId, extractorParameters.ResourceGroup, extractorParameters.SourceApimName, GlobalConstants.ApiVersion);
3737

3838
var groupTemplates = await this.GetPagedResponseAsync<GroupTemplateResource>(azToken, requestUrl);
39-
this.groupDataProcessor.ProcessData(groupTemplates, extractorParameters);
39+
this.groupDataProcessor.ProcessDataAllGroups(groupTemplates, extractorParameters);
4040

4141
return groupTemplates;
4242
}
@@ -49,7 +49,7 @@ public async Task<List<GroupTemplateResource>> GetAllLinkedToProductAsync(string
4949
this.BaseUrl, azSubId, extractorParameters.ResourceGroup, extractorParameters.SourceApimName, productName, GlobalConstants.ApiVersion);
5050

5151
var groupTemplates = await this.GetPagedResponseAsync<GroupTemplateResource>(azToken, requestUrl);
52-
this.groupDataProcessor.ProcessData(groupTemplates, extractorParameters);
52+
this.groupDataProcessor.ProcessDataProductLinkedGroups(groupTemplates, extractorParameters);
5353

5454
return groupTemplates;
5555
}

src/ArmTemplates/Extractor/Models/ExtractorParameters.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public record ExtractorParameters
9090

9191
public bool ExtractIdentityProviders { get; set; }
9292

93+
public bool ExcludeBuildInGroups { get; set; }
94+
9395
public string ParametersOutputDirectoryName { get; set; }
9496

9597
public Dictionary<string, ApiParameterProperty> ApiParameters { get; private set; }
@@ -126,7 +128,8 @@ public ExtractorParameters(ExtractorConsoleAppConfiguration extractorConfig)
126128
this.ParametrizeApiOauth2Scope = (extractorConfig.ParamApiOauth2Scope != null && extractorConfig.ParamApiOauth2Scope.Equals("true", StringComparison.OrdinalIgnoreCase)) || (extractorConfig.ApiParameters != null && extractorConfig.ApiParameters.Any(x => x.Value.Oauth2Scope is not null));
127129
this.ExtractSecrets = extractorConfig.ExtractSecrets != null && extractorConfig.ExtractSecrets.Equals("true", StringComparison.OrdinalIgnoreCase);
128130
this.ExtractIdentityProviders = extractorConfig.ExtractIdentityProviders != null && extractorConfig.ExtractIdentityProviders.Equals("true", StringComparison.OrdinalIgnoreCase);
129-
131+
this.ExcludeBuildInGroups = extractorConfig.ExcludeBuildInGroups != null && extractorConfig.ExcludeBuildInGroups.Equals("true", StringComparison.OrdinalIgnoreCase);
132+
130133
this.ParametersOutputDirectoryName = extractorConfig.ParametersOutputDirectoryName;
131134
if (!string.IsNullOrEmpty(extractorConfig.ParametersOutputDirectoryName))
132135
{
@@ -166,6 +169,7 @@ public ExtractorParameters OverrideConfiguration(ExtractorConsoleAppConfiguratio
166169
this.ExtractGateways = !string.IsNullOrEmpty(overridingConfig.ExtractGateways) ? overridingConfig.ExtractGateways.Equals("true", StringComparison.OrdinalIgnoreCase) : this.ExtractGateways;
167170
this.ParametrizeApiOauth2Scope = !string.IsNullOrEmpty(overridingConfig.ParamApiOauth2Scope) ? overridingConfig.ParamApiOauth2Scope.Equals("true", StringComparison.OrdinalIgnoreCase) : this.ParametrizeApiOauth2Scope;
168171
this.ExtractIdentityProviders = !string.IsNullOrEmpty(overridingConfig.ExtractIdentityProviders) ? overridingConfig.ExtractIdentityProviders.Equals("true", StringComparison.OrdinalIgnoreCase) : this.ExtractIdentityProviders;
172+
this.ExcludeBuildInGroups = !string.IsNullOrEmpty(overridingConfig.ExcludeBuildInGroups) ? overridingConfig.ExcludeBuildInGroups.Equals("true", StringComparison.OrdinalIgnoreCase) : this.ExcludeBuildInGroups;
169173

170174
if (!string.IsNullOrEmpty(overridingConfig.BaseFileName))
171175
{

src/ArmTemplates/Extractor/Utilities/DataProcessors/Absctraction/IGroupDataProcessor.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Utilit
1111
{
1212
public interface IGroupDataProcessor
1313
{
14-
void ProcessData(List<GroupTemplateResource> templates, ExtractorParameters extractorParameters);
14+
void ProcessDataProductLinkedGroups(List<GroupTemplateResource> templates, ExtractorParameters extractorParameters);
1515

16-
void OverrideName(GroupTemplateResource template);
16+
void ProcessDataAllGroups(List<GroupTemplateResource> templates, ExtractorParameters extractorParameters);
1717
}
1818
}

src/ArmTemplates/Extractor/Utilities/DataProcessors/GroupDataProcessor.cs

+17-3
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,28 @@ public GroupDataProcessor()
2525
};
2626
}
2727

28-
public void ProcessData(List<GroupTemplateResource> groupTemplates, ExtractorParameters extractorParameters)
28+
public void ProcessDataAllGroups(List<GroupTemplateResource> groupTemplates, ExtractorParameters extractorParameters)
29+
{
30+
this.OverrideNames(groupTemplates, extractorParameters);
31+
if (extractorParameters.ExcludeBuildInGroups)
32+
{
33+
groupTemplates?.RemoveAll(x => x.Properties.BuiltIn);
34+
}
35+
}
36+
37+
public void ProcessDataProductLinkedGroups(List<GroupTemplateResource> groupTemplates, ExtractorParameters extractorParameters)
38+
{
39+
this.OverrideNames(groupTemplates, extractorParameters);
40+
}
41+
42+
void OverrideNames(List<GroupTemplateResource> groupTemplates, ExtractorParameters extractorParameters)
2943
{
3044
if (groupTemplates.IsNullOrEmpty())
3145
{
3246
return;
3347
}
3448

35-
foreach (var groupTemplate in groupTemplates)
49+
foreach (var groupTemplate in groupTemplates)
3650
{
3751
groupTemplate.OriginalName = groupTemplate.Name;
3852
groupTemplate.NewName = groupTemplate.Name;
@@ -44,7 +58,7 @@ public void ProcessData(List<GroupTemplateResource> groupTemplates, ExtractorPar
4458
}
4559
}
4660

47-
public void OverrideName(GroupTemplateResource template)
61+
void OverrideName(GroupTemplateResource template)
4862
{
4963
if (this.OverrideRules.IsNullOrEmpty())
5064
{

src/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ You have two choices when specifying your settings:
448448
| exctractSecrets | No | By default false. If set to "true" secrets will be extracted as well and parameters templated will be supplied with actual secret values. Currently applies to identityProvider service. |
449449
| extractIdentityProviders | No | By default false. Set to true will attempt to extract the identity providers from service. |
450450
| parametersOutputDirectoryName | No | If set will redefine the output folder name for parameters. By default will be equal to {service-name}-parameters |
451+
| excludeBuildInGroups | No | By default false. Set to true will exclude builtIn groups from groupsTemplate. |
451452

452453

453454
#### Note

tests/ArmTemplates.Tests/Extractor/Abstractions/ExtractorMockerTestsBase.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ protected ExtractorConsoleAppConfiguration GetDefaultExtractorConsoleAppConfigur
112112
string paramApiOauth2Scope = null,
113113
Dictionary<string, ApiParameterProperty> apiParameters = null,
114114
string extractSecrets = null,
115-
string extractIdentityProviders = null
115+
string extractIdentityProviders = null,
116+
string excludeBuildInGroups = null
116117
)
117118
{
118119

@@ -148,7 +149,8 @@ protected ExtractorConsoleAppConfiguration GetDefaultExtractorConsoleAppConfigur
148149
ParamApiOauth2Scope = paramApiOauth2Scope,
149150
ApiParameters = apiParameters,
150151
ExtractSecrets = extractSecrets,
151-
ExtractIdentityProviders = extractIdentityProviders
152+
ExtractIdentityProviders = extractIdentityProviders,
153+
ExcludeBuildInGroups = excludeBuildInGroups
152154
};
153155
}
154156
}

tests/ArmTemplates.Tests/Extractor/Utilities/GroupDataProcessorTest.cs

+49-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public List<GroupTemplateResource> GetMockGroupTemplates() {
4949
}
5050

5151
[Fact]
52-
public void TestOverrideName()
52+
public void TestProcessDataAllGroups_OverrideName()
5353
{
5454
var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration(overrideGroupGuids: "true");
5555
var extractorParameters = new ExtractorParameters(extractorConfig);
@@ -58,25 +58,70 @@ public void TestOverrideName()
5858

5959
var groupDataProcessor = new GroupDataProcessor();
6060

61-
groupDataProcessor.ProcessData(groupTemplates, extractorParameters);
61+
groupDataProcessor.ProcessDataAllGroups(groupTemplates, extractorParameters);
6262

6363
groupTemplates.ElementAt(0).Name.Should().BeEquivalentTo("administrators");
6464
groupTemplates.ElementAt(1).Name.Should().BeEquivalentTo("developers");
6565
}
6666

6767
[Fact]
68-
public void TestSkipOverrideName()
68+
public void TestProcessDataAllGroups_SkipOverrideName()
6969
{
7070
var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration(overrideGroupGuids: "false");
7171
var extractorParameters = new ExtractorParameters(extractorConfig);
7272

7373
var groupDataProcessor = new GroupDataProcessor();
7474

7575
var groupTemplates = this.GetMockGroupTemplates();
76-
groupDataProcessor.ProcessData(groupTemplates, extractorParameters);
76+
groupDataProcessor.ProcessDataAllGroups(groupTemplates, extractorParameters);
7777

7878
groupTemplates.ElementAt(0).Name.Should().BeEquivalentTo("guid-administrators");
7979
groupTemplates.ElementAt(1).Name.Should().BeEquivalentTo("guid-developers");
8080
}
81+
82+
[Fact]
83+
public void TestProcessDataProductLinkedGroups_OverrideName()
84+
{
85+
var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration(overrideGroupGuids: "true");
86+
var extractorParameters = new ExtractorParameters(extractorConfig);
87+
88+
var groupTemplates = this.GetMockGroupTemplates();
89+
90+
var groupDataProcessor = new GroupDataProcessor();
91+
92+
groupDataProcessor.ProcessDataAllGroups(groupTemplates, extractorParameters);
93+
94+
groupTemplates.ElementAt(0).Name.Should().BeEquivalentTo("administrators");
95+
groupTemplates.ElementAt(1).Name.Should().BeEquivalentTo("developers");
96+
}
97+
98+
[Fact]
99+
public void TestProcessDataProductLinkedGroups_SkipOverrideName()
100+
{
101+
var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration(overrideGroupGuids: "false");
102+
var extractorParameters = new ExtractorParameters(extractorConfig);
103+
104+
var groupDataProcessor = new GroupDataProcessor();
105+
106+
var groupTemplates = this.GetMockGroupTemplates();
107+
groupDataProcessor.ProcessDataAllGroups(groupTemplates, extractorParameters);
108+
109+
groupTemplates.ElementAt(0).Name.Should().BeEquivalentTo("guid-administrators");
110+
groupTemplates.ElementAt(1).Name.Should().BeEquivalentTo("guid-developers");
111+
}
112+
113+
[Fact]
114+
public void TestExcludeBuiltInGroups()
115+
{
116+
var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration(excludeBuildInGroups: "true");
117+
var extractorParameters = new ExtractorParameters(extractorConfig);
118+
119+
var groupDataProcessor = new GroupDataProcessor();
120+
121+
var groupTemplates = this.GetMockGroupTemplates();
122+
groupDataProcessor.ProcessDataAllGroups(groupTemplates, extractorParameters);
123+
124+
groupTemplates.Count.Should().Be(0);
125+
}
81126
}
82127
}

0 commit comments

Comments
 (0)