Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #100 from wso2-enterprise/master
Browse files Browse the repository at this point in the history
Merge with master
  • Loading branch information
a5anka authored Dec 19, 2019
2 parents 374bda3 + 175ea32 commit 7db5a3d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 87 deletions.
27 changes: 3 additions & 24 deletions internal/pkg/client/application_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ const pathApplications = "/applications"
const pathApplicationDeployment = pathApplications + "/deployments"
const pathApplicationLogs = pathApplications + "/logs"

type appDeploymentRequest struct {
AppName string `json:"name"`
RepoUrl string `json:"repo_url"`
}

func (c *cliClient) ListApps() ([]runtime.Application, error) {
var apps []runtime.Application

Expand All @@ -47,27 +42,11 @@ func (c *cliClient) CreateNewApp(name string, desc string) error {
return nil
}

func (c *cliClient) CreateAndDeployApp(repoUrl string) (runtime.DeploymentDetails, error) {
deploymentRequest := appDeploymentRequest{
RepoUrl: repoUrl,
}

var deploymentDetails runtime.DeploymentDetails

err := c.httpClient.createRestResourceWithResponse(pathApplicationDeployment, &deploymentRequest, &deploymentDetails)

return deploymentDetails, err
}

func (c *cliClient) CreateAndDeployAppWithName(appName, repoUrl string) (runtime.DeploymentDetails, error) {
deploymentRequest := appDeploymentRequest{
AppName: appName,
RepoUrl: repoUrl,
}
func (c *cliClient) CreateAndDeployApp(deploymentRequest runtime.DeploymentInput) (runtime.DeploymentOut, error) {

var deploymentDetails runtime.DeploymentDetails
var deploymentDetails runtime.DeploymentOut

err := c.httpClient.createRestResourceWithResponse(pathApplicationDeployment, &deploymentRequest, &deploymentDetails)
err := c.httpClient.createRestResourceWithResponse(pathApplicationDeployment, deploymentRequest, &deploymentDetails)

return deploymentDetails, err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/client/application_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestDeployAppError(t *testing.T) {
},
}

_, err := client.CreateAndDeployApp("http://github.com/test/test")
_, err := client.CreateAndDeployApp(runtime.DeploymentInput{Url: "http://github.com/test/test"})

test.AssertNonNil(t, err, "An error should be returned")
}
Expand All @@ -120,15 +120,15 @@ func TestDeployApp(t *testing.T) {
httpClient: &mockHttpClient{
createRestResourceWithResponseImpl: func(resourcePath string, requestData interface{},
responseData interface{}) error {
apps := responseData.(*runtime.DeploymentDetails)
apps := responseData.(*runtime.DeploymentOut)
apps.DeploymentUrl = "http://example.com/apps/url"
apps.ApplicationId = "appd83d56f4c6ff40428e8dd057c5b94bd5"
return nil
},
},
}

deploymentDetails, _ := client.CreateAndDeployApp("http://github.com/test/test")
deploymentDetails, _ := client.CreateAndDeployApp(runtime.DeploymentInput{Url: "http://github.com/test/test"})

test.AssertString(t, "http://example.com/apps/url", deploymentDetails.DeploymentUrl,
"The app URL should be returned")
Expand Down
47 changes: 29 additions & 18 deletions internal/pkg/cmd/application/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,24 @@ func NewDeployCommand(cliContext runtime.CliContext) *cobra.Command {
Short: "Deploy an application",
Example: fmt.Sprint(common.GetAbsoluteCommandName(cmdApplication, cmdDeploy),
" https://github.com/wso2/choreo-ballerina-hello"),
Args: cobra.ExactArgs(1),
Args: checkArgCount(1, 2),
Run: runDeployAppCommand(cliContext),
}
cmd.Flags().StringP("name", "n", "", "the name to be used for the created application")
return cmd
}

func checkArgCount(min int, max int) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
if len(args) > max {
return fmt.Errorf("accepts at most %d arg(s), received %d", max, len(args))
} else if len(args) < min {
return fmt.Errorf("requires at least %d arg(s), only received %d", min, len(args))
}
return nil
}
}

func runDeployAppCommand(cliContext runtime.CliContext) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
failIfUserNotLoggedIn(cliContext)
Expand All @@ -39,24 +50,24 @@ func runDeployAppCommand(cliContext runtime.CliContext) func(cmd *cobra.Command,
if err != nil {
common.ExitWithError(cliContext.Out(), "Error while reading the application name flag value", err)
}

if appName != "" {
deploymentDetails, err := cliContext.Client().CreateAndDeployAppWithName(appName, args[0])
printDeployResponse(err, cliContext, deploymentDetails)
var deploymentRequest runtime.DeploymentInput
deploymentRequest.Name = appName
var msg string
if len(args) == 2 {
deploymentRequest.ApplicationId = args[0]
deploymentRequest.Url = args[1]
msg = "The application with id %s has been updated\nOnce deployed, the app can be accessed from %s"
} else {
deploymentDetails, err := cliContext.Client().CreateAndDeployApp(args[0])
printDeployResponse(err, cliContext, deploymentDetails)
deploymentRequest.Url = args[0]
msg = "A new application is created for deployment with Id: %s" +
"\nOnce deployed, the app can be accessed from %s"
}
deploymentDetails, err := cliContext.Client().CreateAndDeployApp(deploymentRequest)
if err != nil {
common.ExitWithError(cliContext.Out(), "Error occurred while deploying the application", err)
} else {
common.PrintInfo(cliContext.Out(), fmt.Sprintf(msg, deploymentDetails.ApplicationId,
deploymentDetails.DeploymentUrl))
}

}
}

func printDeployResponse(err error, cliContext runtime.CliContext, deploymentDetails runtime.DeploymentDetails) {
if err != nil {
common.ExitWithError(cliContext.Out(), "Error occurred while deploying the application", err)
} else {
common.PrintInfo(cliContext.Out(), "A new application is created for deployment with Id: "+
deploymentDetails.ApplicationId+"\nOnce deployed, the app can be accessed from "+
deploymentDetails.DeploymentUrl)
}
}
44 changes: 26 additions & 18 deletions internal/pkg/cmd/application/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ import (

func TestCreateAndDeployApp(t *testing.T) {
var b bytes.Buffer
deployCommand := NewDeployCommand(&runtime.MockCliContext{
MockOut: &b,
MockUserConfig: config.NewMockConfigHolder(map[string]string{cl.AccessToken: "some-token"}),
MockEnvConfig: config.NewMockConfigHolder(map[string]string{}),
MockClient: &client.MockClient{CreateAndDeployApp_: func(repoUrl string) (d dt.DeploymentDetails, e error) {
return dt.DeploymentDetails{DeploymentUrl: "https://development.choreo.dev/choreoapps/appe1231bbf533d3f2e1287f437ff17d7c8", ApplicationId: "appe1231bbf533d3f2e1287f437ff17d7c8"}, nil
}},
})
deployCommand := NewDeployCommand(createAndGetMockClient(&b))
deployCommand.SetArgs([]string{"https://github.com/someuser/myapp"})
_ = deployCommand.Execute()

Expand All @@ -43,20 +36,35 @@ func TestCreateAndDeployApp(t *testing.T) {

func TestCreateAndDeployAppWithName(t *testing.T) {
var b bytes.Buffer
deployCommand := NewDeployCommand(&runtime.MockCliContext{
MockOut: &b,
MockUserConfig: config.NewMockConfigHolder(map[string]string{cl.AccessToken: "some-token"}),
MockEnvConfig: config.NewMockConfigHolder(map[string]string{}),
MockClient: &client.MockClient{CreateAndDeployAppWithName_:
func(appName, repoUrl string) (d dt.DeploymentDetails, e error) {
return dt.DeploymentDetails{DeploymentUrl: "https://development.choreo.dev/choreoapps/appe1231bbf533d3f2e1287f437ff17d7c8", ApplicationId: "appe1231bbf533d3f2e1287f437ff17d7c8"}, nil
}},
})
deployCommand.SetArgs([]string{"-n", "hello-app","https://github.com/someuser/myapp"})
deployCommand := NewDeployCommand(createAndGetMockClient(&b))
deployCommand.SetArgs([]string{"-n", "hello-app", "https://github.com/someuser/myapp"})
_ = deployCommand.Execute()

expect := "A new application is created for deployment with Id: appe1231bbf533d3f2e1287f437ff17d7c8" +
"\nOnce deployed, the app can be accessed from" +
" https://development.choreo.dev/choreoapps/appe1231bbf533d3f2e1287f437ff17d7c8" + "\n"
test.AssertString(t, expect, b.String(), "Deployment command output is not as expected")
}

func TestDeployExistingAppCommand(t *testing.T) {
var b bytes.Buffer
deployCommand := NewDeployCommand(createAndGetMockClient(&b))
deployCommand.SetArgs([]string{"appe1231bbf533d3f2e1287f437ff17d7c8", "https://github.com/someuser/myapp"})
_ = deployCommand.Execute()

expect := "The application with id appe1231bbf533d3f2e1287f437ff17d7c8 has been updated" +
"\nOnce deployed, the app can be accessed from https://development.choreo.dev/choreoapps/appe1231bbf533d3f2e1287f437ff17d7c8\n"
test.AssertString(t, expect, b.String(), "Deployment command output is not as expected")
}

func createAndGetMockClient(buffer *bytes.Buffer) *runtime.MockCliContext {
return &runtime.MockCliContext{
MockOut: buffer,
MockUserConfig: config.NewMockConfigHolder(map[string]string{cl.AccessToken: "some-token"}),
MockEnvConfig: config.NewMockConfigHolder(map[string]string{}),
MockClient: &client.MockClient{CreateAndDeployApp_: func(deploymentRequest dt.DeploymentInput) (d dt.DeploymentOut, e error) {
return dt.DeploymentOut{DeploymentUrl: "https://development.choreo.dev/choreoapps/appe1231bbf533d3f2e1287f437ff17d7c8",
ApplicationId: "appe1231bbf533d3f2e1287f437ff17d7c8"}, nil
}},
}
}
12 changes: 9 additions & 3 deletions internal/pkg/cmd/runtime/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,22 @@ type ApplicationRequest struct {
Description string `json:"description" header:"Description"`
}

type DeploymentDetails struct {
type DeploymentInput struct {
Url string `json:"repo_url"`
ApplicationId string `json:"app_id"`
Name string `json:"name"`
Description string `json:"description"`
}

type DeploymentOut struct {
DeploymentUrl string `json:"deployment_url"`
ApplicationId string `json:"app_id"`
}

type ApplicationApiClient interface {
CreateNewApp(name string, desc string) error
ListApps() ([]Application, error)
CreateAndDeployApp(repoUrl string) (DeploymentDetails, error)
CreateAndDeployAppWithName(appName, repoUrl string) (DeploymentDetails, error)
CreateAndDeployApp(deploymentRequest DeploymentInput) (DeploymentOut, error)
FetchLogs(appId string, linesCount uint) (string, error)
DeleteApp(appId string) error
GetApplicationStatus(appId string) (string, error)
Expand Down
31 changes: 10 additions & 21 deletions internal/pkg/test/mock/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ package client
import "github.com/wso2/choreo-cli/internal/pkg/cmd/runtime"

type MockClient struct {
CreateNewApp_ func(name string, desc string) error
ListApps_ func() ([]runtime.Application, error)
CreateAndDeployApp_ func(repoUrl string) (runtime.DeploymentDetails, error)
CreateAndDeployAppWithName_ func(appId, repoUrl string) (runtime.DeploymentDetails, error)
FetchLogs_ func(appId string, linesCount uint) (string, error)
CreateOauthStateString_ func() (string, error)
DeleteApp_ func(appId string) error
GetStatus_ func(appId string) (string, error)
CreateNewApp_ func(name string, desc string) error
ListApps_ func() ([]runtime.Application, error)
CreateAndDeployApp_ func(deploymentRequest runtime.DeploymentInput) (runtime.DeploymentOut, error)
FetchLogs_ func(appId string, linesCount uint) (string, error)
CreateOauthStateString_ func() (string, error)
DeleteApp_ func(appId string) error
GetStatus_ func(appId string) (string, error)
}

func (c *MockClient) CreateNewApp(name string, desc string) error {
Expand All @@ -36,21 +35,11 @@ func (c *MockClient) ListApps() ([]runtime.Application, error) {
return nil, nil
}

func (c *MockClient) CreateAndDeployApp(repoUrl string) (runtime.DeploymentDetails, error) {
func (c *MockClient) CreateAndDeployApp(deploymentRequest runtime.DeploymentInput) (runtime.DeploymentOut, error) {
if c.CreateAndDeployApp_ != nil {
return c.CreateAndDeployApp_(repoUrl)
return c.CreateAndDeployApp_(deploymentRequest)
}
return runtime.DeploymentDetails{
DeploymentUrl: "",
ApplicationId: "",
}, nil
}

func (c *MockClient) CreateAndDeployAppWithName(appId, repoUrl string) (runtime.DeploymentDetails, error) {
if c.CreateAndDeployAppWithName_ != nil {
return c.CreateAndDeployAppWithName_(appId, repoUrl)
}
return runtime.DeploymentDetails{
return runtime.DeploymentOut{
DeploymentUrl: "",
ApplicationId: "",
}, nil
Expand Down

0 comments on commit 7db5a3d

Please sign in to comment.