Skip to content

Commit

Permalink
Merge branch 'main' into release-0.25
Browse files Browse the repository at this point in the history
  • Loading branch information
bote795 committed Jul 22, 2021
2 parents 4e88ef6 + 224720e commit 43c0dfa
Show file tree
Hide file tree
Showing 18 changed files with 635 additions and 188 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

All notable changes to this project will be documented in this file.
## [0.25.2] - 2021-06-23
- Add error message for airflow upgrade no-op #3055 (#417)

## [0.23.1] - 2020-11-25

Expand Down
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @schnie @andriisoldatenko @adam2k
* @andriisoldatenko @adam2k @bote795 @aliotta
26 changes: 18 additions & 8 deletions cmd/deployment.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package cmd

import (
"fmt"
"io"
"strings"

"github.com/astronomer/astro-cli/deployment"
"github.com/astronomer/astro-cli/houston"
"github.com/astronomer/astro-cli/messages"
"github.com/astronomer/astro-cli/pkg/input"
sa "github.com/astronomer/astro-cli/serviceaccount"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -14,6 +17,7 @@ import (
var (
allDeployments bool
cancel bool
hardDelete bool
executor string
deploymentId string
desiredAirflowVersion string
Expand Down Expand Up @@ -66,10 +70,7 @@ var (
$ astro deployment service-account create --deployment-id=xxxxx --label=my_label --role=ROLE
`
deploymentSaGetExample = `
# Get deployment service-account
$ astro deployment service-account get <service-account-id> --deployment-id=<deployment-id>
# or using deployment-id
# Get deployment service-accounts
$ astro deployment service-account get --deployment-id=<deployment-id>
`
deploymentSaDeleteExample = `
Expand Down Expand Up @@ -143,6 +144,7 @@ func newDeploymentDeleteCmd(client *houston.Client, out io.Writer) *cobra.Comman
return deploymentDelete(cmd, args, client, out)
},
}
cmd.Flags().BoolVar(&hardDelete, "hard", false, "Deletes all infrastructure and records for this Deployment")
return cmd
}

Expand Down Expand Up @@ -315,8 +317,8 @@ func newDeploymentSaCreateCmd(client *houston.Client, out io.Writer) *cobra.Comm
func newDeploymentSaGetCmd(client *houston.Client, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Short: "Get a service-account by entity type and entity id",
Long: "Get a service-account by entity type and entity id",
Short: "Get a service-account by deployment id",
Long: "Get a service-account by deployment id",
Example: deploymentSaGetExample,
RunE: func(cmd *cobra.Command, args []string) error {
return deploymentSaGet(cmd, client, out)
Expand Down Expand Up @@ -409,8 +411,16 @@ func deploymentCreate(cmd *cobra.Command, args []string, client *houston.Client,
func deploymentDelete(cmd *cobra.Command, args []string, client *houston.Client, out io.Writer) error {
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
if hardDelete {
i, _ := input.InputConfirm(
fmt.Sprintf(messages.CLI_DEPLOYMENT_HARD_DELETE_PROMPT))

return deployment.Delete(args[0], client, out)
if !i {
fmt.Println("Exit: This command was not executed and your Deployment was not hard deleted.\n If you want to delete your Deployment but not permanently, try\n $ astro deployment delete without the --hard flag.")
return nil
}
}
return deployment.Delete(args[0], hardDelete, client, out)
}

func deploymentList(cmd *cobra.Command, args []string, client *houston.Client, out io.Writer) error {
Expand Down Expand Up @@ -519,7 +529,7 @@ func deploymentSaGet(cmd *cobra.Command, client *houston.Client, out io.Writer)
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true

return sa.Get("DEPLOYMENT", deploymentId, client, out)
return sa.GetDeploymentServiceAccounts(deploymentId, client, out)
}

func deploymentSaDelete(cmd *cobra.Command, args []string, client *houston.Client, out io.Writer) error {
Expand Down
257 changes: 201 additions & 56 deletions cmd/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -297,42 +298,6 @@ func TestDeploymentSaDeleteRootCommand(t *testing.T) {
assert.Contains(t, output, "Service Account my_label (q1w2e3r4t5y6u7i8o9p0) successfully deleted")
}

func TestDeploymentSaGetCommand(t *testing.T) {
testUtil.InitTestConfig()
expectedOut := ` NAME CATEGORY ID APIKEY
my_label default q1w2e3r4t5y6u7i8o9p0 000000000000000000000000
`
okResponse := `{
"data": {
"appConfig": {"nfsMountDagDeployment": false},
"serviceAccounts": [{
"id": "q1w2e3r4t5y6u7i8o9p0",
"apiKey": "000000000000000000000000",
"label": "my_label",
"category": "default",
"entityType": "DEPLOYMENT",
"entityUuid": null,
"active": true,
"createdAt": "2019-10-16T21:14:22.105Z",
"updatedAt": "2019-10-16T21:14:22.105Z",
"lastUsedAt": null
}]
}
}`
client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(okResponse)),
Header: make(http.Header),
}
})
api := houston.NewHoustonClient(client)

_, output, err := executeCommandC(api, "deployment", "service-account", "get", "--deployment-id=q1w2e3r4t5y6u7i8o9p0")
assert.NoError(t, err)
assert.Equal(t, expectedOut, output)
}

func TestDeploymentSaCreateCommand(t *testing.T) {
testUtil.InitTestConfig()
expectedOut := ` NAME CATEGORY ID APIKEY
Expand Down Expand Up @@ -485,16 +450,22 @@ func TestDeploymentAirflowUpgradeCommand(t *testing.T) {
testUtil.InitTestConfig()
expectedOut := `The upgrade from Airflow 1.10.5 to 1.10.10 has been started. To complete this process, add an Airflow 1.10.10 image to your Dockerfile and deploy to Astronomer.`

okResponse := `{"data": {
"appConfig": {"nfsMountDagDeployment": false},
"updateDeploymentAirflow": {
"id": "ckggzqj5f4157qtc9lescmehm",
"label": "test",
"airflowVersion": "1.10.5",
"desiredAirflowVersion": "1.10.10"
}
}
}`
okResponse := `{
"data": {
"appConfig": {"nfsMountDagDeployment": false},
"deployment": {
"id": "ckggzqj5f4157qtc9lescmehm",
"airflowVersion": "1.10.5",
"desiredAirflowVersion": "1.10.10"
},
"updateDeploymentAirflow": {
"id": "ckggzqj5f4157qtc9lescmehm",
"label": "test",
"airflowVersion": "1.10.5",
"desiredAirflowVersion": "1.10.10"
}
}
}`

client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
Expand All @@ -514,16 +485,17 @@ func TestDeploymentAirflowUpgradeCancelCommand(t *testing.T) {
testUtil.InitTestConfig()
expectedOut := `Airflow upgrade process has been successfully canceled. Your Deployment was not interrupted and you are still running Airflow 1.10.5.`

okResponse := `{"data": {
"appConfig": {"nfsMountDagDeployment": false},
"deployment": {
"id": "ckggzqj5f4157qtc9lescmehm",
"label": "test",
"airflowVersion": "1.10.5",
"desiredAirflowVersion": "1.10.10"
}
}
}`
okResponse := `{
"data": {
"appConfig": {"nfsMountDagDeployment": false},
"deployment": {
"id": "ckggzqj5f4157qtc9lescmehm",
"label": "test",
"airflowVersion": "1.10.5",
"desiredAirflowVersion": "1.10.10"
}
}
}`

client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
Expand All @@ -538,3 +510,176 @@ func TestDeploymentAirflowUpgradeCancelCommand(t *testing.T) {
assert.NoError(t, err)
assert.Contains(t, output, expectedOut)
}

func TestDeploymentSAGetCommand(t *testing.T) {
testUtil.InitTestConfig()
expectedOut := ` yooo can u see me test ckqvfa2cu1468rn9hnr0bqqfk 658b304f36eaaf19860a6d9eb73f7d8a`
okResponse := `
{
"data": {
"appConfig": {"nfsMountDagDeployment": false},
"deploymentServiceAccounts": [
{
"id": "ckqvfa2cu1468rn9hnr0bqqfk",
"apiKey": "658b304f36eaaf19860a6d9eb73f7d8a",
"label": "yooo can u see me test",
"category": "",
"entityType": "DEPLOYMENT",
"entityUuid": null,
"active": true,
"createdAt": "2021-07-08T21:28:57.966Z",
"updatedAt": "2021-07-08T21:28:57.967Z",
"lastUsedAt": null
}
]
}
}`
client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(okResponse)),
Header: make(http.Header),
}
})
api := houston.NewHoustonClient(client)

_, output, err := executeCommandC(api, "deployment", "sa", "get", "--deployment-id=ckqvf9spa1189rn9hbh5h439u")
assert.NoError(t, err)
assert.Contains(t, output, expectedOut)
}

func TestDeploymentDelete(t *testing.T) {
testUtil.InitTestConfig()
expectedOut := `Successfully deleted deployment`
okResponse := `{
"data": {
"appConfig": {"nfsMountDagDeployment": false},
"deleteDeployment": {
"id": "ckqh2dmzc43548h9hxzspysyi",
"type": "airflow",
"label": "test2",
"description": "",
"releaseName": "combusting-radiant-1610",
"version": "0.17.1",
"workspace": {
"id": "ckqh2d9zh40758h9h650gf8dc"
},
"createdAt": "2021-06-28T20:19:03.193Z",
"updatedAt": "2021-07-07T18:16:52.118Z"
}
}
}`
client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(okResponse)),
Header: make(http.Header),
}
})
api := houston.NewHoustonClient(client)

_, output, err := executeCommandC(api, "deployment", "delete", "ckqh2dmzc43548h9hxzspysyi")
assert.NoError(t, err)
assert.Contains(t, output, expectedOut)
}

func TestDeploymentDeleteHardResponseNo(t *testing.T) {
testUtil.InitTestConfig()
okResponse := `{
"data": {
"appConfig": {"nfsMountDagDeployment": false},
"deleteDeployment": {
"id": "ckqh2dmzc43548h9hxzspysyi",
"type": "airflow",
"label": "test2",
"description": "",
"releaseName": "combusting-radiant-1610",
"version": "0.17.1",
"workspace": {
"id": "ckqh2d9zh40758h9h650gf8dc"
},
"createdAt": "2021-06-28T20:19:03.193Z",
"updatedAt": "2021-07-07T18:16:52.118Z"
}
}
}`
client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(okResponse)),
Header: make(http.Header),
}
})
api := houston.NewHoustonClient(client)


// mock os.Stdin
input := []byte("n")
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
_, err = w.Write(input)
if err != nil {
t.Error(err)
}
w.Close()
stdin := os.Stdin
// Restore stdin right after the test.
defer func() { os.Stdin = stdin }()
os.Stdin = r

_, _, err = executeCommandC(api, "deployment", "delete", "--hard", "ckqh2dmzc43548h9hxzspysyi")
assert.Nil(t, err)
}

func TestDeploymentDeleteHardResponseYes(t *testing.T) {
testUtil.InitTestConfig()
expectedOut := `Successfully deleted deployment`
okResponse := `{
"data": {
"appConfig": {"nfsMountDagDeployment": false},
"deleteDeployment": {
"id": "ckqh2dmzc43548h9hxzspysyi",
"type": "airflow",
"label": "test2",
"description": "",
"releaseName": "combusting-radiant-1610",
"version": "0.17.1",
"workspace": {
"id": "ckqh2d9zh40758h9h650gf8dc"
},
"createdAt": "2021-06-28T20:19:03.193Z",
"updatedAt": "2021-07-07T18:16:52.118Z"
}
}
}`
client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(okResponse)),
Header: make(http.Header),
}
})
api := houston.NewHoustonClient(client)

// mock os.Stdin
input := []byte("y")
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
_, err = w.Write(input)
if err != nil {
t.Error(err)
}
w.Close()
stdin := os.Stdin
// Restore stdin right after the test.
defer func() { os.Stdin = stdin }()
os.Stdin = r

_, output, err := executeCommandC(api, "deployment", "delete", "--hard", "ckqh2dmzc43548h9hxzspysyi")
assert.NoError(t, err)
assert.Contains(t, output, expectedOut)
}
Loading

0 comments on commit 43c0dfa

Please sign in to comment.