Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Swarm Config Api #387

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 74 additions & 2 deletions src/Docker.DotNet/Endpoints/ISwarmOperations.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Docker.DotNet.Models;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Docker.DotNet
{
Expand Down Expand Up @@ -206,5 +206,77 @@ public interface ISwarmOperations
Task UpdateNodeAsync(string id, ulong version, NodeUpdateParameters parameters, CancellationToken cancellationToken = default(CancellationToken));

#endregion

#region Configs

/// <summary>
/// List configs
/// </summary>
/// <remarks>
/// 200 - No error.
/// 500 - Server error
/// 503 - Node is not part of a swarm
/// </remarks>
/// <returns></returns>
Task<IEnumerable<SwarmConfig>> ListConfigsAsync(ConfigsListParameters parameters = null, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Create a config
/// </summary>
/// <remarks>
/// 201 no error
/// 409 name conflicts with an existing object
/// 500 server error
/// 503 node is not part of a swarm
/// </remarks>
/// <param name="parameters"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<ConfigCreateResponse> CreateConfigAsync(SwarmCreateConfigParameters parameters, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Inspect a config
/// </summary>
/// <remarks>
/// 200 no error
/// 404 config not found
/// 500 server error
/// 503 node is not part of a swarm
/// </remarks>
/// <param name="id">ID of the config</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<SwarmConfig> InspectConfigAsync(string id, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Update a Config
/// </summary>
/// <remarks>
/// 200 no error
/// 400 bad parameter
/// 404 no such config
/// 500 server error
/// 503 node is not part of a swarm
/// </remarks>
/// <param name="id">The ID or name of the config</param>
/// <param name="parameters"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task UpdateConfigAsync(string id, ConfigUpdateParameters parameters, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Delete a config
/// </summary>
/// <remarks>
/// 204 no error
/// 404 config not found
/// 500 server error
/// 503 node is not part of a swarm
/// </remarks>
/// <param name="id">ID of the config</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task RemoveConfigAsync(string id, CancellationToken cancellationToken = default(CancellationToken));
#endregion
}
}
47 changes: 47 additions & 0 deletions src/Docker.DotNet/Endpoints/SwarmOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,52 @@ async Task ISwarmOperations.UpdateNodeAsync(string id, ulong version, NodeUpdate
var body = new JsonRequestContent<NodeUpdateParameters>(parameters ?? throw new ArgumentNullException(nameof(parameters)), this._client.JsonSerializer);
await this._client.MakeRequestAsync(new[] { SwarmResponseHandler }, HttpMethod.Post, $"nodes/{id}/update", query, body, cancellationToken);
}

async Task<IEnumerable<SwarmConfig>> ISwarmOperations.ListConfigsAsync(ConfigsListParameters parameters, CancellationToken cancellationToken)
{
var queryParameters = parameters != null ? new QueryString<ConfigsListParameters>(parameters) : null;
var response = await this._client
.MakeRequestAsync(new[] { SwarmResponseHandler }, HttpMethod.Get, $"configs", queryParameters, cancellationToken)
.ConfigureAwait(false);
return this._client.JsonSerializer.DeserializeObject<SwarmConfig[]>(response.Body);
}

async Task<ConfigCreateResponse> ISwarmOperations.CreateConfigAsync(SwarmCreateConfigParameters parameters, CancellationToken cancellationToken)
{
if (parameters == null) throw new ArgumentNullException(nameof(parameters));

var data = new JsonRequestContent<ConfigSpec>(parameters.Config ?? throw new ArgumentNullException(nameof(parameters.Config)), this._client.JsonSerializer);
//var data = new JsonRequestContent<SwarmCreateConfigParameters>(parameters ?? throw new ArgumentNullException(nameof(parameters)), this._client.JsonSerializer);
var response = await this._client
.MakeRequestAsync(new[] { SwarmResponseHandler }, HttpMethod.Post, "configs/create", null, data, cancellationToken)
.ConfigureAwait(false);
return this._client.JsonSerializer.DeserializeObject<ConfigCreateResponse>(response.Body);
}

async Task<SwarmConfig> ISwarmOperations.InspectConfigAsync(string id, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(id)) throw new ArgumentNullException(nameof(id));

var response = await this._client.MakeRequestAsync(new[] { SwarmResponseHandler }, HttpMethod.Get, $"configs/{id}", cancellationToken).ConfigureAwait(false);
return this._client.JsonSerializer.DeserializeObject<SwarmConfig>(response.Body);
}

async Task ISwarmOperations.UpdateConfigAsync(string id, ConfigUpdateParameters parameters, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(id)) throw new ArgumentNullException(nameof(id));
if (parameters == null) throw new ArgumentNullException(nameof(parameters));

var query = new QueryString<ConfigUpdateParameters>(parameters);
var body = new JsonRequestContent<ConfigSpec>(parameters.Config ?? throw new ArgumentNullException(nameof(parameters.Config)), this._client.JsonSerializer);
var response = await this._client.MakeRequestAsync(new[] { SwarmResponseHandler }, HttpMethod.Post, $"configs/{id}/update", query, body, cancellationToken).ConfigureAwait(false);
//this._client.JsonSerializer.DeserializeObject<ConfigUpdateResponse>(response.Body);
}

async Task ISwarmOperations.RemoveConfigAsync(string id, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(id)) throw new ArgumentNullException(nameof(id));

await this._client.MakeRequestAsync(new[] { SwarmResponseHandler }, HttpMethod.Delete, $"configs/{id}", cancellationToken).ConfigureAwait(false);
}
}
}
16 changes: 16 additions & 0 deletions src/Docker.DotNet/Models/ConfigCreateResponse.Generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Docker.DotNet.Models
{
[DataContract]
public class ConfigCreateResponse
{
[DataMember(Name = "ID", EmitDefaultValue = false)]
public string Id { get; set; }
}
}
41 changes: 41 additions & 0 deletions src/Docker.DotNet/Models/ConfigSpec.Generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Docker.DotNet.Models
{
[DataContract]
public class ConfigSpec
{
public ConfigSpec()
{
}

/// <summary>
/// User-defined name of the config
/// </summary>
[DataMember(Name = "Name", EmitDefaultValue = false)]
public string Name { get; set; }

/// <summary>
/// User-defined key/value metadata
/// </summary>
[DataMember(Name = "Labels", EmitDefaultValue = false)]
public IDictionary<string, string> Labels { get; set; }

/// <summary>
/// Base64-url-safe-encoded (RFC 4648) config data
/// </summary>
[DataMember(Name = "Data", EmitDefaultValue = false)]
public string Data { get; set; }

/// <summary>
/// Driver represents a driver (network, logging, secrets).
/// </summary>
[DataMember(Name = "Templating", EmitDefaultValue = false)]
public Templating Templating { get; set; }
}
}
25 changes: 25 additions & 0 deletions src/Docker.DotNet/Models/ConfigUpdateParameters.Generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Docker.DotNet.Models
{
[DataContract]
public class ConfigUpdateParameters
{
/// <summary>
/// The version number of the config object being updated. This is required to avoid conflicting writes
/// </summary>
[QueryStringParameter("version", true)]
public long Version { get; set; }

/// <summary>
///
/// </summary>
[DataMember(Name = "Config", EmitDefaultValue = false)]
public ConfigSpec Config { get; set; }
}
}
13 changes: 13 additions & 0 deletions src/Docker.DotNet/Models/ConfigUpdateResponse.Generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Docker.DotNet.Models
{
public class ConfigUpdateResponse
{

}
}
41 changes: 41 additions & 0 deletions src/Docker.DotNet/Models/ConfigsListParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Docker.DotNet.Models
{
public class ConfigsListParameters
{
[QueryStringParameter("filters", false, typeof(MapQueryStringConverter))]
public ConfigFilter Filters { get; set; }
}

public class ConfigFilter: Dictionary<string, string>
{
public string Id
{
get => this["id"];
set => this["id"] = value;
}

public string Label
{
get => this["label"];
set => this["label"] = value;
}

public string Name
{
get => this["name"];
set => this["name"] = value;
}

public string Names
{
get => this["names"];
set => this["names"] = value;
}
}
}
34 changes: 34 additions & 0 deletions src/Docker.DotNet/Models/SwarmConfig.Generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Docker.DotNet.Models
{
[DataContract]
public class SwarmConfig // (configs)
{

public SwarmConfig()
{

}

[DataMember(Name = "ID", EmitDefaultValue = false)]
public string ID { get; set; }

[DataMember(Name = "Version", EmitDefaultValue = false)]
public Version Version { get; set; }

[DataMember(Name = "CreatedAt", EmitDefaultValue = false)]
public DateTime CreatedAt { get; set; }

[DataMember(Name = "UpdatedAt", EmitDefaultValue = false)]
public DateTime UpdatedAt { get; set; }

[DataMember(Name = "Spec", EmitDefaultValue = false)]
public ConfigSpec Spec { get; set; }
}
}
21 changes: 21 additions & 0 deletions src/Docker.DotNet/Models/SwarmCreateConfigParameters.Generated.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Docker.DotNet.Models
{
[DataContract]
public class SwarmCreateConfigParameters
{
public SwarmCreateConfigParameters()
{

}

[DataMember(Name = "Config", EmitDefaultValue = false)]
public ConfigSpec Config { get; set; }
}
}
19 changes: 19 additions & 0 deletions src/Docker.DotNet/Models/Templating.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Docker.DotNet.Models
{
[DataContract]
public class Templating
{
[DataMember(Name = "Name", EmitDefaultValue = false)]
public string Name { get; set; }

[DataMember(Name = "Options", EmitDefaultValue = false)]
public IDictionary<string, string> Options { get; set; }
}
}