Skip to content

Commit

Permalink
Secrets, Configs: cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Binali Rustamov committed Nov 16, 2023
1 parent 8d52482 commit 25ac4ac
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 1 deletion.
26 changes: 26 additions & 0 deletions PortainerClient/Api/EndpointsApiService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using PortainerClient.Api.Base;
using PortainerClient.Api.Model;

namespace PortainerClient.Api;

/// <summary>
/// API implementation for endpoints API
/// </summary>
public class EndpointsApiService : BaseApiService
{
/// <summary>
/// Get all configs
/// </summary>
/// <param name="endpointId"></param>
/// <returns>List of available configs</returns>
public IEnumerable<DockerDto> GetConfigs(int endpointId) =>
Get<List<DockerDto>>($"endpoints/{endpointId}/docker/configs");
/// <summary>
/// Get all secrets
/// </summary>
/// <param name="endpointId"></param>
/// <returns>List of available secrets</returns>
public IEnumerable<DockerDto> GetSecrets(int endpointId) =>
Get<List<DockerDto>>($"endpoints/{endpointId}/docker/secrets");
}
13 changes: 13 additions & 0 deletions PortainerClient/Api/Model/ConfigSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;

namespace PortainerClient.Api.Model;

/// <summary>
/// Spec of Docker Swarm Config instance
/// </summary>
public class ConfigSpec
{
public string Data { get; set; }
public Dictionary<string, object> Labels { get; set; }
public string Name { get; set; }
}
16 changes: 16 additions & 0 deletions PortainerClient/Api/Model/DockerDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace PortainerClient.Api.Model;

/// <summary>
/// Docker specific instance info
/// </summary>
public class DockerDto
{
public DateTime CreatedAt { get; set; }
public string ID { get; set; }
public Portainer Portainer { get; set; }
public ConfigSpec Spec { get; set; }
public DateTime UpdatedAt { get; set; }
public Version Version { get; set; }
}
12 changes: 12 additions & 0 deletions PortainerClient/Api/Model/Portainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace PortainerClient.Api.Model;

/// <summary>
/// Represents info about Docker Resource in Portainer
/// </summary>
public class Portainer
{
/// <summary>
/// Resource control information from portainer
/// </summary>
public ResourceControl ResourceControl { get; set; }
}
9 changes: 9 additions & 0 deletions PortainerClient/Api/Model/Version.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace PortainerClient.Api.Model;

/// <summary>
/// Version of object
/// </summary>
public class Version
{
public int Index { get; set; }
}
16 changes: 16 additions & 0 deletions PortainerClient/Command/Configs/ConfigsCmd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using McMaster.Extensions.CommandLineUtils;
using PortainerClient.Command.Stack;
using PortainerClient.Helpers;

namespace PortainerClient.Command.Configs;

/// <summary>
/// CMD command for Configs
/// </summary>
[Command(Name = "configs", Description = "Docker Swarm Configs management commands")]
[Subcommand(typeof(ConfigsLsCmd))]
public class ConfigsCmd : ICommand
{
/// <inheritdoc />
public int OnExecute(CommandLineApplication app, IConsole console) => CmdHelpers.SpecifyCommandResult(app, console);
}
32 changes: 32 additions & 0 deletions PortainerClient/Command/Configs/ConfigsLsCmd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using McMaster.Extensions.CommandLineUtils;
using PortainerClient.Api;
using YamlDotNet.Serialization;

namespace PortainerClient.Command.Configs;

/// <summary>
/// CMD command for Configs list operation
/// </summary>
[Command(Name = "ls", Description = "List all configs")]
public class ConfigsLsCmd: BaseApiCommand<EndpointsApiService>
{
/// <summary>
/// Endpoint identifier
/// </summary>
[Argument(order: 0, name: "endpoint-id", description: "ID of endpoint used to deploy (get it from Portainer)")]
[Required]
public int EndpointId { get; set; }

/// <inheritdoc />
protected override void Do(CommandLineApplication app, IConsole console)
{
var list = ApiClient.GetConfigs(EndpointId).Select(c=> c.Spec);
var yamlSerializer = new Serializer();
Console.WriteLine("--- CONFIGS ---");
Console.WriteLine(yamlSerializer.Serialize(list));
Console.WriteLine($"--- Total count: {list.Count()} ---");
}
}
16 changes: 16 additions & 0 deletions PortainerClient/Command/Secrets/SecretsCmd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using McMaster.Extensions.CommandLineUtils;
using PortainerClient.Command.Configs;
using PortainerClient.Helpers;

namespace PortainerClient.Command.Secrets;

/// <summary>
/// CMD command for Secrets
/// </summary>
[Command(Name = "secrets", Description = "Docker Swarm Secrets management commands")]
[Subcommand(typeof(SecretsLsCmd))]
public class SecretsCmd : ICommand
{
/// <inheritdoc />
public int OnExecute(CommandLineApplication app, IConsole console) => CmdHelpers.SpecifyCommandResult(app, console);
}
32 changes: 32 additions & 0 deletions PortainerClient/Command/Secrets/SecretsLsCmd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using McMaster.Extensions.CommandLineUtils;
using PortainerClient.Api;
using YamlDotNet.Serialization;

namespace PortainerClient.Command.Configs;

/// <summary>
/// CMD command for Secrets list operation
/// </summary>
[Command(Name = "ls", Description = "List all secrets")]
public class SecretsLsCmd: BaseApiCommand<EndpointsApiService>
{
/// <summary>
/// Endpoint identifier
/// </summary>
[Argument(order: 0, name: "endpoint-id", description: "ID of endpoint used to deploy (get it from Portainer)")]
[Required]
public int EndpointId { get; set; }

/// <inheritdoc />
protected override void Do(CommandLineApplication app, IConsole console)
{
var list = ApiClient.GetSecrets(EndpointId).Select(c=> c.Spec);
var yamlSerializer = new Serializer();
Console.WriteLine("--- SECRETS ---");
Console.WriteLine(yamlSerializer.Serialize(list));
Console.WriteLine($"--- Total count: {list.Count()} ---");
}
}
4 changes: 3 additions & 1 deletion PortainerClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
using McMaster.Extensions.CommandLineUtils;
using PortainerClient.Command;
using PortainerClient.Command.Auth;
using PortainerClient.Command.Configs;
using PortainerClient.Command.Secrets;
using PortainerClient.Command.Stack;
using PortainerClient.Helpers;

namespace PortainerClient
{
[Command("portainerctl", Description = "Console client for Portainer (2.19.x) by DotNetNomads :)")]
[Subcommand(typeof(AuthCmd), typeof(StackCmd))]
[Subcommand(typeof(AuthCmd), typeof(StackCmd), typeof(ConfigsCmd), typeof(SecretsCmd))]
[HelpOption]
class Program : ICommand
{
Expand Down

0 comments on commit 25ac4ac

Please sign in to comment.