Skip to content

Commit

Permalink
Add support for Vultr
Browse files Browse the repository at this point in the history
  • Loading branch information
arunsathiya committed Jan 23, 2023
1 parent 060095f commit 4b7fee1
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 0 deletions.
66 changes: 66 additions & 0 deletions plugins/vultr/api_key.go
@@ -0,0 +1,66 @@
package vultr

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 APIKey() schema.CredentialType {
return schema.CredentialType{
Name: credname.APIKey,
DocsURL: sdk.URL("https://www.vultr.com/api/#section/Authentication"),
ManagementURL: sdk.URL("https://my.vultr.com/settings/#settingsapi"),
Fields: []schema.CredentialField{
{
Name: fieldname.APIKey,
MarkdownDescription: "API Key used to authenticate to Vultr.",
Secret: true,
Composition: &schema.ValueComposition{
Length: 36,
Charset: schema.Charset{
Uppercase: true,
Digits: true,
},
},
},
},
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
TryVultrConfigFile(),
)}
}

var defaultEnvVarMapping = map[string]sdk.FieldName{
"VULTR_API_KEY": fieldname.APIKey,
}

func TryVultrConfigFile() sdk.Importer {
return importer.TryFile("~/.vultr-cli.yaml", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
var config Config
if err := contents.ToYAML(&config); err != nil {
out.AddError(err)
return
}

if config.APIKey == "" {
return
}

out.AddCandidate(sdk.ImportCandidate{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: config.APIKey,
},
})
})
}

type Config struct {
APIKey string `yaml:"api-key"`
}
53 changes: 53 additions & 0 deletions plugins/vultr/api_key_test.go
@@ -0,0 +1,53 @@
package vultr

import (
"testing"

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

func TestAPIKeyProvisioner(t *testing.T) {
plugintest.TestProvisioner(t, APIKey().DefaultProvisioner, map[string]plugintest.ProvisionCase{
"default": {
ItemFields: map[sdk.FieldName]string{
fieldname.APIKey: "4T7FVVVS23ZMKHLV990NL61F4O6L9EXAMPLE",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"VULTR_API_KEY": "4T7FVVVS23ZMKHLV990NL61F4O6L9EXAMPLE",
},
},
},
})
}

func TestAPIKeyImporter(t *testing.T) {
plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{
"environment": {
Environment: map[string]string{
"VULTR_API_KEY": "4T7FVVVS23ZMKHLV990NL61F4O6L9EXAMPLE",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "4T7FVVVS23ZMKHLV990NL61F4O6L9EXAMPLE",
},
},
},
},
"config file": {
Files: map[string]string{
"~/.vultr-cli.yaml": plugintest.LoadFixture(t, "config.yml"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "4T7FVVVS23ZMKHLV990NL61F4O6L9EXAMPLE",
},
},
},
},
})
}
22 changes: 22 additions & 0 deletions plugins/vultr/plugin.go
@@ -0,0 +1,22 @@
package vultr

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

func New() schema.Plugin {
return schema.Plugin{
Name: "vultr",
Platform: schema.PlatformInfo{
Name: "Vultr",
Homepage: sdk.URL("https://vultr.com"),
},
Credentials: []schema.CredentialType{
APIKey(),
},
Executables: []schema.Executable{
VultrCLI(),
},
}
}
1 change: 1 addition & 0 deletions plugins/vultr/test-fixtures/vultr-cli.yaml
@@ -0,0 +1 @@
api-key: 4T7FVVVS23ZMKHLV990NL61F4O6L9EXAMPLE
22 changes: 22 additions & 0 deletions plugins/vultr/vultr-cli.go
@@ -0,0 +1,22 @@
package vultr

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 VultrCLI() schema.Executable {
return schema.Executable{
Name: "Vultr CLI",
Runs: []string{"vultr-cli"},
DocsURL: sdk.URL("https://github.com/vultr/vultr-cli"),
NeedsAuth: needsauth.NotForHelpOrVersion(),
Uses: []schema.CredentialUsage{
{
Name: credname.APIKey,
},
},
}
}

0 comments on commit 4b7fee1

Please sign in to comment.