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

Add support for Civo, a managed Kubernetes service #296

Merged
merged 14 commits into from
Sep 22, 2023
70 changes: 70 additions & 0 deletions plugins/civo/api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package civo

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.civo.com/docs/account/api-keys"),
ManagementURL: sdk.URL("https://dashboard.civo.com/security"),
Fields: []schema.CredentialField{
{
Name: fieldname.APIKey,
MarkdownDescription: "API Key used to authenticate to Civo.",
Secret: true,
Composition: &schema.ValueComposition{
Length: 50,
Charset: schema.Charset{
Uppercase: true,
Lowercase: true,
Digits: true,
},
},
},
},
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
TryCivoConfigFile("~/.civo.json"),
)}
}

var defaultEnvVarMapping = map[string]sdk.FieldName{
"CIVO_TOKEN": fieldname.APIKey,
AndyTitu marked this conversation as resolved.
Show resolved Hide resolved
}

func TryCivoConfigFile(path string) sdk.Importer {
return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
var config Config
if err := contents.ToJSON(&config); err != nil {
out.AddError(err)
return
}

if len(config.Properties) == 0 {
return
}

for key, value := range config.Properties {
out.AddCandidate(sdk.ImportCandidate{
NameHint: key,
Fields: map[sdk.FieldName]string{
fieldname.APIKey: value,
},
})
}
})
}

type Config struct {
Properties map[string]string `json:"apikeys"`
}
56 changes: 56 additions & 0 deletions plugins/civo/api_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package civo

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: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"CIVO_TOKEN": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
},
},
},
})
}

func TestAPIKeyImporter(t *testing.T) {
plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{
"environment": {
Environment: map[string]string{
"CIVO_TOKEN": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
},
},
},
},

"config file": {
Files: map[string]string{

"~/.civo.json": plugintest.LoadFixture(t, ".civo.json"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
},
NameHint: "testdemoname",
},
},
},
})
}
27 changes: 27 additions & 0 deletions plugins/civo/civo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package civo

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 CivoCLI() schema.Executable {
return schema.Executable{
Name: "Civo CLI",
Runs: []string{"civo"},
DocsURL: sdk.URL("https://www.civo.com/docs/overview/civo-cli"),
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
needsauth.NotForExactArgs("config"),
needsauth.NotWhenContainsArgs("apikey", "save"),
),
Uses: []schema.CredentialUsage{
{
Name: credname.APIKey,
},
},
}
}
22 changes: 22 additions & 0 deletions plugins/civo/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package civo

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

func New() schema.Plugin {
return schema.Plugin{
Name: "civo",
Platform: schema.PlatformInfo{
Name: "Civo",
Homepage: sdk.URL("https://www.civo.com"),
},
Credentials: []schema.CredentialType{
APIKey(),
},
Executables: []schema.Executable{
CivoCLI(),
},
}
}
5 changes: 5 additions & 0 deletions plugins/civo/test-fixtures/.civo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"apikeys": {
"testdemoname": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE"
}
}