Skip to content

Commit ed9c50c

Browse files
authored
Merge pull request #6 from ppc64le-cloud/events
get events command
2 parents a595d86 + 9d9b89d commit ed9c50c

File tree

6 files changed

+115
-5
lines changed

6 files changed

+115
-5
lines changed

cmd/get/events/events.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package events
2+
3+
import (
4+
"fmt"
5+
"github.com/ppc64le-cloud/pvsadm/pkg"
6+
"github.com/ppc64le-cloud/pvsadm/pkg/client"
7+
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
8+
"github.com/spf13/cobra"
9+
"time"
10+
)
11+
12+
var (
13+
since time.Duration
14+
)
15+
16+
var Cmd = &cobra.Command{
17+
Use: "events",
18+
Short: "Get Powervs events",
19+
Long: `Get the PowerVS events`,
20+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
21+
if pkg.Options.InstanceID == "" && pkg.Options.InstanceName == "" {
22+
return fmt.Errorf("--instance-name or --instance-name required")
23+
}
24+
return nil
25+
},
26+
RunE: func(cmd *cobra.Command, args []string) error {
27+
opt := pkg.Options
28+
29+
c, err := client.NewClient(opt.APIKey)
30+
if err != nil {
31+
return err
32+
}
33+
34+
pvmclient, err := client.NewPVMClient(c, opt.InstanceID, opt.InstanceName)
35+
if err != nil {
36+
return err
37+
}
38+
events, err := pvmclient.EventsClient.GetPcloudEventsGetsince(since)
39+
if err != nil {
40+
return err
41+
}
42+
table := utils.NewTable()
43+
table.Render(events.Payload.Events, []string{"user", "timestamp"})
44+
45+
return nil
46+
},
47+
}
48+
49+
func init() {
50+
Cmd.PersistentFlags().DurationVar(&since, "since", 24*time.Hour, "Show events since")
51+
}

cmd/get/get.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package get
2+
3+
import (
4+
"github.com/ppc64le-cloud/pvsadm/cmd/get/events"
5+
"github.com/ppc64le-cloud/pvsadm/pkg"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var Cmd = &cobra.Command{
10+
Use: "get",
11+
Short: "Get the resources",
12+
Long: `Get the resources`,
13+
}
14+
15+
func init() {
16+
Cmd.AddCommand(events.Cmd)
17+
Cmd.PersistentFlags().StringVarP(&pkg.Options.InstanceID, "instance-id", "i", "", "Instance ID of the PowerVS instance")
18+
Cmd.PersistentFlags().StringVarP(&pkg.Options.InstanceName, "instance-name", "n", "", "Instance name of the PowerVS")
19+
}

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"github.com/ppc64le-cloud/pvsadm/cmd/get"
45
"github.com/ppc64le-cloud/pvsadm/cmd/purge"
56
"github.com/ppc64le-cloud/pvsadm/pkg"
67
"github.com/ppc64le-cloud/pvsadm/pkg/audit"
@@ -21,6 +22,7 @@ This is a tool built for the Power Systems Virtual Server helps managing and mai
2122

2223
func init() {
2324
rootCmd.AddCommand(purge.Cmd)
25+
rootCmd.AddCommand(get.Cmd)
2426
rootCmd.PersistentFlags().StringVarP(&pkg.Options.APIKey, "api-key", "k", "", "IBMCLOUD API Key(env name: IBMCLOUD_API_KEY)")
2527
rootCmd.PersistentFlags().BoolVar(&pkg.Options.Debug, "debug", false, "Enable PowerVS debug option")
2628
rootCmd.PersistentFlags().StringVar(&pkg.Options.AuditFile, "audit-file", "pvsadm.log", "Audit logs for the tool")

pkg/client/events/events.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package events
2+
3+
import (
4+
"github.com/IBM-Cloud/power-go-client/ibmpisession"
5+
"github.com/IBM-Cloud/power-go-client/power/client/p_cloud_events"
6+
"github.com/ppc64le-cloud/pvsadm/pkg"
7+
"time"
8+
)
9+
10+
type Client struct {
11+
instanceID string
12+
client *p_cloud_events.Client
13+
session *ibmpisession.IBMPISession
14+
}
15+
16+
func NewClient(sess *ibmpisession.IBMPISession, powerinstanceid string) *Client {
17+
c := &Client{
18+
session: sess,
19+
instanceID: powerinstanceid,
20+
client: sess.Power.PCloudEvents,
21+
}
22+
return c
23+
}
24+
25+
func (c *Client) GetPcloudEventsGetsince(since time.Duration) (*p_cloud_events.PcloudEventsGetsinceOK, error) {
26+
params := p_cloud_events.NewPcloudEventsGetsinceParamsWithTimeout(pkg.TIMEOUT).WithCloudInstanceID(c.instanceID).WithTime(time.Now().UTC().Add(-since).Format(time.RFC3339))
27+
return c.client.PcloudEventsGetsince(params, ibmpisession.NewAuth(c.session, c.instanceID))
28+
}

pkg/client/pvmclient.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev2/controllerv2"
66
"github.com/IBM-Cloud/power-go-client/ibmpisession"
77
"github.com/ppc64le-cloud/pvsadm/pkg"
8+
"github.com/ppc64le-cloud/pvsadm/pkg/client/events"
89
"github.com/ppc64le-cloud/pvsadm/pkg/client/image"
910
"github.com/ppc64le-cloud/pvsadm/pkg/client/instance"
1011
"github.com/ppc64le-cloud/pvsadm/pkg/client/network"
@@ -25,6 +26,7 @@ type PVMClient struct {
2526
ImgClient *image.Client
2627
VolumeClient *volume.Client
2728
NetworkClient *network.Client
29+
EventsClient *events.Client
2830
}
2931

3032
func NewPVMClient(c *Client, instanceID, instanceName string) (*PVMClient, error) {
@@ -73,5 +75,6 @@ func NewPVMClient(c *Client, instanceID, instanceName string) (*PVMClient, error
7375
pvmclient.VolumeClient = volume.NewClient(pvmclient.PISession, instanceID)
7476
pvmclient.InstanceClient = instance.NewClient(pvmclient.PISession, instanceID)
7577
pvmclient.NetworkClient = network.NewClient(pvmclient.PISession, instanceID)
78+
pvmclient.EventsClient = events.NewClient(pvmclient.PISession, instanceID)
7679
return pvmclient, nil
7780
}

pkg/utils/table.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"github.com/go-openapi/strfmt"
66
"github.com/olekukonko/tablewriter"
7-
"k8s.io/klog/v2"
87
"os"
98
"reflect"
109
"strconv"
@@ -29,7 +28,16 @@ func (t *Table) SetHeader(keys []string) {
2928
})
3029
}
3130

32-
func (t *Table) Render(rows interface{}, fields []string) {
31+
func Contains(a []string, x string) bool {
32+
for _, n := range a {
33+
if x == n {
34+
return true
35+
}
36+
}
37+
return false
38+
}
39+
40+
func (t *Table) Render(rows interface{}, exclude []string) {
3341
noData := true
3442
switch reflect.TypeOf(rows).Kind() {
3543
case reflect.Slice:
@@ -40,7 +48,7 @@ func (t *Table) Render(rows interface{}, fields []string) {
4048
val := s.Index(i).Elem()
4149
var row []string
4250
for i := 0; i < val.NumField(); i++ {
43-
if f := strings.ToLower(val.Type().Field(i).Name); f == "href" || f == "specifications" {
51+
if f := strings.ToLower(val.Type().Field(i).Name); Contains(exclude, f) {
4452
continue
4553
}
4654
headers = append(headers, val.Type().Field(i).Name)
@@ -82,8 +90,7 @@ func getcontent(value reflect.Value) (strVal string) {
8290
}
8391
strVal = strings.Join(st, ",")
8492
default:
85-
klog.Infof("I'm here at default type, unable to handle: %s", value.Kind())
86-
strVal = value.String()
93+
strVal = fmt.Sprintf("%+v", value)
8794
}
8895
}
8996
return

0 commit comments

Comments
 (0)