Skip to content
Merged
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
6 changes: 5 additions & 1 deletion cmd/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,11 @@ func (s *Scanner) ExecuteScan(accountIDs []string, selectAll bool) (int, error)
return 0, fmt.Errorf("failed to run stockers: %w", err)
}

inv.PrintInventory()
s.logger.Info("Inventory scan complete",
zap.Int("accounts", len(inv.Accounts)),
zap.Int("clusters", inv.TotalClusters()),
zap.Int("instances", inv.TotalInstances()),
)
if err := postScannerInventory(inv, s.logger); err != nil {
return 0, fmt.Errorf("failed to post inventory: %w", err)
}
Expand Down
9 changes: 0 additions & 9 deletions internal/inventory/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,3 @@ func (a *Account) DisableBilling() {
func (a *Account) IsBillingEnabled() bool {
return a.billingEnabled
}

// PrintAccount prints account info and every cluster on it by stdout
func (a Account) PrintAccount() {
fmt.Printf("\t - Account: %s[%s] #Clusters: %d\n", a.AccountName, a.AccountID, len(a.Clusters))

for _, cluster := range a.Clusters {
cluster.PrintCluster()
}
}
24 changes: 0 additions & 24 deletions internal/inventory/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,27 +207,3 @@ func testIsBillingEnabled_False(t *testing.T) {
assert.False(t, account.billingEnabled, account.IsBillingEnabled())
}

func TestPrintAccount(t *testing.T) {
t.Run("Print Account ", func(t *testing.T) { testPrintAccount_Correct(t) })
t.Run("Print Account No clusters", func(t *testing.T) { testPrintAccount_NoClusters(t) })
}

func testPrintAccount_Correct(t *testing.T) {
account, err := NewAccount("0000-11A", "testAccount", AWSProvider, "user", "password")
assert.Nil(t, err)
assert.NotNil(t, account)

cluster, err := NewCluster("testCluster-1", "XXXX1", AWSProvider, "eu-west-1", "https://url.com", "John Doe")
assert.Nil(t, err)
account.AddCluster(cluster)

account.PrintAccount()
}

func testPrintAccount_NoClusters(t *testing.T) {
account, err := NewAccount("0000-11A", "testAccount", AWSProvider, "user", "password")
assert.Nil(t, err)
assert.NotNil(t, account)

account.PrintAccount()
}
9 changes: 0 additions & 9 deletions internal/inventory/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,3 @@ func (c Cluster) InstancesCount() int {
func GenerateClusterID(name string, infraID string) string {
return name + "-" + infraID
}

// PrintCluster prints cluster info
func (c Cluster) PrintCluster() {
fmt.Printf("\t\tCluster:[%s] -- Status: %s, Region: %s, Provider: %s, #Instances: %d\n", c.ClusterName, c.Status, c.Region, c.Provider, c.InstancesCount())

for _, instance := range c.Instances {
instance.PrintInstance()
}
}
13 changes: 1 addition & 12 deletions internal/inventory/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,22 +412,11 @@ func testInstancesCount(t *testing.T) {
cluster, err := NewCluster("testCluster", "i1", AWSProvider, "us-east-1", "https://console", "user")
assert.NotNil(t, cluster)
assert.Nil(t, err)
assert.Equal(t, len(cluster.Instances), 0)
assert.Equal(t, 0, cluster.InstancesCount())
}

// TestGenerateClusterID tests GenerateClusterID function for 100% coverage
func TestGenerateClusterID(t *testing.T) {
assert.Equal(t, "test-infra", GenerateClusterID("test", "infra"))
}

// TestPrintCluster tests PrintCluster function for 100% coverage
func TestPrintCluster(t *testing.T) {
c, _ := NewCluster("name", "infra", AWSProvider, "region", "link", "owner")
c.PrintCluster()

now := time.Now()
c.Instances = []Instance{
{Status: Running, CreatedAt: now.Add(-24 * time.Hour)},
}
c.PrintCluster()
}
19 changes: 0 additions & 19 deletions internal/inventory/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package inventory

import (
"errors"
"fmt"
"time"
)

Expand Down Expand Up @@ -110,21 +109,3 @@ func (i *Instance) AddExpense(expense *Expense) error {

return nil
}

// String as ToString func
func (i Instance) String() string {
return fmt.Sprintf("(%s): [%s][%s][%s][%s][%s][%d]",
i.InstanceName,
i.Provider,
i.InstanceType,
i.AvailabilityZone,
i.Status,
i.ClusterID,
len(i.Expenses),
)
}

// PrintInstance prints Instance details
func (i Instance) PrintInstance() {
fmt.Printf("\t\t\tInstance: %s\n", i.String())
}
25 changes: 0 additions & 25 deletions internal/inventory/instance_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package inventory

import (
"strings"
"testing"
"time"

Expand Down Expand Up @@ -125,27 +124,3 @@ func testAddExpense_WithNegativeAmount(t *testing.T) {
assert.Zero(t, len(i.Tags))
}

// TestInstance_String verifies String method returns expected format
func TestInstance_String(t *testing.T) {
i := Instance{
InstanceID: "i-123",
InstanceName: "test",
Provider: AWSProvider,
InstanceType: "t2.micro",
AvailabilityZone: "us-east-1a",
Status: Running,
ClusterID: "cluster-x",
Expenses: []Expense{{Amount: 5}},
}

str := i.String()
if !(strings.Contains(str, "test") && strings.Contains(str, "AWS") && strings.Contains(str, "t2.micro")) {
t.Errorf("unexpected output from String(): %s", str)
}
}

// TestPrintInstance verifies PrintInstance runs without panic
func TestPrintInstance(t *testing.T) {
i := Instance{InstanceID: "i-456", InstanceName: "node1"}
i.PrintInstance()
}
20 changes: 16 additions & 4 deletions internal/inventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,22 @@ func (i *Inventory) AddAccount(account *Account) error {
return nil
}

// PrintInventory prints the entire Inventory content
func (i Inventory) PrintInventory() {
fmt.Printf("Inventory created at: %s\nAccounts:\n", i.CreatedAt)
// TotalClusters returns the total number of clusters across all accounts
func (i Inventory) TotalClusters() int {
total := 0
for _, account := range i.Accounts {
account.PrintAccount()
total += len(account.Clusters)
}
return total
}

// TotalInstances returns the total number of instances across all accounts and clusters
func (i Inventory) TotalInstances() int {
total := 0
for _, account := range i.Accounts {
for _, cluster := range account.Clusters {
total += len(cluster.Instances)
}
}
return total
}
43 changes: 35 additions & 8 deletions internal/inventory/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,42 @@ func testAddAccount_Repeated(t *testing.T) {
assert.ErrorContains(t, err, ErrorAddingAccountToInventory.Error())
}

func TestPrintInventory(t *testing.T) {
func TestTotalClusters(t *testing.T) {
inv := NewInventory()
acc := Account{
AccountID: "id-account",
AccountName: "testAccount",
Provider: UnknownProvider,
}

inv.AddAccount(&acc)
acc1 := &Account{AccountID: "acc-1", Clusters: map[string]*Cluster{
"c1": {ClusterID: "c1"},
"c2": {ClusterID: "c2"},
}}
acc2 := &Account{AccountID: "acc-2", Clusters: map[string]*Cluster{
"c3": {ClusterID: "c3"},
}}

inv.AddAccount(acc1)
inv.AddAccount(acc2)

assert.Equal(t, 3, inv.TotalClusters())
}

func TestTotalInstances(t *testing.T) {
inv := NewInventory()

acc := &Account{AccountID: "acc-1", Clusters: map[string]*Cluster{
"c1": {ClusterID: "c1", Instances: []Instance{{InstanceID: "i1"}, {InstanceID: "i2"}}},
"c2": {ClusterID: "c2", Instances: []Instance{{InstanceID: "i3"}}},
}}

inv.AddAccount(acc)

assert.Equal(t, 3, inv.TotalInstances())
}

func TestTotalClusters_Empty(t *testing.T) {
inv := NewInventory()
assert.Equal(t, 0, inv.TotalClusters())
}

inv.PrintInventory()
func TestTotalInstances_Empty(t *testing.T) {
inv := NewInventory()
assert.Equal(t, 0, inv.TotalInstances())
}
5 changes: 0 additions & 5 deletions internal/stocker/aws_billing_stocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,6 @@ func (s *AWSBillingStocker) getInstanceExpenses(instance *inventory.Instance) er
return nil
}

// PrintStock prints the stock (account) of the AWSBillingStocker as a string
func (s AWSBillingStocker) PrintStock() {
s.Account.PrintAccount()
}

// GetAccount returns the account configured for this stocker
func (s AWSBillingStocker) GetAccount() inventory.Account {
return *s.Account
Expand Down
5 changes: 0 additions & 5 deletions internal/stocker/aws_stocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ func (s *AWSStocker) MakeStock() error {
return nil
}

// PrintStock Prints the Account Stock
func (s AWSStocker) PrintStock() {
s.Account.PrintAccount()
}

// GetAccount Returns the Account was scanned on this stocker
func (s AWSStocker) GetAccount() inventory.Account {
return *s.Account
Expand Down
5 changes: 0 additions & 5 deletions internal/stocker/azure_stocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ func (s AzureStocker) MakeStock() error {
return fmt.Errorf("AzureStocker.MakeStock not implemented")
}

// PrintStock prints by stdout the account object belongs to this stocker
func (s AzureStocker) PrintStock() {
s.Account.PrintAccount()
}

// GetAccount resturns the scanned results on this stocker instance
func (s AzureStocker) GetAccount() inventory.Account {
return s.Account
Expand Down
5 changes: 0 additions & 5 deletions internal/stocker/gcp_stocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ func (s GCPStocker) MakeStock() error {
return fmt.Errorf("GCPStocker.MakeStock not implemented")
}

// PrintStock prints by stdout the account object belongs to this stocker
func (s GCPStocker) PrintStock() {
s.Account.PrintAccount()
}

// GetAccount resturns the scanned results on this stocker instance
func (s GCPStocker) GetAccount() inventory.Account {
return s.Account
Expand Down
1 change: 0 additions & 1 deletion internal/stocker/stocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ import "github.com/RHEcosystemAppEng/cluster-iq/internal/inventory"
// Stocker interface
type Stocker interface {
MakeStock() error
PrintStock()
GetAccount() inventory.Account
}
Loading