Skip to content

Commit

Permalink
Refactoring graphql queries to use graphql query variables
Browse files Browse the repository at this point in the history
* Improving graphql query support

* Add create deployment with variabels built into gql query str

* Refactor every query to use gql variable syntax

* Simplify interface for querying houston

* Refactor deployments to use new graphql interface

* Use type map[string]interface{} to allow passing of json payloads

* Make map creation inline with struct definition

* Remove debug prints

* Refactoring workspaces to use new houston request struct

* Adding new houston code - to be cleaned up and removed later

* Comment out queries to be replaced

* Refactor service accounts and users

* Refactor auth queries

* Refactor docker queries

* Remove unused functions so far

* Remove unused messages

* Remove deprecated api and http vars

* Remove unused graphql experiment

* Add more checks around workspace uuid

* Consolidate getWorkspace and getWorkspaces

* Fix service account variable name
  • Loading branch information
andscoop authored Oct 8, 2018
1 parent d484992 commit 96b1b34
Show file tree
Hide file tree
Showing 11 changed files with 560 additions and 700 deletions.
34 changes: 26 additions & 8 deletions airflow/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/astronomerio/astro-cli/docker"
"github.com/astronomerio/astro-cli/houston"
"github.com/astronomerio/astro-cli/messages"
"github.com/astronomerio/astro-cli/pkg/httputil"
"github.com/astronomerio/astro-cli/pkg/input"
"github.com/astronomerio/astro-cli/pkg/printutil"
)
Expand All @@ -35,9 +34,6 @@ const (
)

var (
http = httputil.NewHTTPClient()
api = houston.NewHoustonClient(http)

tab = printutil.Table{
Padding: []int{5, 30, 30, 50},
Header: []string{"#", "RELEASE NAME", "WORKSPACE", "DEPLOYMENT UUID"},
Expand Down Expand Up @@ -306,18 +302,40 @@ func PS(airflowHome string) error {

// Deploy pushes a new docker image
func Deploy(path, name, wsId string, prompt bool) error {
// TODO This will need refactored once graphql query variabels are built
ws, err := api.GetWorkspace(wsId)
if len(wsId) == 0 {
return errors.New("no workspace uuid provided")
}

// Validate workspace
wsReq := houston.Request{
Query: houston.WorkspacesGetRequest,
Variables: map[string]interface{}{"workspaceUuid": wsId},
}

wsResp, err := wsReq.Do()
if err != nil {
return err
}
w := ws[0]

deployments, err := api.GetDeployments(wsId)
if len(wsResp.Data.GetWorkspaces) == 0 {
return fmt.Errorf("no workspaces with uuid (%s) found", wsId)
}

w := wsResp.Data.GetWorkspaces[0]

// Get Deployments from workspace UUID
deReq := houston.Request{
Query: houston.DeploymentsGetRequest,
Variables: map[string]interface{}{"workspaceUuid": w.Uuid},
}

deResp, err := deReq.Do()
if err != nil {
return err
}

deployments := deResp.Data.GetDeployments

c, err := config.GetCurrentContext()
if err != nil {
return err
Expand Down
46 changes: 31 additions & 15 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,39 @@ import (
"github.com/astronomerio/astro-cli/docker"
"github.com/astronomerio/astro-cli/houston"
"github.com/astronomerio/astro-cli/messages"
"github.com/astronomerio/astro-cli/pkg/httputil"
"github.com/astronomerio/astro-cli/pkg/input"
"github.com/pkg/errors"
)

var (
http = httputil.NewHTTPClient()
api = houston.NewHoustonClient(http)
)

// basicAuth handles authentication with the houston api
func basicAuth(username string) (string, error) {
password, _ := input.InputPassword(messages.INPUT_PASSWORD)

token, err := api.CreateBasicToken(username, password)
req := houston.Request{
Query: houston.TokenBasicCreateRequest,
Variables: map[string]interface{}{"identity": username, "password": password},
}

resp, err := req.Do()
if err != nil {
return "", errors.Wrap(err, "failed to fetch local/db auth token")
return "", err
}

return token.Token.Value, nil
return resp.Data.CreateToken.Token.Value, nil
}

func getWorkspaceByLabel(label string) *houston.Workspace {
workspaces, err := api.GetWorkspaceAll()

req := houston.Request{
Query: houston.WorkspacesGetRequest,
}

resp, err := req.Do()
if err != nil {
return nil
}
workspaces := resp.Data.GetWorkspaces

for _, ws := range workspaces {
if ws.Label == label {
return &ws
Expand Down Expand Up @@ -101,10 +107,15 @@ func Login(domain string, oAuthOnly bool) error {
return err
}

authConfig, err := api.GetAuthConfig()
acReq := houston.Request{
Query: houston.AuthConfigGetRequest,
}

acResp, err := acReq.Do()
if err != nil {
return errors.Wrap(err, "failed to fetch auth config")
return err
}
authConfig := acResp.Data.GetAuthConfig

username := ""
if !oAuthOnly && authConfig.LocalEnabled {
Expand All @@ -130,12 +141,17 @@ func Login(domain string, oAuthOnly bool) error {

c.SetContextKey("token", token)

// Attempt to set projectworkspace if there is only one workspace
workspaces, err := api.GetWorkspaceAll()
wsReq := houston.Request{
Query: houston.WorkspacesGetRequest,
}

wsResp, err := wsReq.Do()
if err != nil {
return nil
return err
}

workspaces := wsResp.Data.GetWorkspaces

if len(workspaces) == 1 && len(c.Workspace) == 0 {
w := workspaces[0]
c.SetContextKey("workspace", w.Uuid)
Expand Down
44 changes: 32 additions & 12 deletions deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,29 @@ import (

"github.com/astronomerio/astro-cli/config"
"github.com/astronomerio/astro-cli/houston"
"github.com/astronomerio/astro-cli/pkg/httputil"
"github.com/astronomerio/astro-cli/pkg/jsonstr"
"github.com/astronomerio/astro-cli/pkg/printutil"
)

var (
http = httputil.NewHTTPClient()
api = houston.NewHoustonClient(http)

tab = printutil.Table{
Padding: []int{30, 50, 50},
Header: []string{"NAME", "RELEASE NAME", "DEPLOYMENT ID"},
}
)

func Create(label, ws string) error {
d, err := api.CreateDeployment(label, ws)
req := houston.Request{
Query: houston.DeploymentCreateRequest,
Variables: map[string]interface{}{"label": label, "workspaceUuid": ws},
}

r, err := req.Do()
if err != nil {
return err
}

d := r.Data.CreateDeployment

c, err := config.GetCurrentContext()
if err != nil {
return err
Expand All @@ -41,7 +43,12 @@ func Create(label, ws string) error {
}

func Delete(uuid string) error {
_, err := api.DeleteDeployment(uuid)
req := houston.Request{
Query: houston.DeploymentDeleteRequest,
Variables: map[string]interface{}{"deploymentUuid": uuid},
}

_, err := req.Do()
if err != nil {
return err
}
Expand All @@ -58,20 +65,28 @@ func Delete(uuid string) error {
// List all airflow deployments
func List(ws string, all bool) error {
var deployments []houston.Deployment
var r *houston.HoustonResponse
var err error

req := houston.Request{
Query: houston.DeploymentsGetRequest,
}

if all {
deployments, err = api.GetAllDeployments()
r, err = req.Do()
if err != nil {
return err
}
} else {
deployments, err = api.GetDeployments(ws)
req.Variables = map[string]interface{}{"workspaceUuid": ws}
r, err = req.Do()
if err != nil {
return err
}
}

deployments = r.Data.GetDeployments

// Build rows
for _, d := range deployments {
if all {
Expand All @@ -87,14 +102,19 @@ func List(ws string, all bool) error {
}

// Update an airflow deployment
func Update(deploymentId string, args map[string]string) error {
s := jsonstr.MapToJsonObjStr(args)
func Update(uuid string, args map[string]string) error {
req := houston.Request{
Query: houston.DeploymentUpdateRequest,
Variables: map[string]interface{}{"deploymentUuid": uuid, "payload": args},
}

d, err := api.UpdateDeployment(deploymentId, s)
r, err := req.Do()
if err != nil {
return err
}

d := r.Data.UpdateDeployment

tab.AddRow([]string{d.Label, d.ReleaseName, d.Id}, false)
tab.SuccessMsg = "\n Successfully updated deployment"
tab.Print()
Expand Down
Loading

0 comments on commit 96b1b34

Please sign in to comment.