Skip to content

Commit

Permalink
Add option for mangement endpoints (aka actuator) [#15]
Browse files Browse the repository at this point in the history
  • Loading branch information
ccheetham committed May 24, 2021
1 parent 0584d43 commit f3fcbc6
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
"Framework": {
"longName": "framework"
},
"ManagementEndpoints": {
"longName": "actuator",
"shortName": ""
},
"PlaceholderConfiguration": {
"longName": "placeholder",
"shortName": ""
Expand Down
6 changes: 6 additions & 0 deletions src/DotNetNew.WebApi/CSharp/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@
"pattern": "^netcoreapp2.1$"
}
},
"ManagementEndpoints": {
"type": "parameter",
"datatype": "bool",
"description": "Add endpoints to manage your application, such as health, metrics, etc.",
"defaultValue": "false"
},
"MicrosoftAzureSpringCloudClientVersion": {
"type": "generated",
"generator": "switch",
Expand Down
4 changes: 3 additions & 1 deletion src/DotNetNew.WebApi/CSharp/Company.WebApplication1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="$(NetCoreApp21Version)" />
</ItemGroup>
<!--#endif -->
<!--#if (CloudConfigClient || CloudFoundryHosting || DynamicLogger || PlaceholderConfiguration || RandomValueConfiguration) -->
<!--#if (CloudConfigClient || CloudFoundryHosting || DynamicLogger || ManagementEndpoints || PlaceholderConfiguration || RandomValueConfiguration) -->

<ItemGroup>
<PackageReference Include="Steeltoe.Common.Hosting" Condition="'$(CloudFoundryHosting)'" Version="$(SteeltoeVersion)" />
Expand All @@ -36,6 +36,8 @@
<PackageReference Include="Steeltoe.Extensions.Configuration.PlaceholderCore" Condition="'$(PlaceholderConfiguration)' == 'True'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Extensions.Configuration.RandomValueBase" Condition="'$(RandomValueConfiguration)' == 'True'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Extensions.Logging.DynamicLogger" Condition="'$(CloudFoundryHosting)' == 'True' OR '$(DynamicLogger)' == 'True'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Management.EndpointCore" Condition="'$(ManagementEndpoints)' == 'True' AND '$(Steeltoe2)' != 'True'" Version="$(SteeltoeVersion)" />
<PackageReference Include="Steeltoe.Management.CloudFoundryCore" Condition="'$(ManagementEndpoints)' == 'True' AND '$(Steeltoe2)' == 'True'" Version="$(SteeltoeVersion)" />
</ItemGroup>
<!--#endif -->
<!--#if (AzureSpringCloudHosting && !FrameworkNetCoreApp21) -->
Expand Down
1 change: 1 addition & 0 deletions src/DotNetNew.WebApi/CSharp/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<DynamicLogger>false</DynamicLogger>
<FrameworkNet50>true</FrameworkNet50>
<FrameworkNetCoreApp21>false</FrameworkNetCoreApp21>
<ManagementEndpoints>false</ManagementEndpoints>
<MicrosoftAzureSpringCloudClientVersion>2.0.0-preview.1</MicrosoftAzureSpringCloudClientVersion>
<PlaceholderConfiguration>false</PlaceholderConfiguration>
<RandomValueConfiguration>false</RandomValueConfiguration>
Expand Down
20 changes: 20 additions & 0 deletions src/DotNetNew.WebApi/CSharp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
#if (CloudFoundryHosting)
using Steeltoe.Extensions.Configuration.CloudFoundry;
#endif
#if (ManagementEndpoints)
#if (Steeltoe2)
using Steeltoe.Management.CloudFoundry;
#else
using Steeltoe.Management.Endpoint;
#endif
#endif

namespace Company.WebApplication1
{
Expand All @@ -34,6 +41,13 @@ public void ConfigureServices(IServiceCollection services)
#if (CloudFoundryHosting)
services.ConfigureCloudFoundryOptions(Configuration);
#endif
#if (ManagementEndpoints)
#if (Steeltoe2)
services.AddCloudFoundryActuators(Configuration);
#else
services.AddAllActuators(Configuration);
#endif
#endif
#if (FrameworkNetCoreApp21)
services.AddMvc();
#else
Expand All @@ -56,6 +70,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseDeveloperExceptionPage();
}

#if (ManagementEndpoints)
app.UseCloudFoundryActuators();
#endif
app.UseMvc();
}
#else
Expand All @@ -72,6 +89,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseHttpsRedirection();
app.UseRouting();
#if (ManagementEndpoints && Steeltoe2)
app.UseCloudFoundryActuators();
#endif
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
#endif
Expand Down
91 changes: 91 additions & 0 deletions test/DotNetNew.SteeltoeWebApi.Test/ActuatorOptionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using FluentAssertions;
using Steeltoe.DotNetNew.Test.Utilities.Assertions;
using Xunit;
using Xunit.Abstractions;

namespace Steeltoe.DotNetNew.SteeltoeWebApi.Test
{
public class ActuatorOptionTest : Test
{
public ActuatorOptionTest(ITestOutputHelper logger) : base("actuator", logger)
{
}

[Fact]
public override async void TestHelp()
{
using var sandbox = await TemplateSandbox("--help");
sandbox.CommandOutput.Should().ContainSnippet(@"
--actuator Add endpoints to manage your application, such as health, metrics, etc.
bool - Optional
Default: false
");
}

[Fact]
public async void TestCsproj()
{
using var sandbox = await TemplateSandbox();
var xDoc = await sandbox.GetXmlDocumentAsync($"{sandbox.Name}.csproj");
var expectedPackageRefs = new List<string>
{
"Steeltoe.Management.EndpointCore",
};
var actualPackageRefs =
(
from e in xDoc.Elements().Elements("ItemGroup").Elements("PackageReference").Attributes("Include")
select e.Value
).ToList();
actualPackageRefs.Should().Contain(expectedPackageRefs);
}

[Fact]
public async void TestCsprojSteeltoe2()
{
using var sandbox = await TemplateSandbox("--steeltoe 2.5.3");
var xDoc = await sandbox.GetXmlDocumentAsync($"{sandbox.Name}.csproj");
var expectedPackageRefs = new List<string>
{
"Steeltoe.Management.CloudFoundryCore",
};
var actualPackageRefs =
(
from e in xDoc.Elements().Elements("ItemGroup").Elements("PackageReference").Attributes("Include")
select e.Value
).ToList();
actualPackageRefs.Should().Contain(expectedPackageRefs);
}

[Fact]
public async void TestStartupCs()
{
using var sandbox = await TemplateSandbox();
var programSource = await sandbox.GetFileTextAsync("Startup.cs");
programSource.Should().ContainSnippet("Steeltoe.Management.Endpoint;");
programSource.Should().ContainSnippet("services.AddAllActuators(Configuration);");
}

[Fact]
public async void TestStartupCsSteeltoe2()
{
using var sandbox = await TemplateSandbox("--steeltoe 2.5.3");
var programSource = await sandbox.GetFileTextAsync("Startup.cs");
programSource.Should().ContainSnippet("Steeltoe.Management.CloudFoundry;");
programSource.Should().ContainSnippet("services.AddCloudFoundryActuators(Configuration);");
programSource.Should().ContainSnippet("app.UseCloudFoundryActuators()");
}

[Fact]
public async void TestStartupCsSteeltoe2NetCoreApp21()
{
using var sandbox = await TemplateSandbox("--steeltoe 2.5.3 --framework netcoreapp2.1");
var programSource = await sandbox.GetFileTextAsync("Startup.cs");
programSource.Should().ContainSnippet("Steeltoe.Management.CloudFoundry;");
programSource.Should().ContainSnippet("services.AddCloudFoundryActuators(Configuration);");
programSource.Should().ContainSnippet("app.UseCloudFoundryActuators()");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async void TestStartupCs()
using var sandbox = await TemplateSandbox();
var programSource = await sandbox.GetFileTextAsync("Startup.cs");
programSource.Should().ContainSnippet("Steeltoe.Extensions.Configuration.CloudFoundry;");
programSource.Should().ContainSnippet(".ConfigureCloudFoundryOptions(Configuration);");
programSource.Should().ContainSnippet("services.ConfigureCloudFoundryOptions(Configuration);");
}

[Fact]
Expand Down

0 comments on commit f3fcbc6

Please sign in to comment.