From f030038aa6cf880288b41cb0421a672d361479e2 Mon Sep 17 00:00:00 2001 From: "Giau. Tran Minh" Date: Wed, 18 Sep 2024 22:48:24 +0700 Subject: [PATCH] atlas/schema: fixed missing `ATLAS_TOKEN` --- .../provider/atlas_migration_data_source.go | 4 ++-- internal/provider/atlas_migration_resource.go | 4 ++-- internal/provider/atlas_schema_data_source.go | 9 +++++++-- internal/provider/atlas_schema_resource.go | 19 ++++++++++++------- .../provider/atlas_schema_resource_test.go | 2 +- internal/provider/provider.go | 7 ++++++- 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/internal/provider/atlas_migration_data_source.go b/internal/provider/atlas_migration_data_source.go index e6c18ec..30813e5 100644 --- a/internal/provider/atlas_migration_data_source.go +++ b/internal/provider/atlas_migration_data_source.go @@ -238,11 +238,11 @@ func (d *MigrationDataSourceModel) projectConfig(cloud *AtlasCloudBlock) (*proje }, }, } - if d.Cloud != nil && d.Cloud.Token.ValueString() != "" { + if d.Cloud.Valid() { // Use the data source cloud block if it is set cloud = d.Cloud } - if cloud != nil { + if cloud.Valid() { cfg.Cloud = &cloudConfig{ Token: cloud.Token.ValueString(), Project: cloud.Project.ValueStringPointer(), diff --git a/internal/provider/atlas_migration_resource.go b/internal/provider/atlas_migration_resource.go index 0aea7d0..8e318f8 100644 --- a/internal/provider/atlas_migration_resource.go +++ b/internal/provider/atlas_migration_resource.go @@ -827,11 +827,11 @@ func (d *MigrationResourceModel) projectConfig(cloud *AtlasCloudBlock, devURL st }, }, } - if d.Cloud != nil && d.Cloud.Token.ValueString() != "" { + if d.Cloud.Valid() { // Use the resource's cloud block if it is set cloud = d.Cloud } - if cloud != nil { + if cloud.Valid() { cfg.Cloud = &cloudConfig{ Token: cloud.Token.ValueString(), Project: cloud.Project.ValueStringPointer(), diff --git a/internal/provider/atlas_schema_data_source.go b/internal/provider/atlas_schema_data_source.go index 990d830..179be2b 100644 --- a/internal/provider/atlas_schema_data_source.go +++ b/internal/provider/atlas_schema_data_source.go @@ -120,7 +120,7 @@ func (d *AtlasSchemaDataSource) Read(ctx context.Context, req datasource.ReadReq vars[k] = v } } - cfg, wd, err := data.projectConfig(d.devURL) + cfg, wd, err := data.projectConfig(d.cloud, d.devURL) if err != nil { resp.Diagnostics.AddError("HCL Error", fmt.Sprintf("Unable to create working directory, got error: %s", err), @@ -156,7 +156,7 @@ func (d *AtlasSchemaDataSource) Read(ctx context.Context, req datasource.ReadReq resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } -func (d *AtlasSchemaDataSourceModel) projectConfig(devURL string) (*projectConfig, *atlas.WorkingDir, error) { +func (d *AtlasSchemaDataSourceModel) projectConfig(cloud *AtlasCloudBlock, devURL string) (*projectConfig, *atlas.WorkingDir, error) { cfg := &projectConfig{ Config: baseAtlasHCL, EnvName: "tf", @@ -165,6 +165,11 @@ func (d *AtlasSchemaDataSourceModel) projectConfig(devURL string) (*projectConfi DevURL: defaultString(d.DevURL, devURL), }, } + if cloud.Valid() { + cfg.Cloud = &cloudConfig{ + Token: cloud.Token.ValueString(), + } + } opts := []atlas.Option{atlas.WithAtlasHCL(cfg.Render)} u, err := url.Parse(filepath.ToSlash(d.Src.ValueString())) if err == nil && u.Scheme == SchemaTypeFile { diff --git a/internal/provider/atlas_schema_resource.go b/internal/provider/atlas_schema_resource.go index 7a4b312..281defc 100644 --- a/internal/provider/atlas_schema_resource.go +++ b/internal/provider/atlas_schema_resource.go @@ -283,11 +283,11 @@ func (r *AtlasSchemaResource) ModifyPlan(ctx context.Context, req resource.Modif return } } - resp.Diagnostics.Append(PrintPlanSQL(ctx, r.client, r.getDevURL(plan.DevURL), plan)...) + resp.Diagnostics.Append(PrintPlanSQL(ctx, r.cloud, r.client, r.getDevURL(plan.DevURL), plan)...) } -func PrintPlanSQL(ctx context.Context, fn func(string) (AtlasExec, error), devURL string, data *AtlasSchemaResourceModel) (diags diag.Diagnostics) { - cfg, wd, err := data.projectConfig(devURL) +func PrintPlanSQL(ctx context.Context, cloud *AtlasCloudBlock, fn func(string) (AtlasExec, error), devURL string, data *AtlasSchemaResourceModel) (diags diag.Diagnostics) { + cfg, wd, err := data.projectConfig(cloud, devURL) if err != nil { diags.AddError("HCL Error", fmt.Sprintf("Unable to create working directory, got error: %s", err), @@ -332,7 +332,7 @@ func PrintPlanSQL(ctx context.Context, fn func(string) (AtlasExec, error), devUR } func (r *AtlasSchemaResource) readSchema(ctx context.Context, data *AtlasSchemaResourceModel) (diags diag.Diagnostics) { - cfg, wd, err := data.projectConfig(r.devURL) + cfg, wd, err := data.projectConfig(r.cloud, r.devURL) if err != nil { diags.AddError("HCL Error", fmt.Sprintf("Unable to create working directory, got error: %s", err), @@ -368,7 +368,7 @@ func (r *AtlasSchemaResource) readSchema(ctx context.Context, data *AtlasSchemaR } func (r *AtlasSchemaResource) applySchema(ctx context.Context, data *AtlasSchemaResourceModel) (diags diag.Diagnostics) { - cfg, wd, err := data.projectConfig(r.devURL) + cfg, wd, err := data.projectConfig(r.cloud, r.devURL) if err != nil { diags.AddError("HCL Error", fmt.Sprintf("Unable to create working directory, got error: %s", err), @@ -404,7 +404,7 @@ func (r *AtlasSchemaResource) applySchema(ctx context.Context, data *AtlasSchema } func (r *AtlasSchemaResource) firstRunCheck(ctx context.Context, data *AtlasSchemaResourceModel) (diags diag.Diagnostics) { - cfg, wd, err := data.projectConfig(r.devURL) + cfg, wd, err := data.projectConfig(r.cloud, r.devURL) if err != nil { diags.AddError("HCL Error", fmt.Sprintf("Unable to create working directory, got error: %s", err), @@ -453,7 +453,7 @@ func (r *AtlasSchemaResource) firstRunCheck(ctx context.Context, data *AtlasSche return } -func (data *AtlasSchemaResourceModel) projectConfig(devdb string) (*projectConfig, *atlas.WorkingDir, error) { +func (data *AtlasSchemaResourceModel) projectConfig(cloud *AtlasCloudBlock, devdb string) (*projectConfig, *atlas.WorkingDir, error) { dbURL, err := absoluteSqliteURL(data.URL.ValueString()) if err != nil { return nil, nil, err @@ -468,6 +468,11 @@ func (data *AtlasSchemaResourceModel) projectConfig(devdb string) (*projectConfi Diff: data.Diff, }, } + if cloud.Valid() { + cfg.Cloud = &cloudConfig{ + Token: cloud.Token.ValueString(), + } + } diags := data.Exclude.ElementsAs(context.Background(), &cfg.Env.Exclude, false) if diags.HasError() { return nil, nil, errors.New(diags.Errors()[0].Summary()) diff --git a/internal/provider/atlas_schema_resource_test.go b/internal/provider/atlas_schema_resource_test.go index c71c0aa..b41c9ce 100644 --- a/internal/provider/atlas_schema_resource_test.go +++ b/internal/provider/atlas_schema_resource_test.go @@ -650,7 +650,7 @@ table "orders" { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gotDiags := provider.PrintPlanSQL(tt.args.ctx, func(wd string) (provider.AtlasExec, error) { + gotDiags := provider.PrintPlanSQL(tt.args.ctx, nil, func(wd string) (provider.AtlasExec, error) { return atlas.NewClient(wd, "atlas") }, mysqlDevURL, tt.args.data) require.Equal(t, tt.wantDiags, gotDiags) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 5010826..8f1d9b9 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -275,8 +275,13 @@ func (d *providerData) validateConfig(ctx context.Context, cfg tfsdk.Config) (di return diags } +// Valid returns true if the cloud block is valid. +func (c *AtlasCloudBlock) Valid() bool { + return c != nil && c.Token.ValueString() != "" +} + // checkForUpdate checks for version updates and security advisories for Atlas. -func checkForUpdate(ctx context.Context, version string) (string, error) { +func checkForUpdate(_ context.Context, version string) (string, error) { // Users may skip update checking behavior. if v := os.Getenv(envNoUpdate); v != "" { return "", nil