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
26 changes: 14 additions & 12 deletions pkg/cmd/connection_upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,16 +501,14 @@ func (cu *connectionUpsertCmd) buildUpsertRequest(existing *hookdeck.Connection,
}
req.Source = sourceInput
} else if isUpdate && existing != nil && existing.Source != nil {
// Check if any source config fields are being updated
hasSourceConfigUpdate := cu.SourceWebhookSecret != "" || cu.SourceAPIKey != "" ||
hasSourceUpdate := cu.sourceDescription != "" || cu.SourceWebhookSecret != "" || cu.SourceAPIKey != "" ||
cu.SourceBasicAuthUser != "" || cu.SourceBasicAuthPass != "" ||
cu.SourceHMACSecret != "" || cu.SourceHMACAlgo != "" ||
cu.SourceAllowedHTTPMethods != "" || cu.SourceCustomResponseType != "" ||
cu.SourceCustomResponseBody != "" || cu.SourceConfig != "" || cu.SourceConfigFile != ""

if hasSourceConfigUpdate {
// For partial config updates, we need to send the full source object
// with the updated config merged in
if hasSourceUpdate {
// Merge the flags into the existing source and send it inline.
sourceInput, err := cu.buildSourceInputForUpdate(existing.Source)
if err != nil {
return nil, err
Expand Down Expand Up @@ -551,18 +549,16 @@ func (cu *connectionUpsertCmd) buildUpsertRequest(existing *hookdeck.Connection,
}
req.Destination = destinationInput
} else if isUpdate && existing != nil && existing.Destination != nil {
// Check if any destination config fields are being updated
hasDestinationConfigUpdate := cu.destinationURL != "" || cu.destinationCliPath != "" ||
hasDestinationUpdate := cu.destinationDescription != "" || cu.destinationURL != "" || cu.destinationCliPath != "" ||
cu.destinationPathForwardingDisabled != nil ||
cu.destinationHTTPMethod != "" ||
cu.DestinationRateLimit != 0 || cu.DestinationRateLimitPeriod != "" ||
cu.DestinationDeliveryGroupKey != "" || cu.DestinationDeliveryGroupRate != 0 ||
cu.DestinationDeliveryGroupRatePeriod != "" || cu.DestinationDeliveryGroupOverrides != "" ||
cu.DestinationAuthMethod != ""

if hasDestinationConfigUpdate {
// For partial config updates, we need to send the full destination object
// with the updated config merged in
if hasDestinationUpdate {
// Merge the flags into the existing destination and send it inline.
destinationInput, err := cu.buildDestinationInputForUpdate(existing.Destination)
if err != nil {
return nil, err
Expand Down Expand Up @@ -602,7 +598,7 @@ func (cu *connectionUpsertCmd) buildUpsertRequest(existing *hookdeck.Connection,
return req, nil
}

// buildSourceInputForUpdate builds a source input for partial config updates
// buildSourceInputForUpdate builds a source input for partial updates
// It merges the existing source config with any new flags provided
func (cu *connectionUpsertCmd) buildSourceInputForUpdate(existingSource *hookdeck.Source) (*hookdeck.SourceCreateInput, error) {
// Start with the existing source
Expand All @@ -611,6 +607,9 @@ func (cu *connectionUpsertCmd) buildSourceInputForUpdate(existingSource *hookdec
Type: existingSource.Type,
Description: existingSource.Description,
}
if cu.sourceDescription != "" {
input.Description = &cu.sourceDescription
}

// Get existing config or create new one
sourceConfig := make(map[string]interface{})
Expand Down Expand Up @@ -649,7 +648,7 @@ func (cu *connectionUpsertCmd) buildSourceInputForUpdate(existingSource *hookdec
return input, nil
}

// buildDestinationInputForUpdate builds a destination input for partial config updates
// buildDestinationInputForUpdate builds a destination input for partial updates
// It merges the existing destination config with any new flags provided
func (cu *connectionUpsertCmd) buildDestinationInputForUpdate(existingDest *hookdeck.Destination) (*hookdeck.DestinationCreateInput, error) {
// Start with the existing destination
Expand All @@ -658,6 +657,9 @@ func (cu *connectionUpsertCmd) buildDestinationInputForUpdate(existingDest *hook
Type: existingDest.Type,
Description: existingDest.Description,
}
if cu.destinationDescription != "" {
input.Description = &cu.destinationDescription
}

// Get existing config or create new one
destConfig := make(map[string]interface{})
Expand Down
34 changes: 34 additions & 0 deletions pkg/cmd/connection_upsert_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,37 @@ func TestConnectionUpsertInlineDestinationKeepsStoredSource(t *testing.T) {
require.True(t, ok, "the destination flags must still be sent inline")
assert.Equal(t, "my-api", destination["name"])
}

func TestConnectionUpsertEndpointDescriptions(t *testing.T) {
stored := storedUpsertConnection()
stored.Source.Description = strPtr("old source description")
stored.Destination.Description = strPtr("old destination description")

t.Run("updates descriptions", func(t *testing.T) {
body := captureConnectionUpsertBody(t, stored,
"gateway", "connection", "upsert", "my-conn",
"--source-description", "new source description",
"--destination-description", "new destination description",
)

source, sourceOK := body["source"].(map[string]interface{})
destination, destinationOK := body["destination"].(map[string]interface{})
assert.True(t, sourceOK, "the source must be sent inline")
assert.True(t, destinationOK, "the destination must be sent inline")
assert.Equal(t, "new source description", source["description"])
assert.Equal(t, "new destination description", destination["description"])
assert.NotContains(t, body, "source_id")
assert.NotContains(t, body, "destination_id")
})

t.Run("preserves IDs when omitted", func(t *testing.T) {
body := captureConnectionUpsertBody(t, stored,
"gateway", "connection", "upsert", "my-conn",
)

assert.Equal(t, "src_1", body["source_id"])
assert.Equal(t, "des_1", body["destination_id"])
assert.NotContains(t, body, "source")
assert.NotContains(t, body, "destination")
})
}