-
Notifications
You must be signed in to change notification settings - Fork 45
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
5d813ea
commit d8fb5f5
Showing
9 changed files
with
267 additions
and
10 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
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
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,116 @@ | ||
package userprofile | ||
|
||
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(userProfileName 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 userProfile *sagemaker.DescribeUserProfileOutput | ||
var userProfiles []*sagemaker.DescribeUserProfileOutput | ||
var err error | ||
|
||
switch { | ||
case userProfileName != "": | ||
userProfile, err = g.GetUserProfileByName(userProfileName) | ||
userProfiles = []*sagemaker.DescribeUserProfileOutput{userProfile} | ||
case options.DomainID != "": | ||
userProfiles, err = g.GetUserProfilesByDomainID(options.DomainID) | ||
default: | ||
userProfiles, err = g.GetAllUserProfiles() | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return output.Print(os.Stdout, NewPrinter(userProfiles)) | ||
} | ||
|
||
func (g *Getter) GetAllUserProfiles() ([]*sagemaker.DescribeUserProfileOutput, error) { | ||
return g.getUserProfiles("", "") | ||
} | ||
|
||
func (g *Getter) GetUserProfilesByDomainID(domainID string) ([]*sagemaker.DescribeUserProfileOutput, error) { | ||
return g.getUserProfiles(domainID, "") | ||
|
||
} | ||
|
||
func (g *Getter) GetUserProfileByName(userProfileName string) (*sagemaker.DescribeUserProfileOutput, error) { | ||
profileDetails, err := g.sagemakerClient.ListUserProfiles("", "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
found := []types.UserProfileDetails{} | ||
|
||
for _, up := range profileDetails { | ||
if strings.EqualFold(userProfileName, awssdk.ToString(up.UserProfileName)) { | ||
found = append(found, up) | ||
} | ||
} | ||
|
||
if len(found) == 0 { | ||
return nil, &resource.NotFoundByNameError{Type: "sagemaker-user-profile", Name: userProfileName} | ||
} | ||
|
||
if len(found) > 1 { | ||
return nil, fmt.Errorf("multiple sagemaker user profiles found with name: %s", userProfileName) | ||
} | ||
|
||
userProfile, err := g.sagemakerClient.DescribeUserProfile(awssdk.ToString(found[0].DomainId), userProfileName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return userProfile, nil | ||
} | ||
|
||
func (g *Getter) getUserProfiles(domainID, userProfileNameContains string) ([]*sagemaker.DescribeUserProfileOutput, error) { | ||
profileDetails, err := g.sagemakerClient.ListUserProfiles(domainID, userProfileNameContains) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
userprofiles := make([]*sagemaker.DescribeUserProfileOutput, 0, len(profileDetails)) | ||
|
||
for _, up := range profileDetails { | ||
result, err := g.sagemakerClient.DescribeUserProfile( | ||
awssdk.ToString(up.DomainId), | ||
awssdk.ToString(up.UserProfileName), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
userprofiles = append(userprofiles, result) | ||
} | ||
|
||
return userprofiles, 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,42 @@ | ||
package userprofile | ||
|
||
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: "domain-id", | ||
Description: "id of the sagemaker domain", | ||
Shorthand: "D", | ||
Validate: func(_ *cobra.Command, args []string) error { | ||
if len(args) > 0 && options.DomainID != "" { | ||
return &cmd.ArgumentAndFlagCantBeUsedTogetherError{Arg: "USER_PROFILE_NAME", Flag: "domain-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 userprofile | ||
|
||
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 { | ||
userProfiles []*sagemaker.DescribeUserProfileOutput | ||
} | ||
|
||
func NewPrinter(userProfiles []*sagemaker.DescribeUserProfileOutput) *Printer { | ||
return &Printer{userProfiles} | ||
} | ||
|
||
func (p *Printer) PrintTable(writer io.Writer) error { | ||
table := printer.NewTablePrinter() | ||
table.SetHeader([]string{"Age", "Status", "User Profile", "Domain Id"}) | ||
|
||
for _, up := range p.userProfiles { | ||
age := durafmt.ParseShort(time.Since(aws.ToTime(up.CreationTime))) | ||
|
||
table.AppendRow([]string{ | ||
age.String(), | ||
string(up.Status), | ||
aws.ToString(up.UserProfileName), | ||
aws.ToString(up.DomainId), | ||
}) | ||
} | ||
|
||
table.Print(writer) | ||
|
||
return nil | ||
} | ||
|
||
func (p *Printer) PrintJSON(writer io.Writer) error { | ||
return printer.EncodeJSON(writer, p.userProfiles) | ||
} | ||
|
||
func (p *Printer) PrintYAML(writer io.Writer) error { | ||
return printer.EncodeYAML(writer, p.userProfiles) | ||
} |
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,25 @@ | ||
package userprofile | ||
|
||
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: "user-profile", | ||
Description: "SageMaker User Profile", | ||
Aliases: []string{"user-profiles", "userprofiles", "userprofile", "up"}, | ||
Args: []string{"USER_PROFILE_NAME"}, | ||
}, | ||
|
||
GetFlags: getFlags, | ||
|
||
Getter: &Getter{}, | ||
|
||
Options: options, | ||
} | ||
} |