Skip to content

Commit

Permalink
Merge pull request #6 from ppc64le-cloud/events
Browse files Browse the repository at this point in the history
get events command
  • Loading branch information
mkumatag authored Oct 1, 2020
2 parents a595d86 + 9d9b89d commit ed9c50c
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 5 deletions.
51 changes: 51 additions & 0 deletions cmd/get/events/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package events

import (
"fmt"
"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/ppc64le-cloud/pvsadm/pkg/client"
"github.com/ppc64le-cloud/pvsadm/pkg/utils"
"github.com/spf13/cobra"
"time"
)

var (
since time.Duration
)

var Cmd = &cobra.Command{
Use: "events",
Short: "Get Powervs events",
Long: `Get the PowerVS events`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if pkg.Options.InstanceID == "" && pkg.Options.InstanceName == "" {
return fmt.Errorf("--instance-name or --instance-name required")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
opt := pkg.Options

c, err := client.NewClient(opt.APIKey)
if err != nil {
return err
}

pvmclient, err := client.NewPVMClient(c, opt.InstanceID, opt.InstanceName)
if err != nil {
return err
}
events, err := pvmclient.EventsClient.GetPcloudEventsGetsince(since)
if err != nil {
return err
}
table := utils.NewTable()
table.Render(events.Payload.Events, []string{"user", "timestamp"})

return nil
},
}

func init() {
Cmd.PersistentFlags().DurationVar(&since, "since", 24*time.Hour, "Show events since")
}
19 changes: 19 additions & 0 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package get

import (
"github.com/ppc64le-cloud/pvsadm/cmd/get/events"
"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "get",
Short: "Get the resources",
Long: `Get the resources`,
}

func init() {
Cmd.AddCommand(events.Cmd)
Cmd.PersistentFlags().StringVarP(&pkg.Options.InstanceID, "instance-id", "i", "", "Instance ID of the PowerVS instance")
Cmd.PersistentFlags().StringVarP(&pkg.Options.InstanceName, "instance-name", "n", "", "Instance name of the PowerVS")
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

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

func init() {
rootCmd.AddCommand(purge.Cmd)
rootCmd.AddCommand(get.Cmd)
rootCmd.PersistentFlags().StringVarP(&pkg.Options.APIKey, "api-key", "k", "", "IBMCLOUD API Key(env name: IBMCLOUD_API_KEY)")
rootCmd.PersistentFlags().BoolVar(&pkg.Options.Debug, "debug", false, "Enable PowerVS debug option")
rootCmd.PersistentFlags().StringVar(&pkg.Options.AuditFile, "audit-file", "pvsadm.log", "Audit logs for the tool")
Expand Down
28 changes: 28 additions & 0 deletions pkg/client/events/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package events

import (
"github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM-Cloud/power-go-client/power/client/p_cloud_events"
"github.com/ppc64le-cloud/pvsadm/pkg"
"time"
)

type Client struct {
instanceID string
client *p_cloud_events.Client
session *ibmpisession.IBMPISession
}

func NewClient(sess *ibmpisession.IBMPISession, powerinstanceid string) *Client {
c := &Client{
session: sess,
instanceID: powerinstanceid,
client: sess.Power.PCloudEvents,
}
return c
}

func (c *Client) GetPcloudEventsGetsince(since time.Duration) (*p_cloud_events.PcloudEventsGetsinceOK, error) {
params := p_cloud_events.NewPcloudEventsGetsinceParamsWithTimeout(pkg.TIMEOUT).WithCloudInstanceID(c.instanceID).WithTime(time.Now().UTC().Add(-since).Format(time.RFC3339))
return c.client.PcloudEventsGetsince(params, ibmpisession.NewAuth(c.session, c.instanceID))
}
3 changes: 3 additions & 0 deletions pkg/client/pvmclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev2/controllerv2"
"github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/ppc64le-cloud/pvsadm/pkg"
"github.com/ppc64le-cloud/pvsadm/pkg/client/events"
"github.com/ppc64le-cloud/pvsadm/pkg/client/image"
"github.com/ppc64le-cloud/pvsadm/pkg/client/instance"
"github.com/ppc64le-cloud/pvsadm/pkg/client/network"
Expand All @@ -25,6 +26,7 @@ type PVMClient struct {
ImgClient *image.Client
VolumeClient *volume.Client
NetworkClient *network.Client
EventsClient *events.Client
}

func NewPVMClient(c *Client, instanceID, instanceName string) (*PVMClient, error) {
Expand Down Expand Up @@ -73,5 +75,6 @@ func NewPVMClient(c *Client, instanceID, instanceName string) (*PVMClient, error
pvmclient.VolumeClient = volume.NewClient(pvmclient.PISession, instanceID)
pvmclient.InstanceClient = instance.NewClient(pvmclient.PISession, instanceID)
pvmclient.NetworkClient = network.NewClient(pvmclient.PISession, instanceID)
pvmclient.EventsClient = events.NewClient(pvmclient.PISession, instanceID)
return pvmclient, nil
}
17 changes: 12 additions & 5 deletions pkg/utils/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"github.com/go-openapi/strfmt"
"github.com/olekukonko/tablewriter"
"k8s.io/klog/v2"
"os"
"reflect"
"strconv"
Expand All @@ -29,7 +28,16 @@ func (t *Table) SetHeader(keys []string) {
})
}

func (t *Table) Render(rows interface{}, fields []string) {
func Contains(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}

func (t *Table) Render(rows interface{}, exclude []string) {
noData := true
switch reflect.TypeOf(rows).Kind() {
case reflect.Slice:
Expand All @@ -40,7 +48,7 @@ func (t *Table) Render(rows interface{}, fields []string) {
val := s.Index(i).Elem()
var row []string
for i := 0; i < val.NumField(); i++ {
if f := strings.ToLower(val.Type().Field(i).Name); f == "href" || f == "specifications" {
if f := strings.ToLower(val.Type().Field(i).Name); Contains(exclude, f) {
continue
}
headers = append(headers, val.Type().Field(i).Name)
Expand Down Expand Up @@ -82,8 +90,7 @@ func getcontent(value reflect.Value) (strVal string) {
}
strVal = strings.Join(st, ",")
default:
klog.Infof("I'm here at default type, unable to handle: %s", value.Kind())
strVal = value.String()
strVal = fmt.Sprintf("%+v", value)
}
}
return
Expand Down

0 comments on commit ed9c50c

Please sign in to comment.