-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from DotNetNomads/create-secrets-and-configs
WIP: Secrets, Configs: cmd
- Loading branch information
Showing
29 changed files
with
765 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
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"); | ||
|
||
/// <summary> | ||
/// Get list of available endpoints | ||
/// </summary> | ||
/// <returns>List of endpoints</returns> | ||
public IEnumerable<Endpoint> GetEndpoints() => | ||
Get<List<Endpoint>>("endpoints", false, ("type", 2)).Select(e => | ||
{ | ||
e.SwarmId = Get<EndpointSwarm>($"endpoints/{e.Id}/docker/swarm").Id; | ||
return e; | ||
} | ||
); | ||
|
||
/// <summary> | ||
/// Create new secret in specific Swarm endpoint | ||
/// </summary> | ||
/// <param name="secretName"></param> | ||
/// <param name="fileContent"></param> | ||
/// <param name="memberships"></param> | ||
/// <param name="endpoint"></param> | ||
/// <param name="debug"></param> | ||
public void CreateSecret(string secretName, string fileContent, IEnumerable<Membership>? memberships, | ||
Endpoint endpoint, bool debug = false) | ||
{ | ||
var secretSpec = new ConfigSpec | ||
{ | ||
Name = secretName, | ||
Data = Convert.ToBase64String(Encoding.UTF8.GetBytes(fileContent)) | ||
}; | ||
var secret = Post<DockerDto>($"endpoints/{endpoint.Id}/docker/secrets/create", debug, | ||
("", secretSpec, ParamType.JsonBody)); | ||
SetAcl(memberships, debug, secret.Portainer.ResourceControl.Id); | ||
} | ||
|
||
/// <summary> | ||
/// Create new config in specific Swarm endpoint | ||
/// </summary> | ||
/// <param name="configName"></param> | ||
/// <param name="fileContent"></param> | ||
/// <param name="memberships"></param> | ||
/// <param name="endpoint"></param> | ||
/// <param name="debug"></param> | ||
public void CreateConfig(string configName, string fileContent, IEnumerable<Membership>? memberships, Endpoint endpoint, bool debug) | ||
{ | ||
var configSec = new ConfigSpec | ||
{ | ||
Name = configName, | ||
Data = Convert.ToBase64String(Encoding.UTF8.GetBytes(fileContent)) | ||
}; | ||
var secret = Post<DockerDto>($"endpoints/{endpoint.Id}/docker/configs/create", debug, | ||
("", configSec, ParamType.JsonBody)); | ||
SetAcl(memberships, debug, secret.Portainer.ResourceControl.Id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace PortainerClient.Api.Model; | ||
|
||
/// <summary> | ||
/// Portainer Endpoint | ||
/// </summary> | ||
public class Endpoint | ||
{ | ||
/// <summary> | ||
/// Identifier | ||
/// </summary> | ||
public int Id { get; set; } | ||
/// <summary> | ||
/// Name in portainer's interface | ||
/// </summary> | ||
public string Name { get; set; } = null!; | ||
|
||
/// <summary> | ||
/// Identifier of swarm cluster | ||
/// </summary> | ||
public string SwarmId { get; set; } = null!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace PortainerClient.Api.Model; | ||
|
||
/// <summary> | ||
/// Swarm info for endpoint | ||
/// </summary> | ||
public class EndpointSwarm | ||
{ | ||
/// <summary> | ||
/// Swarm identifier | ||
/// </summary> | ||
public string Id { get; set; } = null!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace PortainerClient.Api.Model; | ||
|
||
/// <summary> | ||
/// Membership entity | ||
/// </summary> | ||
public class Membership | ||
{ | ||
/// <summary> | ||
/// Team ID | ||
/// </summary> | ||
public int TeamId { get; set; } | ||
/// <summary> | ||
/// Role in team | ||
/// </summary> | ||
public int Role { get; set; } | ||
/// <summary> | ||
/// Team name | ||
/// </summary> | ||
public string TeamName { get; set; } = null!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace PortainerClient.Api.Model; | ||
|
||
/// <summary> | ||
/// Model to change resource control settings | ||
/// </summary> | ||
public class ResourceControlRequest | ||
{ | ||
/// <summary> | ||
/// Allow only for admins | ||
/// </summary> | ||
public bool AdministratorsOnly { get; set; } | ||
/// <summary> | ||
/// Accessible for any user | ||
/// </summary> | ||
public bool Public { get; set; } | ||
/// <summary> | ||
/// Users have access | ||
/// </summary> | ||
public IEnumerable<int> Users { get; set; } | ||
/// <summary> | ||
/// Teams have access | ||
/// </summary> | ||
public IEnumerable<int> Teams { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace PortainerClient.Api.Model; | ||
|
||
/// <summary> | ||
/// Team info | ||
/// </summary> | ||
public class Team | ||
{ | ||
/// <summary> | ||
/// Identifier of Team | ||
/// </summary> | ||
public int Id { get; set; } | ||
/// <summary> | ||
/// Team name | ||
/// </summary> | ||
public string Name { get; set; } = null!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using PortainerClient.Api.Base; | ||
using PortainerClient.Api.Model; | ||
|
||
namespace PortainerClient.Api; | ||
|
||
/// <summary> | ||
/// Different APIs of Portainer | ||
/// </summary> | ||
public class PortainerApiService : BaseApiService | ||
{ | ||
/// <summary> | ||
/// Get memberships of user | ||
/// </summary> | ||
/// <param name="userId">User identifier</param> | ||
/// <returns>List of memberships</returns> | ||
public IEnumerable<Membership> GetMemberships(int userId) | ||
{ | ||
var teams = Get<List<Team>>("teams"); | ||
return Get<List<Membership>>($"users/{userId}/memberships").Select(m => | ||
{ | ||
m.TeamName = teams.First(t => t.Id == m.TeamId).Name; | ||
return m; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using McMaster.Extensions.CommandLineUtils; | ||
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), typeof(ConfigsCreateCmd))] | ||
public class ConfigsCmd : ICommand | ||
{ | ||
/// <inheritdoc /> | ||
public int OnExecute(CommandLineApplication app, IConsole console) => CmdHelpers.SpecifyCommandResult(app, console); | ||
} |
Oops, something went wrong.