Skip to content

Commit

Permalink
feat: accounts slugs (#87)
Browse files Browse the repository at this point in the history
* feat: accounts slugs

* chore: bump go-octopusdeploy version

* fix: typo propmt to prompt
  • Loading branch information
domenicsim1 authored Sep 29, 2022
1 parent 7504b7d commit 724f365
Show file tree
Hide file tree
Showing 23 changed files with 950 additions and 49 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/AlecAivazis/survey/v2 v2.3.5
github.com/MakeNowJust/heredoc/v2 v2.0.1
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.4.1
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.5.0
github.com/briandowns/spinner v1.19.0
github.com/google/uuid v1.3.0
github.com/hashicorp/go-multierror v1.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ github.com/MakeNowJust/heredoc/v2 v2.0.1 h1:rlCHh70XXXv7toz95ajQWOWQnN4WNLt0TdpZ
github.com/MakeNowJust/heredoc/v2 v2.0.1/go.mod h1:6/2Abh5s+hc3g9nbWLe9ObDIOhaRrqsyY9MWy+4JdRM=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.4.1 h1:pLegjN2ES6/MkF/LA1aoFbgv7ywzNpfbJissUt7GWPM=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.4.1/go.mod h1:XWqxyDUVElUlTaPqyCBblukpsHSnPcAKkAHgJgbsIAs=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.5.0 h1:oyIAeLNcy1+BI8ok4TwWaP970LyMsmIBMxEVPtgEl9Y=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.5.0/go.mod h1:XWqxyDUVElUlTaPqyCBblukpsHSnPcAKkAHgJgbsIAs=
github.com/briandowns/spinner v1.19.0 h1:s8aq38H+Qju89yhp89b4iIiMzMm8YN3p6vGpwyh/a8E=
github.com/briandowns/spinner v1.19.0/go.mod h1:mQak9GHqbspjC/5iUx3qMlIho8xBS/ppAL/hX5SmPJU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
11 changes: 6 additions & 5 deletions pkg/cmd/account/aws/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package create

import (
"fmt"
"github.com/OctopusDeploy/cli/pkg/util"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/environments"
"io"
"os"

"github.com/OctopusDeploy/cli/pkg/util"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/environments"

"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd/account/helper"
Expand Down Expand Up @@ -109,7 +110,7 @@ func NewCmdCreate(f factory.Factory) *cobra.Command {

func CreateRun(opts *CreateOptions) error {
if !opts.NoPrompt {
if err := promptMissing(opts); err != nil {
if err := PromptMissing(opts); err != nil {
return err
}
}
Expand All @@ -125,7 +126,7 @@ func CreateRun(opts *CreateOptions) error {
return err
}

_, err = fmt.Fprintf(opts.Writer, "Successfully created AWS account %s %s.\n", createdAccount.GetName(), output.Dimf("(%s)", createdAccount.GetID()))
_, err = fmt.Fprintf(opts.Writer, "Successfully created AWS account %s %s.\n", createdAccount.GetName(), output.Dimf("(%s)", createdAccount.GetSlug()))
if err != nil {
return err
}
Expand All @@ -139,7 +140,7 @@ func CreateRun(opts *CreateOptions) error {
return nil
}

func promptMissing(opts *CreateOptions) error {
func PromptMissing(opts *CreateOptions) error {
if opts.Name.Value == "" {
if err := opts.Ask(&survey.Input{
Message: "Name",
Expand Down
146 changes: 146 additions & 0 deletions pkg/cmd/account/aws/create/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package create_test

import (
"bytes"
"net/url"
"testing"

"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd/account/aws/create"
"github.com/OctopusDeploy/cli/pkg/output"
"github.com/OctopusDeploy/cli/pkg/surveyext"
"github.com/OctopusDeploy/cli/test/fixtures"
"github.com/OctopusDeploy/cli/test/testutil"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/accounts"
octopusApiClient "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/environments"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/stretchr/testify/assert"
)

var serverUrl, _ = url.Parse("http://server")

const placeholderApiKey = "API-XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

var rootResource = testutil.NewRootResource()

func TestAWSAccountCreatePromptMissing(t *testing.T) {
const spaceID = "Space-1"
const envID = "Env-1"
_ = fixtures.NewSpace(spaceID, "testspace")
env := fixtures.NewEnvironment(spaceID, envID, "testenv")
api, qa := testutil.NewMockServerAndAsker()
out := &bytes.Buffer{}

opts := &create.CreateOptions{
CreateFlags: create.NewCreateFlags(),
}

errReceiver := testutil.GoBegin(func() error {
defer testutil.Close(api, qa)
octopus, _ := octopusApiClient.NewClient(testutil.NewMockHttpClientWithTransport(api), serverUrl, placeholderApiKey, "")
opts.Ask = qa.AsAsker()
opts.Octopus = octopus
opts.Writer = out
return create.PromptMissing(opts)
})

api.ExpectRequest(t, "GET", "/api").RespondWith(rootResource)

_ = qa.ExpectQuestion(t, &survey.Input{
Message: "Name",
Help: "A short, memorable, unique name for this account.",
}).AnswerWith("TestAccount")

_ = qa.ExpectQuestion(t, &surveyext.OctoEditor{
Editor: &survey.Editor{
Message: "Description",
Help: "A summary explaining the use of the account to other users.",
FileName: "*.md",
},
Optional: true,
}).AnswerWith("test 123")

_ = qa.ExpectQuestion(t, &survey.Input{
Message: "Access Key",
Help: "The AWS access key to use when authenticating against Amazon Web Services.",
}).AnswerWith("testaccesskey123")

_ = qa.ExpectQuestion(t, &survey.Password{
Message: "Secret Key",
Help: "The AWS secret key to use when authenticating against Amazon Web Services.",
}).AnswerWith("testpassword124")

api.ExpectRequest(t, "GET", "/api/Spaces-1/environments").RespondWith(resources.Resources[*environments.Environment]{
Items: []*environments.Environment{env}},
)

_ = qa.ExpectQuestion(t, &survey.MultiSelect{
Message: "Choose the environments that are allowed to use this account.\nIf nothing is selected, the account can be used for deployments to any environment.",
Options: []string{"testenv"},
}).AnswerWith([]string{"testenv"})

err := <-errReceiver
assert.Nil(t, err)

assert.Equal(t, []string{envID}, opts.Environments.Value)
assert.Equal(t, "testpassword124", opts.SecretKey.Value)
assert.Equal(t, "testaccesskey123", opts.AccessKey.Value)
assert.Equal(t, "test 123", opts.Description.Value)
assert.Equal(t, "TestAccount", opts.Name.Value)
}

func TestAWSAccountCreateNoPrompt(t *testing.T) {
const spaceID = "Space-1"
const envID = "Env-1"
_ = fixtures.NewSpace(spaceID, "testspace")
_ = fixtures.NewEnvironment(spaceID, envID, "testenv")
api, qa := testutil.NewMockServerAndAsker()
out := &bytes.Buffer{}

opts := &create.CreateOptions{
CreateFlags: create.NewCreateFlags(),
}
opts.Space = spaceID
opts.Name.Value = "testaccount"
opts.AccessKey.Value = "testaccesskey123"
opts.SecretKey.Value = "testsecretkey123"

errReceiver := testutil.GoBegin(func() error {
defer testutil.Close(api, qa)
octopus, _ := octopusApiClient.NewClient(testutil.NewMockHttpClientWithTransport(api), serverUrl, placeholderApiKey, "")
opts.Ask = qa.AsAsker()
opts.Octopus = octopus
opts.Writer = out
opts.NoPrompt = true
return create.CreateRun(opts)
})

testAccount, err := accounts.NewAmazonWebServicesAccount(
opts.Name.Value,
opts.AccessKey.Value,
core.NewSensitiveValue(opts.SecretKey.Value),
)
assert.Nil(t, err)
testAccount.ID = "Account-1"
testAccount.Slug = "testaccount"
testAccount.SpaceID = spaceID

api.ExpectRequest(t, "GET", "/api").RespondWith(rootResource)
api.ExpectRequest(t, "POST", "/api/Spaces-1/accounts").RespondWithStatus(201, "", testAccount)

err = <-errReceiver
assert.Nil(t, err)
res := out.String()
assert.Equal(t, heredoc.Docf(`
Successfully created AWS account %s %s.
View this account on Octopus Deploy: %s
`,
testAccount.Name,
output.Dimf("(%s)", testAccount.Slug),
output.Bluef("%s/app#/%s/infrastructure/accounts/%s", "", opts.Space, testAccount.ID),
), res)
}
8 changes: 5 additions & 3 deletions pkg/cmd/account/aws/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewCmdList(f factory.Factory) *cobra.Command {
$ octopus account aws list"
`),
Aliases: []string{"ls"},
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
client, err := f.GetSpacedClient()
if err != nil {
return err
Expand Down Expand Up @@ -47,19 +47,21 @@ func listAwsAccounts(client *client.Client, cmd *cobra.Command) error {
acc := item.(*accounts.AmazonWebServicesAccount)
return &struct {
Id string
Slug string
Name string
AccessKey string
}{
Id: acc.GetID(),
Slug: acc.GetSlug(),
Name: acc.GetName(),
AccessKey: acc.AccessKey,
}
},
Table: output.TableDefinition[accounts.IAccount]{
Header: []string{"NAME", "ACCESS KEY"},
Header: []string{"NAME", "SLUG", "ACCESS KEY"},
Row: func(item accounts.IAccount) []string {
acc := item.(*accounts.AmazonWebServicesAccount)
return []string{output.Bold(acc.GetName()), acc.AccessKey}
return []string{output.Bold(acc.GetName()), acc.GetSlug(), acc.AccessKey}
}},
Basic: func(item accounts.IAccount) string {
return item.GetName()
Expand Down
11 changes: 6 additions & 5 deletions pkg/cmd/account/azure/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package create

import (
"fmt"
"github.com/OctopusDeploy/cli/pkg/util"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/environments"
"io"
"os"
"strings"

"github.com/OctopusDeploy/cli/pkg/util"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/environments"

"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd/account/helper"
Expand Down Expand Up @@ -181,7 +182,7 @@ func NewCmdCreate(f factory.Factory) *cobra.Command {

func CreateRun(opts *CreateOptions) error {
if !opts.NoPrompt {
if err := promptMissing(opts); err != nil {
if err := PromptMissing(opts); err != nil {
return err
}
}
Expand Down Expand Up @@ -218,7 +219,7 @@ func CreateRun(opts *CreateOptions) error {
return err
}

_, err = fmt.Fprintf(opts.Writer, "Successfully created Azure account %s %s.\n", createdAccount.GetName(), output.Dimf("(%s)", createdAccount.GetID()))
_, err = fmt.Fprintf(opts.Writer, "Successfully created Azure account %s %s.\n", createdAccount.GetName(), output.Dimf("(%s)", createdAccount.GetSlug()))
if err != nil {
return err
}
Expand All @@ -243,7 +244,7 @@ func CreateRun(opts *CreateOptions) error {
return nil
}

func promptMissing(opts *CreateOptions) error {
func PromptMissing(opts *CreateOptions) error {
if opts.Name.Value == "" {
if err := opts.Ask(&survey.Input{
Message: "Name",
Expand Down
Loading

0 comments on commit 724f365

Please sign in to comment.