Skip to content

Commit

Permalink
provider: allow using cloud.token to login (#156)
Browse files Browse the repository at this point in the history
* internal/provider: allow using cloud.token to login

* test: move cloud block into resource and data intead of provider

* internal/provider: let's check atlas version first, ensure the binary works
  • Loading branch information
luantranminh authored Aug 28, 2024
1 parent 0f9da61 commit 9c66fe8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,4 @@ jobs:
terraform init > /dev/null
terraform apply --auto-approve
env:
ATLAS_TOKEN: ${{ secrets.ATLAS_TOKEN }}
TF_VAR_atlas_token : ${{ secrets.ATLAS_TOKEN }}
11 changes: 11 additions & 0 deletions integration-tests/login-feature/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ terraform {
}
}

variable "atlas_token" {
type = string
sensitive = true
}

provider "atlas" {
cloud {
token = var.atlas_token
}
}

variable "schema" {
type = string
default = "schema-1.hcl"
Expand Down
79 changes: 41 additions & 38 deletions internal/provider/atlas_migration_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,21 +549,24 @@ func TestAccMigrationResource_AtlasURL(t *testing.T) {
}
}))
config = fmt.Sprintf(`
provider "atlas" {
data "atlas_migration" "hello" {
url = "%[2]s"
dir = "atlas://test"
cloud {
token = "aci_bearer_token"
url = "%[1]s"
project = "test"
}
}
data "atlas_migration" "hello" {
url = "%[2]s"
dir = "atlas://test"
}
resource "atlas_migration" "testdb" {
url = "%[2]s"
version = data.atlas_migration.hello.next
dir = data.atlas_migration.hello.dir
cloud {
token = "aci_bearer_token"
url = "%[1]s"
project = "test"
}
}
`, srv.URL, dbURL)
)
Expand Down Expand Up @@ -678,16 +681,16 @@ func TestAccMigrationResource_AtlasURL_WithTag(t *testing.T) {
config := fmt.Sprintf(`
provider "atlas" {
dev_url = "%[1]s"
}
resource "atlas_migration" "hello" {
url = "%[3]s"
dir = "atlas://test"
cloud {
token = "aci_bearer_token"
url = "%[2]s"
project = "test"
}
}
resource "atlas_migration" "hello" {
url = "%[3]s"
dir = "atlas://test"
}
`, devURL, srv.URL, dbURL)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -709,11 +712,6 @@ func TestAccMigrationResource_AtlasURL_WithTag(t *testing.T) {
config = fmt.Sprintf(`
provider "atlas" {
dev_url = "%[1]s"
cloud {
token = "aci_bearer_token"
url = "%[2]s"
project = "test"
}
}
resource "atlas_migration" "hello" {
url = "%[3]s"
Expand All @@ -723,6 +721,11 @@ func TestAccMigrationResource_AtlasURL_WithTag(t *testing.T) {
allow = true
}
}
cloud {
token = "aci_bearer_token"
url = "%[2]s"
project = "test"
}
}
`, devURL, srv.URL, dbURL)
resource.Test(t, resource.TestCase{
Expand All @@ -745,15 +748,15 @@ func TestAccMigrationResource_AtlasURL_WithTag(t *testing.T) {
config = fmt.Sprintf(`
provider "atlas" {
dev_url = "%[1]s"
}
resource "atlas_migration" "hello" {
url = "%[3]s"
dir = "atlas://test?tag=latest"
cloud {
token = "aci_bearer_token"
url = "%[2]s"
project = "test"
}
}
resource "atlas_migration" "hello" {
url = "%[3]s"
dir = "atlas://test?tag=latest"
}`, devURL, srv.URL, dbURL)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -889,15 +892,15 @@ func TestAccMigrationResource_RequireApproval(t *testing.T) {
Config: fmt.Sprintf(`
provider "atlas" {
dev_url = "%[1]s"
}
resource "atlas_migration" "hello" {
url = "%[3]s"
dir = "atlas://test?tag=latest"
cloud {
token = "aci_bearer_token"
url = "%[2]s"
project = "test"
}
}
resource "atlas_migration" "hello" {
url = "%[3]s"
dir = "atlas://test?tag=latest"
}`, devURL, srv.URL, dbURL),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("atlas_migration.hello", "id", "remote_dir://test"),
Expand All @@ -917,15 +920,15 @@ func TestAccMigrationResource_RequireApproval(t *testing.T) {
Config: fmt.Sprintf(`
provider "atlas" {
dev_url = "%[1]s"
}
resource "atlas_migration" "hello" {
url = "%[3]s"
dir = "atlas://test?tag=tag3"
cloud {
token = "aci_bearer_token"
url = "%[2]s"
project = "test"
}
}
resource "atlas_migration" "hello" {
url = "%[3]s"
dir = "atlas://test?tag=tag3"
}`, devURL, srv.URL, dbURL),
ExpectError: regexp.MustCompile("migrate down is not allowed, set `migrate_down.allow` to true to allow"),
},
Expand All @@ -939,15 +942,15 @@ func TestAccMigrationResource_RequireApproval(t *testing.T) {
Config: fmt.Sprintf(`
provider "atlas" {
dev_url = "%[1]s"
}
resource "atlas_migration" "hello" {
url = "%[3]s"
dir = "atlas://test?tag=tag3"
cloud {
token = "aci_bearer_token"
url = "%[2]s"
project = "test"
}
}
resource "atlas_migration" "hello" {
url = "%[3]s"
dir = "atlas://test?tag=tag3"
protected_flows {
migrate_down {
allow = true
Expand All @@ -970,15 +973,15 @@ func TestAccMigrationResource_RequireApproval(t *testing.T) {
Config: fmt.Sprintf(`
provider "atlas" {
dev_url = "%[1]s"
}
resource "atlas_migration" "hello" {
url = "%[3]s"
dir = "atlas://test?tag=tag3"
cloud {
token = "aci_bearer_token"
url = "%[2]s"
project = "test"
}
}
resource "atlas_migration" "hello" {
url = "%[3]s"
dir = "atlas://test?tag=tag3"
protected_flows {
migrate_down {
allow = true
Expand All @@ -1004,15 +1007,15 @@ func TestAccMigrationResource_RequireApproval(t *testing.T) {
Config: fmt.Sprintf(`
provider "atlas" {
dev_url = "%[1]s"
}
resource "atlas_migration" "hello" {
url = "%[3]s"
dir = "atlas://test?tag=tag2"
cloud {
token = "aci_bearer_token"
url = "%[2]s"
project = "test"
}
}
resource "atlas_migration" "hello" {
url = "%[3]s"
dir = "atlas://test?tag=tag2"
protected_flows {
migrate_down {
allow = true
Expand Down
10 changes: 10 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type (
SchemaApply(context.Context, *atlas.SchemaApplyParams) (*atlas.SchemaApply, error)

Version(context.Context) (*atlas.Version, error)

Login(context.Context, *atlas.LoginParams) error
}
providerData struct {
// client is the factory function to create a new AtlasExec client.
Expand Down Expand Up @@ -176,6 +178,14 @@ func (p *AtlasProvider) Configure(ctx context.Context, req provider.ConfigureReq
tflog.Debug(ctx, "found atlas-cli", map[string]any{
"version": version,
})
if model != nil && model.Cloud != nil && model.Cloud.Token.ValueString() != "" {
if err := c.Login(ctx, &atlas.LoginParams{
Token: model.Cloud.Token.ValueString(),
}); err != nil {
resp.Diagnostics.AddError("Login failure", err.Error())
return
}
}
p.data = providerData{client: fnClient, cloud: model.Cloud, version: p.version}
if model != nil {
p.data.devURL = model.DevURL.ValueString()
Expand Down

0 comments on commit 9c66fe8

Please sign in to comment.