Skip to content

Commit

Permalink
Update KeycloakResource in labs
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianEdwards committed Apr 26, 2024
1 parent e6b3640 commit dda8343
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 182 deletions.
67 changes: 41 additions & 26 deletions labs/3-Add-Identity/end/eShop.AppHost/KeycloakResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,72 @@ internal static class KeycloakHostingExtensions
{
private const int DefaultContainerPort = 8080;

public static IResourceBuilder<KeycloakContainerResource> AddKeycloakContainer(
public static IResourceBuilder<TResource> WithReference<TResource>(this IResourceBuilder<TResource> builder,
IResourceBuilder<KeycloakResource> keycloakBuilder,
string env)
where TResource : IResourceWithEnvironment
{
builder.WithReference(keycloakBuilder);
builder.WithEnvironment(env, keycloakBuilder.Resource.ClientSecret);

return builder;
}

public static IResourceBuilder<KeycloakResource> AddKeycloakContainer(
this IDistributedApplicationBuilder builder,
string name,
int? port = null,
string? tag = null)
{
var keycloakContainer = new KeycloakContainerResource(name);
var keycloakContainer = new KeycloakResource(name)
{
ClientSecret = Guid.NewGuid().ToString("N")
};

return builder
var keycloak = builder
.AddResource(keycloakContainer)
.WithAnnotation(new ContainerImageAnnotation { Registry = "quay.io", Image = "keycloak/keycloak", Tag = tag ?? "latest" })
.WithHttpEndpoint(port: port, targetPort: DefaultContainerPort)
.WithEnvironment("KEYCLOAK_ADMIN", "admin")
.WithEnvironment("KEYCLOAK_ADMIN_PASSWORD", "admin")
.WithArgs("start-dev")
.WithManifestPublishingCallback(context => WriteKeycloakContainerToManifest(context, keycloakContainer));
.WithEnvironment("WEBAPP_CLIENT_SECRET", keycloakContainer.ClientSecret);

if (builder.ExecutionContext.IsRunMode)
{
keycloak.WithArgs("start-dev");
}
else
{
keycloak.WithArgs("start");
}

return keycloak;
}

public static IResourceBuilder<KeycloakContainerResource> ImportRealms(this IResourceBuilder<KeycloakContainerResource> builder, string source)
public static IResourceBuilder<KeycloakResource> ImportRealms(this IResourceBuilder<KeycloakResource> builder, string source)
{
builder
.WithBindMount(source, "/opt/keycloak/data/import")
.WithAnnotation(new CommandLineArgsCallbackAnnotation(args =>
{
// TODO: This could be cleaned up to make it properly compose with any other callers who customize args
args.Clear();
args.Add("start-dev");
if (builder.ApplicationBuilder.ExecutionContext.IsRunMode)
{
args.Add("start-dev");
}
else
{
args.Add("start");
}
args.Add("--import-realm");
}));

return builder;
}

private static async Task WriteKeycloakContainerToManifest(ManifestPublishingContext context, KeycloakContainerResource resource)
{
var manifestResource = new KeycloakContainerResource(resource.Name);

foreach (var annotation in resource.Annotations)
{
if (annotation is not CommandLineArgsCallbackAnnotation)
{
manifestResource.Annotations.Add(annotation);
}
}

// Set the container entry point to 'start' instead of 'start-dev'
manifestResource.Annotations.Add(new CommandLineArgsCallbackAnnotation(args => args.Add("start")));

await context.WriteContainerAsync(resource);
}
}

internal class KeycloakContainerResource(string name) : ContainerResource(name), IResourceWithServiceDiscovery
internal class KeycloakResource(string name) : ContainerResource(name), IResourceWithServiceDiscovery
{
public string? ClientSecret { get; set; }
}
67 changes: 41 additions & 26 deletions labs/3-Add-Identity/src/eShop.AppHost/KeycloakResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,72 @@ internal static class KeycloakHostingExtensions
{
private const int DefaultContainerPort = 8080;

public static IResourceBuilder<KeycloakContainerResource> AddKeycloakContainer(
public static IResourceBuilder<TResource> WithReference<TResource>(this IResourceBuilder<TResource> builder,
IResourceBuilder<KeycloakResource> keycloakBuilder,
string env)
where TResource : IResourceWithEnvironment
{
builder.WithReference(keycloakBuilder);
builder.WithEnvironment(env, keycloakBuilder.Resource.ClientSecret);

return builder;
}

public static IResourceBuilder<KeycloakResource> AddKeycloakContainer(
this IDistributedApplicationBuilder builder,
string name,
int? port = null,
string? tag = null)
{
var keycloakContainer = new KeycloakContainerResource(name);
var keycloakContainer = new KeycloakResource(name)
{
ClientSecret = Guid.NewGuid().ToString("N")
};

return builder
var keycloak = builder
.AddResource(keycloakContainer)
.WithAnnotation(new ContainerImageAnnotation { Registry = "quay.io", Image = "keycloak/keycloak", Tag = tag ?? "latest" })
.WithHttpEndpoint(port: port, targetPort: DefaultContainerPort)
.WithEnvironment("KEYCLOAK_ADMIN", "admin")
.WithEnvironment("KEYCLOAK_ADMIN_PASSWORD", "admin")
.WithArgs("start-dev")
.WithManifestPublishingCallback(context => WriteKeycloakContainerToManifest(context, keycloakContainer));
.WithEnvironment("WEBAPP_CLIENT_SECRET", keycloakContainer.ClientSecret);

if (builder.ExecutionContext.IsRunMode)
{
keycloak.WithArgs("start-dev");
}
else
{
keycloak.WithArgs("start");
}

return keycloak;
}

public static IResourceBuilder<KeycloakContainerResource> ImportRealms(this IResourceBuilder<KeycloakContainerResource> builder, string source)
public static IResourceBuilder<KeycloakResource> ImportRealms(this IResourceBuilder<KeycloakResource> builder, string source)
{
builder
.WithBindMount(source, "/opt/keycloak/data/import")
.WithAnnotation(new CommandLineArgsCallbackAnnotation(args =>
{
// TODO: This could be cleaned up to make it properly compose with any other callers who customize args
args.Clear();
args.Add("start-dev");
if (builder.ApplicationBuilder.ExecutionContext.IsRunMode)
{
args.Add("start-dev");
}
else
{
args.Add("start");
}
args.Add("--import-realm");
}));

return builder;
}

private static async Task WriteKeycloakContainerToManifest(ManifestPublishingContext context, KeycloakContainerResource resource)
{
var manifestResource = new KeycloakContainerResource(resource.Name);

foreach (var annotation in resource.Annotations)
{
if (annotation is not CommandLineArgsCallbackAnnotation)
{
manifestResource.Annotations.Add(annotation);
}
}

// Set the container entry point to 'start' instead of 'start-dev'
manifestResource.Annotations.Add(new CommandLineArgsCallbackAnnotation(args => args.Add("start")));

await context.WriteContainerAsync(resource);
}
}

internal class KeycloakContainerResource(string name) : ContainerResource(name), IResourceWithServiceDiscovery
internal class KeycloakResource(string name) : ContainerResource(name), IResourceWithServiceDiscovery
{
public string? ClientSecret { get; set; }
}
67 changes: 41 additions & 26 deletions labs/4-Add-Shopping-Basket/end/eShop.AppHost/KeycloakResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,72 @@ internal static class KeycloakHostingExtensions
{
private const int DefaultContainerPort = 8080;

public static IResourceBuilder<KeycloakContainerResource> AddKeycloakContainer(
public static IResourceBuilder<TResource> WithReference<TResource>(this IResourceBuilder<TResource> builder,
IResourceBuilder<KeycloakResource> keycloakBuilder,
string env)
where TResource : IResourceWithEnvironment
{
builder.WithReference(keycloakBuilder);
builder.WithEnvironment(env, keycloakBuilder.Resource.ClientSecret);

return builder;
}

public static IResourceBuilder<KeycloakResource> AddKeycloakContainer(
this IDistributedApplicationBuilder builder,
string name,
int? port = null,
string? tag = null)
{
var keycloakContainer = new KeycloakContainerResource(name);
var keycloakContainer = new KeycloakResource(name)
{
ClientSecret = Guid.NewGuid().ToString("N")
};

return builder
var keycloak = builder
.AddResource(keycloakContainer)
.WithAnnotation(new ContainerImageAnnotation { Registry = "quay.io", Image = "keycloak/keycloak", Tag = tag ?? "latest" })
.WithHttpEndpoint(port: port, targetPort: DefaultContainerPort)
.WithEnvironment("KEYCLOAK_ADMIN", "admin")
.WithEnvironment("KEYCLOAK_ADMIN_PASSWORD", "admin")
.WithArgs("start-dev")
.WithManifestPublishingCallback(context => WriteKeycloakContainerToManifest(context, keycloakContainer));
.WithEnvironment("WEBAPP_CLIENT_SECRET", keycloakContainer.ClientSecret);

if (builder.ExecutionContext.IsRunMode)
{
keycloak.WithArgs("start-dev");
}
else
{
keycloak.WithArgs("start");
}

return keycloak;
}

public static IResourceBuilder<KeycloakContainerResource> ImportRealms(this IResourceBuilder<KeycloakContainerResource> builder, string source)
public static IResourceBuilder<KeycloakResource> ImportRealms(this IResourceBuilder<KeycloakResource> builder, string source)
{
builder
.WithBindMount(source, "/opt/keycloak/data/import")
.WithAnnotation(new CommandLineArgsCallbackAnnotation(args =>
{
// TODO: This could be cleaned up to make it properly compose with any other callers who customize args
args.Clear();
args.Add("start-dev");
if (builder.ApplicationBuilder.ExecutionContext.IsRunMode)
{
args.Add("start-dev");
}
else
{
args.Add("start");
}
args.Add("--import-realm");
}));

return builder;
}

private static async Task WriteKeycloakContainerToManifest(ManifestPublishingContext context, KeycloakContainerResource resource)
{
var manifestResource = new KeycloakContainerResource(resource.Name);

foreach (var annotation in resource.Annotations)
{
if (annotation is not CommandLineArgsCallbackAnnotation)
{
manifestResource.Annotations.Add(annotation);
}
}

// Set the container entry point to 'start' instead of 'start-dev'
manifestResource.Annotations.Add(new CommandLineArgsCallbackAnnotation(args => args.Add("start")));

await context.WriteContainerAsync(resource);
}
}

internal class KeycloakContainerResource(string name) : ContainerResource(name), IResourceWithServiceDiscovery
internal class KeycloakResource(string name) : ContainerResource(name), IResourceWithServiceDiscovery
{
public string? ClientSecret { get; set; }
}
67 changes: 41 additions & 26 deletions labs/4-Add-Shopping-Basket/src/eShop.AppHost/KeycloakResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,72 @@ internal static class KeycloakHostingExtensions
{
private const int DefaultContainerPort = 8080;

public static IResourceBuilder<KeycloakContainerResource> AddKeycloakContainer(
public static IResourceBuilder<TResource> WithReference<TResource>(this IResourceBuilder<TResource> builder,
IResourceBuilder<KeycloakResource> keycloakBuilder,
string env)
where TResource : IResourceWithEnvironment
{
builder.WithReference(keycloakBuilder);
builder.WithEnvironment(env, keycloakBuilder.Resource.ClientSecret);

return builder;
}

public static IResourceBuilder<KeycloakResource> AddKeycloakContainer(
this IDistributedApplicationBuilder builder,
string name,
int? port = null,
string? tag = null)
{
var keycloakContainer = new KeycloakContainerResource(name);
var keycloakContainer = new KeycloakResource(name)
{
ClientSecret = Guid.NewGuid().ToString("N")
};

return builder
var keycloak = builder
.AddResource(keycloakContainer)
.WithAnnotation(new ContainerImageAnnotation { Registry = "quay.io", Image = "keycloak/keycloak", Tag = tag ?? "latest" })
.WithHttpEndpoint(port: port, targetPort: DefaultContainerPort)
.WithEnvironment("KEYCLOAK_ADMIN", "admin")
.WithEnvironment("KEYCLOAK_ADMIN_PASSWORD", "admin")
.WithArgs("start-dev")
.WithManifestPublishingCallback(context => WriteKeycloakContainerToManifest(context, keycloakContainer));
.WithEnvironment("WEBAPP_CLIENT_SECRET", keycloakContainer.ClientSecret);

if (builder.ExecutionContext.IsRunMode)
{
keycloak.WithArgs("start-dev");
}
else
{
keycloak.WithArgs("start");
}

return keycloak;
}

public static IResourceBuilder<KeycloakContainerResource> ImportRealms(this IResourceBuilder<KeycloakContainerResource> builder, string source)
public static IResourceBuilder<KeycloakResource> ImportRealms(this IResourceBuilder<KeycloakResource> builder, string source)
{
builder
.WithBindMount(source, "/opt/keycloak/data/import")
.WithAnnotation(new CommandLineArgsCallbackAnnotation(args =>
{
// TODO: This could be cleaned up to make it properly compose with any other callers who customize args
args.Clear();
args.Add("start-dev");
if (builder.ApplicationBuilder.ExecutionContext.IsRunMode)
{
args.Add("start-dev");
}
else
{
args.Add("start");
}
args.Add("--import-realm");
}));

return builder;
}

private static async Task WriteKeycloakContainerToManifest(ManifestPublishingContext context, KeycloakContainerResource resource)
{
var manifestResource = new KeycloakContainerResource(resource.Name);

foreach (var annotation in resource.Annotations)
{
if (annotation is not CommandLineArgsCallbackAnnotation)
{
manifestResource.Annotations.Add(annotation);
}
}

// Set the container entry point to 'start' instead of 'start-dev'
manifestResource.Annotations.Add(new CommandLineArgsCallbackAnnotation(args => args.Add("start")));

await context.WriteContainerAsync(resource);
}
}

internal class KeycloakContainerResource(string name) : ContainerResource(name), IResourceWithServiceDiscovery
internal class KeycloakResource(string name) : ContainerResource(name), IResourceWithServiceDiscovery
{
public string? ClientSecret { get; set; }
}
Loading

0 comments on commit dda8343

Please sign in to comment.