Skip to content

Commit

Permalink
Required changes added
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhikhapare committed Jul 6, 2023
1 parent e1fc5a0 commit 696bc44
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 13 deletions.
84 changes: 75 additions & 9 deletions plugins/civo/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"),
)}
}
Expand All @@ -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) {
Expand All @@ -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),
})
}
}
})
}
Expand Down
23 changes: 19 additions & 4 deletions plugins/civo/api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},
Expand All @@ -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",
},
},
},
Expand All @@ -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",
},
Expand Down

0 comments on commit 696bc44

Please sign in to comment.