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 28, 2021
2 parents 43c0dfa + e6d4f02 commit a9a8b3e
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## 🎟 Issue(s)

Resolves astronomer/issues#XXXX
Related astronomer/issues#XXXX

## 🧪 Functional Testing

Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ release:
name: astro-cli
# If set to auto, will mark the release as not ready for production
# in case there is an indicator for this in the tag e.g. v1.0.0-rc1
prerelease: auto
prerelease: true
build:
main: main.go
binary: astro
Expand Down
4 changes: 3 additions & 1 deletion cmd/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ 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")
if deployment.CheckHardDeleteDeployment(client) {
cmd.Flags().BoolVar(&hardDelete, "hard", false, "Deletes all infrastructure and records for this Deployment")
}
return cmd
}

Expand Down
21 changes: 17 additions & 4 deletions cmd/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ func TestDeploymentSAGetCommand(t *testing.T) {
]
}
}`
client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(okResponse)),
Expand Down Expand Up @@ -587,7 +587,14 @@ func TestDeploymentDeleteHardResponseNo(t *testing.T) {
testUtil.InitTestConfig()
okResponse := `{
"data": {
"appConfig": {"nfsMountDagDeployment": false},
"appConfig": {
"version": "0.15.1",
"baseDomain": "local.astronomer.io",
"smtpConfigured": true,
"manualReleaseNames": false,
"hardDeleteDeployment": true,
"nfsMountDagDeployment": false
},
"deleteDeployment": {
"id": "ckqh2dmzc43548h9hxzspysyi",
"type": "airflow",
Expand All @@ -612,7 +619,6 @@ func TestDeploymentDeleteHardResponseNo(t *testing.T) {
})
api := houston.NewHoustonClient(client)


// mock os.Stdin
input := []byte("n")
r, w, err := os.Pipe()
Expand All @@ -638,7 +644,14 @@ func TestDeploymentDeleteHardResponseYes(t *testing.T) {
expectedOut := `Successfully deleted deployment`
okResponse := `{
"data": {
"appConfig": {"nfsMountDagDeployment": false},
"appConfig": {
"version": "0.15.1",
"baseDomain": "local.astronomer.io",
"smtpConfigured": true,
"manualReleaseNames": false,
"hardDeleteDeployment": true,
"nfsMountDagDeployment": false
},
"deleteDeployment": {
"id": "ckqh2dmzc43548h9hxzspysyi",
"type": "airflow",
Expand Down
1 change: 0 additions & 1 deletion cmd/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ func TestWorkspaceSAGetCommand(t *testing.T) {
}
}`


client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Expand Down
8 changes: 8 additions & 0 deletions deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ func CheckNFSMountDagDeployment(client *houston.Client) bool {
return r.Data.GetAppConfig.NfsMountDagDeployment
}

func CheckHardDeleteDeployment(client *houston.Client) bool {
appConfig, err := AppConfig(client)
if err != nil {
return false
}
return appConfig.HardDeleteDeployment
}

// Create airflow deployment
func Create(label, ws, releaseName, cloudRole, executor, airflowVersion, dagDeploymentType, nfsLocation string, client *houston.Client, out io.Writer) error {
vars := map[string]interface{}{"label": label, "workspaceId": ws, "executor": executor, "cloudRole": cloudRole}
Expand Down
70 changes: 55 additions & 15 deletions deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func TestAppVersion(t *testing.T) {

config, err := AppVersion(api)
assert.NoError(t, err)
assert.Equal(t, config.Version, "0.15.1")
assert.Equal(t, config.BaseDomain, "local.astronomer.io")
assert.Equal(t, "0.15.1", config.Version)
assert.Equal(t, "local.astronomer.io", config.BaseDomain)
}

func TestAppConfig(t *testing.T) {
Expand All @@ -46,7 +46,8 @@ func TestAppConfig(t *testing.T) {
"version": "0.15.1",
"baseDomain": "local.astronomer.io",
"smtpConfigured": true,
"manualReleaseNames": false
"manualReleaseNames": false,
"hardDeleteDeployment": false
}
}
}`
Expand All @@ -61,9 +62,9 @@ func TestAppConfig(t *testing.T) {

config, err := AppConfig(api)
assert.NoError(t, err)
assert.Equal(t, config.ManualReleaseNames, false)
assert.Equal(t, config.SmtpConfigured, true)
assert.Equal(t, config.BaseDomain, "local.astronomer.io")
assert.Equal(t, false, config.ManualReleaseNames)
assert.Equal(t, true, config.SmtpConfigured)
assert.Equal(t, "local.astronomer.io", config.BaseDomain)
}

func TestAppConfigError(t *testing.T) {
Expand Down Expand Up @@ -354,7 +355,7 @@ func TestList(t *testing.T) {
expected := ` NAME DEPLOYMENT NAME ASTRO DEPLOYMENT ID TAG AIRFLOW VERSION
test burning-terrestrial-5940 v ckbv801t300qh0760pck7ea0c ?
`
assert.Equal(t, buf.String(), expected)
assert.Equal(t, expected, buf.String())
}

func TestUpdate(t *testing.T) {
Expand Down Expand Up @@ -420,7 +421,7 @@ func TestUpdate(t *testing.T) {
buf := new(bytes.Buffer)
err := Update(id, role, tt.deploymentConfig, tt.dagDeploymentType, "", api, buf)
assert.NoError(t, err)
assert.Equal(t, buf.String(), expected)
assert.Equal(t, expected, buf.String())
}
}

Expand Down Expand Up @@ -485,7 +486,7 @@ To cancel, run:
`

assert.Equal(t, buf.String(), expected)
assert.Equal(t, expected, buf.String())
}

func TestAirflowUpgradeError(t *testing.T) {
Expand Down Expand Up @@ -536,7 +537,7 @@ func TestAirflowUpgradeCancel(t *testing.T) {
expected := `
Airflow upgrade process has been successfully canceled. Your Deployment was not interrupted and you are still running Airflow 1.10.5.
`
assert.Equal(t, buf.String(), expected)
assert.Equal(t, expected, buf.String())
}

func TestAirflowUpgradeCancelError(t *testing.T) {
Expand Down Expand Up @@ -624,7 +625,7 @@ To cancel, run:
$ astro deployment airflow upgrade --cancel
`
assert.Equal(t, buf.String(), expected)
assert.Equal(t, expected, buf.String())
}

func Test_getDeployment(t *testing.T) {
Expand Down Expand Up @@ -719,7 +720,7 @@ func Test_getAirflowVersionSelection(t *testing.T) {
airflowVersion, err := getAirflowVersionSelection("1.10.7", api, buf)
t.Log(buf.String()) // Log the buffer so that this test is recognized by go test
assert.NoError(t, err)
assert.Equal(t, airflowVersion, "1.10.12")
assert.Equal(t, "1.10.12", airflowVersion)
}

func Test_getAirflowVersionSelectionError(t *testing.T) {
Expand All @@ -736,7 +737,7 @@ func Test_getAirflowVersionSelectionError(t *testing.T) {
buf := new(bytes.Buffer)
airflowVersion, err := getAirflowVersionSelection(deploymentId, api, buf)
assert.Error(t, err, "API error (500):")
assert.Equal(t, airflowVersion, "")
assert.Equal(t, "", airflowVersion)
}

func Test_meetsAirflowUpgradeReqs(t *testing.T) {
Expand Down Expand Up @@ -785,7 +786,7 @@ func TestCheckNFSMountDagDeploymentError(t *testing.T) {
}
})
api := houston.NewHoustonClient(client)
assert.Equal(t, CheckNFSMountDagDeployment(api), false)
assert.Equal(t, false, CheckNFSMountDagDeployment(api))
}

func TestCheckNFSMountDagDeploymentSuccess(t *testing.T) {
Expand All @@ -810,5 +811,44 @@ func TestCheckNFSMountDagDeploymentSuccess(t *testing.T) {
}
})
api := houston.NewHoustonClient(client)
assert.Equal(t, CheckNFSMountDagDeployment(api), true)
assert.Equal(t, true, CheckNFSMountDagDeployment(api))
}

func TestCheckHardDeleteDeployment(t *testing.T) {
testUtil.InitTestConfig()
okResponse := `{
"data": {
"appConfig": {
"version": "0.15.1",
"baseDomain": "local.astronomer.io",
"smtpConfigured": true,
"manualReleaseNames": false,
"hardDeleteDeployment": true
}
}
}`
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)

hardDelete := CheckHardDeleteDeployment(api)
assert.Equal(t, true, hardDelete)
}

func TestCheckHardDeleteDeploymentError(t *testing.T) {
testUtil.InitTestConfig()
client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 500,
Body: ioutil.NopCloser(bytes.NewBufferString(``)),
Header: make(http.Header),
}
})
api := houston.NewHoustonClient(client)
assert.Equal(t, false, CheckHardDeleteDeployment(api))
}
15 changes: 15 additions & 0 deletions houston/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,21 @@ mutation UpdateDeployment($deploymentId: Uuid!, $payload: JSON!, $cloudRole: Str
)
}`

WorkspaceGetUserRequest = `
query workspaceGetUser($workspaceUuid: Uuid!, $email: String!) {
workspaceUser(
workspaceUuid: $workspaceUuid
user: { email: $email }
) {
roleBindings {
workspace{
id
}
role
}
}
}`

WorkspaceUserRemoveRequest = `
mutation RemoveWorkspaceUser(
$workspaceId: Uuid!
Expand Down
11 changes: 11 additions & 0 deletions houston/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Response struct {
UpdateWorkspace *Workspace `json:"updateWorkspace,omitempty"`
DeploymentLog []DeploymentLog `json:"logs,omitempty"`
WorkspaceUpdateUserRole string `json:"workspaceUpdateUserRole,omitempty"`
WorkspaceGetUser WorkspaceUserRoleBindings `json:"workspaceUser,omitempty"`
DeploymentConfig DeploymentConfig `json:"deploymentConfig,omitempty"`
} `json:"data"`
Errors []Error `json:"errors,omitempty"`
Expand Down Expand Up @@ -193,6 +194,15 @@ type User struct {
// updated at
// profile
}
type RoleBindingWorkspace struct {
Role string `json:"role"`
Workspace struct {
Id string `json:"id"`
} `json:"workspace"`
}
type WorkspaceUserRoleBindings struct {
RoleBindings []RoleBindingWorkspace `json:"roleBindings"`
}

type RoleBinding struct {
Role string `json:"role"`
Expand Down Expand Up @@ -265,6 +275,7 @@ type AppConfig struct {
ManualReleaseNames bool `json:"manualReleaseNames"`
ConfigureDagDeployment bool `json:"configureDagDeployment"`
NfsMountDagDeployment bool `json:"nfsMountDagDeployment"`
HardDeleteDeployment bool `json:"hardDeleteDeployment"`
}

// coerce a string into SemVer if possible
Expand Down
38 changes: 37 additions & 1 deletion workspace/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package workspace
import (
"fmt"
"io"
"strings"

"github.com/astronomer/astro-cli/houston"
"github.com/astronomer/astro-cli/pkg/printutil"
"github.com/pkg/errors"
)

// Add a user to a workspace with specified role
Expand Down Expand Up @@ -97,7 +99,41 @@ func UpdateRole(workspaceId, email, role string, client *houston.Client, out io.
return err
}
newRole := r.Data.WorkspaceUpdateUserRole
// get user you are updating to show role from before change
roles, err := getUserRole(workspaceId, email, client, out)

fmt.Fprintf(out, "Role has been changed from %s to %s for user %s", role, newRole, email)
if err != nil {
return err
}

var rb houston.RoleBindingWorkspace
for _, val := range roles.RoleBindings {
if val.Workspace.Id == workspaceId && strings.Contains(val.Role, "WORKSPACE") {
rb = val
break
}
}
// check if rolebinding is an empty structure
if (rb == houston.RoleBindingWorkspace{}) {
return errors.New("The user you are trying to change is not part of this workspace")
}

fmt.Fprintf(out, "Role has been changed from %s to %s for user %s", rb.Role, newRole, email)
return nil
}

func getUserRole(workspaceId string, email string, client *houston.Client, out io.Writer) (workspaceUserRolebindings houston.WorkspaceUserRoleBindings, err error) {
req := houston.Request{
Query: houston.WorkspaceGetUserRequest,
Variables: map[string]interface{}{"workspaceUuid": workspaceId, "email": email},
}
r, err := req.DoWithClient(client)

if err != nil {
return workspaceUserRolebindings, err
}
workspaceUserRolebindings = r.Data.WorkspaceGetUser

return workspaceUserRolebindings, nil

}
Loading

0 comments on commit a9a8b3e

Please sign in to comment.