Repository navigation
IBM Cloud plugin #521
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
Merged
Merged
IBM Cloud plugin #521
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
10229f5
add ibmcloud basic login plugin
todr93 502280e
remove unnecessary code
todr93 300b1fe
change example key
todr93 f8de063
add optional resource group option
todr93 81f625e
fix: field name to title case
todr93 77de7df
update docsURL
todr93 025f28a
fix: allow [_-] in api key
todr93 3f36cdc
authenticate only on "login"
todr93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package ibmcloud | ||
|
|
||
| import ( | ||
| "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://cloud.ibm.com/docs/account?topic=account-manapikey"), | ||
| ManagementURL: sdk.URL("https://cloud.ibm.com/iam/overview"), | ||
| Fields: []schema.CredentialField{ | ||
| { | ||
| Name: fieldname.APIKey, | ||
| MarkdownDescription: "API Key used to authenticate to IBM Cloud.", | ||
| Secret: true, | ||
| Composition: &schema.ValueComposition{ | ||
| Length: 44, | ||
| Charset: schema.Charset{ | ||
| Uppercase: true, | ||
| Lowercase: true, | ||
| Digits: true, | ||
| Specific: []rune{'-', '_'}, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| Name: sdk.FieldName("Resource Group"), | ||
| MarkdownDescription: "Resource group to target when logging in to IBM Cloud.", | ||
| Optional: true, | ||
| }, | ||
| }, | ||
| DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), | ||
| Importer: importer.TryAll( | ||
| importer.TryEnvVarPair(defaultEnvVarMapping), | ||
| )} | ||
| } | ||
|
|
||
| var defaultEnvVarMapping = map[string]sdk.FieldName{ | ||
| "IBMCLOUD_API_KEY": fieldname.APIKey, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package ibmcloud | ||
|
|
||
| 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: "GeYS3RmGXo7cQh-Y8_oUSLmWarFF1HGqv4fVKEXAMPLE", | ||
| }, | ||
| ExpectedOutput: sdk.ProvisionOutput{ | ||
| Environment: map[string]string{ | ||
| "IBMCLOUD_API_KEY": "GeYS3RmGXo7cQh-Y8_oUSLmWarFF1HGqv4fVKEXAMPLE", | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func TestAPIKeyImporter(t *testing.T) { | ||
| plugintest.TestImporter(t, APIKey().Importer, map[string]plugintest.ImportCase{ | ||
| "environment": { | ||
| Environment: map[string]string{ | ||
| "IBMCLOUD_API_KEY": "GeYS3RmGXo7cQh-Y8_oUSLmWarFF1HGqv4fVKEXAMPLE", | ||
| }, | ||
| ExpectedCandidates: []sdk.ImportCandidate{ | ||
| { | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.APIKey: "GeYS3RmGXo7cQh-Y8_oUSLmWarFF1HGqv4fVKEXAMPLE", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| "config file": { | ||
| Files: map[string]string{}, | ||
| ExpectedCandidates: []sdk.ImportCandidate{}, | ||
| }, | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package ibmcloud | ||
|
|
||
| import ( | ||
| "context" | ||
| "strings" | ||
|
|
||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/schema/fieldname" | ||
| ) | ||
|
|
||
| // CLIProvisioner is responsible for provisioning the IBM Cloud CLI with the appropriate credentials | ||
| type CLIProvisioner struct { | ||
| } | ||
|
|
||
| func (p CLIProvisioner) Provision(ctx context.Context, in sdk.ProvisionInput, out *sdk.ProvisionOutput) { | ||
| // First, set the API key as an environment variable | ||
| out.AddEnvVar("IBMCLOUD_API_KEY", in.ItemFields[fieldname.APIKey]) | ||
|
|
||
| // Check if we need to add a resource group flag to the login command | ||
| targetGroup := "" | ||
| found := false | ||
| // Try exact match first | ||
| if tg, ok := in.ItemFields[sdk.FieldName("Resource Group")]; ok && tg != "" { | ||
| targetGroup = tg | ||
| found = true | ||
| } | ||
|
|
||
| // Try with trimmed whitespace | ||
| if !found { | ||
| for fieldName, fieldValue := range in.ItemFields { | ||
| fieldNameStr := string(fieldName) | ||
| trimmedName := strings.TrimSpace(fieldNameStr) | ||
| if trimmedName == "Resource Group" && fieldValue != "" { | ||
| targetGroup = fieldValue | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if found && targetGroup != "" { | ||
| // Only modify the command line if it's an ibmcloud login command | ||
| if len(out.CommandLine) > 1 && out.CommandLine[1] == "login" { | ||
| // Check if the command already has a -g or --target-group flag | ||
| hasTargetGroupFlag := false | ||
| for _, arg := range out.CommandLine { | ||
| if arg == "-g" || arg == "--target-group" || strings.HasPrefix(arg, "-g=") || strings.HasPrefix(arg, "--target-group=") { | ||
| hasTargetGroupFlag = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| // If no resource group flag is present, add it | ||
| if !hasTargetGroupFlag { | ||
| newCommandLine := append([]string{}, out.CommandLine[0], out.CommandLine[1]) | ||
| newCommandLine = append(newCommandLine, "-g", targetGroup) | ||
| newCommandLine = append(newCommandLine, out.CommandLine[2:]...) | ||
| out.CommandLine = newCommandLine | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (p CLIProvisioner) Deprovision(ctx context.Context, in sdk.DeprovisionInput, out *sdk.DeprovisionOutput) { | ||
| // Nothing to do here: environment variables get wiped automatically when the process exits. | ||
| } | ||
|
|
||
| func (p CLIProvisioner) Description() string { | ||
| return "Provision environment variables with IBM Cloud API Key and add resource group flag to login command if specified" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package ibmcloud | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/plugintest" | ||
| "github.com/1Password/shell-plugins/sdk/schema/fieldname" | ||
| ) | ||
|
|
||
| func TestCLIProvisioner(t *testing.T) { | ||
| provisioner := CLIProvisioner{} | ||
|
|
||
| testCases := map[string]plugintest.ProvisionCase{ | ||
| "with api key only": { | ||
| ItemFields: map[sdk.FieldName]string{ | ||
| fieldname.APIKey: "GeYS3RmGXo7cQhY8UboUSLmWarFF1HGqv4fVKEXAMPLE", | ||
| }, | ||
| CommandLine: []string{"ibmcloud", "login"}, | ||
| ExpectedOutput: sdk.ProvisionOutput{ | ||
| Environment: map[string]string{ | ||
| "IBMCLOUD_API_KEY": "GeYS3RmGXo7cQhY8UboUSLmWarFF1HGqv4fVKEXAMPLE", | ||
| }, | ||
| CommandLine: []string{"ibmcloud", "login"}, | ||
| }, | ||
| }, | ||
| "with api key and resource group": { | ||
| ItemFields: map[sdk.FieldName]string{ | ||
| fieldname.APIKey: "GeYS3RmGXo7cQhY8UboUSLmWarFF1HGqv4fVKEXAMPLE", | ||
| sdk.FieldName("Resource Group"): "my-resource-group", | ||
| }, | ||
| CommandLine: []string{"ibmcloud", "login"}, | ||
| ExpectedOutput: sdk.ProvisionOutput{ | ||
| Environment: map[string]string{ | ||
| "IBMCLOUD_API_KEY": "GeYS3RmGXo7cQhY8UboUSLmWarFF1HGqv4fVKEXAMPLE", | ||
| }, | ||
| CommandLine: []string{"ibmcloud", "login", "-g", "my-resource-group"}, | ||
| }, | ||
| }, | ||
| "with resource group but not login command": { | ||
| ItemFields: map[sdk.FieldName]string{ | ||
| fieldname.APIKey: "GeYS3RmGXo7cQhY8UboUSLmWarFF1HGqv4fVKEXAMPLE", | ||
| sdk.FieldName("Resource Group"): "my-resource-group", | ||
| }, | ||
| CommandLine: []string{"target"}, | ||
| ExpectedOutput: sdk.ProvisionOutput{ | ||
| Environment: map[string]string{ | ||
| "IBMCLOUD_API_KEY": "GeYS3RmGXo7cQhY8UboUSLmWarFF1HGqv4fVKEXAMPLE", | ||
| }, | ||
| CommandLine: []string{"target"}, | ||
| }, | ||
| }, | ||
| "with resource group and existing group flag": { | ||
| ItemFields: map[sdk.FieldName]string{ | ||
| fieldname.APIKey: "GeYS3RmGXo7cQhY8UboUSLmWarFF1HGqv4fVKEXAMPLE", | ||
| sdk.FieldName("Resource Group"): "my-resource-group", | ||
| }, | ||
| CommandLine: []string{"login", "-g", "existing-group"}, | ||
| ExpectedOutput: sdk.ProvisionOutput{ | ||
| Environment: map[string]string{ | ||
| "IBMCLOUD_API_KEY": "GeYS3RmGXo7cQhY8UboUSLmWarFF1HGqv4fVKEXAMPLE", | ||
| }, | ||
| CommandLine: []string{"login", "-g", "existing-group"}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| plugintest.TestProvisioner(t, provisioner, testCases) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package ibmcloud | ||
|
|
||
| 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 IBMCloudCLI() schema.Executable { | ||
| return schema.Executable{ | ||
| Name: "IBM Cloud CLI", | ||
| Runs: []string{"ibmcloud"}, | ||
| DocsURL: sdk.URL("https://cloud.ibm.com/docs/cli"), | ||
| NeedsAuth: needsauth.IfAll( | ||
| needsauth.NotForHelpOrVersion(), | ||
| needsauth.ForCommand("login"), | ||
| ), | ||
| Uses: []schema.CredentialUsage{ | ||
| { | ||
| Name: credname.APIKey, | ||
| Provisioner: CLIProvisioner{}, | ||
| }, | ||
| }, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package ibmcloud | ||
|
|
||
| import ( | ||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/schema" | ||
| ) | ||
|
|
||
| func New() schema.Plugin { | ||
| return schema.Plugin{ | ||
| Name: "ibmcloud", | ||
| Platform: schema.PlatformInfo{ | ||
| Name: "IBM Cloud", | ||
| Homepage: sdk.URL("https://cloud.ibm.com/"), | ||
| }, | ||
| Credentials: []schema.CredentialType{ | ||
| APIKey(), | ||
| }, | ||
| Executables: []schema.Executable{ | ||
| IBMCloudCLI(), | ||
| }, | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.