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 deployment create and delete #84

Merged
merged 1 commit into from Jul 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 0 additions & 37 deletions airflow/airflow.go
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/iancoleman/strcase"

"github.com/astronomerio/astro-cli/airflow/include"
"github.com/astronomerio/astro-cli/config"
"github.com/astronomerio/astro-cli/houston"
"github.com/astronomerio/astro-cli/messages"
"github.com/astronomerio/astro-cli/pkg/fileutil"
Expand Down Expand Up @@ -94,42 +93,6 @@ func Init(path string) error {
return nil
}

// Create new airflow deployment
func Create(title string) error {
response, err := api.CreateDeployment(title)
if err != nil {
return err
}
deployment, err := api.FetchDeployment(response.Id)
if err != nil {
return err
}

fmt.Println(response.Message)

if response.Success {
fmt.Printf("\n"+messages.EE_LINK_AIRFLOW+"\n", deployment.ReleaseName, config.CFG.CloudDomain.GetString())
fmt.Printf(messages.EE_LINK_FLOWER+"\n", deployment.ReleaseName, config.CFG.CloudDomain.GetString())
fmt.Printf(messages.EE_LINK_GRAFANA+"\n", deployment.ReleaseName, config.CFG.CloudDomain.GetString())
}

return nil
}

// List all airflow deployments
func List() error {
deployments, err := api.FetchDeployments()
if err != nil {
return err
}

for _, d := range deployments {
rowTmp := "Title: %s\nId: %s\nRelease: %s\nVersion: %s\n\n"
fmt.Printf(rowTmp, d.Title, d.Id, d.ReleaseName, d.Version)
}
return nil
}

func validateOrCreateProjectName(path, projectName string) (string, error) {
if len(projectName) != 0 {
projectNameValid := regexp.MustCompile(`^[A-Za-z0-9]([A-Za-z0-9_-]*[A-Za-z0-9])?$`).MatchString
Expand Down
31 changes: 0 additions & 31 deletions cmd/airflow.go
Expand Up @@ -37,23 +37,6 @@ var (
RunE: airflowInit,
}

airflowCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a new airflow deployment",
Long: "Create a new airflow deployment",
Args: cobra.ExactArgs(1),
RunE: airflowCreate,
Deprecated: fmt.Sprintf(messages.CLI_CMD_DEPRECATE, "astro deployment create"),
}

airflowListCmd = &cobra.Command{
Use: "list",
Short: "List airflow clusters",
Long: "List all created airflow clusters",
RunE: airflowList,
Deprecated: fmt.Sprintf(messages.CLI_CMD_DEPRECATE, "astro deployment list"),
}

airflowDeployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy an airflow project",
Expand Down Expand Up @@ -108,12 +91,6 @@ func init() {
airflowInitCmd.Flags().StringVarP(&projectName, "name", "n", "", "Name of airflow project")
airflowRootCmd.AddCommand(airflowInitCmd)

// Airflow create
airflowRootCmd.AddCommand(airflowCreateCmd)

// Airflow list
airflowRootCmd.AddCommand(airflowListCmd)

// Airflow deploy
airflowRootCmd.AddCommand(airflowDeployCmd)
airflowDeployCmd.Flags().BoolVarP(&forceDeploy, "force", "f", false, "Force deploy if uncommited changes")
Expand Down Expand Up @@ -173,14 +150,6 @@ func airflowInit(cmd *cobra.Command, args []string) error {
return nil
}

func airflowCreate(cmd *cobra.Command, args []string) error {
return airflow.Create(args[0])
}

func airflowList(cmd *cobra.Command, args []string) error {
return airflow.List()
}

func airflowDeploy(cmd *cobra.Command, args []string) error {
releaseName := ""
if len(args) > 0 {
Expand Down
24 changes: 17 additions & 7 deletions cmd/deployment.go
@@ -1,10 +1,13 @@
package cmd

import (
"github.com/astronomerio/astro-cli/deployment"
"github.com/spf13/cobra"
)

var (
teamId string

deploymentRootCmd = &cobra.Command{
Use: "deployment",
Aliases: []string{"de"},
Expand All @@ -16,21 +19,24 @@ var (
Use: "create",
Short: "Create a new Astronomer Deployment",
Long: "Create a new Astronomer Deployment",
Args: cobra.ExactArgs(1),
RunE: deploymentCreate,
}

deploymentDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete an airflow deployment",
Long: "Delete an airflow deployment",
Args: cobra.ExactArgs(1),
RunE: deploymentDelete,
}

deploymentListCmd = &cobra.Command{
Use: "list",
Short: "List airflow deployments",
Long: "List airflow deployments",
RunE: deploymentList,
Use: "list",
Aliases: []string{"ls"},
Short: "List airflow deployments",
Long: "List airflow deployments",
RunE: deploymentList,
}

deploymentUpdateCmd = &cobra.Command{
Expand All @@ -47,6 +53,10 @@ func init() {

// deployment create
deploymentRootCmd.AddCommand(deploymentCreateCmd)
deploymentCreateCmd.Flags().StringVarP(&teamId, "teamId", "t", "", "team id to associate with deployment")

// deployment delete
deploymentRootCmd.AddCommand(deploymentDeleteCmd)

// deployment list
deploymentRootCmd.AddCommand(deploymentListCmd)
Expand All @@ -56,15 +66,15 @@ func init() {
}

func deploymentCreate(cmd *cobra.Command, args []string) error {
return nil
return deployment.Create(args[0], teamId)
}

func deploymentDelete(cmd *cobra.Command, args []string) error {
return nil
return deployment.Delete(args[0])
}

func deploymentList(cmd *cobra.Command, args []string) error {
return nil
return deployment.List()
}

func deploymentUpdate(cmd *cobra.Command, args []string) error {
Expand Down
97 changes: 52 additions & 45 deletions houston/houston.go
Expand Up @@ -16,16 +16,17 @@ var (
createDeploymentRequest = `
mutation CreateDeployment {
createDeployment(
title: "%s",
organizationUuid: "",
teamUuid: "",
label: "%s",
type: "airflow",
version: ""
teamUuid: "%s"
) {
success,
message,
id,
code
uuid
type
label
releaseName
version
createdAt
updatedAt
}
}`

Expand Down Expand Up @@ -85,6 +86,19 @@ var (
}
}`

deleteDeploymentRequest = `
mutation DeleteDeployment {
deleteDeployment(deploymentUuid: "%s") {
uuid
type
label
releaseName
version
createdAt
updatedAt
}
}`

deleteWorkspaceRequest = `
mutation DeleteWorkspace {
deleteTeam(teamUuid: "%s") {
Expand All @@ -98,26 +112,30 @@ var (
}`

fetchDeploymentsRequest = `
query FetchAllDeployments {
fetchDeployments {
uuid
query GetDeployments {
deployments {
uuid
type
title
release_name
label
releaseName
version
createdAt
updatedAt
}
}`

fetchDeploymentRequest = `
query FetchDeployment {
fetchDeployments(
query GetDeployment {
deployments(
deploymentUuid: "%s"
) {
uuid
type
title
release_name
label
releaseName
version
createdAt
updatedAt
}
}`

Expand Down Expand Up @@ -163,7 +181,6 @@ type GraphQLQuery struct {

// QueryHouston executes a query against the Houston API
func (c *Client) QueryHouston(query string) (*HoustonResponse, error) {
// logger := log.WithField("function", "QueryHouston")
doOpts := httputil.DoOptions{
Data: GraphQLQuery{query},
Headers: map[string]string{
Expand Down Expand Up @@ -198,13 +215,9 @@ func (c *Client) QueryHouston(query string) (*HoustonResponse, error) {
Body: string(body),
}

// logger.Debug(query)
// logger.Debug(response.Body)

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

Expand All @@ -216,15 +229,11 @@ func (c *Client) QueryHouston(query string) (*HoustonResponse, error) {

// CreateDeployment will send request to Houston to create a new AirflowDeployment
// Returns a StatusResponse which contains the unique id of deployment
func (c *Client) CreateDeployment(title string) (*Status, error) {
// logger := log.WithField("method", "CreateDeployment")
// logger.Debug("Entered CreateDeployment")

request := fmt.Sprintf(createDeploymentRequest, title)

func (c *Client) CreateDeployment(title, teamId string) (*Deployment, error) {
request := fmt.Sprintf(createDeploymentRequest, title, teamId)
fmt.Println(request)
response, err := c.QueryHouston(request)
if err != nil {
// logger.Error(err)
return nil, errors.Wrap(err, "CreateDeployment Failed")
}

Expand All @@ -234,14 +243,10 @@ func (c *Client) CreateDeployment(title string) (*Status, error) {
// CreateBasicToken will request a new token from Houston, passing the users e-mail and password.
// Returns a Token structure with the users ID and Token inside.
func (c *Client) CreateBasicToken(email string, password string) (*AuthUser, error) {
// logger := log.WithField("method", "CreateToken")
// logger.Debug("Entered CreateToken")

request := fmt.Sprintf(createBasicTokenRequest, email, password)

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

Expand Down Expand Up @@ -286,6 +291,17 @@ func (c *Client) CreateWorkspace(label, description string) (*Workspace, error)
return response.Data.CreateWorkspace, nil
}

func (c *Client) DeleteDeployment(uuid string) (*Deployment, error) {
request := fmt.Sprintf(deleteDeploymentRequest, uuid)

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

return response.Data.DeleteDeployment, nil
}

// DeleteWorkspace will send a request to houston to create a new workspace
// Returns an object representing deleted workspace
func (c *Client) DeleteWorkspace(uuid string) (*Workspace, error) {
Expand All @@ -302,38 +318,30 @@ func (c *Client) DeleteWorkspace(uuid string) (*Workspace, error) {
// FetchDeployments will request all airflow deployments from Houston
// Returns a []Deployment structure with deployment details
func (c *Client) FetchDeployments() ([]Deployment, error) {
// logger := log.WithField("method", "FetchDeployments")
// logger.Debug("Entered FetchDeployments")

request := fetchDeploymentsRequest

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

return response.Data.FetchDeployments, nil
return response.Data.GetDeployments, nil
}

// FetchDeployment will request a specific airflow deployments from Houston by uuid
// Returns a Deployment structure with deployment details
func (c *Client) FetchDeployment(deploymentUuid string) (*Deployment, error) {
// logger := log.WithField("method", "FetchDeployments")
// logger.Debug("Entered FetchDeployments")

request := fmt.Sprintf(fetchDeploymentRequest, deploymentUuid)

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

if len(response.Data.FetchDeployments) == 0 {
if len(response.Data.GetDeployments) == 0 {
return nil, fmt.Errorf("deployment not found for uuid \"%s\"", deploymentUuid)
}
return &response.Data.FetchDeployments[0], nil
return &response.Data.GetDeployments[0], nil
}

// GetAuthConfig will fetch authentication configuration from houston
Expand All @@ -342,7 +350,6 @@ func (c *Client) GetAuthConfig() (*AuthConfig, error) {

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

Expand Down