Skip to content

Commit

Permalink
civo plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
itsCheithanya committed Jun 15, 2023
1 parent bb20d67 commit 8941afc
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 0 deletions.
105 changes: 105 additions & 0 deletions plugins/civo/api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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,
MarkdownDescription: "API Name to identify the API Key.",
},
{
Name: fieldname.DefaultRegion,
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_API_KEY_NAME": fieldname.APIKeyID,
"CIVO_API_KEY": fieldname.APIKey,
}

func TryCivoConfigFile() sdk.Importer {

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

}

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

var apiKey string
for key, value := range config.Properties {
if key == config.Meta.CurrentAPIKey {
err := json.Unmarshal(value, &apiKey)
if err != nil {
out.AddError(err)
return
}
}
break
}

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

})
}

type Config struct {
Properties map[string]json.RawMessage `json:"apikeys"`

Meta struct {
Admin bool `json:"admin"`
CurrentAPIKey string `json:"current_apikey"`
DefaultRegion string `json:"default_region"`
LatestReleaseCheck string `json:"latest_release_check"`
URL string `json:"url"`
LastCommandExecuted string `json:"last_command_executed"`
} `json:"meta"`
}
55 changes: 55 additions & 0 deletions plugins/civo/api_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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{ // TODO: Check if this is correct
fieldname.APIKey: "amqsmbexample",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"CIVO_API_KEY": "amqsmbexample",
},
},
},
})
}

func TestAPIKeyImporter(t *testing.T) {
plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{
"environment": {
Environment: map[string]string{ // TODO: Check if this is correct
"CIVO_API_KEY": "amqsmbexample",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "amqsmbexample",
},
},
},
},
// TODO: If you implemented a config file importer, add a test file example in civo/test-fixtures
// and fill the necessary details in the test template below.
"config file": {
Files: map[string]string{
// "~/path/to/config.yml": plugintest.LoadFixture(t, "config.yml"),
},
ExpectedCandidates: []sdk.ImportCandidate{
// {
// Fields: map[sdk.FieldName]string{
// fieldname.Token: "amqsmbexample",
// },
// },
},
},
})
}
25 changes: 25 additions & 0 deletions plugins/civo/civo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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(),
),
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(),
},
}
}
13 changes: 13 additions & 0 deletions plugins/civo/test-fixtures/civo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"apikeys": {
"newspidey": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE"
},
"meta": {
"admin": false,
"current_apikey": "newspidey1",
"default_region": "LON1",
"latest_release_check": "2023-06-11T20:25:06.916682112+05:30",
"url": "https://api.civo.com",
"last_command_executed": "2023-06-11T20:25:06.916237569+05:30"
}
}

0 comments on commit 8941afc

Please sign in to comment.