Skip to content

Commit

Permalink
cr
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenlu committed Jan 9, 2025
1 parent 04b9dab commit 6a36d0f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 76 deletions.
53 changes: 20 additions & 33 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,11 +825,10 @@ func (a *Actions) MonitorSchema(ctx context.Context) error {
env = a.GetInput("env")
)
if (config != "" || env != "") && db.String() != "" {
return errors.New("only one of the inputs 'config' or 'url' should be provided")
return errors.New("only one of the inputs 'config' or 'url' must be given")
}
var (
id = cloud.ScopeIdent{
URL: db.Redacted(),
ExtID: a.GetInput("slug"),
Schemas: a.GetArrayInput("schemas"),
Exclude: a.GetArrayInput("exclude"),
Expand All @@ -840,28 +839,25 @@ func (a *Actions) MonitorSchema(ctx context.Context) error {
}); err != nil {
return fmt.Errorf("failed to login to Atlas Cloud: %w", err)
}
// Get the URL on the indentifier if not provided (needed for the snapshot hash).
if id.URL == "" {
if id.URL, err = a.Atlas.SchemaInspect(ctx, &atlasexec.SchemaInspectParams{
ConfigURL: config,
Env: env,
Format: `{{ .RedactedURL }}`,
}); err != nil {
return fmt.Errorf("failed to inspect the schema: %w", err)
}
}
params := &atlasexec.SchemaInspectParams{
Schema: id.Schemas,
Exclude: id.Schemas,
Format: `{{ printf "# %s\n%s" .Hash .MarshalHCL }}`,
URL: db.String(),
ConfigURL: config,
Env: env,
Schema: id.Schemas,
Exclude: id.Schemas,
Format: `{{ printf "# %s\n%s\n%s" .RedactedURL .Hash .MarshalHCL }}`,
}
if db.String() != "" {
params.URL = db.String()
}
if config != "" {
params.ConfigURL = config
params.Env = env
res, 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 = parts[1]
hcl = parts[2]
)
id.URL = url
cc, err := a.cloudClient(ctx, "cloud-token")
if err != nil {
return err
Expand All @@ -870,19 +866,10 @@ func (a *Actions) MonitorSchema(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to get the schema snapshot hash: %w", err)
}
res, err := a.Atlas.SchemaInspect(ctx, params)
if err != nil {
return fmt.Errorf("failed to inspect the schema: %w", err)
input := &cloud.PushSnapshotInput{
ScopeIdent: id,
HashMatch: strings.HasPrefix(h, "h1:") && OldAgentHash(hcl) == h || hash == h,
}
var (
parts = strings.SplitN(res, "\n", 2)
hash = strings.TrimPrefix(parts[0], "# ")
hcl = parts[1]
input = &cloud.PushSnapshotInput{
ScopeIdent: id,
HashMatch: strings.HasPrefix(h, "h1:") && OldAgentHash(hcl) == h || hash == h,
}
)
if !input.HashMatch {
input.Snapshot = &cloud.SnapshotInput{Hash: hash, HCL: hcl}
}
Expand Down
52 changes: 9 additions & 43 deletions atlasaction/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"log/slog"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -558,7 +557,6 @@ func TestMonitorSchema(t *testing.T) {
name, url, slug, config string
schemas, exclude []string
latestHash, newHash, hcl string
exScopeIdent cloud.ScopeIdent
exSnapshot *cloud.SnapshotInput
exMatch bool
wantErr bool
Expand All @@ -574,11 +572,6 @@ func TestMonitorSchema(t *testing.T) {
Hash: "hash",
HCL: "hcl",
},
exScopeIdent: cloud.ScopeIdent{
URL: must(url.Parse(u)).Redacted(),
Schemas: []string{},
Exclude: []string{},
},
},
{
name: "latest hash no match",
Expand All @@ -592,11 +585,6 @@ func TestMonitorSchema(t *testing.T) {
Hash: "hash",
HCL: "hcl",
},
exScopeIdent: cloud.ScopeIdent{
URL: must(url.Parse(u)).Redacted(),
Schemas: []string{},
Exclude: []string{},
},
},
{
name: "hash match old hash func",
Expand All @@ -607,11 +595,6 @@ func TestMonitorSchema(t *testing.T) {
schemas: []string{},
exclude: []string{},
exMatch: true,
exScopeIdent: cloud.ScopeIdent{
URL: must(url.Parse(u)).Redacted(),
Schemas: []string{},
Exclude: []string{},
},
},
{
name: "hash match new hash func",
Expand All @@ -622,11 +605,6 @@ func TestMonitorSchema(t *testing.T) {
schemas: []string{},
exclude: []string{},
exMatch: true,
exScopeIdent: cloud.ScopeIdent{
URL: must(url.Parse(u)).Redacted(),
Schemas: []string{},
Exclude: []string{},
},
},
{
name: "with slug",
Expand All @@ -638,12 +616,6 @@ func TestMonitorSchema(t *testing.T) {
schemas: []string{},
exclude: []string{},
exMatch: true,
exScopeIdent: cloud.ScopeIdent{
URL: must(url.Parse(u)).Redacted(),
ExtID: "slug",
Schemas: []string{},
Exclude: []string{},
},
},
{
name: "with schema and exclude",
Expand All @@ -655,12 +627,6 @@ func TestMonitorSchema(t *testing.T) {
schemas: []string{"foo", "bar"},
exclude: []string{"foo.*", "bar.*.*"},
exMatch: true,
exScopeIdent: cloud.ScopeIdent{
URL: must(url.Parse(u)).Redacted(),
ExtID: "slug",
Schemas: []string{"foo", "bar"},
Exclude: []string{"foo.*", "bar.*.*"},
},
},
{
name: "url and config should rerurn error",
Expand All @@ -677,11 +643,6 @@ func TestMonitorSchema(t *testing.T) {
schemas: []string{},
exclude: []string{},
exMatch: true,
exScopeIdent: cloud.ScopeIdent{
URL: "# hash\nhcl",
Schemas: []string{},
Exclude: []string{},
},
},
} {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -703,7 +664,7 @@ func TestMonitorSchema(t *testing.T) {
return nil
},
schemaInspect: func(_ context.Context, p *atlasexec.SchemaInspectParams) (string, error) {
return fmt.Sprintf("# %s\n%s", tt.newHash, tt.hcl), nil
return fmt.Sprintf("# %s\n%s\n%s", tt.url, tt.newHash, tt.hcl), nil
},
}
cc = &mockCloudClient{hash: tt.latestHash}
Expand All @@ -723,9 +684,14 @@ func TestMonitorSchema(t *testing.T) {
}
require.NoError(t, err)
require.Equal(t, &cloud.PushSnapshotInput{
ScopeIdent: tt.exScopeIdent,
Snapshot: tt.exSnapshot,
HashMatch: tt.exMatch,
ScopeIdent: cloud.ScopeIdent{
URL: tt.url,
ExtID: tt.slug,
Schemas: tt.schemas,
Exclude: tt.exclude,
},
Snapshot: tt.exSnapshot,
HashMatch: tt.exMatch,
}, cc.lastInput)
require.Equal(t, map[string]string{"url": "url"}, act.output)
})
Expand Down

0 comments on commit 6a36d0f

Please sign in to comment.