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
101 changes: 101 additions & 0 deletions plugins/civo/api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package civo

import (
"context"
"encoding/json"

"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,
},
},
},
{
Name: fieldname.APIKeyID,
SimonBarendse marked this conversation as resolved.
Show resolved Hide resolved
MarkdownDescription: "API Name to identify the API Key.",
},
{
Name: fieldname.DefaultRegion,
AndyTitu marked this conversation as resolved.
Show resolved Hide resolved
MarkdownDescription: "The default region to use for this API Key.",
Optional: true,
},
},
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
TryCivoConfigFile(),
)}
}

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

func TryCivoConfigFile() sdk.Importer {

SimonBarendse marked this conversation as resolved.
Show resolved Hide resolved
return importer.TryFile("~/.civo.json", 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

SimonBarendse marked this conversation as resolved.
Show resolved Hide resolved
}

if len(config.Properties) == 0 && config.Meta.CurrentAPIKey == "" {
return
}

for key, value := range config.Properties {
var apiKey string

err := json.Unmarshal(value, &apiKey)
if err != nil {
out.AddError(err)
return
}
out.AddCandidate(sdk.ImportCandidate{
NameHint: key,
Fields: map[sdk.FieldName]string{
fieldname.APIKey: apiKey,
fieldname.APIKeyID: config.Meta.CurrentAPIKey,
fieldname.DefaultRegion: config.Meta.DefaultRegion,
},
})

break
}

})
}

type Config struct {
Properties map[string]json.RawMessage `json:"apikeys"`
AndyTitu marked this conversation as resolved.
Show resolved Hide resolved

Meta struct {
CurrentAPIKey string `json:"current_apikey"`
DefaultRegion string `json:"default_region"`
} `json:"meta"`
AndyTitu marked this conversation as resolved.
Show resolved Hide resolved
}
64 changes: 64 additions & 0 deletions plugins/civo/api_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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",
fieldname.APIKeyID: "testdemoname",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"CIVO_TOKEN": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
"CIVO_API_KEY": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
"CIVO_API_KEY_NAME": "testdemoname",
},
},
},
})
}

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

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

"~/.civo.json": plugintest.LoadFixture(t, ".civo.json"),
},
ExpectedCandidates: []sdk.ImportCandidate{

{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
fieldname.APIKeyID: "testdemoname",
fieldname.DefaultRegion: "LON1",
},
},
},
},
})
}
26 changes: 26 additions & 0 deletions plugins/civo/civo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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://civo.com/docs/cli"),
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
needsauth.NotWhenContainsArgs("update"),
),
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://civo.com"),
},
Credentials: []schema.CredentialType{
APIKey(),
},
Executables: []schema.Executable{
CivoCLI(),
},
}
}
11 changes: 11 additions & 0 deletions plugins/civo/test-fixtures/.civo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"apikeys": {
"newspidey": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE"
},
"meta": {

"current_apikey": "newspidey1",
"default_region": "LON1"

}
}
AndyTitu marked this conversation as resolved.
Show resolved Hide resolved