Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: delete sagemaker domain #232

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/delete/sagemaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package delete

import (
"github.com/awslabs/eksdemo/pkg/resource"
"github.com/awslabs/eksdemo/pkg/resource/sagemaker/domain"
"github.com/awslabs/eksdemo/pkg/resource/sagemaker/userprofile"
"github.com/spf13/cobra"
)
Expand All @@ -27,6 +28,7 @@ func NewSageMakerCmd() *cobra.Command {

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

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

return err
}

func (c *SageMakerClient) DeleteUserProfile(domainID, userProfileName string) error {
_, err := c.Client.DeleteUserProfile(context.Background(), &sagemaker.DeleteUserProfileInput{
DomainId: aws.String(domainID),
Expand Down
9 changes: 9 additions & 0 deletions pkg/cmd/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ type FlagRequiresFlagError struct {
func (e *FlagRequiresFlagError) Error() string {
return fmt.Sprintf("%q flag requires %q flag", e.Flag1, e.Flag2)
}

type MustIncludeEitherArgumentOrFlag struct {
Arg string
Flag string
}

func (e *MustIncludeEitherArgumentOrFlag) Error() string {
return fmt.Sprintf("must include either %q argument or %q flag", e.Arg, e.Flag)
}
58 changes: 58 additions & 0 deletions pkg/resource/sagemaker/domain/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package domain

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
domainGetter *Getter
}

func (m *Manager) Init() {
if m.sagemakerClient == nil {
m.sagemakerClient = aws.NewSageMakerClient()
}
m.domainGetter = 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 domain.Options")
}

domainID := options.DomainID
domainName := options.Name

if domainID == "" {
domain, err := m.domainGetter.GetDomainByName(domainName)

if err != nil {
var rnfe *resource.NotFoundByNameError
if errors.As(err, &rnfe) {
fmt.Printf("SageMaker Domain with name %q does not exist\n", domainName)
return nil
}
return err
}

domainID = awssdk.ToString(domain.DomainId)
}

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

return nil
}
27 changes: 23 additions & 4 deletions pkg/resource/sagemaker/domain/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,34 @@ 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,
ClusterFlagDisabled: true,
DeleteArgumentOptional: true,
},
}

deleteFlags = cmd.Flags{
&cmd.StringFlag{
CommandFlag: cmd.CommandFlag{
Name: "id",
Description: "delete by id instead of name",
Validate: func(_ *cobra.Command, args []string) error {
if len(args) == 0 && options.DomainID == "" {
return &cmd.MustIncludeEitherArgumentOrFlag{Arg: "DOMAIN_NAME", Flag: "--id"}
}
if options.DomainID != "" && len(args) > 0 {
return &cmd.ArgumentAndFlagCantBeUsedTogetherError{Arg: "DOMAIN_NAME", Flag: "--id"}
}
return nil
},
},
Option: &options.DomainID,
},
}

Expand Down
8 changes: 6 additions & 2 deletions pkg/resource/sagemaker/domain/sagemaker_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ import (
)

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

return &resource.Resource{
Command: cmd.Command{
Name: "domain",
Description: "SageMaker Domain",
Aliases: []string{"do"},
Args: []string{"DOMAIN_NAME"},
},

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

Getter: &Getter{},

Manager: &Manager{},

Options: options,
}
}