From f2bc8da4fec7fbf2c4834a24e99050c2fe90130a Mon Sep 17 00:00:00 2001 From: "Pilz, Alexander" Date: Fri, 6 Oct 2023 10:54:04 +0200 Subject: [PATCH] implement get tenant history command --- cmd/tenant.go | 34 ++++++++++++++++++++++- docs/metalctl_tenant.md | 1 + docs/metalctl_tenant_history.md | 48 +++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 4 +-- 5 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 docs/metalctl_tenant_history.md diff --git a/cmd/tenant.go b/cmd/tenant.go index 714a759..38a7ebd 100644 --- a/cmd/tenant.go +++ b/cmd/tenant.go @@ -3,7 +3,10 @@ package cmd import ( "errors" "fmt" + "strconv" + "time" + "github.com/go-openapi/strfmt" tenantmodel "github.com/metal-stack/metal-go/api/client/tenant" "github.com/metal-stack/metal-go/api/models" "github.com/metal-stack/metal-lib/pkg/genericcli" @@ -54,7 +57,18 @@ func newTenantCmd(c *config) *cobra.Command { }, } - return genericcli.NewCmds(cmdsConfig) + tenantHistoryCmd := &cobra.Command{ + Use: "history", + Short: "get tenant at given timestamp", + RunE: func(cmd *cobra.Command, args []string) error { + return w.GetTenantHistory() + }, + } + + tenantHistoryCmd.Flags().StringP("id", "", "", "ID of tenant") + tenantHistoryCmd.Flags().StringP("at", "", "", "tenant will be returned at it was at this timestamp") + + return genericcli.NewCmds(cmdsConfig, tenantHistoryCmd) } func (c tenantCmd) Get(id string) (*models.V1TenantResponse, error) { @@ -208,3 +222,21 @@ func (w *tenantCmd) createFromCLI() (*models.V1TenantCreateRequest, error) { }, nil } + +func (c tenantCmd) GetTenantHistory() error { + id, err := strconv.ParseInt(viper.GetString("at"), 10, 64) + if err != nil { + return err + } + + params := tenantmodel.NewGetTenantHistoryParams().WithID(viper.GetString("id")).WithBody(&models.V1TenantGetHistoryRequest{ + At: strfmt.DateTime(time.Unix(id, 0)), + }) + + resp, err := c.client.Tenant().GetTenantHistory(params, nil) + if err != nil { + return err + } + + return c.listPrinter.Print(resp.Payload) +} diff --git a/docs/metalctl_tenant.md b/docs/metalctl_tenant.md index afacabb..386fc88 100644 --- a/docs/metalctl_tenant.md +++ b/docs/metalctl_tenant.md @@ -48,6 +48,7 @@ a tenant belongs to a tenant and groups together entities in metal-stack. * [metalctl tenant delete](metalctl_tenant_delete.md) - deletes the tenant * [metalctl tenant describe](metalctl_tenant_describe.md) - describes the tenant * [metalctl tenant edit](metalctl_tenant_edit.md) - edit the tenant through an editor and update +* [metalctl tenant history](metalctl_tenant_history.md) - get tenant at given timestamp * [metalctl tenant list](metalctl_tenant_list.md) - list all tenants * [metalctl tenant update](metalctl_tenant_update.md) - updates the tenant diff --git a/docs/metalctl_tenant_history.md b/docs/metalctl_tenant_history.md new file mode 100644 index 0000000..6212a3b --- /dev/null +++ b/docs/metalctl_tenant_history.md @@ -0,0 +1,48 @@ +## metalctl tenant history + +get tenant at given timestamp + +``` +metalctl tenant history [flags] +``` + +### Options + +``` + --at string tenant will be returned at it was at this timestamp + -h, --help help for history + --id string ID of tenant +``` + +### Options inherited from parent commands + +``` + --api-token string api token to authenticate. Can be specified with METALCTL_API_TOKEN environment variable. + --api-url string api server address. Can be specified with METALCTL_API_URL environment variable. + -c, --config string alternative config file path, (default is ~/.metalctl/config.yaml). + Example config.yaml: + + --- + apitoken: "alongtoken" + ... + + + --debug debug output + --force-color force colored output even without tty + --kubeconfig string Path to the kube-config to use for authentication and authorization. Is updated by login. Uses default path if not specified. + --no-headers do not print headers of table output format (default print headers) + -o, --output-format string output format (table|wide|markdown|json|yaml|template), wide is a table with more columns. (default "table") + --template string output template for template output-format, go template format. + For property names inspect the output of -o json or -o yaml for reference. + Example for machines: + + metalctl machine list -o template --template "{{ .id }}:{{ .size.id }}" + + + --yes-i-really-mean-it skips security prompts (which can be dangerous to set blindly because actions can lead to data loss or additional costs) +``` + +### SEE ALSO + +* [metalctl tenant](metalctl_tenant.md) - manage tenant entities + diff --git a/go.mod b/go.mod index c77efc5..c118f26 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/go-openapi/runtime v0.26.0 github.com/go-openapi/strfmt v0.21.7 github.com/google/go-cmp v0.5.9 - github.com/metal-stack/metal-go v0.24.1 + github.com/metal-stack/metal-go v0.24.2-0.20231006065021-96e01902da6a github.com/metal-stack/metal-lib v0.13.3 github.com/metal-stack/updater v1.1.5 github.com/metal-stack/v v1.0.3 diff --git a/go.sum b/go.sum index 4f3bb6e..bb2dd5a 100644 --- a/go.sum +++ b/go.sum @@ -443,8 +443,8 @@ github.com/mdlayher/sdnotify v1.0.0 h1:Ma9XeLVN/l0qpyx1tNeMSeTjCPH6NtuD6/N9XdTlQ github.com/mdlayher/sdnotify v1.0.0/go.mod h1:HQUmpM4XgYkhDLtd+Uad8ZFK1T9D5+pNxnXQjCeJlGE= github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U= github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA= -github.com/metal-stack/metal-go v0.24.1 h1:S4g3LXeWBELCKIyyCsRMa9B1pC5EClqcX35oV/8xv2U= -github.com/metal-stack/metal-go v0.24.1/go.mod h1:jNJ0dWIBRwKeJoP+RGqTyE5qLsdZFISFrNHU5m3IDwA= +github.com/metal-stack/metal-go v0.24.2-0.20231006065021-96e01902da6a h1:QNNX5jguRm07QKSJ2CX7C7/82qgTFhMYwsgwUDhbUtc= +github.com/metal-stack/metal-go v0.24.2-0.20231006065021-96e01902da6a/go.mod h1:jNJ0dWIBRwKeJoP+RGqTyE5qLsdZFISFrNHU5m3IDwA= github.com/metal-stack/metal-lib v0.13.3 h1:BOhwcKHILmBZd2pz2YMOhj8QxzDaz3G0F/CGuYhnu8o= github.com/metal-stack/metal-lib v0.13.3/go.mod h1:BAR7fjdoV7DDg8i9GpJQBDaNSFirOcBs0vLYTBnhHQU= github.com/metal-stack/security v0.6.7 h1:8wstGy0pdUmphVclAlT+9RKQmx9lF+cIGklJZAB5cIc=