From 983a1ee8007a3b92b74361925b9ed62d42f4c4d4 Mon Sep 17 00:00:00 2001 From: parthiv11 Date: Sat, 24 Jun 2023 17:37:08 +0530 Subject: [PATCH 1/2] added yugabyte shell plugin --- plugins/yugabytedb/database_credentials.go | 54 ++++++++++++++++++ .../yugabytedb/database_credentials_test.go | 57 +++++++++++++++++++ plugins/yugabytedb/plugin.go | 22 +++++++ plugins/yugabytedb/ysqlsh.go | 25 ++++++++ 4 files changed, 158 insertions(+) create mode 100644 plugins/yugabytedb/database_credentials.go create mode 100644 plugins/yugabytedb/database_credentials_test.go create mode 100644 plugins/yugabytedb/plugin.go create mode 100644 plugins/yugabytedb/ysqlsh.go diff --git a/plugins/yugabytedb/database_credentials.go b/plugins/yugabytedb/database_credentials.go new file mode 100644 index 000000000..a941f7fa5 --- /dev/null +++ b/plugins/yugabytedb/database_credentials.go @@ -0,0 +1,54 @@ +package yugabytedb + +import ( + "context" + + "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.yugabyte.com/preview/admin/ysqlsh/#connect-to-a-database"), + ManagementURL: sdk.URL("https://cloud.yugabyte.com/"), + Fields: []schema.CredentialField{ + { + Name: fieldname.Host, + MarkdownDescription: "Yugabyte host to connect to.", + }, + { + Name: fieldname.Port, + MarkdownDescription: "Port used to connect to Yugabyte.", + Optional: true, + }, + { + Name: fieldname.User, + MarkdownDescription: "Yugabyte user to authenticate as.", + }, + { + Name: fieldname.Password, + MarkdownDescription: "Password used to authenticate to Yugabyte.", + 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{ + "PGHOST": fieldname.Host, + "PGPORT": fieldname.Port, + "PGUSER": fieldname.User, + "PGPASSWORD": fieldname.Password, + "PGDATABASE": fieldname.Database, +} diff --git a/plugins/yugabytedb/database_credentials_test.go b/plugins/yugabytedb/database_credentials_test.go new file mode 100644 index 000000000..eea2a8669 --- /dev/null +++ b/plugins/yugabytedb/database_credentials_test.go @@ -0,0 +1,57 @@ +package yugabytedb + +import ( + "testing" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/plugintest" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TestDatabaseCredentialsImporter(t *testing.T) { + plugintest.TestImporter(t, DatabaseCredentials().Importer, map[string]plugintest.ImportCase{ + "default": { + Environment: map[string]string{ + "PGHOST": "localhost", + "PGPORT": "5432", + "PGUSER": "root", + "PGPASSWORD": "123456", + "PGDATABASE": "test", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Host: "localhost", + fieldname.Port: "5432", + fieldname.User: "root", + fieldname.Password: "123456", + fieldname.Database: "test", + }, + }, + }, + }, + }) +} + +func TestDatabaseCredentialsProvisioner(t *testing.T) { + plugintest.TestProvisioner(t, DatabaseCredentials().DefaultProvisioner, map[string]plugintest.ProvisionCase{ + "default": { + ItemFields: map[sdk.FieldName]string{ + fieldname.Host: "localhost", + fieldname.Port: "5432", + fieldname.User: "root", + fieldname.Password: "123456", + fieldname.Database: "test", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "PGHOST": "localhost", + "PGPORT": "5432", + "PGUSER": "root", + "PGPASSWORD": "123456", + "PGDATABASE": "test", + }, + }, + }, + }) +} diff --git a/plugins/yugabytedb/plugin.go b/plugins/yugabytedb/plugin.go new file mode 100644 index 000000000..f342fe4de --- /dev/null +++ b/plugins/yugabytedb/plugin.go @@ -0,0 +1,22 @@ +package yugabytedb + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema" +) + +func New() schema.Plugin { + return schema.Plugin{ + Name: "yugabytedb", + Platform: schema.PlatformInfo{ + Name: "YugabyteDB", + Homepage: sdk.URL("https://yugabyte.com"), + }, + Credentials: []schema.CredentialType{ + DatabaseCredentials(), + }, + Executables: []schema.Executable{ + YugabyteDBCLI(), + }, + } +} diff --git a/plugins/yugabytedb/ysqlsh.go b/plugins/yugabytedb/ysqlsh.go new file mode 100644 index 000000000..52ba2121b --- /dev/null +++ b/plugins/yugabytedb/ysqlsh.go @@ -0,0 +1,25 @@ +package yugabytedb + +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 YugabyteDBCLI() schema.Executable { + return schema.Executable{ + Name: "YugabyteDB SQL Shell", + Runs: []string{"ysqlsh"}, + DocsURL: sdk.URL("https://docs.yugabyte.com/preview/admin/ysqlsh/"), + NeedsAuth: needsauth.IfAll( + needsauth.NotForHelpOrVersion(), + needsauth.NotWithoutArgs(), + ), + Uses: []schema.CredentialUsage{ + { + Name: credname.DatabaseCredentials, + }, + }, + } +} From 2dcf34a1210904ff72ddf79169ce9196d0d511d8 Mon Sep 17 00:00:00 2001 From: parthiv11 Date: Fri, 29 Sep 2023 09:48:42 +0530 Subject: [PATCH 2/2] Recommited with sign --- plugins/yugabytedb/database_credentials.go | 15 +++++++-------- plugins/yugabytedb/database_credentials_test.go | 9 +++++---- plugins/yugabytedb/ysqlsh.go | 7 +++---- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/plugins/yugabytedb/database_credentials.go b/plugins/yugabytedb/database_credentials.go index a941f7fa5..3d34bd15b 100644 --- a/plugins/yugabytedb/database_credentials.go +++ b/plugins/yugabytedb/database_credentials.go @@ -1,8 +1,6 @@ package yugabytedb import ( - "context" - "github.com/1Password/shell-plugins/sdk" "github.com/1Password/shell-plugins/sdk/importer" "github.com/1Password/shell-plugins/sdk/provision" @@ -14,8 +12,8 @@ import ( func DatabaseCredentials() schema.CredentialType { return schema.CredentialType{ Name: credname.DatabaseCredentials, - DocsURL: sdk.URL("https://docs.yugabyte.com/preview/admin/ysqlsh/#connect-to-a-database"), - ManagementURL: sdk.URL("https://cloud.yugabyte.com/"), + DocsURL: sdk.URL("https://docs.yugabyte.com/preview/admin/ysqlsh/#connect-to-a-database"), + ManagementURL: sdk.URL("https://cloud.yugabyte.com/clusters"), Fields: []schema.CredentialField{ { Name: fieldname.Host, @@ -27,8 +25,8 @@ func DatabaseCredentials() schema.CredentialType { Optional: true, }, { - Name: fieldname.User, - MarkdownDescription: "Yugabyte user to authenticate as.", + Name: fieldname.Username, + MarkdownDescription: "Yugabyte user to get authenticate.", }, { Name: fieldname.Password, @@ -37,7 +35,7 @@ func DatabaseCredentials() schema.CredentialType { }, { Name: fieldname.Database, - MarkdownDescription: "Database name to connect to.", + MarkdownDescription: "Database name to connect to Yugabyte.", Optional: true, }, }, @@ -45,10 +43,11 @@ func DatabaseCredentials() schema.CredentialType { Importer: importer.TryEnvVarPair(defaultEnvVarMapping), } } + var defaultEnvVarMapping = map[string]sdk.FieldName{ "PGHOST": fieldname.Host, "PGPORT": fieldname.Port, - "PGUSER": fieldname.User, + "PGUSER": fieldname.Username, "PGPASSWORD": fieldname.Password, "PGDATABASE": fieldname.Database, } diff --git a/plugins/yugabytedb/database_credentials_test.go b/plugins/yugabytedb/database_credentials_test.go index eea2a8669..1a17b0cab 100644 --- a/plugins/yugabytedb/database_credentials_test.go +++ b/plugins/yugabytedb/database_credentials_test.go @@ -10,7 +10,7 @@ import ( func TestDatabaseCredentialsImporter(t *testing.T) { plugintest.TestImporter(t, DatabaseCredentials().Importer, map[string]plugintest.ImportCase{ - "default": { + "yugabyte": { Environment: map[string]string{ "PGHOST": "localhost", "PGPORT": "5432", @@ -23,7 +23,7 @@ func TestDatabaseCredentialsImporter(t *testing.T) { Fields: map[sdk.FieldName]string{ fieldname.Host: "localhost", fieldname.Port: "5432", - fieldname.User: "root", + fieldname.Username: "root", fieldname.Password: "123456", fieldname.Database: "test", }, @@ -39,7 +39,7 @@ func TestDatabaseCredentialsProvisioner(t *testing.T) { ItemFields: map[sdk.FieldName]string{ fieldname.Host: "localhost", fieldname.Port: "5432", - fieldname.User: "root", + fieldname.Username: "root", fieldname.Password: "123456", fieldname.Database: "test", }, @@ -53,5 +53,6 @@ func TestDatabaseCredentialsProvisioner(t *testing.T) { }, }, }, - }) + }, + ) } diff --git a/plugins/yugabytedb/ysqlsh.go b/plugins/yugabytedb/ysqlsh.go index 52ba2121b..f32acb0fc 100644 --- a/plugins/yugabytedb/ysqlsh.go +++ b/plugins/yugabytedb/ysqlsh.go @@ -9,12 +9,11 @@ import ( func YugabyteDBCLI() schema.Executable { return schema.Executable{ - Name: "YugabyteDB SQL Shell", - Runs: []string{"ysqlsh"}, - DocsURL: sdk.URL("https://docs.yugabyte.com/preview/admin/ysqlsh/"), + Name: "YugabyteDB SQL Shell", + Runs: []string{"ysqlsh"}, + DocsURL: sdk.URL("https://docs.yugabyte.com/preview/admin/ysqlsh/"), NeedsAuth: needsauth.IfAll( needsauth.NotForHelpOrVersion(), - needsauth.NotWithoutArgs(), ), Uses: []schema.CredentialUsage{ {