Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to fetch all deployments across all workspaces #115

Merged
merged 9 commits into from Sep 7, 2018
4 changes: 4 additions & 0 deletions auth/auth.go
Expand Up @@ -57,6 +57,10 @@ func registryAuth() error {
return err
}

if c.Domain == "localhost" {
return nil
}

registry := "registry." + c.Domain
token := c.Token
err = docker.ExecLogin(registry, "user", token)
Expand Down
3 changes: 1 addition & 2 deletions cmd/airflow.go
Expand Up @@ -170,7 +170,7 @@ func airflowInit(cmd *cobra.Command, args []string) error {
emtpyDir := fileutil.IsEmptyDir(config.WorkingPath)
if !emtpyDir {
i, _ := input.InputConfirm(
fmt.Sprintf("%s \nYou are not in an empty directory, are you you want to initialize a project?", config.WorkingPath))
fmt.Sprintf("%s \nYou are not in an empty directory. Are you sure you want to initialize a project?", config.WorkingPath))

if !i {
fmt.Println("Cancelling project initialization...\n")
Expand Down Expand Up @@ -255,7 +255,6 @@ func airflowLogs(cmd *cobra.Command, args []string) error {
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true


return airflow.Logs(config.WorkingPath, webserverLogs, schedulerLogs, followLogs)
}

Expand Down
16 changes: 12 additions & 4 deletions cmd/deployment.go
Expand Up @@ -7,6 +7,8 @@ import (
)

var (
allDeployments bool

deploymentUpdateAttrs = []string{"label"}

deploymentRootCmd = &cobra.Command{
Expand Down Expand Up @@ -68,6 +70,7 @@ func init() {

// deployment list
deploymentRootCmd.AddCommand(deploymentListCmd)
deploymentListCmd.Flags().BoolVarP(&allDeployments, "all", "a", false, "Show deployments across all workspaces")

// deployment update
deploymentRootCmd.AddCommand(deploymentUpdateCmd)
Expand All @@ -81,7 +84,7 @@ func deploymentCreate(cmd *cobra.Command, args []string) error {
}

// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
cmd.SilenceUsage = true

return deployment.Create(args[0], ws)
}
Expand All @@ -99,11 +102,16 @@ func deploymentList(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "failed to find a valid workspace")
// fmt.Println("Default workspace id not set, set default workspace id or pass a workspace in via the --workspace-id flag")
}


// Don't validate workspace if viewing all deployments
if allDeployments {
ws = ""
}

// Silence Usage as we have now validated command input
cmd.SilenceUsage = true

return deployment.List(ws)
return deployment.List(ws, allDeployments)
}

func deploymentUpdate(cmd *cobra.Command, args []string) error {
Expand All @@ -114,6 +122,6 @@ func deploymentUpdate(cmd *cobra.Command, args []string) error {

// Silence Usage as we have now validated command input
cmd.SilenceUsage = true

return deployment.Update(args[0], argsMap)
}
1 change: 0 additions & 1 deletion config/config.go
Expand Up @@ -41,7 +41,6 @@ var (
CloudAPIPort: newCfg("cloud.api.port", "443"),
CloudAPIToken: newCfg("cloud.api.token", ""),
Context: newCfg("context", ""),
LocalEnabled: newCfg("local.enabled", ""),
LocalHouston: newCfg("local.houston", ""),
LocalOrbit: newCfg("local.orbit", ""),
PostgresUser: newCfg("postgres.user", "postgres"),
Expand Down
6 changes: 4 additions & 2 deletions config/context.go
Expand Up @@ -168,9 +168,10 @@ func (c Context) SwitchContext() error {

// GetAPIURL returns full Houston API Url for the provided Context
func (c Context) GetAPIURL() string {
if len(CFG.LocalEnabled.GetString()) != 0 {
if c.Domain == "localhost" {
return CFG.LocalHouston.GetString()
}

return fmt.Sprintf(
"%s://houston.%s:%s/v1",
CFG.CloudAPIProtocol.GetString(),
Expand All @@ -181,9 +182,10 @@ func (c Context) GetAPIURL() string {

// GetAppURL returns full Houston API Url for the provided Context
func (c Context) GetAppURL() string {
if len(CFG.LocalEnabled.GetString()) != 0 {
if c.Domain == "localhost" {
return CFG.LocalOrbit.GetString()
}

return fmt.Sprintf(
"%s://app.%s",
CFG.CloudAPIProtocol.GetString(),
Expand Down
30 changes: 22 additions & 8 deletions deployment/deployment.go
@@ -1,9 +1,10 @@
package deployment

import (
"errors"
"fmt"

"github.com/pkg/errors"

"github.com/astronomerio/astro-cli/config"
"github.com/astronomerio/astro-cli/houston"
"github.com/astronomerio/astro-cli/messages"
Expand Down Expand Up @@ -52,21 +53,34 @@ func Delete(uuid string) error {
}

// List all airflow deployments
func List(ws string) error {
r := " %-30s %-50s %-30s"
func List(ws string, all bool) error {
var deployments []houston.Deployment
var err error

r := " %-30s %-50s %-30s %-50s"
h := fmt.Sprintf(r, "NAME", "UUID", "RELEASE NAME", "WORKSPACE")
// colorFmt := "\033[33;m"
// colorTrm := "\033[0m"

deployments, err := api.GetDeployments(ws)
if err != nil {
return err
if all {
deployments, err = api.GetAllDeployments()
if err != nil {
return err
}
} else {
deployments, err = api.GetDeployments(ws)
if err != nil {
return err
}
}

h := fmt.Sprintf(r, "NAME", "UUID", "RELEASE NAME")
fmt.Println(h)

for _, d := range deployments {
fullStr := fmt.Sprintf(r, d.Label, d.Id, d.ReleaseName)
if all {
ws = d.Workspace.Uuid
}
fullStr := fmt.Sprintf(r, d.Label, d.Id, d.ReleaseName, ws)
fmt.Println(fullStr)
}
return nil
Expand Down
67 changes: 64 additions & 3 deletions houston/houston.go
Expand Up @@ -77,6 +77,29 @@ var (
type
label
releaseName
workspace {
uuid
}
deployInfo {
latest
next
}
version
createdAt
updatedAt
}
}`

deploymentsGetAllRequest = `
query GetAllDeployments {
deployments {
uuid
type
label
releaseName
workspace {
uuid
}
deployInfo {
latest
next
Expand Down Expand Up @@ -169,6 +192,18 @@ var (
}
}`

workspaceGetRequest = `
query GetWorkspaces {
workspaces(workspaceUuid:"%s") {
uuid
label
description
active
createdAt
updatedAt
}
}`

workspaceCreateRequest = `
mutation CreateWorkspace {
createWorkspace(
Expand Down Expand Up @@ -300,15 +335,15 @@ func (c *Client) QueryHouston(query string) (*HoustonResponse, error) {
Raw: httpResponse,
Body: string(body),
}

decode := HoustonResponse{}
err = json.NewDecoder(strings.NewReader(response.Body)).Decode(&decode)
if err != nil {
return nil, errors.Wrap(err, "Failed to JSON decode Houston response")
}

// Houston Specific Errors
if decode.Errors != nil {
return nil, errors.New("failed to successfully decode response")
return nil, errors.New(decode.Errors[0].Message)
}

return &decode, nil
Expand Down Expand Up @@ -404,6 +439,19 @@ func (c *Client) DeleteWorkspace(uuid string) (*Workspace, error) {
return response.Data.DeleteWorkspace, nil
}

// GetAllDeployments will request all airflow deployments from Houston
// Returns a []Deployment structure with deployment details
func (c *Client) GetAllDeployments() ([]Deployment, error) {
request := deploymentsGetAllRequest

response, err := c.QueryHouston(request)
if err != nil {
return nil, errors.Wrap(err, "GetAllDeployments Failed")
}

return response.Data.GetDeployments, nil
}

// GetDeployments will request all airflow deployments from Houston
// Returns a []Deployment structure with deployment details
func (c *Client) GetDeployments(ws string) ([]Deployment, error) {
Expand Down Expand Up @@ -446,6 +494,19 @@ func (c *Client) GetAuthConfig() (*AuthConfig, error) {
return response.Data.GetAuthConfig, nil
}

// GetWorkspaceAll returns all available workspaces from houston API
// Returns a slice of all Workspaces a user has access to
func (c *Client) GetWorkspace(uuid string) (*Workspace, error) {
request := fmt.Sprintf(workspaceGetRequest, uuid)

response, err := c.QueryHouston(request)
if err != nil {
return nil, errors.Wrap(err, "GetWorkspace Failed")
}

return response.Data.GetWorkspace, nil
}

// GetWorkspaceAll returns all available workspaces from houston API
// Returns a slice of all Workspaces a user has access to
func (c *Client) GetWorkspaceAll() ([]Workspace, error) {
Expand All @@ -456,7 +517,7 @@ func (c *Client) GetWorkspaceAll() ([]Workspace, error) {
return nil, errors.Wrap(err, "GetWorkspaceAll Failed")
}

return response.Data.GetWorkspace, nil
return response.Data.GetWorkspaces, nil
}

// GetUserAll sends a request to Houston in order to fetch a slice of users
Expand Down
6 changes: 4 additions & 2 deletions houston/types.go
Expand Up @@ -14,7 +14,8 @@ type HoustonResponse struct {
GetDeployments []Deployment `json:"deployments,omitempty"`
GetAuthConfig *AuthConfig `json:"authConfig,omitempty"`
GetUsers []User `json:"users,omitempty"`
GetWorkspace []Workspace `json:"workspaces,omitempty"`
GetWorkspace *Workspace `json:"workspace,omitempty"`
GetWorkspaces []Workspace `json:"workspaces,omitempty"`
UpdateDeployment *Deployment `json:"updateDeployment,omitempty"`
UpdateWorkspace *Workspace `json:"updateWorkspace,omitempty"`
} `json:"data"`
Expand Down Expand Up @@ -50,7 +51,7 @@ type Deployment struct {
ReleaseName string `json:"releaseName"`
Version string `json:"version"`
DeploymentInfo DeploymentInfo `json:"deployInfo"`
Workspace string `json:"workspace"`
Workspace Workspace `json:"workspace"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
}
Expand All @@ -73,6 +74,7 @@ type Email struct {
// Error defines struct of a houston response Error object
type Error struct {
Message string `json:"message"`
Name string `json:"name"`
}

// Status defines structure of a houston response StatusResponse object
Expand Down
9 changes: 8 additions & 1 deletion workspace/workspace.go
@@ -1,9 +1,10 @@
package workspace

import (
"errors"
"fmt"

"github.com/pkg/errors"

"github.com/astronomerio/astro-cli/config"
"github.com/astronomerio/astro-cli/houston"
"github.com/astronomerio/astro-cli/messages"
Expand Down Expand Up @@ -84,6 +85,12 @@ func GetCurrentWorkspace() (string, error) {

// Switch switches workspaces
func Switch(uuid string) error {
// validate workspace
_, err := api.GetWorkspace(uuid)
if err != nil {
return errors.Wrap(err, "workspace uuid is not valid")
}

c, err := config.GetCurrentContext()
if err != nil {
return err
Expand Down