Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

atlas/schema: fixed missing ATLAS_TOKEN #166

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/provider/atlas_migration_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/atlas_migration_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
9 changes: 7 additions & 2 deletions internal/provider/atlas_schema_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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",
Expand All @@ -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 {
Expand Down
19 changes: 12 additions & 7 deletions internal/provider/atlas_schema_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/atlas_schema_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading