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

Merged
merged 8 commits into from
Feb 25, 2025
Merged
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 @@ -166,3 +166,42 @@ func (v *PartnerInterconnectAttachmentBgpAuthKey) KV() []map[string]any {
out = append(out, o)
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
}
37 changes: 37 additions & 0 deletions commands/partner_interconnect_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ vpc-ids "270a76ed-1bb7-4c5d-a6a5-e863de086940"`
cmdGetPartnerIAGetBGPAuthKey.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-bgp-auth-key f81d4fae-7dec-11d0-a765-00a0c91e6bf6`

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

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 @@ -366,6 +378,31 @@ func RunGetPartnerInterconnectAttachmentBGPAuthKey(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
24 changes: 23 additions & 1 deletion commands/partner_interconnect_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,21 @@ var (
Value: "test-bgp-auth-key",
},
}

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", "regenerate-service-key", "get-bgp-auth-key")
assertCommandNames(t, cmd, "create", "get", "list", "delete", "update", "list-routes", "regenerate-service-key", "get-bgp-auth-key", "get-service-key")
}

func TestPartnerInterconnectAttachmentCreate(t *testing.T) {
Expand Down Expand Up @@ -213,3 +221,17 @@ func TestInterconnectAttachmentsBgpAuthKey(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 @@ -45,6 +45,11 @@ type PartnerInterconnectAttachmentRegenerateServiceKey struct {
*godo.RegenerateServiceKey
}

// 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 @@ -56,6 +61,7 @@ type PartnerInterconnectAttachmentsService interface {
ListPartnerInterconnectAttachmentRoutes(iaID string) (PartnerInterconnectAttachmentRoutes, error)
GetBGPAuthKey(iaID string) (*PartnerInterconnectAttachmentBGPAuthKey, error)
RegenerateServiceKey(iaID string) (*PartnerInterconnectAttachmentRegenerateServiceKey, error)
GetServiceKey(iaID string) (*PartnerInterconnectAttachmentServiceKey, error)
}

var _ PartnerInterconnectAttachmentsService = &partnerInterconnectAttachmentsService{}
Expand Down Expand Up @@ -178,3 +184,12 @@ func (p *partnerInterconnectAttachmentsService) RegenerateServiceKey(iaID string
}
return &PartnerInterconnectAttachmentRegenerateServiceKey{RegenerateServiceKey: regenerateServiceKey}, 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
}
85 changes: 85 additions & 0 deletions integration/partner_interconnect_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ test-bgp-auth-key
"bgp_auth_key": {
"value": "test-bgp-auth-key"
}
}`
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"
}
}`
)

Expand Down Expand Up @@ -355,3 +368,75 @@ var _ = suite("partner_interconnect_attachments/get-bgp-auth-key", func(t *testi
})
})
})

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)))
})
})
})
Loading