From 148e9615d28707995713eee790d52315b06fef6a Mon Sep 17 00:00:00 2001 From: incubator4 Date: Wed, 5 Jun 2024 19:18:07 +0800 Subject: [PATCH 1/3] feat: add Client function to create custom image service --- pkg/api/interface.go | 1 + pkg/api/service.go | 17 +++++++++++ pkg/model/service.go | 70 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) diff --git a/pkg/api/interface.go b/pkg/api/interface.go index 3d50990..3f6da53 100644 --- a/pkg/api/interface.go +++ b/pkg/api/interface.go @@ -58,6 +58,7 @@ type ( SuspendService(ctx context.Context, id string, environmentID string) error ExposeService(ctx context.Context, id string, environmentID string, projectID string, name string) (*model.TempTCPPort, error) CreatePrebuiltService(ctx context.Context, projectID string, marketplaceCode string) (*model.Service, error) + CreateCustomService(ctx context.Context, projectID string, schema model.ServiceSpecSchemaInput) (*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) diff --git a/pkg/api/service.go b/pkg/api/service.go index ff7c897..1ef0fb0 100644 --- a/pkg/api/service.go +++ b/pkg/api/service.go @@ -292,6 +292,23 @@ func (c *client) CreatePrebuiltService(ctx context.Context, projectID string, ma return &mutation.CreatePrebuiltService, nil } +func (c *client) CreateCustomService(ctx context.Context, projectID string, schema model.ServiceSpecSchemaInput) (*model.Service, error) { + var mutation struct { + CreateCustomService model.Service `graphql:"createPrebuiltService(projectID: $projectID, schema: $schema)"` + } + + err := c.Mutate(ctx, &mutation, V{ + "projectID": ObjectID(projectID), + "schema": schema, + }) + + if err != nil { + return nil, err + } + + return &mutation.CreateCustomService, nil +} + func (c *client) SearchGitRepositories(ctx context.Context, keyword *string) ([]model.GitRepo, error) { var query struct { SearchGitRepositories []model.GitRepo `graphql:"searchGitRepositories(Limit: 5, provider: GITHUB, keyword: $keyword)"` diff --git a/pkg/model/service.go b/pkg/model/service.go index 4126767..c1e52ec 100644 --- a/pkg/model/service.go +++ b/pkg/model/service.go @@ -230,3 +230,73 @@ type ServiceInstruction struct { Title string `graphql:"title"` Type ServiceSpecConnectionInstructionType `graphql:"type"` } + +type ServiceSpecSchemaInput struct { + ID string `json:"id" graphql:"id"` + Name string `json:"name" graphql:"name"` + Description string `json:"description" graphql:"description"` + Tags []string `json:"tags" graphql:"tags"` + Icon string `json:"icon" graphql:"icon"` + Docs string `json:"docs" graphql:"docs"` + + Source *ServiceSpecSourceInput `json:"source" graphql:"source"` + Ports []ServiceSpecPortInput `json:"ports" graphql:"ports"` + Volumes []ServiceSpecVolumeEntryInput `json:"volumes" graphql:"volumes"` + Instructions []ServiceInstruction `json:"instructions" graphql:"instructions"` + Env []ServiceSpecEnvInput `json:"env" graphql:"env"` + InitRules []ServiceSpecInitRuleInput `json:"initRules" graphql:"initRules"` + Configs []ServiceSpecConfigInput `json:"configs" graphql:"configs"` +} + +type ServiceSpecEnvInput struct { + Key string `json:"key" graphql:"key"` + Required bool `json:"required" graphql:"required"` + Default string `json:"default" graphql:"default"` + Expose bool `json:"expose" graphql:"expose"` + Readonly bool `json:"readonly" graphql:"readonly"` +} + +type ServiceSpecSourceInput struct { + Image string `json:"image" graphql:"image"` + Command []string `json:"command" graphql:"command"` + Args []string `json:"args" graphql:"args"` + + // Git only fields + Source string `json:"source,omitempty" graphql:"source"` + RepoID int `json:"repoID,omitempty" graphql:"repoID"` + Branch string `json:"branch,omitempty" graphql:"branch"` + SubModuleName string `json:"subModuleName,omitempty" graphql:"subModuleName"` + WatchPaths []string `json:"watchPaths,omitempty" graphql:"watchPaths"` +} + +type ServiceSpecPortInput struct { + ID string `json:"id" graphql:"id"` + Port int `json:"port" graphql:"port"` + // Type is the type of the port, e.g. TCP, UDP, HTTP. + Type string `json:"type" graphql:"type"` +} + +type ServiceSpecVolumeEntryInput struct { + ID string `json:"id" graphql:"id"` + // Dir should be started with `/` (absolute). + Dir string `json:"dir" graphql:"dir"` +} + +type ServiceSpecInitVolumeMountInput struct { + ID string `json:"id" graphql:"id"` + MountPath string `json:"mountPath" graphql:"mountPath"` + SubPath string `json:"subPath" graphql:"subPath"` +} + +type ServiceSpecInitRuleInput struct { + ID string `json:"id" graphql:"id"` + Image string `json:"image" graphql:"image"` + Command []string `json:"command" graphql:"command"` + + Volumes []ServiceSpecInitVolumeMountInput `json:"volumes" graphql:"volumes"` +} + +type ServiceSpecConfigInput struct { + Path string `json:"path" graphql:"path"` + Template string `json:"template" graphql:"template"` +} From 1c835d009e38aeea7abf5975fba0822114af2ee0 Mon Sep 17 00:00:00 2001 From: incubator4 Date: Wed, 5 Jun 2024 21:26:08 +0800 Subject: [PATCH 2/3] feat: add function to create service by rawSchema string --- pkg/api/interface.go | 1 + pkg/api/service.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pkg/api/interface.go b/pkg/api/interface.go index 3f6da53..96d6b78 100644 --- a/pkg/api/interface.go +++ b/pkg/api/interface.go @@ -59,6 +59,7 @@ type ( ExposeService(ctx context.Context, id string, environmentID string, projectID string, name string) (*model.TempTCPPort, error) CreatePrebuiltService(ctx context.Context, projectID string, marketplaceCode string) (*model.Service, error) CreateCustomService(ctx context.Context, projectID string, schema model.ServiceSpecSchemaInput) (*model.Service, error) + CreateRawService(ctx context.Context, projectID string, rawSchema 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) diff --git a/pkg/api/service.go b/pkg/api/service.go index 1ef0fb0..3f40980 100644 --- a/pkg/api/service.go +++ b/pkg/api/service.go @@ -292,6 +292,23 @@ func (c *client) CreatePrebuiltService(ctx context.Context, projectID string, ma return &mutation.CreatePrebuiltService, nil } +func (c *client) CreateRawService(ctx context.Context, projectID string, rawSchema string) (*model.Service, error) { + var mutation struct { + CreateCustomService model.Service `graphql:"createPrebuiltService(projectID: $projectID, rawSchema: $rawSchema)"` + } + + err := c.Mutate(ctx, &mutation, V{ + "projectID": ObjectID(projectID), + "schema": rawSchema, + }) + + if err != nil { + return nil, err + } + + return &mutation.CreateCustomService, nil +} + func (c *client) CreateCustomService(ctx context.Context, projectID string, schema model.ServiceSpecSchemaInput) (*model.Service, error) { var mutation struct { CreateCustomService model.Service `graphql:"createPrebuiltService(projectID: $projectID, schema: $schema)"` From f76f6e1609ffaeeeb7b63ed1ff6e7c11b76cf262 Mon Sep 17 00:00:00 2001 From: incubator4 Date: Thu, 6 Jun 2024 11:17:50 +0800 Subject: [PATCH 3/3] chore: rename function name --- pkg/api/interface.go | 4 ++-- pkg/api/service.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/api/interface.go b/pkg/api/interface.go index 96d6b78..5559507 100644 --- a/pkg/api/interface.go +++ b/pkg/api/interface.go @@ -58,8 +58,8 @@ type ( SuspendService(ctx context.Context, id string, environmentID string) error ExposeService(ctx context.Context, id string, environmentID string, projectID string, name string) (*model.TempTCPPort, error) CreatePrebuiltService(ctx context.Context, projectID string, marketplaceCode string) (*model.Service, error) - CreateCustomService(ctx context.Context, projectID string, schema model.ServiceSpecSchemaInput) (*model.Service, error) - CreateRawService(ctx context.Context, projectID string, rawSchema string) (*model.Service, error) + CreatePrebuiltServiceCustom(ctx context.Context, projectID string, schema model.ServiceSpecSchemaInput) (*model.Service, error) + CreatePrebuiltServiceRaw(ctx context.Context, projectID string, rawSchema 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) diff --git a/pkg/api/service.go b/pkg/api/service.go index 3f40980..0abedda 100644 --- a/pkg/api/service.go +++ b/pkg/api/service.go @@ -292,7 +292,7 @@ func (c *client) CreatePrebuiltService(ctx context.Context, projectID string, ma return &mutation.CreatePrebuiltService, nil } -func (c *client) CreateRawService(ctx context.Context, projectID string, rawSchema string) (*model.Service, error) { +func (c *client) CreatePrebuiltServiceRaw(ctx context.Context, projectID string, rawSchema string) (*model.Service, error) { var mutation struct { CreateCustomService model.Service `graphql:"createPrebuiltService(projectID: $projectID, rawSchema: $rawSchema)"` } @@ -309,7 +309,7 @@ func (c *client) CreateRawService(ctx context.Context, projectID string, rawSche return &mutation.CreateCustomService, nil } -func (c *client) CreateCustomService(ctx context.Context, projectID string, schema model.ServiceSpecSchemaInput) (*model.Service, error) { +func (c *client) CreatePrebuiltServiceCustom(ctx context.Context, projectID string, schema model.ServiceSpecSchemaInput) (*model.Service, error) { var mutation struct { CreateCustomService model.Service `graphql:"createPrebuiltService(projectID: $projectID, schema: $schema)"` }