From 0694cf21879990d63940511dab87295a0b6ca52e Mon Sep 17 00:00:00 2001 From: "Giau. Tran Minh" <12751435+giautm@users.noreply.github.com> Date: Tue, 28 Nov 2023 20:30:12 +0700 Subject: [PATCH] schema: fixed absolute path for SQLite (#107) --- .github/workflows/ci.yml | 91 +++++++++++++--------- integration-tests/.gitignore | 3 +- internal/provider/atlas_schema_resource.go | 22 +++++- internal/provider/provider.go | 20 +++++ 4 files changed, 95 insertions(+), 41 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 07d8eec..a70bd4c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -66,9 +66,6 @@ jobs: fail-fast: false matrix: terraform-version: - - '1.2.*' - - '1.3.*' - - '1.4.*' - '1.5.*' - '1.6.*' steps: @@ -122,41 +119,13 @@ jobs: retention-days: 5 if-no-files-found: error integration: - name: Integration (Terraform ${{ matrix.terraform-version }}) + name: Integration SQLite (Terraform ${{ matrix.terraform-version }}) runs-on: ubuntu-latest needs: [build-dev] - services: - mysql8prod: - image: mysql:8 - env: - MYSQL_ROOT_PASSWORD: pass - ports: - - 3306:3306 - options: >- - --health-cmd "mysqladmin ping -ppass" - --health-interval 10s - --health-start-period 10s - --health-timeout 5s - --health-retries 10 - mysql8dev: - image: mysql:8 - env: - MYSQL_ROOT_PASSWORD: pass - ports: - - 3307:3306 - options: >- - --health-cmd "mysqladmin ping -ppass" - --health-interval 10s - --health-start-period 10s - --health-timeout 5s - --health-retries 10 strategy: fail-fast: false matrix: terraform-version: - - '1.2.*' - - '1.3.*' - - '1.4.*' - '1.5.*' - '1.6.*' steps: @@ -177,7 +146,12 @@ jobs: run: | ../../scripts/local.sh ../../dist 0.0.0-pre.0 terraform init - terraform apply --auto-approve + echo "Apply terraform plan" + terraform apply -no-color --auto-approve > stdout.txt + cat stdout.txt | grep --silent "Apply complete! Resources: 1 added, 0 changed, 0 destroyed." + echo "Ensure that there is no diff" + terraform plan -no-color > stdout.txt + cat stdout.txt | grep --silent "No changes. Your infrastructure matches the configuration." - name: Terraform (no-dev-url) working-directory: integration-tests/no-dev-url run: | @@ -201,6 +175,54 @@ jobs: ! cat stdout.txt | grep --silent "Warning: version is unset" env: TF_VAR_atlas_token: ${{ secrets.ATLAS_TOKEN }} + integration-mysql: + name: Integration MySQL (Terraform ${{ matrix.terraform-version }}) + runs-on: ubuntu-latest + needs: [build-dev] + services: + mysql8prod: + image: mysql:8 + env: + MYSQL_ROOT_PASSWORD: pass + ports: + - 3306:3306 + options: >- + --health-cmd "mysqladmin ping -ppass" + --health-interval 10s + --health-start-period 10s + --health-timeout 5s + --health-retries 10 + mysql8dev: + image: mysql:8 + env: + MYSQL_ROOT_PASSWORD: pass + ports: + - 3307:3306 + options: >- + --health-cmd "mysqladmin ping -ppass" + --health-interval 10s + --health-start-period 10s + --health-timeout 5s + --health-retries 10 + strategy: + fail-fast: false + matrix: + terraform-version: + - '1.5.*' + - '1.6.*' + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v3 + with: + go-version: '1.21' + - uses: actions/download-artifact@v3 + with: + name: atlas-provider + path: ./dist + - uses: hashicorp/setup-terraform@v2 + with: + terraform_version: ${{ matrix.terraform-version }} + terraform_wrapper: false - name: Terraform (skip-policy) working-directory: integration-tests/skip-policy run: | @@ -254,9 +276,6 @@ jobs: fail-fast: false matrix: terraform-version: - - '1.2.*' - - '1.3.*' - - '1.4.*' - '1.5.*' - '1.6.*' steps: diff --git a/integration-tests/.gitignore b/integration-tests/.gitignore index 216cfef..16c9b8c 100644 --- a/integration-tests/.gitignore +++ b/integration-tests/.gitignore @@ -1,6 +1,7 @@ **/.terraform.lock.hcl **/.terraform/* **/terraform.d +**/terraform.tfstate* **/*.db -**/stdout.txt **/actual.hcl +**/stdout.txt diff --git a/internal/provider/atlas_schema_resource.go b/internal/provider/atlas_schema_resource.go index 116f446..e9463d5 100644 --- a/internal/provider/atlas_schema_resource.go +++ b/internal/provider/atlas_schema_resource.go @@ -310,10 +310,17 @@ func (r *AtlasSchemaResource) ModifyPlan(ctx context.Context, req resource.Modif } func PrintPlanSQL(ctx context.Context, c *atlas.Client, devURL string, data *AtlasSchemaResourceModel) (diags diag.Diagnostics) { + u, err := absPath(data.URL.ValueString()) + if err != nil { + diags.AddError("URL Error", + fmt.Sprintf("Unable to get absolute path for URL, got error: %s", err), + ) + return + } d := &schemaData{ - Source: "schema.hcl", - URL: data.URL.ValueString(), + URL: u, DevURL: devURL, + Source: "schema.hcl", Diff: data.Diff, } diags.Append(data.GetExclude(ctx, &d.Exclude)...) @@ -369,10 +376,17 @@ func PrintPlanSQL(ctx context.Context, c *atlas.Client, devURL string, data *Atl } func (r *AtlasSchemaResource) applySchema(ctx context.Context, data *AtlasSchemaResourceModel) (diags diag.Diagnostics) { + u, err := absPath(data.URL.ValueString()) + if err != nil { + diags.AddError("URL Error", + fmt.Sprintf("Unable to get absolute path for URL, got error: %s", err), + ) + return + } d := &schemaData{ - Source: "schema.hcl", - URL: data.URL.ValueString(), + URL: u, DevURL: r.getDevURL(data.DevURL, data.DeprecatedDevURL), + Source: "schema.hcl", Diff: data.Diff, } diags.Append(data.GetExclude(ctx, &d.Exclude)...) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 1c26829..0e56866 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -4,11 +4,13 @@ import ( "bytes" "context" "fmt" + "net/url" "os" "os/exec" "path" "path/filepath" "runtime" + "strings" "github.com/mitchellh/go-homedir" @@ -292,3 +294,21 @@ func execPath(dir, name string) (file string, err error) { // try to find it in the PATH. return exec.LookPath(name) } + +// absPath returns the absolute path of a file URL. +func absPath(path string) (string, error) { + u, err := url.Parse(path) + if err != nil { + return "", err + } + switch s := u.Scheme; strings.ToLower(s) { + case "file", "sqlite": + scheme := fmt.Sprintf("%s://", s) + p, err := filepath.Abs(strings.TrimPrefix(path, scheme)) + if err != nil { + return "", err + } + return scheme + p, nil + } + return path, nil +}