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

update get deployment query #1365

Merged
merged 21 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ae4b0b2
update get deployment query
Simpcyclassy Aug 23, 2023
dab003a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 23, 2023
5a44736
fix query incomaptibility
Simpcyclassy Aug 24, 2023
7541e06
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 24, 2023
e44263f
get first item in get deployment client function
Simpcyclassy Aug 24, 2023
1ae76c4
Merge branch 'update-get-deployment-query' of github.com:astronomer/a…
Simpcyclassy Aug 24, 2023
18909bb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 24, 2023
941dbe1
define error with error handler
Simpcyclassy Aug 24, 2023
f39e952
merge branch
Simpcyclassy Aug 24, 2023
e8b3cd0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 24, 2023
1e08fa7
update variable global declaration
Simpcyclassy Aug 24, 2023
e10622c
merge branch
Simpcyclassy Aug 24, 2023
5a7eb31
fix test
Simpcyclassy Aug 24, 2023
24827a9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 24, 2023
6de2cf3
add test for successful but empty get deployment
Simpcyclassy Aug 24, 2023
02a04c7
Merge branch 'update-get-deployment-query' of github.com:astronomer/a…
Simpcyclassy Aug 24, 2023
4e83fab
remove new line
Simpcyclassy Aug 24, 2023
788cb7a
Merge branch 'main' into update-get-deployment-query
Simpcyclassy Aug 24, 2023
1445790
fix backward compatibility
Simpcyclassy Aug 25, 2023
7c78c38
Merge branch 'update-get-deployment-query' of github.com:astronomer/a…
Simpcyclassy Aug 25, 2023
c6a428c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 69 additions & 21 deletions houston/deployment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package houston

import "time"
import (
"encoding/json"
"fmt"
"time"
)

var errDeploymentNotFound = fmt.Errorf("deployment not found")

// ListDeploymentsRequest - filters to list deployments according to set values
type ListDeploymentsRequest struct {
Expand Down Expand Up @@ -330,25 +336,27 @@ var (
},
{
version: "0.29.0",
query: `
query GetDeployment(
$id: String!
){
deployment(
where: {id: $id}
){
id
airflowVersion
desiredAirflowVersion
runtimeVersion
desiredRuntimeVersion
runtimeAirflowVersion
urls {
type
url
}
}
}`,
query: generateGetDeploymentQuery("id"),
},
{
version: "0.30.8",
query: generateGetDeploymentQuery("deploymentId"),
},
{
version: "0.32.0",
query: generateGetDeploymentQuery("id"),
},
{
version: "0.32.3",
query: generateGetDeploymentQuery("deploymentId"),
},
{
version: "0.33.0",
query: generateGetDeploymentQuery("id"),
},
{
version: "0.33.1",
query: generateGetDeploymentQuery("deploymentId"),
},
}

Expand Down Expand Up @@ -464,6 +472,29 @@ var (
}`
)

// GenerateQuery generates the deployment query based on the given version and query parameter
func generateGetDeploymentQuery(queryParam string) string {
return `
query GetDeployment(
$id: String!
) {
deployment(
where: {` + queryParam + `: $id}
) {
id
airflowVersion
desiredAirflowVersion
runtimeVersion
desiredRuntimeVersion
runtimeAirflowVersion
urls {
type
url
}
}
}`
}

// CreateDeployment - create a deployment
func (h ClientImplementation) CreateDeployment(vars map[string]interface{}) (*Deployment, error) {
reqQuery := DeploymentCreateRequest.GreatestLowerBound(version)
Expand Down Expand Up @@ -551,7 +582,24 @@ func (h ClientImplementation) GetDeployment(deploymentID string) (*Deployment, e
return nil, handleAPIErr(err)
}

return &res.Data.GetDeployment, nil
var deploymentSlice []Deployment
var deployment Deployment

err = json.Unmarshal(res.Data.GetDeployment, &deployment)
if err == nil {
return &deployment, nil
}

err = json.Unmarshal(res.Data.GetDeployment, &deploymentSlice)
if err != nil {
return nil, handleAPIErr(err)
}

if len(deploymentSlice) > 0 {
return &deploymentSlice[0], nil
}

return nil, handleAPIErr(fmt.Errorf("GetDeployment failed for id: %s: %w", deploymentID, errDeploymentNotFound))
}

// UpdateDeploymentAirflow - update airflow on a deployment
Expand Down
62 changes: 42 additions & 20 deletions houston/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,25 +255,25 @@ func TestGetDeployment(t *testing.T) {

mockDeployment := &Response{
Data: ResponseData{
GetDeployment: Deployment{
ID: "deployment-test-id",
Type: "airflow",
Label: "test deployment",
ReleaseName: "prehistoric-gravity-930",
Version: "2.2.0",
AirflowVersion: "2.2.0",
DesiredAirflowVersion: "2.2.0",
DeploymentInfo: DeploymentInfo{},
Workspace: Workspace{
ID: "test-workspace-id",
},
Urls: []DeploymentURL{
{Type: "airflow", URL: "http://airflow.com"},
{Type: "flower", URL: "http://flower.com"},
},
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
},
GetDeployment: []byte(`
{
"id": "deployment-test-id",
"type": "airflow",
"label": "test deployment",
"releaseName": "prehistoric-gravity-930",
"version": "2.2.0",
"airflowVersion": "2.2.0",
"desiredAirflowVersion": "2.2.0",
"deploymentInfo": {},
"workspace": {
"id": "test-workspace-id"
},
"urls": [
{"type": "airflow", "url": "http://airflow.com"},
{"type": "flower", "url": "http://flower.com"}
]
}
`),
},
}
jsonResponse, err := json.Marshal(mockDeployment)
Expand All @@ -291,7 +291,7 @@ func TestGetDeployment(t *testing.T) {

deployment, err := api.GetDeployment("deployment-id")
assert.NoError(t, err)
assert.Equal(t, deployment, &mockDeployment.Data.GetDeployment)
assert.Equal(t, deployment.ID, "deployment-test-id")
})

t.Run("error", func(t *testing.T) {
Expand All @@ -307,6 +307,28 @@ func TestGetDeployment(t *testing.T) {
_, err := api.GetDeployment("deployment-id")
assert.Contains(t, err.Error(), "Internal Server Error")
})

mockDeployment = &Response{
Data: ResponseData{
GetDeployment: []byte(`[]`),
},
}
jsonResponse, err = json.Marshal(mockDeployment)
assert.NoError(t, err)

t.Run("successful query but empty result error", func(t *testing.T) {
client := testUtil.NewTestClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(bytes.NewBuffer(jsonResponse)),
Header: make(http.Header),
}
})
api := NewClient(client)

_, err := api.GetDeployment("deployment-id")
assert.Contains(t, err.Error(), "GetDeployment failed for id:")
})
}

func TestUpdateDeploymentAirflow(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion houston/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package houston

import (
"encoding/json"
"fmt"
"time"

Expand Down Expand Up @@ -30,7 +31,7 @@ type ResponseData struct {
DeleteWorkspaceServiceAccount *ServiceAccount `json:"deleteWorkspaceServiceAccount,omitempty"`
DeleteDeploymentServiceAccount *ServiceAccount `json:"deleteDeploymentServiceAccount,omitempty"`
DeleteWorkspace *Workspace `json:"deleteWorkspace,omitempty"`
GetDeployment Deployment `json:"deployment,omitempty"`
GetDeployment json.RawMessage `json:"deployment,omitempty"`
GetDeployments []Deployment `json:"workspaceDeployments,omitempty"`
GetAuthConfig *AuthConfig `json:"authConfig,omitempty"`
GetAppConfig *AppConfig `json:"appConfig,omitempty"`
Expand Down