Skip to content

Commit

Permalink
Fix ComposeBuilder WithVolumes (#80)
Browse files Browse the repository at this point in the history
* Fix ComposeBuilder WithVolumes

* Refactor ComposeBuilder

* Implement IObject in Service model

* Use IDictionary as a type
  • Loading branch information
trejjam authored Nov 8, 2023
1 parent aebfc8e commit f25a86f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 73 deletions.
4 changes: 4 additions & 0 deletions src/DockerComposeBuilder.Examples.Complex/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
var dbPass = "pass";
var dbName = "wordpress";

var certificatesVolume = Builder.MakeVolume("certificates")
.Build();

var network1 = Builder.MakeNetwork("my-net")
.SetExternal(true)
.Build();
Expand Down Expand Up @@ -66,6 +69,7 @@
.WithServices(mysql, wordpress)
.WithNetworks(network1, network2)
.WithSecrets(secret1)
.WithVolumes(certificatesVolume)
.Build();

// serialize our object graph to yaml for writing to a docker-compose file
Expand Down
112 changes: 44 additions & 68 deletions src/DockerComposeBuilder/Builders/ComposeBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DockerComposeBuilder.Builders.Base;
using DockerComposeBuilder.Interfaces;
using DockerComposeBuilder.Model;
using System;
using System.Collections.Generic;
Expand All @@ -14,100 +15,75 @@ public ComposeBuilder WithVersion(string version)
}

/// <summary>
/// Add services to the Compose object
/// Add services to the Compose object
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public ComposeBuilder WithServices(params Service[] services)
{
if (WorkingObject.Services == null)
{
WorkingObject.Services = new Dictionary<string, Service>();
}

foreach (var service in services)
{
if (WorkingObject.Services.ContainsKey(service.Name))
{
throw new Exception("Service name already added to services, please pick a unique one!");
}

WorkingObject.Services.Add(service.Name, service);
}

return this;
}
public ComposeBuilder WithServices(params Service[] services) => WithT(
() => WorkingObject.Services,
x => WorkingObject.Services = x,
services
);

/// <summary>
/// Add networks to the compose object
/// Add networks to the compose object
/// </summary>
/// <param name="networks"></param>
/// <returns></returns>
public ComposeBuilder WithNetworks(params Network[] networks)
{
if (WorkingObject.Networks == null)
{
WorkingObject.Networks = new Dictionary<string, Network>();
}

foreach (var network in networks)
{
if (WorkingObject.Networks.ContainsKey(network.Name))
{
throw new Exception("Network name already added to networks, please pick a unique one!");
}

WorkingObject.Networks.Add(network.Name, network);
}

return this;
}
public ComposeBuilder WithNetworks(params Network[] networks) => WithT(
() => WorkingObject.Networks,
x => WorkingObject.Networks = x,
networks
);

/// <summary>
/// Add volumes to the compose object
/// Add volumes to the compose object
/// </summary>
/// <param name="volumes"></param>
/// <returns></returns>
public ComposeBuilder WithVolumes(params Volume[] volumes)
{
if (WorkingObject.Networks == null)
{
WorkingObject.Volumes = new Dictionary<string, Volume>();
}

foreach (var volume in volumes)
{
if (WorkingObject.Volumes!.ContainsKey(volume.Name))
{
throw new Exception("Volume name already added to volumes, please pick a unique one!");
}

WorkingObject.Volumes.Add(volume.Name, volume);
}

return this;
}
public ComposeBuilder WithVolumes(params Volume[] volumes) => WithT(
() => WorkingObject.Volumes,
x => WorkingObject.Volumes = x,
volumes
);

/// <summary>
/// Add secrets to the compose object
/// Add secrets to the compose object
/// </summary>
/// <param name="secrets"></param>
/// <returns></returns>
public ComposeBuilder WithSecrets(params Secret[] secrets)
public ComposeBuilder WithSecrets(params Secret[] secrets) => WithT(
() => WorkingObject.Secrets,
x => WorkingObject.Secrets = x,
secrets
);

/// <summary>
/// Add services to the Compose object
/// </summary>
/// <returns></returns>
private ComposeBuilder WithT<T>(
Func<IDictionary<string, T>?> getCollection,
Action<IDictionary<string, T>> setCollection,
params T[] parameters
) where T : IObject
{
if (WorkingObject.Secrets == null)
var collection = getCollection();

if (collection == null)
{
WorkingObject.Secrets = new Dictionary<string, Secret>();
collection = new Dictionary<string, T>();
setCollection(collection);
}

foreach (var secret in secrets)
foreach (var parameter in parameters)
{
if (WorkingObject.Secrets.ContainsKey(secret.Name))
if (collection.ContainsKey(parameter.Name))
{
throw new Exception("Secret name already added to secrets, please pick a unique one!");
throw new Exception($"{typeof(T).Name} name ('{parameter.Name}') already added to the target collection, please pick a unique one!");
}

WorkingObject.Secrets.Add(secret.Name, secret);
collection.Add(parameter.Name, parameter);
}

return this;
Expand Down
8 changes: 4 additions & 4 deletions src/DockerComposeBuilder/Model/Compose.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public class Compose
public string? Version { get; set; }

[YamlMember(Alias = "services")]
public Dictionary<string, Service>? Services { get; set; }
public IDictionary<string, Service>? Services { get; set; }

[YamlMember(Alias = "networks")]
public Dictionary<string, Network>? Networks { get; set; }
public IDictionary<string, Network>? Networks { get; set; }

[YamlMember(Alias = "secrets")]
public Dictionary<string, Secret>? Secrets { get; set; }
public IDictionary<string, Secret>? Secrets { get; set; }

[YamlMember(Alias = "volumes")]
public Dictionary<string, Volume>? Volumes { get; set; }
public IDictionary<string, Volume>? Volumes { get; set; }
}
3 changes: 2 additions & 1 deletion src/DockerComposeBuilder/Model/Service.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DockerComposeBuilder.Enums;
using DockerComposeBuilder.Interfaces;
using DockerComposeBuilder.Model.Services;
using System;
using System.Collections.Generic;
Expand All @@ -7,7 +8,7 @@
namespace DockerComposeBuilder.Model;

[Serializable]
public class Service
public class Service : IObject
{
[YamlIgnore]
public string Name { get; set; } = null!;
Expand Down

0 comments on commit f25a86f

Please sign in to comment.