Skip to content

Commit

Permalink
Chore(tenant-usage): use urfave/cli
Browse files Browse the repository at this point in the history
  • Loading branch information
till committed Mar 13, 2024
1 parent c1e4457 commit 75d0770
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .envrc-dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# for tenant-usage
export ACI_CLIENT_ID=
export ACI_SECRET=
export ACI_DC_URL=https://eu2-cloud.acronis.com
# export ACI_DC_URL=

# for bucket-usage (system user)
export S3_ENDPOINT=
Expand Down
97 changes: 68 additions & 29 deletions cmd/tenant-usage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,87 @@ package main

import (
"fmt"
"log"
"math"
"os"

"github.com/Luzilla/acronis-s3-usage/internal/utils"
"github.com/Luzilla/acronis-s3-usage/pkg/acronis"
"github.com/urfave/cli/v2"
)

func main() {
aci := acronis.NewClient(
os.Getenv("ACI_CLIENT_ID"),
os.Getenv("ACI_SECRET"),
os.Getenv("ACI_DC_URL"),
)

tenantId, err := aci.GetTenantID()
if err != nil {
panic(err)
}
fmt.Printf("Got tenant id: %s\n\n", tenantId)
var (
version = "dev"
commit = "none"
date = "unknown"
)

usageData, err := aci.GetUsage(tenantId)
if err != nil {
panic(err)
}
func main() {
app := &cli.App{
Name: "tenant-usgae",
HelpName: "a program to interact with the ACI APIs to extract s3 basic usage",
Version: fmt.Sprintf("%s (%s, date: %s)", version, commit, date),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "client-id",
Required: true,
EnvVars: []string{"ACI_CLIENT_ID"},
},
&cli.StringFlag{
Name: "secret",
Required: true,
EnvVars: []string{"ACI_SECRET"},
},
&cli.StringFlag{
Name: "dc-url",
Required: true,
EnvVars: []string{"ACI_DC_URL"},
Value: "https://eu2-cloud.acronis.com",
},
},
Action: func(cCtx *cli.Context) error {
aci := acronis.NewClient(
cCtx.String("client-id"),
cCtx.String("secret"),
cCtx.String("dc-url"),
)

for _, items := range usageData.Items {
for _, usages := range items.Usages {
if usages.Name != "hci_s3_storage" {
continue
tenantId, err := aci.GetTenantID()

Check warning on line 50 in cmd/tenant-usage/main.go

View workflow job for this annotation

GitHub Actions / revive

var tenantId should be tenantID
if err != nil {
return err
}
fmt.Printf("Got tenant id: %s\n\n", tenantId)

app, err := aci.GetApplication(usages.ApplicationID)
usageData, err := aci.GetUsage(tenantId)
if err != nil {
panic(err)
return err
}

fmt.Printf("%s (Type: %s)\n%s -- %s\n\n",
app.Name,
app.Type,
usages.Name,
utils.PrettyByteSize(int(math.Round(usages.AbsoluteValue))),
)
}
for _, items := range usageData.Items {
for _, usages := range items.Usages {
if usages.Name != "hci_s3_storage" {
continue
}

app, err := aci.GetApplication(usages.ApplicationID)
if err != nil {
panic(err)
}

fmt.Printf("%s (Type: %s)\n%s -- %s\n\n",
app.Name,
app.Type,
usages.Name,
utils.PrettyByteSize(int(math.Round(usages.AbsoluteValue))),
)
}
}

return nil
},
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}

0 comments on commit 75d0770

Please sign in to comment.