Skip to content

Commit

Permalink
databases: add get-ca to retrieve db certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
loosla committed Sep 11, 2024
1 parent 798111c commit e66b4ce
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
24 changes: 24 additions & 0 deletions commands/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func Databases() *Command {
- The date and time when the database cluster was created`+databaseListDetails, Writer, aliasOpt("g"), displayerType(&displayers.Databases{}))
cmdDatabaseGet.Example = `The following example retrieves the details for a database cluster with the ID ` + "`" + `f81d4fae-7dec-11d0-a765-00a0c91e6bf6` + "`" + ` and uses the ` + "`" + `--format` + "`" + ` flag to return only the database's ID, engine, and engine version: doctl databases get f81d4fae-7dec-11d0-a765-00a0c91e6bf6`

// TODO: update draft descriptions, examples when ready
cmdDatabaseGetCA := CmdBuilder(cmd, RunDatabaseGetCA, "get-ca", "Provides the CA certificate for a DigitalOcean database", `Retrieves a list of database clusters and their following details:`+clusterDetails, Writer, aliasOpt("ls"), displayerType(&displayers.Databases{}))
cmdDatabaseGetCA.Example = `The following example lists all database associated with your account and uses the ` + "`" + `--format` + "`" + ` flag to return only the ID, engine, and engine version of each database: doctl databases list --format ID,Engine,Version`

nodeSizeDetails := "The size of the nodes in the database cluster, for example `db-s-1vcpu-1gb` indicates a 1 CPU, 1GB node. For a list of available size slugs, visit: https://docs.digitalocean.com/reference/api/api-reference/#tag/Databases"
nodeNumberDetails := "The number of nodes in the database cluster. Valid values are 1-3. In addition to the primary node, up to two standby nodes may be added for high availability."
storageSizeMiBDetails := "The amount of disk space allocated to the cluster. Applicable for PostgreSQL and MySQL clusters. Each plan size has a default value but can be increased in increments up to a maximum amount. For ranges, visit: https://www.digitalocean.com/pricing/managed-databases"
Expand Down Expand Up @@ -188,6 +192,21 @@ func RunDatabaseGet(c *CmdConfig) error {
return displayDatabases(c, false, *db)
}

// RunDatabaseGetCA returns a CA certificate for a database
func RunDatabaseGetCA(c *CmdConfig) error {
if len(c.Args) == 0 {
return doctl.NewMissingArgsErr(c.NS)
}

id := c.Args[0]
dbCA, err := c.Databases().GetCA(id)
if err != nil {
return err
}

return displayDatabaseCA(c, *dbCA)
}

// RunDatabaseCreate creates a database cluster
func RunDatabaseCreate(c *CmdConfig) error {
if len(c.Args) == 0 {
Expand Down Expand Up @@ -906,6 +925,11 @@ func displayDatabaseUsers(c *CmdConfig, users ...do.DatabaseUser) error {
return c.Display(item)
}

func displayDatabaseCA(c *CmdConfig, dbCAs ...do.DatabaseCA) error {
item := &displayers.DatabaseCAs{DatabaseCAs: dbCAs}
return c.Display(item)
}

func databaseOptions() *Command {
cmd := &Command{
Command: &cobra.Command{
Expand Down
35 changes: 35 additions & 0 deletions commands/displayers/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,41 @@ func (db *DatabaseBackups) KV() []map[string]any {
return out
}

type DatabaseCAs struct {
DatabaseCAs do.DatabaseCAs
}

var _ Displayable = &DatabaseCAs{}

func (dc *DatabaseCAs) JSON(out io.Writer) error {
return writeJSON(dc.DatabaseCAs, out)
}

func (dc *DatabaseCAs) Cols() []string {
return []string{
"Certificate",
}
}

func (dc *DatabaseCAs) ColMap() map[string]string {
return map[string]string{
"Certificate": "Certificate",
}
}

func (dc *DatabaseCAs) KV() []map[string]any {
out := make([]map[string]any, 0, len(dc.DatabaseCAs))

for _, c := range dc.DatabaseCAs {
o := map[string]any{
"Certificate": string(c.Certificate),
}
out = append(out, o)
}

return out
}

type DatabaseUsers struct {
DatabaseUsers do.DatabaseUsers
}
Expand Down
18 changes: 18 additions & 0 deletions do/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ type Database struct {
*godo.Database
}

// DatabaseCA is a wrapper for godo.DatabaseCA
type DatabaseCA struct {
*godo.DatabaseCA
}

// DatabaseCAs is a slice of DatabaseCA
type DatabaseCAs []DatabaseCA

// Databases is a slice of Database
type Databases []Database

Expand Down Expand Up @@ -145,6 +153,7 @@ type DatabaseIndex struct {
type DatabasesService interface {
List() (Databases, error)
Get(string) (*Database, error)
GetCA(string) (*DatabaseCA, error)
Create(*godo.DatabaseCreateRequest) (*Database, error)
Delete(string) error
GetConnection(string, bool) (*DatabaseConnection, error)
Expand Down Expand Up @@ -257,6 +266,15 @@ func (ds *databasesService) Get(databaseID string) (*Database, error) {
return &Database{Database: db}, nil
}

func (ds *databasesService) GetCA(databaseID string) (*DatabaseCA, error) {
dbCA, _, err := ds.client.Databases.GetCA(context.TODO(), databaseID)
if err != nil {
return nil, err
}

return &DatabaseCA{DatabaseCA: dbCA}, nil
}

func (ds *databasesService) Create(req *godo.DatabaseCreateRequest) (*Database, error) {
db, _, err := ds.client.Databases.Create(context.TODO(), req)
if err != nil {
Expand Down

0 comments on commit e66b4ce

Please sign in to comment.