Skip to content

Commit

Permalink
civo plugin added
Browse files Browse the repository at this point in the history
  • Loading branch information
itsCheithanya committed Jun 12, 2023
1 parent bb20d67 commit 6a5678b
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 0 deletions.
117 changes: 117 additions & 0 deletions plugins/civo/api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
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,
},
})

})
}

// {
// "apikeys": {
// "newspidey": "Vdi1GHFqXLG47VcfdvfvfvfvfvfvfvfvEgOd",
// },
// "meta": {
// "admin": false,
// "current_apikey": "newspidey",
// "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"
// }
// }

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

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": "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
},
},
},
},
// 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: "XFIx85McyfCQc490j1tBa5b5s2XiWerNdOdfnkrOnchEXAMPLE",
// },
// },
},
},
})
}
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", // TODO: Check if this is correct
Runs: []string{"civo"},
DocsURL: sdk.URL("https://civo.com/docs/cli"), // TODO: Replace with actual URL
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"), // TODO: Check if this is correct
},
Credentials: []schema.CredentialType{
APIKey(),
},
Executables: []schema.Executable{
CivoCLI(),
},
}
}

0 comments on commit 6a5678b

Please sign in to comment.