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

Added plugin for dbt CLI #328

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
54 changes: 54 additions & 0 deletions plugins/dbt/database_credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package dbt

import (
"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/importer"
"github.com/1Password/shell-plugins/sdk/provision"
"github.com/1Password/shell-plugins/sdk/schema"
"github.com/1Password/shell-plugins/sdk/schema/credname"
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
)

func DatabaseCredentials() schema.CredentialType {
return schema.CredentialType{
Name: credname.DatabaseCredentials,
DocsURL: sdk.URL("https://docs.getdbt.com/docs/core/connect-data-platform/redshift-setup#password-based-authentication"),
Fields: []schema.CredentialField{
{
Name: fieldname.Host,
MarkdownDescription: "Host to connect to.",
Optional: true,
},
{
Name: fieldname.Port,
MarkdownDescription: "Port used to connect to.",
Optional: true,
},
{
Name: fieldname.User,
MarkdownDescription: "User to authenticate as.",
Optional: true,
},
{
Name: fieldname.Password,
MarkdownDescription: "Password used to authenticate to.",
Secret: true,
},
{
Name: fieldname.Database,
MarkdownDescription: "Database name to connect to.",
Optional: true,
},
},
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryEnvVarPair(defaultEnvVarMapping),
}
}

var defaultEnvVarMapping = map[string]sdk.FieldName{
"DBT_HOST": fieldname.Host,
"DBT_PORT": fieldname.Port,
"DBT_USER": fieldname.User,
"DBT_PASSWORD": fieldname.Password,
"DBT_DB": fieldname.Database,
}
Comment on lines +48 to +54
Copy link
Member

Choose a reason for hiding this comment

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

From what I gather from the documentation, these environment variables are only used to be referenced in a configuration file, is that correct?

With these provisioned, a config file such as the one documented here: https://docs.getdbt.com/reference/dbt-jinja-functions/profiles-yml-context

is still necessary for the plugin to work. Is there some documentation that I have missed?

Copy link
Member

@hculea hculea Jun 30, 2023

Choose a reason for hiding this comment

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

I see that this is indeed what you are documenting in the PR description as well.

Rather than creating an additional requirement for the user to write their own config file, I would prefer if we were to provision a configuration file directly, using provision.TempFile, as in plugins such as mysql or akamai.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the suggestion. Users would still need to configure profiles.yml file as lot of other attributes can be tweaked in it. I will add changes to incorporate provision.TempFile and keep env variables as it is.

Copy link
Member

Choose a reason for hiding this comment

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

I see, thank you for the clarification.

@libutcher do you know of any other plugins that have a similar behaviour? Asking you since you tested/documented their vast majority, I wonder if there is already any occurrence of a plugin that requires additional setup on top of the 1Password CLI-related stuff.

Copy link
Contributor

Choose a reason for hiding this comment

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

Following up here, it doesn't look like there are any plugins with that behaviour. @guru-pochineni, are you still planning to add changes to incorporate provision.TempFile?

Copy link
Author

Choose a reason for hiding this comment

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

Haven't got the chance to make changes. Let me take a look into this next weekend.

57 changes: 57 additions & 0 deletions plugins/dbt/database_credentials_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package dbt

import (
"testing"

"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/plugintest"
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
)

func TestDatabaseCredentialsProvisioner(t *testing.T) {
plugintest.TestProvisioner(t, DatabaseCredentials().DefaultProvisioner, map[string]plugintest.ProvisionCase{
"default": {
ItemFields: map[sdk.FieldName]string{
fieldname.Host: "examplecluster.abc123xyz789.us-west-1.redshift.amazonaws.com",
fieldname.Port: "5439",
fieldname.User: "awsuser",
fieldname.Password: "my_password",
fieldname.Database: "dev",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"DBT_HOST": "examplecluster.abc123xyz789.us-west-1.redshift.amazonaws.com",
"DBT_PORT": "5439",
"DBT_USER": "awsuser",
"DBT_PASSWORD": "my_password",
"DBT_DB": "dev",
},
},
},
})
}

func TestDatabaseCredentialsImporter(t *testing.T) {
plugintest.TestImporter(t, DatabaseCredentials().Importer, map[string]plugintest.ImportCase{
"default": {
Environment: map[string]string{
"DBT_HOST": "examplecluster.abc123xyz789.us-west-1.redshift.amazonaws.com",
"DBT_PORT": "5439",
"DBT_USER": "awsuser",
"DBT_PASSWORD": "my_password",
"DBT_DB": "dev",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Host: "examplecluster.abc123xyz789.us-west-1.redshift.amazonaws.com",
fieldname.Port: "5439",
fieldname.User: "awsuser",
fieldname.Password: "my_password",
fieldname.Database: "dev",
},
},
},
},
})
}
25 changes: 25 additions & 0 deletions plugins/dbt/dbt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dbt

import (
"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/needsauth"
"github.com/1Password/shell-plugins/sdk/schema"
"github.com/1Password/shell-plugins/sdk/schema/credname"
)

func dbtCLI() schema.Executable {
return schema.Executable{
Name: "dbt",
Runs: []string{"dbt"},
DocsURL: sdk.URL("https://docs.getdbt.com/docs/core/connect-data-platform/about-core-connections"),
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
),
Uses: []schema.CredentialUsage{
{
Name: credname.DatabaseCredentials,
},
},
}
}
22 changes: 22 additions & 0 deletions plugins/dbt/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dbt

import (
"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/schema"
)

func New() schema.Plugin {
return schema.Plugin{
Name: "dbt",
Platform: schema.PlatformInfo{
Name: "DBT",
Homepage: sdk.URL("https://www.getdbt.com"),
},
Credentials: []schema.CredentialType{
DatabaseCredentials(),
},
Executables: []schema.Executable{
dbtCLI(),
},
}
}