Skip to content

Commit

Permalink
minor changes to astro deployment inspect (#884)
Browse files Browse the repository at this point in the history
* rename fields

- `information` is now `metadata`
- `scheduler_replicas` is now `scheduler_count`
- `astronomer_variables` is now `environment_variables`
- re-order fields in the `deploymentMetadata` and `deploymentConfiguration` structs

* apply order to printed deployments

- use `mapstructre.Decode()` to convert to struct once to get ordered output

* add dag_deploy_enabled to printed deployments

- rename `getDeploymentInspectInfo()` to `getDeploymentInfo()`
  • Loading branch information
jemishp committed Nov 18, 2022
1 parent 04bacf0 commit cbc88e4
Show file tree
Hide file tree
Showing 2 changed files with 402 additions and 206 deletions.
122 changes: 92 additions & 30 deletions cloud/deployment/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,71 @@ import (
"fmt"
"io"
"strings"
"time"

"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v3"

"github.com/astronomer/astro-cli/astro-client"
"github.com/astronomer/astro-cli/cloud/deployment"
)

type deploymentMetadata struct {
DeploymentID string `mapstructure:"deployment_id" yaml:"deployment_id" json:"deployment_id"`
WorkspaceID string `mapstructure:"workspace_id" yaml:"workspace_id" json:"workspace_id"`
ClusterID string `mapstructure:"cluster_id" yaml:"cluster_id" json:"cluster_id"`
ReleaseName string `mapstructure:"release_name" yaml:"release_name" json:"release_name"`
AirflowVersion string `mapstructure:"airflow_version" yaml:"airflow_version" json:"airflow_version"`
DagDeployEnabled bool `mapstructure:"dag_deploy_enabled" yaml:"dag_deploy_enabled" json:"dag_deploy_enabled"`
Status string `mapstructure:"status" yaml:"status" json:"status"`
CreatedAt time.Time `mapstructure:"created_at" yaml:"created_at" json:"created_at"`
UpdatedAt time.Time `mapstructure:"updated_at" yaml:"updated_at" json:"updated_at"`
DeploymentURL string `mapstructure:"deployment_url" yaml:"deployment_url" json:"deployment_url"`
WebserverURL string `mapstructure:"webserver_url" yaml:"webserver_url" json:"webserver_url"`
}

type deploymentConfig struct {
Name string `mapstructure:"name" yaml:"name" json:"name"`
Description string `mapstructure:"description" yaml:"description" json:"description"`
RunTimeVersion string `mapstructure:"runtime_version" yaml:"runtime_version" json:"runtime_version"`
SchedulerAU int `mapstructure:"scheduler_au" yaml:"scheduler_au" json:"scheduler_au"`
SchedulerCount int `mapstructure:"scheduler_count" yaml:"scheduler_count" json:"scheduler_count"`
ClusterID string `mapstructure:"cluster_id" yaml:"cluster_id" json:"cluster_id"` // this is also in deploymentMetadata
}

type workerq struct {
Name string `mapstructure:"name" yaml:"name" json:"name"`
ID string `mapstructure:"id" yaml:"id" json:"id"`
IsDefault bool `mapstructure:"is_default" yaml:"is_default" json:"is_default"`
MaxWorkerCount int `mapstructure:"max_worker_count" yaml:"max_worker_count" json:"max_worker_count"`
MinWorkerCount int `mapstructure:"min_worker_count" yaml:"min_worker_count" json:"min_worker_count"`
WorkerConcurrency int `mapstructure:"worker_concurrency" yaml:"worker_concurrency" json:"worker_concurrency"`
NodePoolID string `mapstructure:"node_pool_id" yaml:"node_pool_id" json:"node_pool_id"`
}

type environmentVariable struct {
IsSecret bool `mapstructure:"is_secret" yaml:"is_secret" json:"is_secret"`
Key string `mapstructure:"key" yaml:"key" json:"key"`
UpdatedAt string `mapstructure:"updated_at" yaml:"updated_at" json:"updated_at"`
Value string `mapstructure:"value" yaml:"value" json:"value"`
}

type orderedPieces struct {
EnvVars []environmentVariable `mapstructure:"environment_variables" yaml:"environment_variables" json:"environment_variables"`
Configuration deploymentConfig `mapstructure:"configuration" yaml:"configuration" json:"configuration"`
WorkerQs []workerq `mapstructure:"worker_queues" yaml:"worker_queues" json:"worker_queues"`
Metadata deploymentMetadata `mapstructure:"metadata" yaml:"metadata" json:"metadata"`
AlertEmails []string `mapstructure:"alert_emails" yaml:"alert_emails" json:"alert_emails"`
}

type formattedDeployment struct {
Deployment orderedPieces `mapstructure:"deployment" yaml:"deployment" json:"deployment"`
}

var (
jsonMarshal = json.MarshalIndent
yamlMarshal = yaml.Marshal
decodeToStruct = mapstructure.Decode
errKeyNotFound = errors.New("not found in deployment")
)

Expand All @@ -37,7 +92,7 @@ func Inspect(wsID, deploymentName, deploymentID, outputFormat string, client ast
}

// create a map for deployment.information
deploymentInfoMap, err = getDeploymentInspectInfo(&requestedDeployment)
deploymentInfoMap, err = getDeploymentInfo(&requestedDeployment)
if err != nil {
return err
}
Expand Down Expand Up @@ -65,7 +120,7 @@ func Inspect(wsID, deploymentName, deploymentID, outputFormat string, client ast
return nil
}

func getDeploymentInspectInfo(sourceDeployment *astro.Deployment) (map[string]interface{}, error) {
func getDeploymentInfo(sourceDeployment *astro.Deployment) (map[string]interface{}, error) {
var (
deploymentURL string
err error
Expand All @@ -76,35 +131,36 @@ func getDeploymentInspectInfo(sourceDeployment *astro.Deployment) (map[string]in
return nil, err
}
return map[string]interface{}{
"deployment_id": sourceDeployment.ID,
"workspace_id": sourceDeployment.Workspace.ID,
"cluster_id": sourceDeployment.Cluster.ID,
"airflow_version": sourceDeployment.RuntimeRelease.AirflowVersion,
"release_name": sourceDeployment.ReleaseName,
"deployment_url": deploymentURL,
"webserver_url": sourceDeployment.DeploymentSpec.Webserver.URL,
"created_at": sourceDeployment.CreatedAt,
"updated_at": sourceDeployment.UpdatedAt,
"status": sourceDeployment.Status,
"deployment_id": sourceDeployment.ID,
"workspace_id": sourceDeployment.Workspace.ID,
"cluster_id": sourceDeployment.Cluster.ID,
"airflow_version": sourceDeployment.RuntimeRelease.AirflowVersion,
"release_name": sourceDeployment.ReleaseName,
"deployment_url": deploymentURL,
"webserver_url": sourceDeployment.DeploymentSpec.Webserver.URL,
"created_at": sourceDeployment.CreatedAt,
"updated_at": sourceDeployment.UpdatedAt,
"status": sourceDeployment.Status,
"dag_deploy_enabled": sourceDeployment.DagDeployEnabled,
}, nil
}

func getDeploymentConfig(sourceDeployment *astro.Deployment) map[string]interface{} {
return map[string]interface{}{
"name": sourceDeployment.Label,
"description": sourceDeployment.Description,
"cluster_id": sourceDeployment.Cluster.ID,
"runtime_version": sourceDeployment.RuntimeRelease.Version,
"scheduler_au": sourceDeployment.DeploymentSpec.Scheduler.AU,
"scheduler_replicas": sourceDeployment.DeploymentSpec.Scheduler.Replicas,
"name": sourceDeployment.Label,
"description": sourceDeployment.Description,
"cluster_id": sourceDeployment.Cluster.ID,
"runtime_version": sourceDeployment.RuntimeRelease.Version,
"scheduler_au": sourceDeployment.DeploymentSpec.Scheduler.AU,
"scheduler_count": sourceDeployment.DeploymentSpec.Scheduler.Replicas,
}
}

func getAdditional(sourceDeployment *astro.Deployment) map[string]interface{} {
return map[string]interface{}{
"alert_emails": sourceDeployment.AlertEmails,
"worker_queues": getQMap(sourceDeployment.WorkerQueues),
"astronomer_variables": getVariablesMap(sourceDeployment.DeploymentSpec.EnvironmentVariablesObjects), // API only returns values when !EnvironmentVariablesObject.isSecret
"alert_emails": sourceDeployment.AlertEmails,
"worker_queues": getQMap(sourceDeployment.WorkerQueues),
"environment_variables": getVariablesMap(sourceDeployment.DeploymentSpec.EnvironmentVariablesObjects), // API only returns values when !EnvironmentVariablesObject.isSecret
}
}

Expand Down Expand Up @@ -141,18 +197,24 @@ func getVariablesMap(sourceDeploymentVars []astro.EnvironmentVariablesObject) []

func formatPrintableDeployment(outputFormat string, printableDeployment map[string]interface{}) ([]byte, error) {
var (
infoToPrint []byte
err error
infoToPrint []byte
err error
formatWithOrder formattedDeployment
)

// use mapstructure to decode to a struct
err = decodeToStruct(printableDeployment, &formatWithOrder)
if err != nil {
return []byte{}, err
}
switch outputFormat {
case jsonFormat:
if infoToPrint, err = jsonMarshal(printableDeployment, "", " "); err != nil {
if infoToPrint, err = jsonMarshal(formatWithOrder, "", " "); err != nil {
return []byte{}, err
}
default:
// always yaml by default
if infoToPrint, err = yamlMarshal(printableDeployment); err != nil {
if infoToPrint, err = yamlMarshal(formatWithOrder); err != nil {
return []byte{}, err
}
}
Expand Down Expand Up @@ -188,11 +250,11 @@ func getSpecificField(deploymentMap map[string]interface{}, requestedField strin
func getPrintableDeployment(infoMap, configMap, additionalMap map[string]interface{}) map[string]interface{} {
printableDeployment := map[string]interface{}{
"deployment": map[string]interface{}{
"information": infoMap,
"configuration": configMap,
"alert_emails": additionalMap["alert_emails"],
"worker_queues": additionalMap["worker_queues"],
"astronomer_variables": additionalMap["astronomer_variables"],
"metadata": infoMap,
"configuration": configMap,
"alert_emails": additionalMap["alert_emails"],
"worker_queues": additionalMap["worker_queues"],
"environment_variables": additionalMap["environment_variables"],
},
}
return printableDeployment
Expand Down

0 comments on commit cbc88e4

Please sign in to comment.