Skip to content

Commit

Permalink
atlasaction: refactor monitor/schema (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm authored Jan 13, 2025
1 parent 0c3d8ed commit 6f789be
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
55 changes: 25 additions & 30 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,52 +816,47 @@ func (a *Actions) SchemaApply(ctx context.Context) error {

// MonitorSchema runs the Action for "ariga/atlas-action/monitor/schema"
func (a *Actions) MonitorSchema(ctx context.Context) error {
db, err := a.GetURLInput("url")
if err != nil {
if err := a.RequiredInputs("cloud-token"); err != nil {
return err
}
var (
config = a.GetInput("config")
env = a.GetInput("env")
)
if (config != "" || env != "") && db.String() != "" {
return errors.New("only one of the inputs 'config' or 'url' must be given")
}
var (
id = cloud.ScopeIdent{
ExtID: a.GetInput("slug"),
Schemas: a.GetArrayInput("schemas"),
Exclude: a.GetArrayInput("exclude"),
}
)
if err = a.Atlas.Login(ctx, &atlasexec.LoginParams{
if err := a.Atlas.Login(ctx, &atlasexec.LoginParams{
Token: a.GetInput("cloud-token"),
}); err != nil {
return fmt.Errorf("failed to login to Atlas Cloud: %w", err)
}
params := &atlasexec.SchemaInspectParams{
URL: db.String(),
ConfigURL: config,
Env: env,
Schema: id.Schemas,
Exclude: id.Schemas,
URL: a.GetInput("url"),
ConfigURL: a.GetInput("config"),
Env: a.GetInput("env"),
Schema: a.GetArrayInput("schemas"),
Exclude: a.GetArrayInput("exclude"),
Format: `{{ printf "# %s\n# %s\n%s" .RedactedURL .Hash .MarshalHCL }}`,
}
res, err := a.Atlas.SchemaInspect(ctx, params)
if (params.ConfigURL != "" || params.Env != "") && params.URL != "" {
return errors.New("only one of the inputs 'config' or 'url' must be given")
}
hcl, err := a.Atlas.SchemaInspect(ctx, params)
if err != nil {
return fmt.Errorf("failed to inspect the schema: %w", err)
}
var (
parts = strings.SplitN(res, "\n", 3)
url = strings.TrimPrefix(parts[0], "# ") // redacted URL.
hash = strings.TrimPrefix(parts[1], "# ")
hcl = parts[2]
)
id.URL = url
var redactedURL, hash string
if parts := strings.SplitN(hcl, "\n", 3); len(parts) != 3 {
return fmt.Errorf("invalid inspect output, expect 3 lines, got %d", len(parts))
} else {
redactedURL = strings.TrimPrefix(parts[0], "# ")
hash = strings.TrimPrefix(parts[1], "# ")
hcl = parts[2]
}
cc, err := a.cloudClient(ctx, "cloud-token")
if err != nil {
return err
}
id := cloud.ScopeIdent{
URL: redactedURL,
Schemas: params.Schema,
Exclude: params.Exclude,
ExtID: a.GetInput("slug"),
}
h, err := cc.SnapshotHash(ctx, &cloud.SnapshotHashInput{ScopeIdent: id})
if err != nil {
return fmt.Errorf("failed to get the schema snapshot hash: %w", err)
Expand Down
3 changes: 1 addition & 2 deletions monitor/schema/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ inputs:
For example, `file://config/atlas.hcl`, learn more about [Atlas configuration files](https://atlasgo.io/atlas-schema/projects).'
required: false
env:
description: 'The environment to use from the Atlas configuration file. For example, `dev`
(mutually exclusive with `url`).'
description: 'The environment to use from the Atlas configuration file. For example, `dev` (mutually exclusive with `url`).'
required: false
slug:
description: 'Optional unique identifier for the database server.'
Expand Down

0 comments on commit 6f789be

Please sign in to comment.