Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions plugins/octopus/api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package octopus

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://octopus.com/docs/octopus-rest-api/how-to-create-an-api-key"),
ManagementURL: nil,
Fields: []schema.CredentialField{
{
Name: fieldname.APIKey,
MarkdownDescription: "API Key used to authenticate to Octopus Deploy.",
Secret: true,
Composition: &schema.ValueComposition{
Charset: schema.Charset{
Uppercase: true,
Digits: true,
},
Prefix: "API-",
},
},
{
Name: fieldname.URL,
MarkdownDescription: "URL of the Octopus Deploy server.",
Optional: true,
},
{
Name: fieldname.Space,
MarkdownDescription: "Space to use for the Octopus Deploy CLI.",
Optional: true,
},
},
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
Importer: importer.TryAll(
importer.TryEnvVarPair(defaultEnvVarMapping),
TryOctopusDeployConfigFile(),
)}
}

var defaultEnvVarMapping = map[string]sdk.FieldName{
"OCTOPUS_API_KEY": fieldname.APIKey,
"OCTOPUS_URL": fieldname.URL,
"OCTOPUS_SPACE": fieldname.Space,
}

func TryOctopusDeployConfigFile() sdk.Importer {
return importer.TryFile("~/.config/octopus/cli_config.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 config.APIKey == "" {
return
}

fields := map[sdk.FieldName]string{
fieldname.APIKey: config.APIKey,
}

if config.URL != "" {
fields[fieldname.URL] = config.URL
}

if config.Space != "" {
fields[fieldname.Space] = config.Space
}

out.AddCandidate(sdk.ImportCandidate{
Fields: fields,
})
})
}

type Config struct {
APIKey string `json:"apikey"`
URL string `json:"url"`
Space string `json:"space"`
}
63 changes: 63 additions & 0 deletions plugins/octopus/api_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package octopus

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: "API-PFIPLPKUTCYAWJXUREGWTUYNYEEXAMPLE",
fieldname.URL: "https://my.octopus.app",
fieldname.Space: "Default",
},
ExpectedOutput: sdk.ProvisionOutput{
Environment: map[string]string{
"OCTOPUS_API_KEY": "API-PFIPLPKUTCYAWJXUREGWTUYNYEEXAMPLE",
"OCTOPUS_URL": "https://my.octopus.app",
"OCTOPUS_SPACE": "Default",
},
},
},
})
}

func TestAPIKeyImporter(t *testing.T) {
plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{
"environment": {
Environment: map[string]string{
"OCTOPUS_API_KEY": "API-PFIPLPKUTCYAWJXUREGWTUYNYEEXAMPLE",
"OCTOPUS_URL": "https://my.octopus.app",
"OCTOPUS_SPACE": "Default",
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "API-PFIPLPKUTCYAWJXUREGWTUYNYEEXAMPLE",
fieldname.URL: "https://my.octopus.app",
fieldname.Space: "Default",
},
},
},
},
"config file": {
Files: map[string]string{
"~/.config/octopus/cli_config.json": plugintest.LoadFixture(t, "cli_config.json"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.APIKey: "API-PFIPLPKUTCYAWJXUREGWTUYNYEEXAMPLE",
fieldname.URL: "https://my.octopus.app",
fieldname.Space: "Default",
},
},
},
},
})
}
27 changes: 27 additions & 0 deletions plugins/octopus/octopus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package octopus

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 OctopusDeployCLI() schema.Executable {
return schema.Executable{
Name: "Octopus CLI",
Runs: []string{"octopus"},
DocsURL: sdk.URL("https://octopus.com/docs/octopus-rest-api/cli"),
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWhenContainsArgs("login"),
needsauth.NotWhenContainsArgs("logout"),
needsauth.NotWhenContainsArgs("config"),
),
Uses: []schema.CredentialUsage{
{
Name: credname.APIKey,
},
},
}
}
22 changes: 22 additions & 0 deletions plugins/octopus/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package octopus

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

func New() schema.Plugin {
return schema.Plugin{
Name: "octopus",
Platform: schema.PlatformInfo{
Name: "Octopus Deploy",
Homepage: sdk.URL("https://octopus.com"),
},
Credentials: []schema.CredentialType{
APIKey(),
},
Executables: []schema.Executable{
OctopusDeployCLI(),
},
}
}
6 changes: 6 additions & 0 deletions plugins/octopus/test-fixtures/cli_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"accesstoken": "",
"apikey": "API-PFIPLPKUTCYAWJXUREGWTUYNYEEXAMPLE",
"space": "Default",
"url": "https://my.octopus.app"
}
2 changes: 2 additions & 0 deletions sdk/schema/fieldname/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const (
Region = sdk.FieldName("Region")
Secret = sdk.FieldName("Secret")
SecretAccessKey = sdk.FieldName("Secret Access Key")
Space = sdk.FieldName("Space")
Subdomain = sdk.FieldName("Subdomain")
Token = sdk.FieldName("Token")
URL = sdk.FieldName("URL")
Expand Down Expand Up @@ -110,6 +111,7 @@ func ListAll() []sdk.FieldName {
Region,
Secret,
SecretAccessKey,
Space,
Token,
URL,
User,
Expand Down