From 396ae0ec96806edef02808a9f6f045cddc41e31a Mon Sep 17 00:00:00 2001 From: Aaron Miller Date: Tue, 3 Sep 2024 17:50:23 -0700 Subject: [PATCH] add: `get efs-filesystem` --- cmd/get/get.go | 2 + go.mod | 8 ++-- go.sum | 16 ++++--- pkg/aws/efs.go | 32 +++++++++++++ pkg/resource/efs/filesystem/efs_filesystem.go | 25 ++++++++++ pkg/resource/efs/filesystem/get.go | 32 +++++++++++++ pkg/resource/efs/filesystem/printer.go | 48 +++++++++++++++++++ 7 files changed, 154 insertions(+), 9 deletions(-) create mode 100644 pkg/aws/efs.go create mode 100644 pkg/resource/efs/filesystem/efs_filesystem.go create mode 100644 pkg/resource/efs/filesystem/get.go create mode 100644 pkg/resource/efs/filesystem/printer.go diff --git a/cmd/get/get.go b/cmd/get/get.go index 141958e..3cb3f24 100644 --- a/cmd/get/get.go +++ b/cmd/get/get.go @@ -19,6 +19,7 @@ import ( "github.com/awslabs/eksdemo/pkg/resource/ec2/ami" "github.com/awslabs/eksdemo/pkg/resource/ec2/instance" "github.com/awslabs/eksdemo/pkg/resource/ecr_repository" + efsfilesystem "github.com/awslabs/eksdemo/pkg/resource/efs/filesystem" "github.com/awslabs/eksdemo/pkg/resource/eks/accessentry" "github.com/awslabs/eksdemo/pkg/resource/elastic_ip" "github.com/awslabs/eksdemo/pkg/resource/event_rule" @@ -91,6 +92,7 @@ func NewGetCmd() *cobra.Command { cmd.AddCommand(NewGetAliasCmds(cognitoResources, "cognito-")...) cmd.AddCommand(dns_record.NewResource().NewGetCmd()) cmd.AddCommand(instance.NewResource().NewGetCmd()) + cmd.AddCommand(efsfilesystem.NewResource().NewGetCmd()) cmd.AddCommand(ecr_repository.NewResource().NewGetCmd()) cmd.AddCommand(elastic_ip.NewResource().NewGetCmd()) cmd.AddCommand(event_rule.NewResource().NewGetCmd()) diff --git a/go.mod b/go.mod index 743978f..0fad2d0 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ toolchain go1.21.10 require ( github.com/Masterminds/goutils v1.1.1 github.com/Masterminds/sprig/v3 v3.2.3 - github.com/aws/aws-sdk-go-v2 v1.30.4 + github.com/aws/aws-sdk-go-v2 v1.30.5 github.com/aws/aws-sdk-go-v2/config v1.27.4 github.com/aws/aws-sdk-go-v2/service/acm v1.25.1 github.com/aws/aws-sdk-go-v2/service/amp v1.25.1 @@ -19,6 +19,7 @@ require ( github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider v1.35.1 github.com/aws/aws-sdk-go-v2/service/ec2 v1.149.2 github.com/aws/aws-sdk-go-v2/service/ecr v1.27.1 + github.com/aws/aws-sdk-go-v2/service/efs v1.31.5 github.com/aws/aws-sdk-go-v2/service/eks v1.41.0 github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.24.1 github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2 v1.30.1 @@ -69,8 +70,8 @@ require ( github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.1 // indirect github.com/aws/aws-sdk-go-v2/credentials v1.17.4 // indirect github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.15.2 // indirect - github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.16 // indirect - github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.16 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.17 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.17 // indirect github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.2 // indirect github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.1 // indirect @@ -94,6 +95,7 @@ require ( github.com/docker/docker-credential-helpers v0.7.0 // indirect github.com/docker/go-connections v0.5.0 // indirect github.com/docker/go-metrics v0.0.1 // indirect + github.com/dustin/go-humanize v1.0.1 // indirect github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 // indirect github.com/emicklei/go-restful/v3 v3.11.0 // indirect github.com/evanphx/json-patch v5.7.0+incompatible // indirect diff --git a/go.sum b/go.sum index 3b6d6ea..36f4f7b 100644 --- a/go.sum +++ b/go.sum @@ -71,8 +71,8 @@ github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 h1:4daAzAu0 github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg= github.com/aws/aws-sdk-go v1.34.9 h1:cUGBW9CVdi0mS7K1hDzxIqTpfeWhpoQiguq81M1tjK0= github.com/aws/aws-sdk-go v1.34.9/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0= -github.com/aws/aws-sdk-go-v2 v1.30.4 h1:frhcagrVNrzmT95RJImMHgabt99vkXGslubDaDagTk8= -github.com/aws/aws-sdk-go-v2 v1.30.4/go.mod h1:CT+ZPWXbYrci8chcARI3OmI/qgd+f6WtuLOoaIA8PR0= +github.com/aws/aws-sdk-go-v2 v1.30.5 h1:mWSRTwQAb0aLE17dSzztCVJWI9+cRMgqebndjwDyK0g= +github.com/aws/aws-sdk-go-v2 v1.30.5/go.mod h1:CT+ZPWXbYrci8chcARI3OmI/qgd+f6WtuLOoaIA8PR0= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.1 h1:gTK2uhtAPtFcdRRJilZPx8uJLL2J85xK11nKtWL0wfU= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.1/go.mod h1:sxpLb+nZk7tIfCWChfd+h4QwHNUR57d8hA1cleTkjJo= github.com/aws/aws-sdk-go-v2/config v1.27.4 h1:AhfWb5ZwimdsYTgP7Od8E9L1u4sKmDW2ZVeLcf2O42M= @@ -81,10 +81,10 @@ github.com/aws/aws-sdk-go-v2/credentials v1.17.4 h1:h5Vztbd8qLppiPwX+y0Q6WiwMZgp github.com/aws/aws-sdk-go-v2/credentials v1.17.4/go.mod h1:+30tpwrkOgvkJL1rUZuRLoxcJwtI/OkeBLYnHxJtVe0= github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.15.2 h1:AK0J8iYBFeUk2Ax7O8YpLtFsfhdOByh2QIkHmigpRYk= github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.15.2/go.mod h1:iRlGzMix0SExQEviAyptRWRGdYNo3+ufW/lCzvKVTUc= -github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.16 h1:TNyt/+X43KJ9IJJMjKfa3bNTiZbUP7DeCxfbTROESwY= -github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.16/go.mod h1:2DwJF39FlNAUiX5pAc0UNeiz16lK2t7IaFcm0LFHEgc= -github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.16 h1:jYfy8UPmd+6kJW5YhY0L1/KftReOGxI/4NtVSTh9O/I= -github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.16/go.mod h1:7ZfEPZxkW42Afq4uQB8H2E2e6ebh6mXTueEpYzjCzcs= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.17 h1:pI7Bzt0BJtYA0N/JEC6B8fJ4RBrEMi1LBrkMdFYNSnQ= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.17/go.mod h1:Dh5zzJYMtxfIjYW+/evjQ8uj2OyR/ve2KROHGHlSFqE= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.17 h1:Mqr/V5gvrhA2gvgnF42Zh5iMiQNcOYthFYwCyrnuWlc= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.17/go.mod h1:aLJpZlCmjE+V+KtN1q1uyZkfnUWpQGpbsn89XPKyzfU= github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU= github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY= github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.2 h1:en92G0Z7xlksoOylkUhuBSfJgijC7rHVLRdnIlHEs0E= @@ -109,6 +109,8 @@ github.com/aws/aws-sdk-go-v2/service/ec2 v1.149.2 h1:fkyeLQZoBqfchqru+hDTfioQohQ github.com/aws/aws-sdk-go-v2/service/ec2 v1.149.2/go.mod h1:XxJNg7fIkR8cbm89i0zVZSxKpcPYsC8BWRwMIJOWbnk= github.com/aws/aws-sdk-go-v2/service/ecr v1.27.1 h1:GFt/4yMrCuMDi4YzKy0HCW9NhwbxoYZUMqIWqWJFsqE= github.com/aws/aws-sdk-go-v2/service/ecr v1.27.1/go.mod h1:ydHfHlVpaydWdStDKNcV6BnI0fD+ZwlPWvDgVG2fLt0= +github.com/aws/aws-sdk-go-v2/service/efs v1.31.5 h1:McOTl1R1sTdZx57Q7CaeLFgDnhEMOT/adK8sCpprrjM= +github.com/aws/aws-sdk-go-v2/service/efs v1.31.5/go.mod h1:XGcTOlJ2nuj3KemxjGAGc/fpl2Cb8Z0DE+cTnKHmBGU= github.com/aws/aws-sdk-go-v2/service/eks v1.41.0 h1:/bitqsA6wgIS2vgjtHJi1JG3SOTbobs1mCdeJBLOacY= github.com/aws/aws-sdk-go-v2/service/eks v1.41.0/go.mod h1:GFqWNwDLyuSevADun69Dg5aurANpv8KNrz2vxYPEqmw= github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing v1.24.1 h1:jdxeIGg32hhpOQSRVokquRVh5f6eh3/NChPJmEQ0O9Y= @@ -218,6 +220,8 @@ github.com/docker/go-metrics v0.0.1 h1:AgB/0SvBxihN0X8OR4SjsblXkbMvalQ8cjmtKQ2rQ github.com/docker/go-metrics v0.0.1/go.mod h1:cG1hvH2utMXtqgqqYE9plW6lDxS3/5ayHzueweSI3Vw= github.com/docker/libtrust v0.0.0-20150114040149-fa567046d9b1 h1:ZClxb8laGDf5arXfYcAtECDFgAgHklGI8CxgjHnXKJ4= github.com/docker/libtrust v0.0.0-20150114040149-fa567046d9b1/go.mod h1:cyGadeNEkKy96OOhEzfZl+yxihPEzKnqJwvfuSUqbZE= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 h1:XBBHcIb256gUJtLmY22n99HaZTz+r2Z51xUPi01m3wg= github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203/go.mod h1:E1jcSv8FaEny+OP/5k9UxZVw9YFWGj7eI4KR/iOBqCg= github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g= diff --git a/pkg/aws/efs.go b/pkg/aws/efs.go new file mode 100644 index 0000000..3f06c5d --- /dev/null +++ b/pkg/aws/efs.go @@ -0,0 +1,32 @@ +package aws + +import ( + "context" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/efs" + "github.com/aws/aws-sdk-go-v2/service/efs/types" +) + +type EFSClient struct { + *efs.Client +} + +func NewEFSClient() *EFSClient { + return &EFSClient{efs.NewFromConfig(GetConfig())} +} + +func (c *EFSClient) DescribeFileSystems(fileSystemID string) ([]types.FileSystemDescription, error) { + input := efs.DescribeFileSystemsInput{} + + if fileSystemID != "" { + input.FileSystemId = aws.String(fileSystemID) + } + + result, err := c.Client.DescribeFileSystems(context.Background(), &input) + if err != nil { + return nil, err + } + + return result.FileSystems, nil +} diff --git a/pkg/resource/efs/filesystem/efs_filesystem.go b/pkg/resource/efs/filesystem/efs_filesystem.go new file mode 100644 index 0000000..b6f029c --- /dev/null +++ b/pkg/resource/efs/filesystem/efs_filesystem.go @@ -0,0 +1,25 @@ +package filesystem + +import ( + "github.com/awslabs/eksdemo/pkg/cmd" + "github.com/awslabs/eksdemo/pkg/resource" +) + +func NewResource() *resource.Resource { + res := &resource.Resource{ + Command: cmd.Command{ + Name: "efs-filesystem", + Description: "EFS File Systems", + Aliases: []string{"efs"}, + Args: []string{"ID"}, + }, + + Getter: &Getter{}, + + Options: &resource.CommonOptions{ + ClusterFlagDisabled: true, + }, + } + + return res +} diff --git a/pkg/resource/efs/filesystem/get.go b/pkg/resource/efs/filesystem/get.go new file mode 100644 index 0000000..5181a84 --- /dev/null +++ b/pkg/resource/efs/filesystem/get.go @@ -0,0 +1,32 @@ +package filesystem + +import ( + "os" + + "github.com/awslabs/eksdemo/pkg/aws" + "github.com/awslabs/eksdemo/pkg/printer" + "github.com/awslabs/eksdemo/pkg/resource" +) + +type Getter struct { + efsClient *aws.EFSClient +} + +func NewGetter(efsClient *aws.EFSClient) *Getter { + return &Getter{efsClient} +} + +func (g *Getter) Init() { + if g.efsClient == nil { + g.efsClient = aws.NewEFSClient() + } +} + +func (g *Getter) Get(id string, output printer.Output, options resource.Options) error { + fileSystems, err := g.efsClient.DescribeFileSystems(id) + if err != nil { + return err + } + + return output.Print(os.Stdout, NewPrinter(fileSystems)) +} diff --git a/pkg/resource/efs/filesystem/printer.go b/pkg/resource/efs/filesystem/printer.go new file mode 100644 index 0000000..d38e62f --- /dev/null +++ b/pkg/resource/efs/filesystem/printer.go @@ -0,0 +1,48 @@ +package filesystem + +import ( + "io" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/efs/types" + "github.com/awslabs/eksdemo/pkg/printer" + "github.com/dustin/go-humanize" + "github.com/hako/durafmt" +) + +type Printer struct { + fileSystems []types.FileSystemDescription +} + +func NewPrinter(fileSystems []types.FileSystemDescription) *Printer { + return &Printer{fileSystems} +} + +func (p *Printer) PrintTable(writer io.Writer) error { + table := printer.NewTablePrinter() + table.SetHeader([]string{"Age", "Status", "Id", "Name", "Size"}) + + for _, fs := range p.fileSystems { + age := durafmt.ParseShort(time.Since(aws.ToTime(fs.CreationTime))) + + table.AppendRow([]string{ + age.String(), + string(fs.LifeCycleState), + aws.ToString(fs.FileSystemId), + aws.ToString(fs.Name), + humanize.IBytes(uint64(fs.SizeInBytes.Value)), + }) + } + table.Print(writer) + + return nil +} + +func (p *Printer) PrintJSON(writer io.Writer) error { + return printer.EncodeJSON(writer, p.fileSystems) +} + +func (p *Printer) PrintYAML(writer io.Writer) error { + return printer.EncodeYAML(writer, p.fileSystems) +}