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

Commit acc56d6

Browse files
authored
Supports product subscriptions (#453)
* Initialized sample unit-test for generating subscriptions * Supports product subscriptions * Supports product subscriptions
1 parent 7c4bff7 commit acc56d6

File tree

9 files changed

+214
-9
lines changed

9 files changed

+214
-9
lines changed

src/APIM_ARMTemplate/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,18 @@ _Additional properties found in [ApiVersionSetContractProperties](https://docs.m
9292
|-----------------------|-----------------------|-----------------------|--------------------------------------------------|
9393
| name | string | No | Name of the product resource. If omitted, the display name is used. |
9494
| policy | string | No | Location of the Product policy XML file. Can be url or local file. |
95+
| subscriptions | Array<[SubscriptionConfiguration](#SubscriptionConfiguration)> | No | List of Subscriptions
9596

9697
_Additional properties found in [ProductContractProperties](https://docs.microsoft.com/en-us/azure/templates/microsoft.apimanagement/2019-01-01/service/products#ProductContractProperties)_
9798

99+
#### SubscriptionConfiguration
100+
101+
| Property | Type | Required | Value |
102+
|-----------------------|-----------------------|-----------------------|--------------------------------------------------|
103+
| name | string | No | Name of the subscription resource. If omitted, the display name is used. |
104+
105+
_Additional properties found in [SubscriptionCreateParameterProperties](https://docs.microsoft.com/en-us/azure/templates/microsoft.apimanagement/2019-01-01/service/subscriptions#subscriptioncreateparameterproperties-object)_
106+
98107
#### PropertyConfiguration
99108

100109
| Property | Type | Required | Value |
@@ -218,6 +227,12 @@ products:
218227
subscriptionsLimit: 1
219228
state: notPublished
220229
policy: C:\Users\myUsername\Projects\azure-api-management-devops-example\src\APIM_ARMTemplate\apimtemplate\Creator\ExampleFile\XMLPolicies\productSetBodyBasic.xml
230+
subscriptions:
231+
- name: platinum
232+
primaryKey: a240691f-03fd-4557-a5cb-6e0f65cd976a
233+
secondaryKey: 032338aa-0076-4379-910c-32ddd42f38a1
234+
state: active
235+
allowTracing: true
221236
tags:
222237
- displayName: Universe
223238
loggers:

src/APIM_ARMTemplate/apimtemplate.test/Creator/TemplateCreatorFactories/ProductTemplateCreatorFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ public static ProductTemplateCreator GenerateProductTemplateCreator()
99
{
1010
PolicyTemplateCreator policyTemplateCreator = PolicyTemplateCreatorFactory.GeneratePolicyTemplateCreator();
1111
ProductGroupTemplateCreator productGroupTemplateCreator = new ProductGroupTemplateCreator();
12-
ProductTemplateCreator productTemplateCreator = new ProductTemplateCreator(policyTemplateCreator, productGroupTemplateCreator);
12+
SubscriptionTemplateCreator productSubscriptionsTemplateCreator = new SubscriptionTemplateCreator();
13+
ProductTemplateCreator productTemplateCreator = new ProductTemplateCreator(policyTemplateCreator, productGroupTemplateCreator, productSubscriptionsTemplateCreator);
1314
return productTemplateCreator;
1415
}
1516
}
16-
}
17+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System.Collections.Generic;
2+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common;
3+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create;
4+
using Xunit;
5+
6+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Test
7+
{
8+
public class SubscriptionTemplateCreatorTests
9+
{
10+
[Fact]
11+
public void ShouldCreateSubscriptionResourceFromSubscriptionConfig()
12+
{
13+
// arrange
14+
SubscriptionTemplateCreator subscriptionTemplateCreator = new SubscriptionTemplateCreator();
15+
CreatorConfig creatorConfig = new CreatorConfig() { subscriptions = new List<SubscriptionConfig>() };
16+
17+
SubscriptionConfig subscription = new SubscriptionConfig()
18+
{
19+
name = "subscriptionName",
20+
ownerId = "user/ownerId",
21+
scope = "/products/productId",
22+
displayName = "displayName",
23+
primaryKey = "primaryKey",
24+
secondaryKey = "secondaryKey",
25+
state = "active",
26+
allowTracing = true,
27+
};
28+
29+
string[] dependsOn = new string[] { "dependsOn" };
30+
31+
// act
32+
SubscriptionsTemplateResource subscriptionsTemplateResource = subscriptionTemplateCreator.CreateSubscriptionsTemplateResource(subscription, dependsOn);
33+
34+
// assert
35+
36+
Assert.Equal($"[concat(parameters('ApimServiceName'), '/{subscription.name}')]", subscriptionsTemplateResource.name);
37+
Assert.Equal($"Microsoft.ApiManagement/service/subscriptions", subscriptionsTemplateResource.type);
38+
Assert.Equal("2019-01-01", subscriptionsTemplateResource.apiVersion);
39+
40+
Assert.Equal(subscription.scope, subscriptionsTemplateResource.properties.scope);
41+
Assert.Equal(subscription.displayName, subscriptionsTemplateResource.properties.displayName);
42+
Assert.Equal(subscription.primaryKey, subscriptionsTemplateResource.properties.primaryKey);
43+
Assert.Equal(subscription.secondaryKey, subscriptionsTemplateResource.properties.secondaryKey);
44+
Assert.Equal(subscription.state, subscriptionsTemplateResource.properties.state);
45+
Assert.Equal(subscription.allowTracing, subscriptionsTemplateResource.properties.allowTracing);
46+
47+
Assert.Equal(dependsOn, subscriptionsTemplateResource.dependsOn);
48+
}
49+
50+
[Fact]
51+
public void ShouldCreateProductSubscriptionTemplateResourceFromCreatorConfigWithCorrectContent()
52+
{
53+
// arrange
54+
SubscriptionTemplateCreator subscriptionTemplateCreator = new SubscriptionTemplateCreator();
55+
56+
CreatorConfig creatorConfig = new CreatorConfig() { products = new List<ProductConfig>() };
57+
ProductConfig product = new ProductConfig()
58+
{
59+
name = "productName",
60+
displayName = "displayName",
61+
description = "description",
62+
terms = "terms",
63+
subscriptionRequired = true,
64+
approvalRequired = true,
65+
subscriptionsLimit = 1,
66+
state = "state",
67+
};
68+
69+
SubscriptionConfig subscription = new SubscriptionConfig()
70+
{
71+
name = "subscriptionName",
72+
ownerId = "user/ownerId",
73+
displayName = "displayName",
74+
primaryKey = "primaryKey",
75+
secondaryKey = "secondaryKey",
76+
state = "active",
77+
allowTracing = true,
78+
};
79+
80+
product.subscriptions = new List<SubscriptionConfig>();
81+
product.subscriptions.Add(subscription);
82+
83+
creatorConfig.products.Add(product);
84+
85+
var dependsOn = new[] { $"[resourceId('Microsoft.ApiManagement/service/products', parameters('ApimServiceName'), '{product.name}')]" };
86+
87+
// act
88+
var subscriptionsTemplateResources = subscriptionTemplateCreator.CreateSubscriptionsTemplateResources(product, dependsOn);
89+
90+
// assert
91+
92+
var subscriptionsTemplateResource = subscriptionsTemplateResources[0];
93+
94+
Assert.Equal($"/products/{product.name}", subscriptionsTemplateResource.properties.scope);
95+
Assert.Equal(subscription.displayName, subscriptionsTemplateResource.properties.displayName);
96+
Assert.Equal(subscription.primaryKey, subscriptionsTemplateResource.properties.primaryKey);
97+
Assert.Equal(subscription.secondaryKey, subscriptionsTemplateResource.properties.secondaryKey);
98+
Assert.Equal(subscription.state, subscriptionsTemplateResource.properties.state);
99+
Assert.Equal(subscription.allowTracing, subscriptionsTemplateResource.properties.allowTracing);
100+
}
101+
}
102+
}

src/APIM_ARMTemplate/apimtemplate/Commands/Create.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ public CreateCommand()
6666
TagAPITemplateCreator tagAPITemplateCreator = new TagAPITemplateCreator();
6767
PolicyTemplateCreator policyTemplateCreator = new PolicyTemplateCreator(fileReader);
6868
ProductGroupTemplateCreator productGroupTemplateCreator = new ProductGroupTemplateCreator();
69+
SubscriptionTemplateCreator productSubscriptionsTemplateCreator = new SubscriptionTemplateCreator();
6970
DiagnosticTemplateCreator diagnosticTemplateCreator = new DiagnosticTemplateCreator();
7071
ReleaseTemplateCreator releaseTemplateCreator = new ReleaseTemplateCreator();
71-
ProductTemplateCreator productTemplateCreator = new ProductTemplateCreator(policyTemplateCreator, productGroupTemplateCreator);
72+
ProductTemplateCreator productTemplateCreator = new ProductTemplateCreator(policyTemplateCreator, productGroupTemplateCreator, productSubscriptionsTemplateCreator);
7273
PropertyTemplateCreator propertyTemplateCreator = new PropertyTemplateCreator();
7374
TagTemplateCreator tagTemplateCreator = new TagTemplateCreator();
7475
APITemplateCreator apiTemplateCreator = new APITemplateCreator(fileReader, policyTemplateCreator, productAPITemplateCreator, tagAPITemplateCreator, diagnosticTemplateCreator, releaseTemplateCreator);
@@ -185,4 +186,4 @@ public CreateCommand()
185186
});
186187
}
187188
}
188-
}
189+
}

src/APIM_ARMTemplate/apimtemplate/Common/Constants/ResourceTypeConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal static class ResourceTypeConstants
2323
public const string ProductTag = "Microsoft.ApiManagement/service/products/tags";
2424
public const string ProductPolicy = "Microsoft.ApiManagement/service/products/policies";
2525
public const string Property = "Microsoft.ApiManagement/service/properties";
26+
public const string Subscription = "Microsoft.ApiManagement/service/subscriptions";
2627
public const string Tag = "Microsoft.ApiManagement/service/tags";
2728
}
2829
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common
2+
{
3+
public class SubscriptionsTemplateResource: TemplateResource
4+
{
5+
public SubscriptionsTemplateProperties properties { get; set; }
6+
}
7+
8+
public class SubscriptionsTemplateProperties
9+
{
10+
public string ownerId { get; set; }
11+
public string scope { get; set; }
12+
public string displayName { get; set; }
13+
public string primaryKey { get; set; }
14+
public string secondaryKey { get; set; }
15+
public string state { get; set; }
16+
public bool? allowTracing { get; set; }
17+
}
18+
}

src/APIM_ARMTemplate/apimtemplate/Creator/Models/CreatorConfiguration.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@ public class CreatorConfig
2121
public List<APIVersionSetConfig> apiVersionSets { get; set; }
2222
public List<APIConfig> apis { get; set; }
2323
public List<ProductConfig> products { get; set; }
24-
public List<PropertyConfig> namedValues {get;set;}
24+
public List<PropertyConfig> namedValues { get; set; }
2525
public List<LoggerConfig> loggers { get; set; }
2626
public List<AuthorizationServerTemplateProperties> authorizationServers { get; set; }
2727
public List<BackendTemplateProperties> backends { get; set; }
2828
public List<TagTemplateProperties> tags { get; set; }
29+
public List<SubscriptionConfig> subscriptions { get; set; }
2930
public string outputLocation { get; set; }
3031
public bool linked { get; set; }
3132
public string linkedTemplatesBaseUrl { get; set; }
3233
public string linkedTemplatesUrlQueryString { get; set; }
3334
public string baseFileName { get; set; }
3435
}
3536

36-
public class APIVersionSetConfig: APIVersionSetProperties
37+
public class APIVersionSetConfig : APIVersionSetProperties
3738
{
3839
public string id { get; set; }
3940
}
@@ -93,11 +94,16 @@ public class ProductConfig : ProductsTemplateProperties
9394
public string policy { get; set; }
9495
// coma separated names
9596
public string groups { get; set; }
97+
public List<SubscriptionConfig> subscriptions { get; set; }
9698
}
9799

98100
public class PropertyConfig : PropertyResourceProperties
99101
{
100102

101103
}
102104

103-
}
105+
public class SubscriptionConfig : SubscriptionsTemplateProperties
106+
{
107+
public string name { get; set; }
108+
}
109+
}

src/APIM_ARMTemplate/apimtemplate/Creator/TemplateCreators/ProductTemplateCreator.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ public class ProductTemplateCreator : TemplateCreator
77
{
88
private PolicyTemplateCreator policyTemplateCreator;
99
private ProductGroupTemplateCreator productGroupTemplateCreator;
10+
private SubscriptionTemplateCreator subscriptionTemplateCreator;
1011

11-
public ProductTemplateCreator(PolicyTemplateCreator policyTemplateCreator, ProductGroupTemplateCreator productGroupTemplateCreator)
12+
public ProductTemplateCreator(
13+
PolicyTemplateCreator policyTemplateCreator,
14+
ProductGroupTemplateCreator productGroupTemplateCreator,
15+
SubscriptionTemplateCreator subscriptionTemplateCreator
16+
)
1217
{
1318
this.policyTemplateCreator = policyTemplateCreator;
1419
this.productGroupTemplateCreator = productGroupTemplateCreator;
20+
this.subscriptionTemplateCreator = subscriptionTemplateCreator;
1521
}
1622

1723
public Template CreateProductTemplate(CreatorConfig creatorConfig)
@@ -66,10 +72,18 @@ public Template CreateProductTemplate(CreatorConfig creatorConfig)
6672
List<ProductGroupsValue> productGroups = this.productGroupTemplateCreator.CreateProductGroupTemplateResources(product, dependsOn);
6773
resources.AddRange(productGroups);
6874
}
75+
76+
// create product subscriptions if provided
77+
if (product.subscriptions != null)
78+
{
79+
string[] dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/products', parameters('{ParameterNames.ApimServiceName}'), '{product.name}')]" };
80+
List<SubscriptionsTemplateResource> subscriptions = this.subscriptionTemplateCreator.CreateSubscriptionsTemplateResources(product, dependsOn);
81+
resources.AddRange(subscriptions);
82+
}
6983
}
7084

7185
productTemplate.resources = resources.ToArray();
7286
return productTemplate;
7387
}
7488
}
75-
}
89+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common;
4+
5+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create
6+
{
7+
public class SubscriptionTemplateCreator : TemplateCreator
8+
{
9+
public SubscriptionsTemplateResource CreateSubscriptionsTemplateResource(SubscriptionConfig subscription, string[] dependsOn)
10+
{
11+
return new SubscriptionsTemplateResource
12+
{
13+
name = $"[concat(parameters('ApimServiceName'), '/{subscription.name}')]",
14+
type = "Microsoft.ApiManagement/service/subscriptions",
15+
apiVersion = "2019-01-01",
16+
properties = new SubscriptionsTemplateProperties
17+
{
18+
ownerId = subscription.ownerId,
19+
scope = subscription.scope,
20+
displayName = subscription.displayName,
21+
primaryKey = subscription.primaryKey,
22+
secondaryKey = subscription.secondaryKey,
23+
state = subscription.state,
24+
allowTracing = subscription.allowTracing,
25+
},
26+
dependsOn = dependsOn,
27+
};
28+
}
29+
30+
public List<SubscriptionsTemplateResource> CreateSubscriptionsTemplateResources(ProductConfig product, string[] dependsOn)
31+
{
32+
// TODO: throw error if scope is not null
33+
34+
var scope = $"/products/{product.name}";
35+
36+
var resources = new List<SubscriptionsTemplateResource>(product.subscriptions.Count);
37+
38+
foreach (var subscription in product.subscriptions)
39+
{
40+
subscription.scope = scope;
41+
resources.Add(CreateSubscriptionsTemplateResource(subscription, dependsOn));
42+
}
43+
44+
return resources;
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)