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/migration: support custom env_name on datasource #159

Merged
merged 2 commits into from
Sep 15, 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
1 change: 1 addition & 0 deletions docs/data-sources/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ data "atlas_migration" "hello" {
- `cloud` (Block, Optional) (see [below for nested schema](#nestedblock--cloud))
- `config` (String) The configuration file for the migration
- `dir` (String) Select migration directory using URL format
- `env_name` (String) The name of the environment used for reporting runs to Atlas Cloud. Default: tf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `env_name` (String) The name of the environment used for reporting runs to Atlas Cloud. Default: tf
- `env_name` (String, Optional) The name of the environment used for reporting runs to Atlas Cloud. Default: tf

It’s optional, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's listing under the optional attributes list, ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-09-15 at 18 09 13

- `remote_dir` (Block, Optional, Deprecated) (see [below for nested schema](#nestedblock--remote_dir))
- `revisions_schema` (String) The name of the schema the revisions table resides in
- `url` (String, Sensitive) [driver://username:password@address/dbname?param=value] select a resource using the URL format
Expand Down
7 changes: 6 additions & 1 deletion internal/provider/atlas_migration_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type (
DirURL types.String `tfsdk:"dir"`
Cloud *AtlasCloudBlock `tfsdk:"cloud"`
RemoteDir *RemoteDirBlock `tfsdk:"remote_dir"`
EnvName types.String `tfsdk:"env_name"`

Status types.String `tfsdk:"status"`
Current types.String `tfsdk:"current"`
Expand Down Expand Up @@ -108,6 +109,10 @@ func (d *MigrationDataSource) Schema(_ context.Context, _ datasource.SchemaReque
Description: "Select migration directory using URL format",
Optional: true,
},
"env_name": schema.StringAttribute{
Description: "The name of the environment used for reporting runs to Atlas Cloud. Default: tf",
Optional: true,
},
"revisions_schema": schema.StringAttribute{
Description: "The name of the schema the revisions table resides in",
Optional: true,
Expand Down Expand Up @@ -225,7 +230,7 @@ func (d *MigrationDataSourceModel) projectConfig(cloud *AtlasCloudBlock) (*proje
}
cfg := projectConfig{
Config: defaultString(d.Config, baseAtlasHCL),
EnvName: "tf",
EnvName: defaultString(d.EnvName, "tf"),
Env: &envConfig{
URL: dbURL,
Migration: &migrationConfig{
Expand Down
1 change: 1 addition & 0 deletions internal/provider/atlas_migration_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ locals {
db_url = getenv("DB_URL")
}
env {
name = atlas.env
url = urlsetpath(local.db_url, var.schema_name)
migration {
dir = "this-dir-does-not-exist-and-always-gets-overrides"
Expand Down
1 change: 1 addition & 0 deletions internal/provider/atlas_migration_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ variable "schemas" {
type = list(string)
}
env {
name = atlas.env
for_each = toset(var.schemas)
url = urlsetpath(var.url, each.value)
migration {
Expand Down
6 changes: 1 addition & 5 deletions internal/provider/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type (
)

// we will allow the user configure the base atlas.hcl file
const baseAtlasHCL = "env {\n}"
const baseAtlasHCL = "env {\n name = atlas.env\n}"

// Render writes the atlas config to the given writer.
func (c *projectConfig) Render(w io.Writer) error {
Expand Down Expand Up @@ -83,10 +83,6 @@ func (c *projectConfig) File() *hclwrite.File {
}
if env := c.Env; env != nil {
e := r.AppendNewBlock("env", nil).Body()
e.SetAttributeTraversal("name", hcl.Traversal{
hcl.TraverseRoot{Name: "atlas"},
hcl.TraverseAttr{Name: "env"},
})
if env.URL != "" {
e.SetAttributeValue("url", cty.StringVal(env.URL))
}
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func Test_SchemaTemplate(t *testing.T) {
out := &bytes.Buffer{}
require.NoError(t, data.Render(out))
require.Equal(t, `env {
dev = "mysql://user:pass@localhost:3307/tf-db"
name = atlas.env
dev = "mysql://user:pass@localhost:3307/tf-db"
src = "file://schema.hcl"
url = "mysql://user:pass@localhost:3306/tf-db"
diff {
Expand Down
Loading