From 696bc44a8f8818aeee0f8eeb947960501f168efb Mon Sep 17 00:00:00 2001 From: siddhikhapare Date: Thu, 6 Jul 2023 15:48:48 +0530 Subject: [PATCH] Required changes added --- plugins/civo/api_key.go | 84 ++++++++++++++++++++++++++++++++---- plugins/civo/api_key_test.go | 23 ++++++++-- 2 files changed, 94 insertions(+), 13 deletions(-) diff --git a/plugins/civo/api_key.go b/plugins/civo/api_key.go index f4fc88b28..44cfaebd4 100644 --- a/plugins/civo/api_key.go +++ b/plugins/civo/api_key.go @@ -2,10 +2,12 @@ package civo import ( "context" + "os" + "strings" "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/provision" "github.com/1Password/shell-plugins/sdk/schema" "github.com/1Password/shell-plugins/sdk/schema/credname" "github.com/1Password/shell-plugins/sdk/schema/fieldname" @@ -30,10 +32,16 @@ func APIKey() schema.CredentialType { }, }, }, + { + Name: fieldname.APIKeyID, + MarkdownDescription: "The Name of apikey used to authenticate to civo", + Optional: true, + }, }, - DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), + DefaultProvisioner: civoProvisioner{}, Importer: importer.TryAll( importer.TryEnvVarPair(defaultEnvVarMapping), + importer.TryEnvVarPair(secondEnvVarMapping), TryCivoConfigFile("~/.civo.json"), )} } @@ -42,6 +50,43 @@ var defaultEnvVarMapping = map[string]sdk.FieldName{ "CIVO_TOKEN": fieldname.APIKey, } +var secondEnvVarMapping = map[string]sdk.FieldName{ + "CIVO_API_KEY": fieldname.APIKey, + "CIVO_API_KEY_NAME": fieldname.APIKeyID, +} + +type civoProvisioner struct { + sdk.Provisioner + Schema map[string]sdk.FieldName +} + +func (p civoProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) { + if apiKey, ok := in.ItemFields[fieldname.APIKey]; ok { + out.AddEnvVar("CIVO_TOKEN", apiKey) + } + + if apiKey, ok := in.ItemFields[fieldname.APIKey]; ok { + out.AddEnvVar("CIVO_API_KEY", apiKey) + //out.AddEnvVar("CIVO_API_KEY_NAME", apiKeyID) + } + + if apiKeyID, ok := in.ItemFields[fieldname.APIKeyID]; ok { + out.AddEnvVar("CIVO_API_KEY_NAME", apiKeyID) + } +} + +func (p civoProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) { + // Nothing to do here: environment variables get wiped automatically when the process exits. +} + +func (p civoProvisioner) Description() string { + var envVarNames []string + for envVarName := range p.Schema { + envVarNames = append(envVarNames, envVarName) + } + return strings.Join(envVarNames, ", ") +} + func TryCivoConfigFile(path string) sdk.Importer { return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { @@ -56,13 +101,34 @@ func TryCivoConfigFile(path string) sdk.Importer { return } - for key, value := range config.Properties { - out.AddCandidate(sdk.ImportCandidate{ - NameHint: key, - Fields: map[sdk.FieldName]string{ - fieldname.APIKey: value, - }, - }) + for key := range config.APIKeys { + apiKey := os.Getenv("CIVO_TOKEN") + if apiKey == "" { + apiKey = os.Getenv("CIVO_API_KEY") + if apiKey == "" { + continue + } + + apiKeyName := os.Getenv("CIVO_API_KEY_NAME") + if apiKeyName == "" { + continue + } + + out.AddCandidate(sdk.ImportCandidate{ + Fields: map[sdk.FieldName]string{ + fieldname.APIKey: apiKey, + fieldname.APIKeyID: apiKeyName, + }, + NameHint: importer.SanitizeNameHint(key), + }) + } else { + out.AddCandidate(sdk.ImportCandidate{ + Fields: map[sdk.FieldName]string{ + fieldname.APIKey: apiKey, + }, + NameHint: importer.SanitizeNameHint(key), + }) + } } }) } diff --git a/plugins/civo/api_key_test.go b/plugins/civo/api_key_test.go index 6478af7e9..510ee64cb 100644 --- a/plugins/civo/api_key_test.go +++ b/plugins/civo/api_key_test.go @@ -12,11 +12,14 @@ func TestAPIKeyProvisioner(t *testing.T) { plugintest.TestProvisioner(t, APIKey().DefaultProvisioner, map[string]plugintest.ProvisionCase{ "default": { ItemFields: map[sdk.FieldName]string{ - fieldname.APIKey: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", + fieldname.APIKey: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", + fieldname.APIKeyID: "testdemoname", }, ExpectedOutput: sdk.ProvisionOutput{ Environment: map[string]string{ - "CIVO_TOKEN": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", + "CIVO_TOKEN": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", + "CIVO_API_KEY": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", + "CIVO_API_KEY_NAME": "testdemoname", }, }, }, @@ -27,13 +30,17 @@ func TestAPIKeyImporter(t *testing.T) { plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{ "environment": { Environment: map[string]string{ - "CIVO_TOKEN": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", + "CIVO_TOKEN": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", + "CIVO_API_KEY": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", + "CIVO_API_KEY_NAME": "testdemoname", }, ExpectedCandidates: []sdk.ImportCandidate{ { Fields: map[sdk.FieldName]string{ - fieldname.APIKey: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", + fieldname.APIKey: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", + fieldname.APIKeyID: "testdemoname", }, + NameHint: "testdemoname", }, }, }, @@ -47,6 +54,14 @@ func TestAPIKeyImporter(t *testing.T) { { Fields: map[sdk.FieldName]string{ fieldname.APIKey: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", + //fieldname.APIKeyID:"testdemoname", + }, + NameHint: "testdemoname", + }, + { + Fields: map[sdk.FieldName]string{ + fieldname.APIKey: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE", + fieldname.APIKeyID: "testdemoname", }, NameHint: "testdemoname", },