Skip to content

Commit

Permalink
provider: add binary_path attribute (#116)
Browse files Browse the repository at this point in the history
* provider: add `binary_path` attribute

* chore: remove atlas-binary from bundle

* .github/workflows: fixed CI with new breaking changes

* .github/workflows: add health check for SQL Server
  • Loading branch information
giautm authored Jan 24, 2024
1 parent 4fb5cdc commit 3361940
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 54 deletions.
36 changes: 35 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ jobs:
with:
terraform_version: ${{ matrix.terraform-version }}
terraform_wrapper: false
- name: Install Atlas CLI
run: |
curl -sSf https://atlasgo.sh | sh
env:
ATLAS_DEBUG: "true"
ATLAS_FLAVOR: enterprise
- name: Terraform (sqlite)
working-directory: integration-tests/sqlite
run: |
Expand Down Expand Up @@ -224,6 +230,12 @@ jobs:
with:
terraform_version: ${{ matrix.terraform-version }}
terraform_wrapper: false
- name: Install Atlas CLI
run: |
curl -sSf https://atlasgo.sh | sh
env:
ATLAS_DEBUG: "true"
ATLAS_FLAVOR: enterprise
- name: Terraform (skip-policy)
working-directory: integration-tests/skip-policy
run: |
Expand All @@ -241,7 +253,7 @@ jobs:
! cat stdout.txt | grep --silent "DROP TABLE \`test\`.\`t2\`"
terraform apply --auto-approve -no-color -var="schema=schema-t2" -var="skip_drop_table=true"
# Confirm that the table was not dropped
../../scripts/atlas-cli.sh 0.0.0-pre.0 schema inspect \
atlas schema inspect \
-u "mysql://root:pass@localhost:3306/" > actual.hcl
cmp -s "expected.hcl" "actual.hcl"
integration-postgres:
Expand Down Expand Up @@ -292,6 +304,12 @@ jobs:
with:
terraform_version: ${{ matrix.terraform-version }}
terraform_wrapper: false
- name: Install Atlas CLI
run: |
curl -sSf https://atlasgo.sh | sh
env:
ATLAS_DEBUG: "true"
ATLAS_FLAVOR: enterprise
- name: Terraform (concurrent_index-policy)
working-directory: integration-tests/concurrent_index-policy
run: |
Expand All @@ -312,6 +330,11 @@ jobs:
MSSQL_SA_PASSWORD: P@ssw0rd0995
ports:
- 1433:1433
options: >-
--health-cmd "/opt/mssql-tools/bin/sqlcmd -U sa -P \"${MSSQL_SA_PASSWORD}\" -Q \"SELECT 1\""
--health-interval 10s
--health-timeout 5s
--health-retries 5
sqlserver-dev:
image: mcr.microsoft.com/mssql/server:2022-latest
env:
Expand All @@ -320,6 +343,11 @@ jobs:
MSSQL_SA_PASSWORD: P@ssw0rd0995
ports:
- 1434:1433
options: >-
--health-cmd "/opt/mssql-tools/bin/sqlcmd -U sa -P \"${MSSQL_SA_PASSWORD}\" -Q \"SELECT 1\""
--health-interval 10s
--health-timeout 5s
--health-retries 5
strategy:
fail-fast: false
matrix:
Expand All @@ -339,6 +367,12 @@ jobs:
with:
terraform_version: ${{ matrix.terraform-version }}
terraform_wrapper: false
- name: Install Atlas CLI
run: |
curl -sSf https://atlasgo.sh | sh
env:
ATLAS_DEBUG: "true"
ATLAS_FLAVOR: enterprise
- name: Terraform (login-feature)
working-directory: integration-tests/login-feature
run: |
Expand Down
3 changes: 0 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ builds:
- goos: windows
goarch: arm64
binary: '{{ .ProjectName }}_v{{ .Version }}'
hooks:
post:
- ./scripts/atlas.sh {{ .Os }}-{{ .Arch }} {{ dir .Path }}/atlas{{ .Ext }}
archives:
- format: zip
files:
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ resource "atlas_schema" "market" {

### Optional

- `binary_path` (String) The path to the atlas-cli binary. If not set, the provider will look for the binary in the PATH.
- `cloud` (Block, Optional) (see [below for nested schema](#nestedblock--cloud))
- `dev_url` (String, Sensitive) The URL of the dev database. This configuration is shared for all resources if there is no config on the resource.

Expand Down
50 changes: 14 additions & 36 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (
"fmt"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"

"github.com/mitchellh/go-homedir"
Expand All @@ -34,8 +31,6 @@ type (
AtlasProvider struct {
// client is the client used to interact with the Atlas CLI.
client *atlas.Client
// dir is the directory where the provider is installed.
dir string
// version is set to the provider version on release, "dev" when the
// provider is built and ran locally, and "test" when running acceptance
// testing.
Expand All @@ -44,6 +39,8 @@ type (
}
// AtlasProviderModel describes the provider data model.
AtlasProviderModel struct {
// BinaryPath is the path to the atlas-cli binary.
BinaryPath types.String `tfsdk:"binary_path"`
// DevURL is the URL of the dev-db.
DevURL types.String `tfsdk:"dev_url"`
// Cloud is the Atlas Cloud configuration.
Expand Down Expand Up @@ -97,15 +94,8 @@ const (

// New returns a new provider.
func New(address, version, commit string) func() provider.Provider {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
providersDir := path.Join(wd, ".terraform", "providers")
platform := fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH)
return func() provider.Provider {
return &AtlasProvider{
dir: path.Join(providersDir, address, version, platform),
version: version,
}
}
Expand All @@ -126,6 +116,10 @@ func (p *AtlasProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp
"cloud": cloudBlock,
},
Attributes: map[string]schema.Attribute{
"binary_path": schema.StringAttribute{
Description: "The path to the atlas-cli binary. If not set, the provider will look for the binary in the PATH.",
Optional: true,
},
"dev_url": schema.StringAttribute{
Description: "The URL of the dev database. This configuration is shared for all resources if there is no config on the resource.",
Optional: true,
Expand All @@ -137,12 +131,16 @@ func (p *AtlasProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp

// Configure implements provider.Provider.
func (p *AtlasProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
atlasPath, err := execPath(p.dir, "atlas")
if err != nil {
resp.Diagnostics.AddError("Unable to find atlas-cli", err.Error())
var model *AtlasProviderModel
resp.Diagnostics.Append(req.Config.Get(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}
c, err := atlas.NewClient("", atlasPath)
binPath := "atlas"
if s := model.BinaryPath.ValueString(); s != "" {
binPath = s
}
c, err := atlas.NewClient("", binPath)
if err != nil {
resp.Diagnostics.AddError("Failed to create client", err.Error())
return
Expand All @@ -157,16 +155,9 @@ func (p *AtlasProvider) Configure(ctx context.Context, req provider.ConfigureReq
version += "-canary"
}
tflog.Debug(ctx, "found atlas-cli", map[string]any{
"path": atlasPath,
"version": version,
})
p.client = c

var model *AtlasProviderModel
resp.Diagnostics.Append(req.Config.Get(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}
p.data = providerData{client: c, cloud: model.Cloud, version: p.version}
if model != nil {
p.data.devURL = model.DevURL.ValueString()
Expand Down Expand Up @@ -282,19 +273,6 @@ func checkForUpdate(ctx context.Context, version string) (string, error) {
return b.String(), nil
}

func execPath(dir, name string) (file string, err error) {
if runtime.GOOS == "windows" {
name += ".exe"
}
file = filepath.Join(dir, name)
if _, err = os.Stat(file); err == nil {
return file, nil
}
// If the binary is not in the current directory,
// 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)
Expand Down
11 changes: 0 additions & 11 deletions scripts/atlas-cli.sh

This file was deleted.

3 changes: 0 additions & 3 deletions scripts/atlas.sh

This file was deleted.

0 comments on commit 3361940

Please sign in to comment.