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

feat: deploy prebuilt services #57

Merged
merged 1 commit into from
Jan 2, 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
53 changes: 26 additions & 27 deletions internal/cmd/service/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
)

type Options struct {
projectID string
template string
itemCode string
branchName string
name string
keyword string
repoID int
projectID string
template string
marketplaceCode string
branchName string
name string
keyword string
repoID int
}

func NewCmdDeploy(f *cmdutil.Factory) *cobra.Command {
Expand All @@ -39,7 +39,7 @@ func NewCmdDeploy(f *cmdutil.Factory) *cobra.Command {

cmd.Flags().StringVar(&opts.name, "name", "", "Service Name")
cmd.Flags().StringVar(&opts.template, "template", "", "Service template")
cmd.Flags().StringVar(&opts.itemCode, "item-code", "", "Marketplace item code")
cmd.Flags().StringVar(&opts.marketplaceCode, "marketplace-code", "", "Marketplace item code")
cmd.Flags().IntVar(&opts.repoID, "repo-id", 0, "Git repository ID")
cmd.Flags().StringVar(&opts.branchName, "branch-name", "", "Git branch name")
cmd.Flags().StringVar(&opts.keyword, "keyword", "", "Git repository keyword")
Expand All @@ -63,11 +63,11 @@ func runDeployNonInteractive(f *cmdutil.Factory, opts *Options) error {

ctx := context.Background()

if opts.template == "MARKETPLACE" {
opts.name = opts.itemCode
service, err := f.ApiClient.CreateServiceFromMarketplace(ctx, opts.projectID, opts.name, opts.itemCode)
if opts.template == "PREBUILT" {
opts.name = opts.marketplaceCode
service, err := f.ApiClient.CreatePrebuiltService(ctx, opts.projectID, opts.marketplaceCode)
if err != nil {
return fmt.Errorf("create service failed: %w", err)
return fmt.Errorf("create prebuilt service failed: %w", err)
}

f.Log.Infof("Service %s created", service.Name)
Expand All @@ -82,7 +82,7 @@ func runDeployInteractive(f *cmdutil.Factory, opts *Options) error {
return err
}

serviceTemplate, err := f.Prompter.Select("Select service template", "MARKETPLACE", []string{"MARKETPLACE", "GIT"})
serviceTemplate, err := f.Prompter.Select("Select service template", "PREBUILT", []string{"PREBUILT", "GIT"})
if err != nil {
return err
}
Expand All @@ -92,28 +92,27 @@ func runDeployInteractive(f *cmdutil.Factory, opts *Options) error {
if serviceTemplate == 0 {
s := spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval,
spinner.WithColor(cmdutil.SpinnerColor),
spinner.WithSuffix(" Fetching marketplace items..."),
spinner.WithFinalMSG(cmdutil.SuccessIcon+" Marketplace fetched 🌇\n"),
spinner.WithSuffix(" Fetching prebuilt marketplae..."),
spinner.WithFinalMSG(cmdutil.SuccessIcon+" Prebuilt marketplace fetched 🌇\n"),
)
s.Start()
marketplaceItems, err := f.ApiClient.GetMarketplaceItems(ctx)
prebuiltItems, err := f.ApiClient.GetPrebuiltItems(ctx)
if err != nil {
return fmt.Errorf("get marketplace items failed: %w", err)
return fmt.Errorf("get prebuilt marketplace failed: %w", err)
}
s.Stop()

marketplaceItemsList := make([]string, len(marketplaceItems))
for i, item := range marketplaceItems {
marketplaceItemsList[i] = item.Name + " (" + item.Description + ")"
prebuiltItemsList := make([]string, len(prebuiltItems))
for i, item := range prebuiltItems {
prebuiltItemsList[i] = item.Name + " (" + item.Description + ")"
}

index, err := f.Prompter.Select("Select marketplace item", "", marketplaceItemsList)
index, err := f.Prompter.Select("Select prebuilt item", "", prebuiltItemsList)
if err != nil {
return fmt.Errorf("select marketplace item failed: %w", err)
return fmt.Errorf("select prebuilt item failed: %w", err)
}

opts.itemCode = marketplaceItems[index].Code
opts.name = opts.itemCode
opts.marketplaceCode = prebuiltItems[index].ID

// use a closure to get the service name after creation
serviceName := ""
Expand All @@ -131,9 +130,9 @@ func runDeployInteractive(f *cmdutil.Factory, opts *Options) error {
}
s.Start()

service, err := f.ApiClient.CreateServiceFromMarketplace(ctx, opts.projectID, opts.name, opts.itemCode)
service, err := f.ApiClient.CreatePrebuiltService(ctx, opts.projectID, opts.marketplaceCode)
if err != nil {
return fmt.Errorf("create service failed: %w", err)
return fmt.Errorf("create prebuilt service failed: %w", err)
}
serviceName = service.Name

Expand Down Expand Up @@ -208,7 +207,7 @@ func paramCheck(opts *Options) error {
return fmt.Errorf("please specify service template with --template")
}

if opts.template == "MARKETPLACE" && opts.itemCode == "" {
if opts.template == "PREBUILT" && opts.marketplaceCode == "" {
return fmt.Errorf("please specify marketplace item code with --item-code")
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ type (
GetService(ctx context.Context, id, ownerName, projectName, name string) (*model.Service, error)
GetServiceDetailByEnvironment(ctx context.Context, id, ownerName, projectName, name, environmentID string) (*model.ServiceDetail, error)
ServiceMetric(ctx context.Context, id, environmentID, metricType string, startTime, endTime time.Time) (*model.ServiceMetric, error)
GetMarketplaceItems(ctx context.Context) ([]model.MarketplaceItem, error)
GetPrebuiltItems(ctx context.Context) ([]model.PrebuiltItem, error)
SearchGitRepositories(ctx context.Context, keyword *string) ([]model.GitRepo, error)

RestartService(ctx context.Context, id string, environmentID string) error
RedeployService(ctx context.Context, id string, environmentID string) error
SuspendService(ctx context.Context, id string, environmentID string) error
ExposeService(ctx context.Context, id string, environmentID string, projectID string, name string) (*model.TempTCPPort, error)
CreateServiceFromMarketplace(ctx context.Context, projectID string, name string, itemCode string) (*model.Service, error)
CreatePrebuiltService(ctx context.Context, projectID string, marketplaceCode string) (*model.Service, error)
CreateService(ctx context.Context, projectID string, name string, repoID int, branchName string) (*model.Service, error)
CreateEmptyService(ctx context.Context, projectID string, name string) (*model.Service, error)
UploadZipToService(ctx context.Context, projectID string, serviceID string, environmentID string, zipBytes []byte) (*model.Service, error)
Expand Down
17 changes: 8 additions & 9 deletions pkg/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ func (c *client) ExposeService(ctx context.Context, id string, environmentID str
return &mutation.ExposeService, nil
}

func (c *client) GetMarketplaceItems(ctx context.Context) ([]model.MarketplaceItem, error) {
func (c *client) GetPrebuiltItems(ctx context.Context) ([]model.PrebuiltItem, error) {
var query struct {
MarketplaceItems []model.MarketplaceItem `graphql:"marketplaceItems"`
PrebuiltItems []model.PrebuiltItem `graphql:"prebuiltMarketplaceItems"`
}

err := c.Query(ctx, &query, nil)
Expand All @@ -254,25 +254,24 @@ func (c *client) GetMarketplaceItems(ctx context.Context) ([]model.MarketplaceIt
return nil, err
}

return query.MarketplaceItems, nil
return query.PrebuiltItems, nil
}

func (c *client) CreateServiceFromMarketplace(ctx context.Context, projectID string, name string, itemCode string) (*model.Service, error) {
func (c *client) CreatePrebuiltService(ctx context.Context, projectID string, marketplaceCode string) (*model.Service, error) {
var mutation struct {
CreateServiceFromMarketplace model.Service `graphql:"createServiceFromMarketplace(projectID: $projectID, name: $name, itemCode: $itemCode)"`
CreatePrebuiltService model.Service `graphql:"createGenericService(projectID: $projectID, marketplaceCode: $marketplaceCode)"`
}

err := c.Mutate(ctx, &mutation, V{
"projectID": ObjectID(projectID),
"name": name,
"itemCode": itemCode,
"projectID": ObjectID(projectID),
"marketplaceCode": marketplaceCode,
})

if err != nil {
return nil, err
}

return &mutation.CreateServiceFromMarketplace, nil
return &mutation.CreatePrebuiltService, nil
}

func (c *client) SearchGitRepositories(ctx context.Context, keyword *string) ([]model.GitRepo, error) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/model/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ type MarketplaceItem struct {
NetworkType string `graphql:"networkType"`
}

type PrebuiltItem struct {
ID string `graphql:"id"`
Name string `graphql:"name"`
Description string `graphql:"description"`
}

type GitRepo struct {
Name string `graphql:"name"`
Owner string `graphql:"owner"`
Expand Down