Skip to content

Commit

Permalink
add handle for create CD service
Browse files Browse the repository at this point in the history
  • Loading branch information
zreigz committed Oct 4, 2023
1 parent fab5f27 commit cfd5452
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
28 changes: 14 additions & 14 deletions cmd/plural/cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,21 +225,21 @@ func (p *Plural) handleListCDRepositories(_ *cli.Context) error {

}

func (p *Plural) handleListClusterServices(c *cli.Context) error {
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
return err
}
input := c.Args().Get(0)
var handle *string
var clusterId *string
func getClusterIdClusterName(input string) (clusterId, clusterName *string) {
if strings.HasPrefix(input, "@") {
h := strings.Trim(input, "@")
handle = &h
clusterName = &h
} else {
clusterId = &input
}
return
}

sd, err := p.ConsoleClient.ListClusterServices(clusterId, handle)
func (p *Plural) handleListClusterServices(c *cli.Context) error {
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
return err
}
sd, err := p.ConsoleClient.ListClusterServices(getClusterIdClusterName(c.Args().Get(0)))
if err != nil {
return err
}
Expand All @@ -258,7 +258,6 @@ func (p *Plural) handleCreateClusterService(c *cli.Context) error {
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
return err
}
clusterId := c.Args().Get(0)
v, err := validateFlag(c, "version", "0.0.1")
if err != nil {
return err
Expand All @@ -278,7 +277,7 @@ func (p *Plural) handleCreateClusterService(c *cli.Context) error {
RepositoryID: repoId,
Git: gqlclient.GitRefAttributes{
Ref: gitRef,
Folder: c.String(gitFolder),
Folder: gitFolder,
},
Configuration: []*gqlclient.ConfigAttributes{},
}
Expand Down Expand Up @@ -308,7 +307,8 @@ func (p *Plural) handleCreateClusterService(c *cli.Context) error {
}
}

sd, err := p.ConsoleClient.CreateClusterService(clusterId, attributes)
clusterId, clusterName := getClusterIdClusterName(c.Args().Get(0))
sd, err := p.ConsoleClient.CreateClusterService(clusterId, clusterName, attributes)
if err != nil {
return err
}
Expand All @@ -317,8 +317,8 @@ func (p *Plural) handleCreateClusterService(c *cli.Context) error {
}

headers := []string{"Id", "Name", "Namespace", "Git Ref", "Git Folder", "Repo"}
return utils.PrintTable([]*gqlclient.CreateServiceDeployment{sd}, headers, func(sd *gqlclient.CreateServiceDeployment) ([]string, error) {
return []string{sd.CreateServiceDeployment.ID, sd.CreateServiceDeployment.Name, sd.CreateServiceDeployment.Namespace, sd.CreateServiceDeployment.Git.Ref, sd.CreateServiceDeployment.Git.Folder, sd.CreateServiceDeployment.Repository.URL}, nil
return utils.PrintTable([]*gqlclient.ServiceDeploymentFragment{sd}, headers, func(sd *gqlclient.ServiceDeploymentFragment) ([]string, error) {
return []string{sd.ID, sd.Name, sd.Namespace, sd.Git.Ref, sd.Git.Folder, sd.Repository.URL}, nil
})
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ require (
github.com/packethost/packngo v0.29.0
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/pluralsh/cluster-api-migration v0.2.15
github.com/pluralsh/console-client-go v0.0.12
github.com/pluralsh/console-client-go v0.0.13
github.com/pluralsh/gqlclient v1.10.0
github.com/pluralsh/plural-operator v0.5.5
github.com/pluralsh/polly v0.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1389,8 +1389,8 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pluralsh/cluster-api-migration v0.2.15 h1:TIfusD+wnhZTGmwNfIlKlKJOT2dE3rUaZawDJw98GjY=
github.com/pluralsh/cluster-api-migration v0.2.15/go.mod h1:J6lEvC/70KouikX16mE331cxc3y3sBwtmfHGwZqu06w=
github.com/pluralsh/console-client-go v0.0.12 h1:BwW0R3Xi7pxx+tC4Vlvm43aJTz6iHlMBUF1QyM/g9ig=
github.com/pluralsh/console-client-go v0.0.12/go.mod h1:kZjk0pXAWnvyj+miXveCho4kKQaX1Tm3CGAM+iwurWU=
github.com/pluralsh/console-client-go v0.0.13 h1:14mcP55LtEYVc8N3cSe5hmkUhi11Aw2Un8eM7Hf1R/8=
github.com/pluralsh/console-client-go v0.0.13/go.mod h1:kZjk0pXAWnvyj+miXveCho4kKQaX1Tm3CGAM+iwurWU=
github.com/pluralsh/controller-reconcile-helper v0.0.4 h1:1o+7qYSyoeqKFjx+WgQTxDz4Q2VMpzprJIIKShxqG0E=
github.com/pluralsh/controller-reconcile-helper v0.0.4/go.mod h1:AfY0gtteD6veBjmB6jiRx/aR4yevEf6K0M13/pGan/s=
github.com/pluralsh/gqlclient v1.10.0 h1:ccYB+A0JbPYkEeVzdfajd29l65N6x/buSKPMMxM8OIA=
Expand Down
2 changes: 1 addition & 1 deletion pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type ConsoleClient interface {
CreateRepository(url string, privateKey, passphrase, username, password *string) (*consoleclient.CreateGitRepository, error)
ListRepositories() (*consoleclient.ListGitRepositories, error)
UpdateRepository(id string, attrs consoleclient.GitAttributes) (*consoleclient.UpdateGitRepository, error)
CreateClusterService(clusterId string, attr consoleclient.ServiceDeploymentAttributes) (*consoleclient.CreateServiceDeployment, error)
CreateClusterService(clusterId, clusterName *string, attr consoleclient.ServiceDeploymentAttributes) (*consoleclient.ServiceDeploymentFragment, error)
UpdateClusterService(serviceId string, attr consoleclient.ServiceUpdateAttributes) (*consoleclient.UpdateServiceDeployment, error)
GetClusterService(serviceId, serviceName, clusterName *string) (*consoleclient.ServiceDeploymentExtended, error)
DeleteClusterService(serviceId string) (*consoleclient.DeleteServiceDeployment, error)
Expand Down
34 changes: 25 additions & 9 deletions pkg/console/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/pluralsh/plural/pkg/api"
)

func (c *consoleClient) ListClusterServices(clusterId, cluster *string) ([]*gqlclient.ServiceDeploymentEdgeFragment, error) {
if clusterId == nil && cluster == nil {
return nil, fmt.Errorf("clusterId and cluster can not be null")
func (c *consoleClient) ListClusterServices(clusterId, clusterName *string) ([]*gqlclient.ServiceDeploymentEdgeFragment, error) {
if clusterId == nil && clusterName == nil {
return nil, fmt.Errorf("clusterId and clusterName can not be null")
}
if clusterId != nil {
result, err := c.client.ListServiceDeployment(c.ctx, nil, nil, nil, clusterId)
Expand All @@ -21,7 +21,7 @@ func (c *consoleClient) ListClusterServices(clusterId, cluster *string) ([]*gqlc
}
return result.ServiceDeployments.Edges, nil
}
result, err := c.client.ListServiceDeploymentByHandle(c.ctx, nil, nil, nil, cluster)
result, err := c.client.ListServiceDeploymentByHandle(c.ctx, nil, nil, nil, clusterName)
if err != nil {
return nil, api.GetErrorResponse(err, "ListServiceDeploymentByHandle")
}
Expand All @@ -31,13 +31,29 @@ func (c *consoleClient) ListClusterServices(clusterId, cluster *string) ([]*gqlc
return result.ServiceDeployments.Edges, nil
}

func (c *consoleClient) CreateClusterService(clusterId string, attributes gqlclient.ServiceDeploymentAttributes) (*gqlclient.CreateServiceDeployment, error) {
result, err := c.client.CreateServiceDeployment(c.ctx, clusterId, attributes)
if err != nil {
return nil, api.GetErrorResponse(err, "CreateServiceDeployment")
func (c *consoleClient) CreateClusterService(clusterId, clusterName *string, attributes gqlclient.ServiceDeploymentAttributes) (*gqlclient.ServiceDeploymentFragment, error) {
if clusterId == nil && clusterName == nil {
return nil, fmt.Errorf("clusterId and clusterName can not be null")
}
if clusterId != nil {
result, err := c.client.CreateServiceDeployment(c.ctx, *clusterId, attributes)
if err != nil {
return nil, api.GetErrorResponse(err, "CreateServiceDeployment")
}
if result == nil {
return nil, fmt.Errorf("the result from CreateServiceDeployment is null")
}
return result.CreateServiceDeployment, nil
}

return result, nil
result, err := c.client.CreateServiceDeploymentWithHandle(c.ctx, *clusterName, attributes)
if err != nil {
return nil, api.GetErrorResponse(err, "CreateServiceDeploymentWithHandle")
}
if result == nil {
return nil, fmt.Errorf("the result from CreateServiceDeploymentWithHandle is null")
}
return result.CreateServiceDeployment, nil
}

func (c *consoleClient) UpdateClusterService(serviceId string, attributes gqlclient.ServiceUpdateAttributes) (*gqlclient.UpdateServiceDeployment, error) {
Expand Down

0 comments on commit cfd5452

Please sign in to comment.