From a5175b4b17d8487a1181dae042269617ac2128cd Mon Sep 17 00:00:00 2001 From: Ronen Lubin Date: Thu, 9 Jan 2025 16:56:22 +0200 Subject: [PATCH] cr --- atlasaction/action.go | 46 ++++++++++++++-------------------- atlasaction/action_test.go | 51 +++++++------------------------------- 2 files changed, 28 insertions(+), 69 deletions(-) diff --git a/atlasaction/action.go b/atlasaction/action.go index e04a3fb..0877d91 100644 --- a/atlasaction/action.go +++ b/atlasaction/action.go @@ -825,7 +825,7 @@ 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{ @@ -840,28 +840,26 @@ 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{ + URL: db.String(), Schema: id.Schemas, Exclude: id.Schemas, - Format: `{{ printf "# %s\n%s" .Hash .MarshalHCL }}`, - } - if db.String() != "" { - params.URL = db.String() + Format: `{{ printf "# %s\n%s\n%s" .RedactedURL .Hash .MarshalHCL }}`, } 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] + ) cc, err := a.cloudClient(ctx, "cloud-token") if err != nil { return err @@ -870,19 +868,13 @@ 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) + if id.URL == "" { + id.URL = url + } + 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} } diff --git a/atlasaction/action_test.go b/atlasaction/action_test.go index 0e37d47..345ebc2 100644 --- a/atlasaction/action_test.go +++ b/atlasaction/action_test.go @@ -558,7 +558,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 @@ -574,11 +573,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", @@ -592,11 +586,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", @@ -607,11 +596,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", @@ -622,11 +606,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", @@ -638,12 +617,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", @@ -655,12 +628,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", @@ -677,11 +644,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) { @@ -703,7 +665,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.newHash, tt.hcl), nil }, } cc = &mockCloudClient{hash: tt.latestHash} @@ -723,9 +685,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: must(url.Parse(tt.url)).Redacted(), + 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) })