Skip to content

Commit

Permalink
update sail conn list command to show tags and versions and sort the …
Browse files Browse the repository at this point in the history
…table by Alias name
  • Loading branch information
darrell-thobe-sp committed Sep 12, 2024
1 parent 2635d12 commit 8fc2147
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
67 changes: 62 additions & 5 deletions cmd/connector/conn_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"fmt"
"io"
"net/http"
"sort"

"github.com/olekukonko/tablewriter"
"github.com/sailpoint-oss/sailpoint-cli/internal/client"
"github.com/sailpoint-oss/sailpoint-cli/internal/util"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -40,19 +42,74 @@ func newConnListCmd(client client.Client) *cobra.Command {
return err
}

var conns []connector
var conns []connectorList
err = json.Unmarshal(raw, &conns)
if err != nil {
return err
}

// Sort connectors by Alias
sort.Slice(conns, func(i, j int) bool {
return conns[i].Alias < conns[j].Alias
})

table := tablewriter.NewWriter(cmd.OutOrStdout())
table.SetHeader(connectorColumns)
for _, v := range conns {
table.Append(v.columns())
table.SetHeader(connectorListColumns)

// Process each connector and populate the table
for _, conn := range conns {
connectorRef := conn.ID
if connectorRef == "" {
continue
}

// Build the tags endpoint using the connectorRef
tagsEndpoint := util.ResourceUrl(endpoint, connectorRef, "tags")
tagsResp, err := client.Get(cmd.Context(), tagsEndpoint)
if err != nil {
return err
}
defer tagsResp.Body.Close()

if tagsResp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(tagsResp.Body)
return fmt.Errorf("non-200 response: %s\nbody: %s", tagsResp.Status, body)
}

// Process the response for the tags request
tagsRaw, err := io.ReadAll(tagsResp.Body)
if err != nil {
return err
}

var tags []tag
err = json.Unmarshal(tagsRaw, &tags)
if err != nil {
return err
}

// Prepare data for the table
var tagNames []string
var versions []string
for _, t := range tags {
tagNames = append(tagNames, t.TagName)
versions = append(versions, fmt.Sprintf("%d", t.ActiveVersion))
}
tagsString := fmt.Sprintf("%s", tagNames)
versionsString := fmt.Sprintf("%s", versions)

// Add the row to the table
row := []string{
conn.ID,
conn.Alias,
tagsString,
versionsString,
}
table.Append(row)
}
table.Render()

// Render the table
table.Render()
return nil
},
}
Expand Down
13 changes: 13 additions & 0 deletions cmd/connector/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ func (c connector) columns() []string {

var connectorColumns = []string{"ID", "Alias"}

type connectorList struct {
ID string `json:"id"`
Alias string `json:"alias"`
TagName string `json:"tagName"`
ActiveVersion uint32 `json:"activeVersion"`
}

func (c connectorList) columns() []string {
return []string{c.ID, c.Alias, c.TagName, fmt.Sprint(c.ActiveVersion)}
}

var connectorListColumns = []string{"ID", "Alias", "Tags", "Version"}

type connectorVersion struct {
ConnectorID string `json:"connectorId"`
Version int `json:"version"`
Expand Down

0 comments on commit 8fc2147

Please sign in to comment.