/
vpc_list.go
115 lines (102 loc) · 3.46 KB
/
vpc_list.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// Copyright © 2019 Stephen Hoekstra
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package cmd
import (
"os"
"github.com/shoekstra/cosmic-cli/internal/config"
"github.com/shoekstra/cosmic-cli/internal/cosmic"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func newVPCListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List VPCs",
PreRun: func(cmd *cobra.Command, args []string) {
// Bind local flags in the PreRun stage to not overwrite bindings in other commands.
viper.BindPFlag("filter", cmd.Flags().Lookup("filter"))
viper.BindPFlag("profile", cmd.Flags().Lookup("profile"))
viper.BindPFlag("output", cmd.Flags().Lookup("output"))
viper.BindPFlag("reverse-sort", cmd.Flags().Lookup("reverse-sort"))
viper.BindPFlag("show-id", cmd.Flags().Lookup("show-id"))
viper.BindPFlag("show-redundant-status", cmd.Flags().Lookup("show-redundant-status"))
viper.BindPFlag("show-restart-required", cmd.Flags().Lookup("show-restart-required"))
viper.BindPFlag("show-snat", cmd.Flags().Lookup("show-snat"))
viper.BindPFlag("sort-by", cmd.Flags().Lookup("sort-by"))
},
Run: func(cmd *cobra.Command, args []string) {
if err := runVPCListCmd(); err != nil {
printErr(err)
os.Exit(1)
}
},
}
// Add local flags.
cmd.Flags().BoolP("reverse-sort", "", false, "reverse sort order")
cmd.Flags().BoolP("show-id", "", false, "show VPC id in result")
cmd.Flags().BoolP("show-redundant-status", "", false, "show VPC redundant router status in result")
cmd.Flags().BoolP("show-restart-required", "", false, "show VPC restart required status in result")
cmd.Flags().BoolP("show-snat", "", false, "show VPC Source NAT IP in result")
cmd.Flags().StringSliceP("filter", "f", nil, "filter results (supports regex)")
cmd.Flags().StringP("output", "o", "table", "specify output type")
cmd.Flags().StringP("profile", "p", "", "specify profile(s) to use")
cmd.Flags().StringP("sort-by", "s", "name", "field to sort by")
cmd.Flags().MarkHidden("output") // Not in use yet.
return cmd
}
func runVPCListCmd() error {
cfg, err := config.New()
if err != nil {
return err
}
vpcs, err := cosmic.ListVPCs(cosmic.NewAsyncClients(cfg))
if err != nil {
return err
}
vpcs.Sort(cfg.SortBy, cfg.ReverseSort)
if cfg.ShowSNAT {
publicIPs, err := cosmic.ListPublicIPAddresses(cosmic.NewAsyncClients(cfg))
if err != nil {
return err
}
for _, p := range publicIPs {
if !p.Issourcenat {
continue
}
for _, v := range vpcs {
if v.Id == p.Vpcid {
v.Sourcenatip = p.Ipaddress
}
}
}
}
// Print output
fields := []string{"Name", "CIDR", "VPCOfferingName", "ZoneName"}
if cfg.ShowID {
fields = append(fields, "ID")
}
if cfg.ShowSNAT {
fields = append(fields, "SourceNATIP")
}
if cfg.ShowRedundantStatus {
fields = append(fields, "RedundantVPCRouter")
}
if cfg.ShowRestartRequired {
fields = append(fields, "RestartRequired")
}
printResult(cfg.Output, "VPC", cfg.Filter, fields, vpcs)
return nil
}