Skip to content
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

[VPC-3917] Support PIA get service key operation #1663

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
39 changes: 39 additions & 0 deletions commands/displayers/partner_interconnect_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,42 @@ func (v *PartnerInterconnectAttachmentRoute) KV() []map[string]any {

return out
}

type PartnerInterconnectAttachmentServiceKey struct {
Key do.PartnerInterconnectAttachmentServiceKey
}

var _ Displayable = &PartnerInterconnectAttachmentServiceKey{}

func (v *PartnerInterconnectAttachmentServiceKey) JSON(out io.Writer) error {
return writeJSON(v.Key, out)
}

func (v *PartnerInterconnectAttachmentServiceKey) Cols() []string {
return []string{
"Value",
"State",
"CreatedAt",
}
}

func (v *PartnerInterconnectAttachmentServiceKey) ColMap() map[string]string {
return map[string]string{
"Value": "Value",
"State": "State",
"CreatedAt": "CreatedAt",
}
}

func (v *PartnerInterconnectAttachmentServiceKey) KV() []map[string]any {
out := make([]map[string]any, 0, 1)

o := map[string]any{
"Value": v.Key.ServiceKey.Value,
"State": v.Key.ServiceKey.State,
"CreatedAt": v.Key.ServiceKey.CreatedAt,
}
out = append(out, o)

return out
}
36 changes: 36 additions & 0 deletions commands/partner_interconnect_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ vpc-ids "270a76ed-1bb7-4c5d-a6a5-e863de086940"`
cmdPartnerIARouteList.Example = `The following example lists the Network Interconnect Attachments on your account :` +
` doctl network --type "partner" interconnect-attachment list-routes --format ID,Cidr `

interconnectAttachmentServiceKeyDetails := `
- The Service key Value
- The Service key State`

cmdGetPartnerIAServiceKey := CmdBuilder(cmd, RunGetPartnerInterconnectAttachmentServiceKey, "get-service-key <interconnect-attachment-id>",
"Retrieves a Service key of Partner Interconnect Attachment", "Retrieves information about a Service key of Partner Interconnect Attachment, including:"+interconnectAttachmentServiceKeyDetails, Writer,
aliasOpt("g-service-key"), displayerType(&displayers.PartnerInterconnectAttachmentServiceKey{}))
AddStringFlag(cmdGetPartnerIAServiceKey, doctl.ArgInterconnectAttachmentType, "", "partner", "Specify interconnect attachment type (e.g., partner)")
cmdGetPartnerIAServiceKey.Example = `The following example retrieves information about a Service key of Partner Interconnect Attachment with the ID ` + "`" + `f81d4fae-7dec-11d0-a765-00a0c91e6bf6` + "`" +
`: doctl network --type "partner" interconnect-attachment get-service-key f81d4fae-7dec-11d0-a765-00a0c91e6bf6`

return cmd
}

Expand Down Expand Up @@ -296,6 +307,31 @@ func RunPartnerInterconnectAttachmentUpdate(c *CmdConfig) error {
return c.Display(item)
}

// RunGetPartnerInterconnectAttachmentServiceKey retrieves service key of existing Partner Interconnect Attachment
func RunGetPartnerInterconnectAttachmentServiceKey(c *CmdConfig) error {

if err := ensurePartnerAttachmentType(c); err != nil {
return err
}

err := ensureOneArg(c)
if err != nil {
return err
}
iaID := c.Args[0]

pias := c.PartnerInterconnectAttachments()
serviceKey, err := pias.GetServiceKey(iaID)
if err != nil {
return err
}

item := &displayers.PartnerInterconnectAttachmentServiceKey{
Key: *serviceKey,
}
return c.Display(item)
}

// RunPartnerInterconnectAttachmentDelete deletes an existing Partner Interconnect Attachment by its identifier.
func RunPartnerInterconnectAttachmentDelete(c *CmdConfig) error {

Expand Down
29 changes: 26 additions & 3 deletions commands/partner_interconnect_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"testing"
"time"

"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/do"
"github.com/digitalocean/godo"
"github.com/stretchr/testify/assert"

"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/do"
)

var (
Expand Down Expand Up @@ -39,13 +40,21 @@ var (
testPartnerIARouteList = do.PartnerInterconnectAttachmentRoutes{
testPartnerAttachmentRoute,
}

testServiceKey = do.PartnerInterconnectAttachmentServiceKey{
ServiceKey: &godo.ServiceKey{
Value: "test-service-key",
State: "active",
CreatedAt: time.Date(2025, 1, 30, 0, 0, 0, 0, time.UTC),
},
}
)

func TestPartnerInterconnectAttachmentsCommand(t *testing.T) {
cmd := PartnerInterconnectAttachments()
assert.NotNil(t, cmd)

assertCommandNames(t, cmd, "create", "get", "list", "delete", "update", "list-routes")
assertCommandNames(t, cmd, "create", "get", "list", "delete", "update", "list-routes", "get-service-key")
}

func TestPartnerInterconnectAttachmentCreate(t *testing.T) {
Expand Down Expand Up @@ -174,3 +183,17 @@ func TestInterconnectAttachmentRoutesList(t *testing.T) {
assert.NoError(t, err)
})
}

func TestInterconnectAttachmentsGetServiceKey(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
config.Doit.Set(config.NS, doctl.ArgInterconnectAttachmentType, "partner")

iaID := "e819b321-a9a1-4078-b437-8e6b8bf13530"
tm.partnerInterconnectAttachment.EXPECT().GetServiceKey(iaID).Return(&testServiceKey, nil)

config.Args = append(config.Args, iaID)

err := RunGetPartnerInterconnectAttachmentServiceKey(config)
assert.NoError(t, err)
})
}
15 changes: 15 additions & 0 deletions do/mocks/PartnerInterconnectAttachmentsService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions do/partner_interconnect_attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ type PartnerInterconnectAttachmentRoute struct {
// PartnerInterconnectAttachmentRoutes is a slice of PartnerInterconnectAttachmentRoute.
type PartnerInterconnectAttachmentRoutes []PartnerInterconnectAttachmentRoute

// PartnerInterconnectAttachmentServiceKey wraps a godo ServiceKey.
type PartnerInterconnectAttachmentServiceKey struct {
*godo.ServiceKey
}

// PartnerInterconnectAttachmentsService is an interface for interacting with
// DigitalOcean's partner interconnect attachments api.
type PartnerInterconnectAttachmentsService interface {
Expand All @@ -44,6 +49,7 @@ type PartnerInterconnectAttachmentsService interface {
DeletePartnerInterconnectAttachment(iaID string) error
UpdatePartnerInterconnectAttachment(iaID string, req *godo.PartnerInterconnectAttachmentUpdateRequest) (*PartnerInterconnectAttachment, error)
ListPartnerInterconnectAttachmentRoutes(iaID string) (PartnerInterconnectAttachmentRoutes, error)
GetServiceKey(iaID string) (*PartnerInterconnectAttachmentServiceKey, error)
}

var _ PartnerInterconnectAttachmentsService = &partnerInterconnectAttachmentsService{}
Expand Down Expand Up @@ -150,3 +156,12 @@ func (p *partnerInterconnectAttachmentsService) ListPartnerInterconnectAttachmen

return list, nil
}

// GetServiceKey retrieves a service key of a partner interconnect attachment.
func (p *partnerInterconnectAttachmentsService) GetServiceKey(iaID string) (*PartnerInterconnectAttachmentServiceKey, error) {
serviceKey, _, err := p.client.PartnerInterconnectAttachments.GetServiceKey(context.TODO(), iaID)
if err != nil {
return nil, err
}
return &PartnerInterconnectAttachmentServiceKey{ServiceKey: serviceKey}, nil
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22
require (
github.com/blang/semver v3.5.1+incompatible
github.com/creack/pty v1.1.21
github.com/digitalocean/godo v1.136.0
github.com/digitalocean/godo v1.138.0
github.com/docker/cli v24.0.5+incompatible
github.com/docker/docker v25.0.6+incompatible
github.com/docker/docker-credential-helpers v0.7.0 // indirect
Expand Down Expand Up @@ -39,6 +39,8 @@ require (
sigs.k8s.io/yaml v1.3.0
)

replace github.com/digitalocean/godo v1.138.0 => github.com/guptado/godo v0.0.0-20250219083849-2f80d81d1f61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you shouldn't be pointing to a local godo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aah yes. I have created another pr in godo with new updated model. as soon as we get the new godo releae version i can use it. I did above replace to test my code with new godo changes


require (
github.com/MakeNowJust/heredoc v1.0.0
github.com/apache/openwhisk-client-go v0.0.0-20221014112704-1ca897633f2d
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/digitalocean/godo v1.136.0 h1:DTxugljFJSMBPfEGq4KeXpnKeAHicggNqogcrw/YdZw=
github.com/digitalocean/godo v1.136.0/go.mod h1:PU8JB6I1XYkQIdHFop8lLAY9ojp6M0XcU0TWaQSxbrc=
github.com/digitalocean/godo v1.138.0 h1:0l0UEaVyNKvEmrn4r5NkZCpDysimJjVhLj30Gl6HgiQ=
github.com/digitalocean/godo v1.138.0/go.mod h1:PU8JB6I1XYkQIdHFop8lLAY9ojp6M0XcU0TWaQSxbrc=
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/docker/cli v24.0.5+incompatible h1:WeBimjvS0eKdH4Ygx+ihVq1Q++xg36M/rMi4aXAvodc=
Expand Down Expand Up @@ -217,6 +219,8 @@ github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWm
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 h1:YBftPWNWd4WwGqtY2yeZL2ef8rHAxPBD8KFhJpmcqms=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0/go.mod h1:YN5jB8ie0yfIUg6VvR9Kz84aCaG7AsGZnLjhHbUqwPg=
github.com/guptado/godo v0.0.0-20250219083849-2f80d81d1f61 h1:OyzWZganTGA7zHbB2hVnvV/qdG0qtOWAko4bgu0Oa7M=
github.com/guptado/godo v0.0.0-20250219083849-2f80d81d1f61/go.mod h1:PU8JB6I1XYkQIdHFop8lLAY9ojp6M0XcU0TWaQSxbrc=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
85 changes: 85 additions & 0 deletions integration/partner_interconnect_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ a0eb6eb0-fa38-41a8-a5de-1a75524667fe 169.250.0.0/29
}
}
`
interconnectGetServiceKeyOutput = `
Value State CreatedAt
test-service-key active 2025-01-30 12:00:00 +0000 UTC
`

interconnectGetServiceKeyResponse = `
{
"service_key": {
"created_at": "2025-01-30T12:00:00Z",
"value": "test-service-key",
"state": "active"
}
}`
)

var _ = suite("partner_interconnect_attachments/create", func(t *testing.T, when spec.G, it spec.S) {
Expand Down Expand Up @@ -216,3 +229,75 @@ var _ = suite("partner_interconnect_attachments/list-routes", func(t *testing.T,
})
})
})

var _ = suite("partner_interconnect_attachments/get-service-key", func(t *testing.T, when spec.G, it spec.S) {
var (
expect *require.Assertions
server *httptest.Server
)

it.Before(func() {
expect = require.New(t)

server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/v2/partner_interconnect/attachments/c5537207-ebf0-47cb-bc10-6fac717cd672/service_key":
auth := req.Header.Get("Authorization")
if auth != "Bearer some-magic-token" {
w.WriteHeader(http.StatusUnauthorized)
return
}

if req.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

w.Write([]byte(interconnectGetServiceKeyResponse))
default:
dump, err := httputil.DumpRequest(req, true)
if err != nil {
t.Fatal("failed to dump request")
}

t.Fatalf("received unknown request: %s", dump)
}
}))
})

when("no flags are passed", func() {
it("gets the specified service key", func() {
cmd := exec.Command(builtBinaryPath,
"-t", "some-magic-token",
"-u", server.URL,
"network",
"interconnect-attachment",
"get-service-key",
"c5537207-ebf0-47cb-bc10-6fac717cd672",
)

output, err := cmd.CombinedOutput()
expect.NoError(err, fmt.Sprintf("received error output: %s", output))
expect.Equal(strings.TrimSpace(interconnectGetServiceKeyOutput), strings.TrimSpace(string(output)))
})
})

when("format and no-header flags are passed", func() {
it("gets the specified service key", func() {
cmd := exec.Command(builtBinaryPath,
"-t", "some-magic-token",
"-u", server.URL,
"network",
"interconnect-attachment",
"get-service-key",
"--format", "Value",
"--no-header",
"c5537207-ebf0-47cb-bc10-6fac717cd672",
)

output, err := cmd.CombinedOutput()
expect.NoError(err, fmt.Sprintf("received error output: %s", output))
expect.Equal("test-service-key", strings.TrimSpace(string(output)))
})
})
})
12 changes: 12 additions & 0 deletions vendor/github.com/digitalocean/godo/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/digitalocean/godo/godo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading