-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aca0e89
commit 06b4f0c
Showing
9 changed files
with
315 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package get | ||
|
||
import ( | ||
"github.com/awslabs/eksdemo/pkg/resource" | ||
"github.com/awslabs/eksdemo/pkg/resource/sagemaker/domain" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var sagemaker []func() *resource.Resource | ||
|
||
func NewGetSageMakerCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "sagemaker", | ||
Short: "Amazon SageMaker Resources", | ||
Aliases: []string{"sm"}, | ||
} | ||
|
||
// Don't show flag errors for `get sagemaker` without a subcommand | ||
cmd.DisableFlagParsing = true | ||
|
||
for _, r := range sagemaker { | ||
cmd.AddCommand(r().NewGetCmd()) | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func init() { | ||
sagemaker = []func() *resource.Resource{ | ||
domain.New, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/sagemaker" | ||
"github.com/aws/aws-sdk-go-v2/service/sagemaker/types" | ||
) | ||
|
||
type SageMakerClient struct { | ||
*sagemaker.Client | ||
} | ||
|
||
func NewSageMakerClient() *SageMakerClient { | ||
return &SageMakerClient{sagemaker.NewFromConfig(GetConfig())} | ||
} | ||
|
||
func (c *SageMakerClient) DescribeDomain(id string) (*sagemaker.DescribeDomainOutput, error) { | ||
result, err := c.Client.DescribeDomain(context.Background(), &sagemaker.DescribeDomainInput{ | ||
DomainId: aws.String(id), | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (c *SageMakerClient) ListDomains() ([]types.DomainDetails, error) { | ||
result, err := c.Client.ListDomains(context.Background(), &sagemaker.ListDomainsInput{}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return result.Domains, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package domain | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
awssdk "github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/sagemaker" | ||
"github.com/aws/aws-sdk-go-v2/service/sagemaker/types" | ||
"github.com/awslabs/eksdemo/pkg/aws" | ||
"github.com/awslabs/eksdemo/pkg/printer" | ||
"github.com/awslabs/eksdemo/pkg/resource" | ||
) | ||
|
||
type Getter struct { | ||
sagemakerClient *aws.SageMakerClient | ||
} | ||
|
||
func NewGetter(sagemakerClient *aws.SageMakerClient) *Getter { | ||
return &Getter{sagemakerClient} | ||
} | ||
|
||
func (g *Getter) Init() { | ||
if g.sagemakerClient == nil { | ||
g.sagemakerClient = aws.NewSageMakerClient() | ||
} | ||
} | ||
|
||
func (g *Getter) Get(domainName string, output printer.Output, o resource.Options) error { | ||
options, ok := o.(*Options) | ||
if !ok { | ||
return fmt.Errorf("internal error, unable to cast options to domain.Options") | ||
} | ||
|
||
var domain *sagemaker.DescribeDomainOutput | ||
var domains []*sagemaker.DescribeDomainOutput | ||
var err error | ||
|
||
switch { | ||
case domainName != "": | ||
domain, err = g.GetDomainByName(domainName) | ||
domains = []*sagemaker.DescribeDomainOutput{domain} | ||
case options.DomainID != "": | ||
domain, err = g.GetDomainByID(options.DomainID) | ||
domains = []*sagemaker.DescribeDomainOutput{domain} | ||
default: | ||
domains, err = g.GetAllDomains() | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return output.Print(os.Stdout, NewPrinter(domains)) | ||
} | ||
|
||
func (g *Getter) GetAllDomains() ([]*sagemaker.DescribeDomainOutput, error) { | ||
domainDetails, err := g.sagemakerClient.ListDomains() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
domains := make([]*sagemaker.DescribeDomainOutput, 0, len(domainDetails)) | ||
|
||
for _, dd := range domainDetails { | ||
result, err := g.sagemakerClient.DescribeDomain(awssdk.ToString(dd.DomainId)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
domains = append(domains, result) | ||
} | ||
|
||
return domains, nil | ||
} | ||
|
||
func (g *Getter) GetDomainByID(domainID string) (*sagemaker.DescribeDomainOutput, error) { | ||
domain, err := g.sagemakerClient.DescribeDomain(domainID) | ||
if err != nil { | ||
return nil, aws.FormatError(err) | ||
} | ||
|
||
return domain, nil | ||
} | ||
|
||
func (g *Getter) GetDomainByName(domainName string) (*sagemaker.DescribeDomainOutput, error) { | ||
domainDetails, err := g.sagemakerClient.ListDomains() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
found := []types.DomainDetails{} | ||
|
||
for _, dd := range domainDetails { | ||
if strings.EqualFold(domainName, awssdk.ToString(dd.DomainName)) { | ||
found = append(found, dd) | ||
} | ||
} | ||
|
||
if len(found) == 0 { | ||
return nil, &resource.NotFoundByNameError{Type: "sagemaker-domain", Name: domainName} | ||
} | ||
|
||
if len(found) > 1 { | ||
return nil, fmt.Errorf("multiple sagemaker domains found with name: %s", domainName) | ||
} | ||
|
||
domain, err := g.sagemakerClient.DescribeDomain(awssdk.ToString(found[0].DomainId)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return domain, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package domain | ||
|
||
import ( | ||
"github.com/awslabs/eksdemo/pkg/cmd" | ||
"github.com/awslabs/eksdemo/pkg/resource" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type Options struct { | ||
resource.CommonOptions | ||
|
||
// Get | ||
DomainID string | ||
} | ||
|
||
func newOptions() (options *Options, getFlags cmd.Flags) { | ||
options = &Options{ | ||
CommonOptions: resource.CommonOptions{ | ||
Name: "sagemaker-domain", | ||
ClusterFlagDisabled: true, | ||
}, | ||
} | ||
|
||
getFlags = cmd.Flags{ | ||
&cmd.StringFlag{ | ||
CommandFlag: cmd.CommandFlag{ | ||
Name: "id", | ||
Description: "get by id instead of name", | ||
Validate: func(_ *cobra.Command, args []string) error { | ||
if options.DomainID != "" && len(args) > 0 { | ||
return &cmd.ArgumentAndFlagCantBeUsedTogetherError{Arg: "DOMAIN_NAME", Flag: "--id"} | ||
} | ||
return nil | ||
}, | ||
}, | ||
Option: &options.DomainID, | ||
}, | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package domain | ||
|
||
import ( | ||
"io" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/sagemaker" | ||
"github.com/awslabs/eksdemo/pkg/printer" | ||
"github.com/hako/durafmt" | ||
) | ||
|
||
type Printer struct { | ||
domains []*sagemaker.DescribeDomainOutput | ||
} | ||
|
||
func NewPrinter(domains []*sagemaker.DescribeDomainOutput) *Printer { | ||
return &Printer{domains} | ||
} | ||
|
||
func (p *Printer) PrintTable(writer io.Writer) error { | ||
table := printer.NewTablePrinter() | ||
table.SetHeader([]string{"Age", "Status", "Id", "Domain"}) | ||
|
||
for _, d := range p.domains { | ||
age := durafmt.ParseShort(time.Since(aws.ToTime(d.CreationTime))) | ||
|
||
table.AppendRow([]string{ | ||
age.String(), | ||
string(d.Status), | ||
aws.ToString(d.DomainId), | ||
aws.ToString(d.DomainName), | ||
}) | ||
} | ||
|
||
table.Print(writer) | ||
|
||
return nil | ||
} | ||
|
||
func (p *Printer) PrintJSON(writer io.Writer) error { | ||
return printer.EncodeJSON(writer, p.domains) | ||
} | ||
|
||
func (p *Printer) PrintYAML(writer io.Writer) error { | ||
return printer.EncodeYAML(writer, p.domains) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package domain | ||
|
||
import ( | ||
"github.com/awslabs/eksdemo/pkg/cmd" | ||
"github.com/awslabs/eksdemo/pkg/resource" | ||
) | ||
|
||
func New() *resource.Resource { | ||
options, getFlags := newOptions() | ||
|
||
return &resource.Resource{ | ||
Command: cmd.Command{ | ||
Name: "domain", | ||
Description: "SageMaker Domain", | ||
Args: []string{"DOMAIN_NAME"}, | ||
}, | ||
|
||
GetFlags: getFlags, | ||
|
||
Getter: &Getter{}, | ||
|
||
Options: options, | ||
} | ||
} |