aboutsummaryrefslogtreecommitdiffstats
path: root/arthur/exts/kubernetes/certificates.py
blob: a2383da6e5b6ae227604e69130e768a471f69645 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""The Certificates cog helps with managing TLS certificates."""

from textwrap import dedent

from discord.ext import commands
from tabulate import tabulate

from arthur.apis.kubernetes import certificates
from arthur.bot import KingArthur


class Certificates(commands.Cog):
    """Commands for working with TLS certificates."""

    def __init__(self, bot: KingArthur) -> None:
        self.bot = bot

    @commands.group(name="certificates", aliases=["certs"], invoke_without_command=True)
    async def certificates(self, ctx: commands.Context) -> None:
        """Commands for working with TLS certificates."""
        await ctx.send_help(ctx.command)

    @certificates.command(name="list", aliases=["ls"])
    async def certificates_list(self, ctx: commands.Context, namespace: str = "default") -> None:
        """List TLS certificates in the selected namespace (defaults to default)."""
        certs = await certificates.list_certificates(namespace)

        table_data = [
            [
                certificate["metadata"]["name"],
                ", ".join(certificate["spec"]["dnsNames"]),
                certificate["spec"]["issuerRef"]["name"],
                certificate["status"]["conditions"][0]["message"],
            ]
            for certificate in certs["items"]
        ]

        table = tabulate(
            table_data, headers=["Name", "DNS Names", "Issuer", "Status"], tablefmt="psql"
        )

        return_message = dedent("""
            **Certificates in namespace `{0}`**
            ```
            {1}
            ```
            """)

        await ctx.send(return_message.format(namespace, table))


async def setup(bot: KingArthur) -> None:
    """Add the extension to the bot."""
    await bot.add_cog(Certificates(bot))