Skip to content
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
19 changes: 19 additions & 0 deletions doc/ovhcloud_cloud_managed-database_edit.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ There are two ways to define the edition parameters:

ovhcloud cloud managed-database edit <service_id> --editor --description "My database cluster"

Network update:

You can switch a database service between public and private networks without recreating it.

To switch from public to private network:

ovhcloud cloud managed-database edit <service_id> --network-id <private-network-uuid> --subnet-id <subnet-uuid>

To switch from private to public network:

ovhcloud cloud managed-database edit <service_id> --public-network

Note: --public-network is mutually exclusive with --network-id and --subnet-id.
Changing the network triggers a service rebuild. The service will be temporarily unavailable
during the transition.


```
ovhcloud cloud managed-database edit <service_id> [flags]
Expand All @@ -38,7 +54,10 @@ ovhcloud cloud managed-database edit <service_id> [flags]
-h, --help help for edit
--ip-restrictions strings IP blocks authorized to access the cluster (CIDR format)
--maintenance-time string Time on which maintenances can start every day
--network-id string Private network ID
--plan string Plan of the cluster
--public-network Switch the service to public network
--subnet-id string Private subnet ID
--version string Version of the engine deployed on the cluster
```

Expand Down
21 changes: 21 additions & 0 deletions internal/cmd/cloud_managed_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,22 @@ There are two ways to define the edition parameters:
Note that it is also possible to override values in the presented examples using command line flags like the following:

ovhcloud cloud managed-database edit <service_id> --editor --description "My database cluster"

Network update:

You can switch a database service between public and private networks without recreating it.

To switch from public to private network:

ovhcloud cloud managed-database edit <service_id> --network-id <private-network-uuid> --subnet-id <subnet-uuid>

To switch from private to public network:

ovhcloud cloud managed-database edit <service_id> --public-network

Note: --public-network is mutually exclusive with --network-id and --subnet-id.
Changing the network triggers a service rebuild. The service will be temporarily unavailable
during the transition.
`,
ValidArgsFunction: completion.CloudResources("/v1/cloud/project/%s/database"),
Run: cloud.EditManagedDatabase,
Expand All @@ -336,6 +352,11 @@ There are two ways to define the edition parameters:

// Network configuration
managedDatabaseEditCmd.Flags().StringSliceVar(&cloud.ManagedDatabaseSpec.CLIIPRestrictions, "ip-restrictions", nil, "IP blocks authorized to access the cluster (CIDR format)")
managedDatabaseEditCmd.Flags().StringVar(&cloud.ManagedDatabaseSpec.CLINetworkID, "network-id", "", "Private network ID")
managedDatabaseEditCmd.Flags().StringVar(&cloud.ManagedDatabaseSpec.CLISubnetID, "subnet-id", "", "Private subnet ID")
managedDatabaseEditCmd.Flags().BoolVar(&cloud.ManagedDatabaseSpec.CLIPublicNetwork, "public-network", false, "Switch the service to public network")
managedDatabaseEditCmd.MarkFlagsMutuallyExclusive("public-network", "network-id")
managedDatabaseEditCmd.MarkFlagsMutuallyExclusive("public-network", "subnet-id")

// Common flags for other mean to define parameters
addInteractiveEditorFlag(managedDatabaseEditCmd)
Expand Down
23 changes: 21 additions & 2 deletions internal/services/cloud/cloud_managed_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ var (
Engine string `json:"-"`
CLIIPRestrictions []string `json:"-"`
CLINodesList []string `json:"-"`
CLINetworkID string `json:"-"`
CLISubnetID string `json:"-"`
CLIPublicNetwork bool `json:"-"`
}

ManagedDatabaseDatabaseSpec struct {
Expand Down Expand Up @@ -236,13 +239,29 @@ func EditManagedDatabase(cmd *cobra.Command, args []string) {
ManagedDatabaseSpec.IPRestrictions = append(ManagedDatabaseSpec.IPRestrictions, managedDatabaseIPRestriction{IP: restriction})
}

// Edit resource
// Build extra network fields that bypass the OpenAPI filter
var networkFields map[string]any

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't get why you create an extra map of fields instead of using ManagedDatabaseSpec.CLIPublicNetwork to fill ManagedDatabaseSpec.NetworkID and ManagedDatabaseSpec.SubnetID ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The extraFields approach is needed for two reasons:

  1. OpenAPI filter strips networkId/subnetId — EditResource calls FilterEditableFields which removes any field not declared in the PUT schema. Even if we set ManagedDatabaseSpec.NetworkID and .SubnetID directly, they get filtered out before the PUT request is sent. The extraFields parameter injects them after the filter, in a single PUT call.
  2. null vs empty string — When switching to public network, the API expects "networkId": null. But NetworkID is a Go string with omitempty — an empty string is omitted from the JSON entirely, not serialized as null. The map[string]any with nil values is the only way to produce proper JSON null.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For your point 1), I don't see why the API-accepted fields would be dropped before the request is sent, we already do this kind of thing here: https://github.com/ovh/ovhcloud-cli/blob/main/internal/services/cloud/cloud_managed_database.go#L235

Anyway, your solution works even if I'm not a big fan of the code, so I'll merge it.

if ManagedDatabaseSpec.CLIPublicNetwork {
networkFields = map[string]any{
"networkId": nil,
"subnetId": nil,
}
} else if ManagedDatabaseSpec.CLINetworkID != "" {
networkFields = map[string]any{
"networkId": ManagedDatabaseSpec.CLINetworkID,
"subnetId": ManagedDatabaseSpec.CLISubnetID,
}
}

endpoint := fmt.Sprintf("/v1/cloud/project/%s/database/%s/%s", projectID, url.PathEscape(databaseService["engine"].(string)), url.PathEscape(args[0]))

if err := common.EditResource(
cmd,
fmt.Sprintf("/cloud/project/{serviceName}/database/%s/{clusterId}", url.PathEscape(databaseService["engine"].(string))),
fmt.Sprintf("/v1/cloud/project/%s/database/%s/%s", projectID, url.PathEscape(databaseService["engine"].(string)), url.PathEscape(args[0])),
endpoint,
ManagedDatabaseSpec,
assets.CloudOpenapiSchema,
networkFields,
); err != nil {
display.OutputError(&flags.OutputFormatConfig, "%s", err)
return
Expand Down
9 changes: 8 additions & 1 deletion internal/services/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func CreateResource(cmd *cobra.Command, path, endpoint, defaultExample string,
return createdResource, nil
}

func EditResource(cmd *cobra.Command, path, url string, cliParams any, openapiSpec []byte) error {
func EditResource(cmd *cobra.Command, path, url string, cliParams any, openapiSpec []byte, extraFields ...map[string]any) error {
if cmd.Flags().NFlag() == 0 {
display.OutputInfo(&flags.OutputFormatConfig, nil, "🟠 No parameters given, nothing to edit")
return nil
Expand Down Expand Up @@ -282,6 +282,13 @@ func EditResource(cmd *cobra.Command, path, url string, cliParams any, openapiSp
return fmt.Errorf("failed to extract writable properties: %w", err)
}

// Inject extra fields that bypass the OpenAPI filter
for _, extra := range extraFields {
for k, v := range extra {
editableBody[k] = v
}
}

// If editor not needed, update the resource directly
if !flags.ParametersViaEditor {
if err := httpLib.Client.Put(url, editableBody, nil); err != nil {
Expand Down
Loading