Skip to content

Commit

Permalink
provider: merge env block only with its name (#164)
Browse files Browse the repository at this point in the history
- If the `config` attribute is provided, the `env_name` also required.
- We only merge the env block that generated by the resource with the
matched env block.
- The `atlas { cloud { ... } }` block no longer generated, we supply the
`cloud.token` via `ATLAS_TOKEN` env.
- `cloud.url` and `cloud.project` has no effect, and will be remove
  • Loading branch information
giautm authored Sep 18, 2024
1 parent e3ffadf commit af7a4b7
Show file tree
Hide file tree
Showing 16 changed files with 385 additions and 316 deletions.
37 changes: 24 additions & 13 deletions internal/provider/atlas_migration_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ type (

// Ensure provider defined types fully satisfy framework interfaces
var (
_ datasource.DataSource = &MigrationDataSource{}
_ datasource.DataSourceWithConfigure = &MigrationDataSource{}
_ datasource.DataSource = &MigrationDataSource{}
_ datasource.DataSourceWithConfigure = &MigrationDataSource{}
_ datasource.DataSourceWithValidateConfig = &MigrationDataSource{}
)
var (
latestVersion = "Already at latest version"
Expand Down Expand Up @@ -82,6 +83,21 @@ func (d *MigrationDataSource) Configure(ctx context.Context, req datasource.Conf
resp.Diagnostics.Append(d.configure(req.ProviderData)...)
}

// Validate implements resource.ResourceWithValidateConfig.
func (r MigrationDataSource) ValidateConfig(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) {
var data MigrationDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
if data.Config.ValueString() != "" && !data.EnvName.IsUnknown() && data.EnvName.ValueString() == "" {
resp.Diagnostics.AddError(
"env_name is empty",
"env_name is required when config is set",
)
}
}

// GetSchema implements datasource.DataSource.
func (d *MigrationDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Expand Down Expand Up @@ -167,7 +183,7 @@ func (d *MigrationDataSource) Read(ctx context.Context, req datasource.ReadReque
})
}
}()
c, err := d.client(wd.Path())
c, err := d.client(wd.Path(), cfg.Cloud)
if err != nil {
resp.Diagnostics.AddError("Failed to create client", err.Error())
return
Expand Down Expand Up @@ -228,8 +244,8 @@ func (d *MigrationDataSourceModel) projectConfig(cloud *AtlasCloudBlock) (*proje
if err != nil {
return nil, err
}
cfg := projectConfig{
Config: defaultString(d.Config, baseAtlasHCL),
cfg := &projectConfig{
Config: defaultString(d.Config, ""),
EnvName: defaultString(d.EnvName, "tf"),
Env: &envConfig{
URL: dbURL,
Expand All @@ -243,16 +259,11 @@ func (d *MigrationDataSourceModel) projectConfig(cloud *AtlasCloudBlock) (*proje
cloud = d.Cloud
}
if cloud.Valid() {
cfg.Cloud = &cloudConfig{
Token: cloud.Token.ValueString(),
Project: cloud.Project.ValueStringPointer(),
URL: cloud.URL.ValueStringPointer(),
cfg.Cloud = &CloudConfig{
Token: cloud.Token.ValueString(),
}
}
if rd := d.RemoteDir; rd != nil {
if cfg.Cloud == nil {
return nil, fmt.Errorf("cloud configuration is not set")
}
cfg.Env.Migration.DirURL, err = rd.AtlasURL()
} else {
cfg.Env.Migration.DirURL, err = absoluteFileURL(
Expand All @@ -266,7 +277,7 @@ func (d *MigrationDataSourceModel) projectConfig(cloud *AtlasCloudBlock) (*proje
return nil, fmt.Errorf("failed to parse variables: %w", err)
}
}
return &cfg, nil
return cfg, nil
}

// AtlasURL returns the atlas URL for the remote directory.
Expand Down
37 changes: 24 additions & 13 deletions internal/provider/atlas_migration_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ func TestAccMigrationDataSource(t *testing.T) {
data "atlas_migration" "hello" {
# The dir attribute is required to be set, and
# can't be supplied from the atlas.hcl
dir = "file://migrations?format=atlas"
config = <<-HCL
dir = "file://migrations?format=atlas"
env_name = "tf"
config = <<-HCL
variable "schema_name" {
type = string
}
Expand Down Expand Up @@ -122,12 +123,17 @@ func TestAccMigrationDataSource_AtlasURL(t *testing.T) {
}))
config = fmt.Sprintf(`
data "atlas_migration" "hello" {
url = "%s"
dir = "atlas://test"
cloud {
token = "aci_bearer_token"
url = "%s"
}
url = "%s"
dir = "atlas://test"
env_name = "tf"
config = <<-HCL
atlas {
cloud {
token = "aci_bearer_token"
url = "%s"
}
}
HCL
}`, dbURL, srv.URL)
)
t.Cleanup(srv.Close)
Expand All @@ -140,14 +146,19 @@ data "atlas_migration" "hello" {
{
Config: fmt.Sprintf(`
data "atlas_migration" "hello" {
url = "%s"
url = "%s"
env_name = "tf"
config = <<-HCL
atlas {
cloud {
token = "aci_bearer_token"
url = "%s"
}
}
HCL
remote_dir {
name = "test"
}
cloud {
token = "aci_bearer_token"
url = "%s"
}
}`, dbURL, srv.URL),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.atlas_migration.hello", "id", "remote_dir://test"),
Expand Down
34 changes: 14 additions & 20 deletions internal/provider/atlas_migration_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,6 @@ func (r MigrationResource) ValidateConfig(ctx context.Context, req resource.Vali
resp.Diagnostics.AddError("url is invalid", err.Error())
return
case u.Scheme == SchemaTypeAtlas:
// Remote dir, validate config for cloud
// providerData.client is set when the provider is configured
if data.Cloud == nil && (r.cloud == nil && r.providerData.client != nil) {
resp.Diagnostics.AddError(
"cloud is unset", "cloud is required when using atlas:// URL",
)
}
if f := data.ProtectedFlows; f != nil {
if d := f.MigrateDown; d != nil {
if d.Allow.ValueBool() && d.AutoApprove.ValueBool() {
Expand Down Expand Up @@ -385,6 +378,12 @@ func (r MigrationResource) ValidateConfig(ctx context.Context, req resource.Vali
"`atlas_migration.next` or `atlas_migration.latest`\n",
)
}
if data.Config.ValueString() != "" && !data.EnvName.IsUnknown() && data.EnvName.ValueString() == "" {
resp.Diagnostics.AddError(
"env_name is empty",
"env_name is required when config is set",
)
}
}

// ModifyPlan implements resource.ResourceWithModifyPlan.
Expand Down Expand Up @@ -416,7 +415,7 @@ func (r *MigrationResource) ModifyPlan(ctx context.Context, req resource.ModifyP
})
}
}()
c, err := r.client(wd.Path())
c, err := r.client(wd.Path(), cfg.Cloud)
if err != nil {
resp.Diagnostics.AddError("Failed to create client", err.Error())
return
Expand Down Expand Up @@ -513,7 +512,7 @@ func (r *MigrationResource) migrate(ctx context.Context, data *MigrationResource
fmt.Sprintf("Failed to create atlas.hcl: %s", err.Error()))
return
}
c, err := r.client(wd.Path())
c, err := r.client(wd.Path(), cfg.Cloud)
if err != nil {
diags.AddError("Failed to create client", err.Error())
return
Expand Down Expand Up @@ -629,7 +628,7 @@ func (r *MigrationResource) buildStatus(ctx context.Context, data *MigrationReso
})
}
}()
c, err := r.client(wd.Path())
c, err := r.client(wd.Path(), cfg.Cloud)
if err != nil {
diags.AddError("Failed to create client", err.Error())
return
Expand Down Expand Up @@ -814,8 +813,8 @@ func (d *MigrationResourceModel) projectConfig(cloud *AtlasCloudBlock, devURL st
if err != nil {
return nil, err
}
cfg := projectConfig{
Config: defaultString(d.Config, baseAtlasHCL),
cfg := &projectConfig{
Config: defaultString(d.Config, ""),
EnvName: defaultString(d.EnvName, "tf"),
Env: &envConfig{
URL: dbURL,
Expand All @@ -832,16 +831,11 @@ func (d *MigrationResourceModel) projectConfig(cloud *AtlasCloudBlock, devURL st
cloud = d.Cloud
}
if cloud.Valid() {
cfg.Cloud = &cloudConfig{
Token: cloud.Token.ValueString(),
Project: cloud.Project.ValueStringPointer(),
URL: cloud.URL.ValueStringPointer(),
cfg.Cloud = &CloudConfig{
Token: cloud.Token.ValueString(),
}
}
if rd := d.RemoteDir; rd != nil {
if cfg.Cloud == nil {
return nil, fmt.Errorf("cloud configuration is not set")
}
cfg.Env.Migration.DirURL, err = rd.AtlasURL()
} else {
cfg.Env.Migration.DirURL, err = absoluteFileURL(
Expand Down Expand Up @@ -869,5 +863,5 @@ func (d *MigrationResourceModel) projectConfig(cloud *AtlasCloudBlock, devURL st
return nil, fmt.Errorf("failed to parse variables: %w", err)
}
}
return &cfg, nil
return cfg, nil
}
Loading

0 comments on commit af7a4b7

Please sign in to comment.