Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cli/cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (r *runners) completeNetworkNames(cmd *cobra.Command, args []string, toComp
return nil, cobra.ShellCompDirectiveNoFileComp
}

networks, err := r.kotsAPI.ListNetworks(nil, nil)
networks, err := r.kotsAPI.ListNetworks(false, nil, nil)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand All @@ -71,7 +71,7 @@ func (r *runners) completeNetworkIDs(cmd *cobra.Command, args []string, toComple
return nil, cobra.ShellCompDirectiveNoFileComp
}

networks, err := r.kotsAPI.ListNetworks(nil, nil)
networks, err := r.kotsAPI.ListNetworks(false, nil, nil)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand All @@ -89,7 +89,7 @@ func (r *runners) completeNetworkIDsAndNames(cmd *cobra.Command, args []string,
return nil, cobra.ShellCompDirectiveNoFileComp
}

networks, err := r.kotsAPI.ListNetworks(nil, nil)
networks, err := r.kotsAPI.ListNetworks(false, nil, nil)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand All @@ -115,7 +115,7 @@ func (r *runners) getNetworkIDFromArg(arg string) (string, error) {
return "", errors.Wrap(err, "get network")
}

networks, err := r.kotsAPI.ListNetworks(nil, nil)
networks, err := r.kotsAPI.ListNetworks(false, nil, nil)
if errors.Cause(err) == platformclient.ErrForbidden {
return "", ErrCompatibilityMatrixTermsNotAccepted
} else if err != nil {
Expand Down
28 changes: 26 additions & 2 deletions cli/cmd/network_ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func (r *runners) InitNetworkList(parent *cobra.Command) *cobra.Command {
}
parent.AddCommand(cmd)

cmd.Flags().BoolVar(&r.args.lsNetworkShowTerminated, "show-terminated", false, "when set, only show terminated networks")
cmd.Flags().BoolVar(&r.args.lsNetworkShowReports, "show-reports", false, "when set, only show networks that have reports")
cmd.Flags().StringVar(&r.args.lsNetworkStartTime, "start-time", "", "start time for the query (Format: 2006-01-02T15:04:05Z)")
cmd.Flags().StringVar(&r.args.lsNetworkEndTime, "end-time", "", "end time for the query (Format: 2006-01-02T15:04:05Z)")
cmd.Flags().StringVarP(&r.outputFormat, "output", "o", "table", "The output format to use. One of: json|table|wide")
Expand All @@ -48,13 +50,24 @@ func (r *runners) listNetworks(_ *cobra.Command, args []string) error {
endTime = &et
}

networks, err := r.kotsAPI.ListNetworks(startTime, endTime)
networks, err := r.kotsAPI.ListNetworks(r.args.lsNetworkShowTerminated, startTime, endTime)
if errors.Cause(err) == platformclient.ErrForbidden {
return ErrCompatibilityMatrixTermsNotAccepted
} else if err != nil {
return errors.Wrap(err, "list networks")
}

// Filter networks if --show-reports is set
if r.args.lsNetworkShowReports {
filteredNetworks := make([]*types.Network, 0)
for _, network := range networks {
if network.HasReport {
filteredNetworks = append(filteredNetworks, network)
}
}
networks = filteredNetworks
}

header := true
if r.args.lsNetworkWatch {

Expand All @@ -74,7 +87,7 @@ func (r *runners) listNetworks(_ *cobra.Command, args []string) error {

// Runs until ctrl C is recognized
for range time.Tick(2 * time.Second) {
newNetworks, err := r.kotsAPI.ListNetworks(startTime, endTime)
newNetworks, err := r.kotsAPI.ListNetworks(r.args.lsNetworkShowTerminated, startTime, endTime)
if err != nil {
if err == promptui.ErrInterrupt {
return errors.New("interrupted")
Expand All @@ -83,6 +96,17 @@ func (r *runners) listNetworks(_ *cobra.Command, args []string) error {
return errors.Wrap(err, "watch networks")
}

// Filter networks if --show-reports is set
if r.args.lsNetworkShowReports {
filteredNetworks := make([]*types.Network, 0)
for _, network := range newNetworks {
if network.HasReport {
filteredNetworks = append(filteredNetworks, network)
}
}
newNetworks = filteredNetworks
}

// Create a map from the IDs of the new networks
newNetworkMap := make(map[string]*types.Network)
for _, newNetwork := range newNetworks {
Expand Down
6 changes: 3 additions & 3 deletions cli/cmd/network_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r *runners) removeNetworks(_ *cobra.Command, args []string) error {
}

if len(r.args.removeNetworkNames) > 0 {
networks, err := r.kotsAPI.ListNetworks(nil, nil)
networks, err := r.kotsAPI.ListNetworks(false, nil, nil)
if err != nil {
return errors.Wrap(err, "list networks")
}
Expand All @@ -77,7 +77,7 @@ func (r *runners) removeNetworks(_ *cobra.Command, args []string) error {
}

if r.args.removeNetworkAll {
networks, err := r.kotsAPI.ListNetworks(nil, nil)
networks, err := r.kotsAPI.ListNetworks(false, nil, nil)
if err != nil {
return errors.Wrap(err, "list networks")
}
Expand All @@ -99,7 +99,7 @@ func (r *runners) removeNetworks(_ *cobra.Command, args []string) error {
continue
}

networks, err := r.kotsAPI.ListNetworks(nil, nil)
networks, err := r.kotsAPI.ListNetworks(false, nil, nil)
if err != nil {
return errors.Wrap(err, "list networks")
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/network_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (r *runners) ensureUpdateNetworkIDArg(args []string) error {
}
r.args.updateNetworkID = networkID
} else if r.args.updateNetworkName != "" {
networks, err := r.kotsAPI.ListNetworks(nil, nil)
networks, err := r.kotsAPI.ListNetworks(false, nil, nil)
if errors.Cause(err) == platformclient.ErrForbidden {
return ErrCompatibilityMatrixTermsNotAccepted
} else if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions cli/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,11 @@ type runnerArgs struct {
createNetworkWaitDuration time.Duration
createNetworkDryRun bool

lsNetworkStartTime string
lsNetworkEndTime string
lsNetworkWatch bool
lsNetworkShowTerminated bool
lsNetworkShowReports bool
lsNetworkStartTime string
lsNetworkEndTime string
lsNetworkWatch bool

networkReportID string
networkReportWatch bool
Expand Down
10 changes: 6 additions & 4 deletions cli/print/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

// Table formatting
var (
networksTmplTableHeaderSrc = `ID NAME STATUS CREATED EXPIRES POLICY REPORTING`
networksTmplTableHeaderSrc = `ID NAME STATUS CREATED EXPIRES POLICY HAS REPORT`
networksTmplTableRowSrc = `{{ range . -}}
{{ .ID }} {{ padding .Name 27 }} {{ padding (printf "%s" .Status) 12 }} {{ padding (printf "%s" (localeTime .CreatedAt)) 30 }} {{if .ExpiresAt.IsZero}}{{ padding "-" 30 }}{{else}}{{ padding (printf "%s" (localeTime .ExpiresAt)) 30 }}{{end}} {{if eq .Policy ""}}{{ padding "open" 30 }}{{else}}{{ padding (printf "%s" (.Policy)) 30 }}{{end}} {{if .CollectReport}}{{ padding "on" 30 }}{{else}}{{ padding "off" 30 }}{{end}}
{{ printf "%.8s" .ID }} {{ padding .Name 27 }} {{ padding (printf "%s" .Status) 12 }} {{ padding (printf "%s" (localeTime .CreatedAt)) 30 }} {{if .ExpiresAt.IsZero}}{{ padding "-" 30 }}{{else}}{{ padding (printf "%s" (localeTime .ExpiresAt)) 30 }}{{end}} {{if eq .Policy ""}}{{ padding "open" 10 }}{{else}}{{ padding (printf "%s" (.Policy)) 10 }}{{end}} {{if .HasReport}}{{ padding "yes" 10 }}{{else}}{{ padding "no" 10 }}{{end}}
{{ end }}`
)

Expand All @@ -25,9 +25,11 @@ var (

// Wide table formatting
var (
networksTmplWideHeaderSrc = `ID NAME STATUS CREATED EXPIRES POLICY REPORTING`
networksTmplWideHeaderSrc = `ID NAME STATUS CREATED EXPIRES POLICY HAS REPORT REPORTING RESOURCES`
networksTmplWideRowSrc = `{{ range . -}}
{{ .ID }} {{ padding .Name 27 }} {{ padding (printf "%s" .Status) 12 }} {{ padding (printf "%s" (localeTime .CreatedAt)) 30 }} {{if .ExpiresAt.IsZero}}{{ padding "-" 30 }}{{else}}{{ padding (printf "%s" (localeTime .ExpiresAt)) 30 }}{{end}} {{if eq .Policy ""}}{{ padding "open" 30 }}{{else}}{{ padding (printf "%s" (.Policy)) 30 }}{{end}} {{if .CollectReport}}{{ padding "on" 30 }}{{else}}{{ padding "off" 30 }}{{end}}
{{ printf "%.8s" .ID }} {{ padding .Name 27 }} {{ padding (printf "%s" .Status) 12 }} {{ padding (printf "%s" (localeTime .CreatedAt)) 30 }} {{if .ExpiresAt.IsZero}}{{ padding "-" 30 }}{{else}}{{ padding (printf "%s" (localeTime .ExpiresAt)) 30 }}{{end}} {{if eq .Policy ""}}{{ padding "open" 10 }}{{else}}{{ padding (printf "%s" (.Policy)) 10 }}{{end}} {{if .HasReport}}{{ padding "yes" 10 }}{{else}}{{ padding "no" 10 }}{{end}} {{if .CollectReport}}{{ padding "on" 10 }}{{else}}{{ padding "off" 10 }}{{end}} {{if eq (len .Resources) 0}}-{{else}}{{ range $index, $resource := .Resources }}{{if $index}}
{{end}}{{ $resource.Distribution }}: {{ $resource.Name }} ({{ $resource.ID }}){{ end }}{{end}}

{{ end }}`
)

Expand Down
3 changes: 2 additions & 1 deletion pkg/kotsclient/network_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ListNetworksResponse struct {
TotalNetworks int `json:"totalNetworks"`
}

func (c *VendorV3Client) ListNetworks(startTime *time.Time, endTime *time.Time) ([]*types.Network, error) {
func (c *VendorV3Client) ListNetworks(includeTerminated bool, startTime *time.Time, endTime *time.Time) ([]*types.Network, error) {
allNetworks := []*types.Network{}
page := 0
for {
Expand All @@ -32,6 +32,7 @@ func (c *VendorV3Client) ListNetworks(startTime *time.Time, endTime *time.Time)
v.Set("end-time", endTime.Format(time.RFC3339))
}
v.Set("currentPage", strconv.Itoa(page))
v.Set("show-terminated", strconv.FormatBool(includeTerminated))
url := fmt.Sprintf("/v3/networks?%s", v.Encode())
err := c.DoJSON(context.TODO(), "GET", url, http.StatusOK, nil, &networks)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions pkg/types/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ type Network struct {
OverlayEndpoint string `json:"overlay_endpoint,omitempty"`
OverlayToken string `json:"overlay_token,omitempty"`

Policy string `json:"policy,omitempty"`
CollectReport bool `json:"collect_report,omitempty"`
Policy string `json:"policy,omitempty"`
CollectReport bool `json:"collect_report,omitempty"`
HasReport bool `json:"has_report,omitempty"`
Resources []*NetworkResource `json:"resources,omitempty"`
}

type NetworkResource struct {
Name string `json:"name"`
Distribution string `json:"distribution"`
ID string `json:"id"`
}

type NetworkStatus string
Expand Down