Skip to content

Commit

Permalink
tls-config list
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Aug 17, 2022
1 parent 4c34815 commit 85ffc3c
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ type Interface interface {
NewListServicesPaginator(i *fastly.ListServicesInput) fastly.PaginatorServices

GetCustomTLSConfiguration(i *fastly.GetCustomTLSConfigurationInput) (*fastly.CustomTLSConfiguration, error)
ListCustomTLSConfigurations(i *fastly.ListCustomTLSConfigurationsInput) ([]*fastly.CustomTLSConfiguration, error)
}

// RealtimeStatsInterface is the subset of go-fastly's realtime stats API used here.
Expand Down
168 changes: 168 additions & 0 deletions pkg/commands/tls/config/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package config

import (
"encoding/json"
"fmt"
"io"
"strings"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v6/fastly"
)

// NewListCommand returns a usable command registered under the parent.
func NewListCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *ListCommand {
var c ListCommand
c.CmdClause = parent.Command("list", "List all TLS configurations")
c.Globals = globals
c.manifest = data

// Optional Flags
c.CmdClause.Flag("filter-bulk", "Optionally filter by the bulk attribute").Action(c.filterBulk.Set).BoolVar(&c.filterBulk.Value)
c.CmdClause.Flag("include", "Include related objects (comma-separated values)").HintOptions(include).EnumVar(&c.include, include)
c.RegisterFlagBool(cmd.BoolFlagOpts{
Name: cmd.FlagJSONName,
Description: cmd.FlagJSONDesc,
Dst: &c.json,
Short: 'j',
})
c.CmdClause.Flag("page", "Page number of data set to fetch").IntVar(&c.pageNumber)
c.CmdClause.Flag("per-page", "Number of records per page").IntVar(&c.pageSize)

return &c
}

// ListCommand calls the Fastly API to list appropriate resources.
type ListCommand struct {
cmd.Base

filterBulk cmd.OptionalBool
include string
json bool
manifest manifest.Data
pageNumber int
pageSize int
}

// Exec invokes the application logic for the command.
func (c *ListCommand) Exec(in io.Reader, out io.Writer) error {
if c.Globals.Verbose() && c.json {
return fsterr.ErrInvalidVerboseJSONCombo
}

input := c.constructInput()

rs, err := c.Globals.APIClient.ListCustomTLSConfigurations(input)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]interface{}{})
return err
}

if c.Globals.Verbose() {
c.printVerbose(out, rs)
} else {
err = c.printSummary(out, rs)
if err != nil {
return err
}
}
return nil
}

// constructInput transforms values parsed from CLI flags into an object to be used by the API client library.
func (c *ListCommand) constructInput() *fastly.ListCustomTLSConfigurationsInput {
var input fastly.ListCustomTLSConfigurationsInput

if c.filterBulk.WasSet {
input.FilterBulk = c.filterBulk.Value
}
if c.include != "" {
input.Include = c.include
}
if c.pageNumber > 0 {
input.PageNumber = c.pageNumber
}
if c.pageSize > 0 {
input.PageSize = c.pageSize
}

return &input
}

// printVerbose displays the information returned from the API in a verbose
// format.
func (c *ListCommand) printVerbose(out io.Writer, rs []*fastly.CustomTLSConfiguration) {
for _, r := range rs {
fmt.Fprintf(out, "ID: %s\n", r.ID)
fmt.Fprintf(out, "Name: %s\n", r.Name)

if len(r.DNSRecords) > 0 {
for _, v := range r.DNSRecords {
fmt.Fprintf(out, "DNS Record ID: %s\n", v.ID)
fmt.Fprintf(out, "DNS Record Type: %s\n", v.RecordType)
fmt.Fprintf(out, "DNS Record Region: %s\n", v.Region)
}
}

fmt.Fprintf(out, "Bulk: %t\n", r.Bulk)
fmt.Fprintf(out, "Default: %t\n", r.Default)

if len(r.HTTPProtocols) > 0 {
for _, v := range r.HTTPProtocols {
fmt.Fprintf(out, "HTTP Protocol: %s\n", v)
}
}

if len(r.TLSProtocols) > 0 {
for _, v := range r.TLSProtocols {
fmt.Fprintf(out, "TLS Protocol: %s\n", v)
}
}

if r.CreatedAt != nil {
fmt.Fprintf(out, "Created at: %s\n", r.CreatedAt)
}
if r.UpdatedAt != nil {
fmt.Fprintf(out, "Updated at: %s\n", r.UpdatedAt)
}

fmt.Fprintf(out, "\n")
}
}

// printSummary displays the information returned from the API in a summarised
// format.
func (c *ListCommand) printSummary(out io.Writer, rs []*fastly.CustomTLSConfiguration) error {
if c.json {
data, err := json.Marshal(rs)
if err != nil {
return err
}
out.Write(data)
return nil
}

t := text.NewTable(out)
t.AddHeader("NAME", "ID", "BULK", "DEFAULT", "TLS PROTOCOLS", "HTTP PROTOCOLS", "DNS RECORDS")
for _, r := range rs {
drs := make([]string, len(r.DNSRecords))
for i, v := range r.DNSRecords {
drs[i] = v.ID
}
t.AddLine(
r.Name,
r.ID,
r.Bulk,
r.Default,
strings.Join(r.TLSProtocols, ", "),
strings.Join(r.HTTPProtocols, ", "),
strings.Join(drs, ", "),
)
}
t.Print()
return nil
}
8 changes: 7 additions & 1 deletion pkg/mock/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ type API struct {
NewListDictionaryItemsPaginatorFn func(i *fastly.ListDictionaryItemsInput) fastly.PaginatorDictionaryItems
NewListServicesPaginatorFn func(i *fastly.ListServicesInput) fastly.PaginatorServices

GetCustomTLSConfigurationFn func(i *fastly.GetCustomTLSConfigurationInput) (*fastly.CustomTLSConfiguration, error)
GetCustomTLSConfigurationFn func(i *fastly.GetCustomTLSConfigurationInput) (*fastly.CustomTLSConfiguration, error)
ListCustomTLSConfigurationsFn func(i *fastly.ListCustomTLSConfigurationsInput) ([]*fastly.CustomTLSConfiguration, error)
}

// AllDatacenters implements Interface.
Expand Down Expand Up @@ -1411,3 +1412,8 @@ func (m API) NewListServicesPaginator(i *fastly.ListServicesInput) fastly.Pagina
func (m API) GetCustomTLSConfiguration(i *fastly.GetCustomTLSConfigurationInput) (*fastly.CustomTLSConfiguration, error) {
return m.GetCustomTLSConfigurationFn(i)
}

// ListCustomTLSConfigurations implements Interface.
func (m API) ListCustomTLSConfigurations(i *fastly.ListCustomTLSConfigurationsInput) ([]*fastly.CustomTLSConfiguration, error) {
return m.ListCustomTLSConfigurationsFn(i)
}

0 comments on commit 85ffc3c

Please sign in to comment.