Skip to content

Commit

Permalink
add: delete sagemaker-user-profile (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroniscode authored Sep 5, 2024
1 parent 0ce9a0b commit 536b7ac
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 7 deletions.
3 changes: 3 additions & 0 deletions cmd/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func NewDeleteCmd() *cobra.Command {
cmd.AddCommand(log_group.NewResource().NewDeleteCmd())
cmd.AddCommand(nodegroup.NewResource().NewDeleteCmd())
cmd.AddCommand(organization.NewResource().NewDeleteCmd())
cmd.AddCommand(NewSageMakerCmd())
cmd.AddCommand(NewDeleteAliasCmds(sagemaker, "sagemaker-")...)
cmd.AddCommand(NewDeleteAliasCmds(sagemaker, "sm-")...)
cmd.AddCommand(security_group.NewResource().NewDeleteCmd())
cmd.AddCommand(target_group.NewResource().NewDeleteCmd())
cmd.AddCommand(volume.NewResource().NewDeleteCmd())
Expand Down
32 changes: 32 additions & 0 deletions cmd/delete/sagemaker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package delete

import (
"github.com/awslabs/eksdemo/pkg/resource"
"github.com/awslabs/eksdemo/pkg/resource/sagemaker/userprofile"
"github.com/spf13/cobra"
)

var sagemaker []func() *resource.Resource

func NewSageMakerCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "sagemaker",
Short: "Amazon SageMaker Resources",
Aliases: []string{"sm"},
}

// Don't show flag errors for `delete sagemaker` without a subcommand
cmd.DisableFlagParsing = true

for _, sm := range sagemaker {
cmd.AddCommand(sm().NewDeleteCmd())
}

return cmd
}

func init() {
sagemaker = []func() *resource.Resource{
userprofile.New,
}
}
9 changes: 9 additions & 0 deletions pkg/aws/sagemaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ func NewSageMakerClient() *SageMakerClient {
return &SageMakerClient{sagemaker.NewFromConfig(GetConfig())}
}

func (c *SageMakerClient) DeleteUserProfile(domainID, userProfileName string) error {
_, err := c.Client.DeleteUserProfile(context.Background(), &sagemaker.DeleteUserProfileInput{
DomainId: aws.String(domainID),
UserProfileName: aws.String(userProfileName),
})

return err
}

func (c *SageMakerClient) DescribeDomain(id string) (*sagemaker.DescribeDomainOutput, error) {
result, err := c.Client.DescribeDomain(context.Background(), &sagemaker.DescribeDomainInput{
DomainId: aws.String(id),
Expand Down
34 changes: 31 additions & 3 deletions pkg/resource/manager.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
package resource

import (
"fmt"

"github.com/spf13/cobra"
)

type Manager interface {
Create(options Options) error
Delete(options Options) error
Create(Options) error
Delete(Options) error
Init()
SetDryRun()
Update(options Options, cmd *cobra.Command) error
Update(Options, *cobra.Command) error
}

type CreateNotSupported struct{}

func (*CreateNotSupported) Create(_ Options) error {
return fmt.Errorf("create not supported for this resource")
}

type DeleteNotSupported struct{}

func (*DeleteNotSupported) Delete(_ Options) error {
return fmt.Errorf("delete not supported for this resource")
}

type UpdateNotSupported struct{}

func (*UpdateNotSupported) Update(_ Options, _ *cobra.Command) error {
return fmt.Errorf("update not supported for this resource")
}

type DryRun struct {
DryRun bool
}

func (m *DryRun) SetDryRun() {
m.DryRun = true
}
57 changes: 57 additions & 0 deletions pkg/resource/sagemaker/userprofile/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package userprofile

import (
"errors"
"fmt"

awssdk "github.com/aws/aws-sdk-go-v2/aws"
"github.com/awslabs/eksdemo/pkg/aws"
"github.com/awslabs/eksdemo/pkg/resource"
)

type Manager struct {
resource.CreateNotSupported
resource.DryRun
resource.UpdateNotSupported
sagemakerClient *aws.SageMakerClient
userProfileGetter *Getter
}

func (m *Manager) Init() {
if m.sagemakerClient == nil {
m.sagemakerClient = aws.NewSageMakerClient()
}
m.userProfileGetter = NewGetter(m.sagemakerClient)
}

func (m *Manager) Delete(o resource.Options) error {
options, ok := o.(*Options)
if !ok {
return fmt.Errorf("internal error, unable to cast options to userprofile.Options")
}

domainID := options.DomainID
userProfileName := options.Name

if domainID == "" {
userProfile, err := m.userProfileGetter.GetUserProfileByName(userProfileName)

if err != nil {
var rnfe *resource.NotFoundByNameError
if errors.As(err, &rnfe) {
fmt.Printf("SageMaker User Profile with name %q does not exist\n", userProfileName)
return nil
}
return err
}
domainID = awssdk.ToString(userProfile.DomainId)
}

err := m.sagemakerClient.DeleteUserProfile(domainID, userProfileName)
if err != nil {
return aws.FormatErrorAsMessageOnly(err)
}
fmt.Printf("SageMaker User Profile %q with Domain Id %q deleted\n", userProfileName, domainID)

return nil
}
15 changes: 13 additions & 2 deletions pkg/resource/sagemaker/userprofile/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,29 @@ import (
type Options struct {
resource.CommonOptions

// Get
// Get, Delete
DomainID string
}

func newOptions() (options *Options, getFlags cmd.Flags) {
func newOptions() (options *Options, deleteFlags, getFlags cmd.Flags) {
options = &Options{
CommonOptions: resource.CommonOptions{
Name: "sagemaker-domain",
ClusterFlagDisabled: true,
},
}

deleteFlags = cmd.Flags{
&cmd.StringFlag{
CommandFlag: cmd.CommandFlag{
Name: "domain-id",
Description: "id of the sagemaker domain",
Shorthand: "D",
},
Option: &options.DomainID,
},
}

getFlags = cmd.Flags{
&cmd.StringFlag{
CommandFlag: cmd.CommandFlag{
Expand Down
7 changes: 5 additions & 2 deletions pkg/resource/sagemaker/userprofile/user_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func New() *resource.Resource {
options, getFlags := newOptions()
options, deleteFlags, getFlags := newOptions()

return &resource.Resource{
Command: cmd.Command{
Expand All @@ -16,10 +16,13 @@ func New() *resource.Resource {
Args: []string{"USER_PROFILE_NAME"},
},

GetFlags: getFlags,
DeleteFlags: deleteFlags,
GetFlags: getFlags,

Getter: &Getter{},

Manager: &Manager{},

Options: options,
}
}

0 comments on commit 536b7ac

Please sign in to comment.