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
3 changes: 3 additions & 0 deletions .changelog/4325.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_mongodb_instance: support `cpu` parameter to manage the CPU core count during instance configuration changes
```
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.3.105
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.1033
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.3.131
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.132
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.133
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/config v1.3.80
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.3.130
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp v1.3.30
Expand All @@ -69,7 +69,7 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.3.108
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/live v1.3.95
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb v1.3.102
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.3.103
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.3.133
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.3.101
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mps v1.3.45
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.3.110
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,6 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.93/go.mod h
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.95/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.101/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.102/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.103/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.104/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.105/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.107/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
Expand All @@ -1004,8 +1003,9 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.125/go.mod
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.127/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.130/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.131/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.132 h1:42RfJnwzMprfKEJrM9fyhHu1jXyg8xvSJ1A1qb9Qun8=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.132/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.133 h1:EKJsaGHbifEKqWCdJ4tJNLxbJ5h/Pm3CaLKzcCS/pkw=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.3.133/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/config v1.3.80 h1:chnsNBeJn3MieFLki4hpbzoml5NiTvLVzOTqYRVxQho=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/config v1.3.80/go.mod h1:B/ezGtlvDhZlleNM+2QdvZccrUi+J+fN6vMnDDsZnuM=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/controlcenter v1.1.51 h1:pGwrfCBBCt1u+EDHwfNj9NLQpvk5MVKVMcsE7SvwqM4=
Expand Down Expand Up @@ -1065,8 +1065,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/live v1.3.95 h1:rWiKud6
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/live v1.3.95/go.mod h1:uDGk0Bi0diOd1VaQPnfZO0MLKPKjqIT4Hi0sdanwD9s=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb v1.3.102 h1:I4QaWjVMOfuEsmxjTS8BxYgLcgdATWIAGutcHDnIvB4=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb v1.3.102/go.mod h1:Thfxer0Xf7Oq//ceL5i5nObFEXyQkUxzLHVNFh3p7jw=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.3.103 h1:EQTALkZayL6KrXCcx0yjJ3ksl75dFNFJWhTnSUxaYds=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.3.103/go.mod h1:0CcAKrx6oYC/WfuN04wsNtQ77dLgMv74in3kY5nwqgE=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.3.133 h1:J/x518dKdFwHa7KatmCWNw7/daJuf7yw+1bov8Du3d0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.3.133/go.mod h1:MloftTaY0U0CNcDc2PkbNw7pGwE089AadY4tJ1bc5hM=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.3.101 h1:r8PjkCuXrztjLyBROWTww0RJUq+a+l5ybPLl6GGlov8=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.3.101/go.mod h1:+cX4PEEehIIbyHy9plpXOTShlFWtc9OC1AWcmOrOMT8=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mps v1.3.45 h1:35PD/GXaOl3QhL1fI5a3Q+jcenzR1PqfuGxltOeO48w=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-07-21
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## Context

`tencentcloud_mongodb_instance` 是腾讯云 MongoDB 实例的通用资源(RESOURCE_KIND_GENERAL),管理实例整个生命周期。当前资源通过 `MongodbService.UpgradeInstance` 方法(封装 `ModifyDBInstanceSpec` 接口)支持 `memory`、`volume`、`node_num`、`add_node_list`、`remove_node_list`、`in_maintenance` 等参数的变配,但未暴露 `cpu` 参数。

腾讯云 MongoDB `ModifyDBInstanceSpec` 接口已支持 `Cpu` 入参(`*int64`,单位:C),允许在变更实例规格时指定变更后的 CPU 大小;`DescribeDBInstances` 返回的 `InstanceDetail.CpuNum`(`*uint64`)字段记录实例当前 CPU 核数。本次变更将 `cpu` 参数接入 Terraform 资源,补全实例规格变更能力。

约束:
- 资源 schema 已存在且已发布,必须保持向后兼容,只能新增 `Optional` + `Computed` 字段。
- `ModifyDBInstanceSpecRequest.Cpu` 为 `*int64` 类型;`InstanceDetail.CpuNum` 为 `*uint64` 类型,需注意类型转换。
- 变配操作可能返回订单 ID,需通过 `DescribeDBInstanceDeal` 轮询订单状态确认完成(现有逻辑已实现,本次复用)。

## Goals / Non-Goals

**Goals:**
- 为 `tencentcloud_mongodb_instance` 资源新增 `cpu` 参数(`Optional` + `Computed`,`TypeInt`)。
- 在 Update 流程中,当 `cpu` 发生变化时,将 `cpu` 传递给 `ModifyDBInstanceSpec` 接口的 `Cpu` 字段完成变配。
- 在 Read 流程中,从 `DescribeDBInstances` 返回的 `InstanceDetail.CpuNum` 回填 `cpu` 到 state。
- 补充基于 gomonkey 的单元测试,覆盖 `cpu` 变更触发变配与回填逻辑。

**Non-Goals:**
- 不修改 Create 流程:`CreateDBInstanceHour`/`CreateDBInstance` 创建时使用的是 `CpuCore` 字段(非本次需求范围内的 `Cpu`),且本次需求仅针对 `ModifyDBInstanceSpec` 的 `Cpu` 入参,不扩展到创建参数。
- 不修改 `mongos_cpu` 等已有分片集群相关 CPU 参数。
- 不修改已发布字段的 schema 行为(不新增 `ForceNew`,不改变 `Required`/`Optional` 语义)。
- 不执行 `make doc`、`gofmt`、`.changelog` 等收尾操作(由 tfpacer-finalize 阶段统一处理)。

## Decisions

### 决策 1:`cpu` 字段定义为 `Optional` + `Computed`,不设置 `ForceNew`

**理由**:`cpu` 变化通过 in-place update(调用 `ModifyDBInstanceSpec`)完成,无需重建实例。设为 `Computed` 使 Read 能从云上回填实际 CPU 值,避免用户未指定时 state 与云上不一致。与已有 `memory`(`Required`)、`volume`(`Required`)不同,`cpu` 为可选参数(云 API 注释说明为空值时默认取实例当前 CPU 大小),因此设为 `Optional` + `Computed` 更贴合 API 语义且向后兼容。

**备选方案**:将 `cpu` 设为 `Required`。否决,因为会破坏向后兼容(已有配置未设置 `cpu`)且与 API 语义不符。

### 决策 2:Update 触发条件扩展为包含 `cpu`

**理由**:现有触发条件为 `d.HasChange("memory") || d.HasChange("volume") || d.HasChange("node_num")`,新增 `d.HasChange("cpu")`。当用户单独修改 `cpu` 时也应触发变配。`cpu` 通过现有 `params map[string]interface{}` 传递到 `UpgradeInstance`,复用现有 `in_maintenance`/`node_num` 的传参模式,最小改动。

**备选方案**:为 `cpu` 单独创建一个 `ModifyDBInstanceSpec` 调用分支。否决,会引入重复的订单轮询与错误处理逻辑,违背 DRY,且云 API `ModifyDBInstanceSpec` 本身支持同时变更多个规格参数。

### 决策 3:服务层 `UpgradeInstance` 新增 `params["cpu"]` 映射到 `request.Cpu`

**理由**:`ModifyDBInstanceSpecRequest.Cpu` 为 `*int64`,使用 `helper.Int64(v.(int))` 构造指针。与现有 `params["in_maintenance"]` 等使用 `helper.IntUint64` 不同,需按字段实际类型 `int64` 使用 `helper.Int64`,避免类型不匹配。

### 决策 4:Read 回填使用 `instance.CpuNum`(`*uint64`)转为 `int`

**理由**:`DescribeDBInstances` 返回的 `InstanceDetail.CpuNum` 即实例 CPU 核数。在 Read 中先判断 `instance.CpuNum != nil` 再 `d.Set("cpu", int(*instance.CpuNum))`,遵循项目规范「调用 setXX 前先判断字段是否为 nil」。不将 `CpuNum` 加入 `CheckNil` 强校验列表,避免对未返回该字段的存量实例造成 Read 失败。

## Risks / Trade-offs

- **[风险] `CpuNum` 为 `*uint64`,`Cpu` 为 `*int64`,类型转换溢出** → 实际 CPU 核数为正小整数,不会溢出;转换使用 `int()` 显式转换,安全。
- **[风险] 用户单独修改 `cpu` 而不修改 `memory`/`volume` 时,云 API 是否允许** → 云 API 注释明确支持单独变更 CPU("变更 mongod 或 mongos 的 CPU 与内存规格时,NodeNum 可以不配置"),且 `Cpu` 注释说明为空时取当前值,语义允许单独变更。具体可用规格由 `DescribeSpecInfo` 约束,由云 API 校验,Provider 不做额外校验。
- **[权衡] 未将 `CpuNum` 加入 `CheckNil` 强校验** → 避免对未返回该字段的存量实例造成 Read 失败;回填前单独判 nil,安全且向后兼容。
- **[风险] 既有 `UpgradeInstance` 复用同一订单轮询逻辑** → 不新增轮询逻辑,沿用现有 `DescribeDBInstanceDeal` 轮询,行为一致。
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## Why

`tencentcloud_mongodb_instance` 资源当前支持通过 `memory`、`volume`、`node_num` 等参数调整实例配置(调用 `ModifyDBInstanceSpec` 接口),但缺少对 CPU 核数的管理。腾讯云 MongoDB `ModifyDBInstanceSpec` 接口已支持 `Cpu` 入参,允许在变更实例规格时指定变更后的 CPU 大小(单位:C)。本次变更新增 `cpu` 参数,使用户能够通过 Terraform 管理 MongoDB 实例的 CPU 规格,补全实例配置变更能力。

## What Changes

- 在 `tencentcloud_mongodb_instance` 资源 schema 中新增 `cpu` 参数(`Optional` + `Computed`,`TypeInt`),用于指定实例变更后的 CPU 核数。
- 在资源 Update 流程中,将 `cpu` 加入 `ModifyDBInstanceSpec` 的触发条件,当 `cpu` 发生变化时(与 `memory`、`volume`、`node_num` 等一起或单独)调用 `ModifyDBInstanceSpec` 接口完成变配。
- 在 `MongodbService.UpgradeInstance` 服务方法中,将 `params["cpu"]` 映射到 `ModifyDBInstanceSpecRequest.Cpu` 字段(`*int64`)。
- 在资源 Read 流程中,通过 `DescribeDBInstances` 返回的 `InstanceDetail.CpuNum` 字段回填 `cpu` 参数到 state。
- 补充 `resource_tc_mongodb_instance_test.go` 的单元测试用例(使用 gomonkey mock 云 API)。
- 更新 `resource_tc_mongodb_instance.md` 资源文档。

## Capabilities

### New Capabilities
- `mongodb-instance-cpu`: 为 `tencentcloud_mongodb_instance` 资源新增 `cpu` 参数,支持在实例配置变更时设置 CPU 核数,并通过查询接口回填 CPU 状态。

### Modified Capabilities
<!-- 无需修改现有 spec 级别的需求 -->

## Impact

### 受影响的代码
- `tencentcloud/services/mongodb/resource_tc_mongodb_instance.go` - schema 新增 `cpu` 字段、Read 回填、Update 触发与传参
- `tencentcloud/services/mongodb/service_tencentcloud_mongodb.go` - `UpgradeInstance` 方法新增 `cpu` 参数映射到 `ModifyDBInstanceSpecRequest.Cpu`
- `tencentcloud/services/mongodb/resource_tc_mongodb_instance_test.go` - 新增/补充单元测试
- `tencentcloud/services/mongodb/resource_tc_mongodb_instance.md` - 文档更新

### 受影响的 API
- `ModifyDBInstanceSpec`(变配时设置 `Cpu` 入参,`*int64`)
- `DescribeDBInstances`(读取时从 `InstanceDetail.CpuNum` 回填,`*uint64`)

### 向后兼容性
- ✅ 完全向后兼容:仅新增 `Optional` + `Computed` 字段,不修改已有 schema,不影响现有 TF 配置和 state。
- ✅ 不设置 `ForceNew`,`cpu` 变化通过 in-place update 处理。

### 依赖关系
- 无新增依赖,复用现有 `ModifyDBInstanceSpec` 与 `DescribeDBInstances` 接口及现有 `UpgradeInstance` 服务方法。
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
## ADDED Requirements

### Requirement: MongoDB Instance CPU Parameter
The `tencentcloud_mongodb_instance` resource SHALL provide a `cpu` schema field to manage the instance CPU core count, allowing users to change the CPU specification of a running MongoDB instance through in-place update via the `ModifyDBInstanceSpec` API.

#### Scenario: Schema defines optional and computed cpu field
- **GIVEN** the `tencentcloud_mongodb_instance` resource schema definition
- **THEN** `cpu` SHALL be a field of type `schema.TypeInt`
- **AND** `cpu` SHALL have `Optional: true` and `Computed: true`
- **AND** `cpu` SHALL NOT have `ForceNew` set (changes are applied via in-place update)
- **AND** the field description SHALL document that the unit is C and that the supported CPU specifications can be obtained via the `DescribeSpecInfo` API

#### Scenario: Update triggers ModifyDBInstanceSpec when cpu changes
- **GIVEN** an existing `tencentcloud_mongodb_instance` resource
- **WHEN** the user updates only the `cpu` field (without changing `memory`, `volume`, or `node_num`)
- **THEN** the system SHALL detect `d.HasChange("cpu")` and enter the spec-modification branch
- **AND** the system SHALL read the current `cpu` value from the schema
- **AND** the system SHALL pass `cpu` into the params map of `MongodbService.UpgradeInstance`
- **AND** the system SHALL call the `ModifyDBInstanceSpec` API with the `Cpu` request field set to the new CPU value
- **AND** the system SHALL poll the returned deal id via `DescribeDBInstanceDeal` until the deal succeeds (when `in_maintenance == 0`)
- **AND** the resource state SHALL be updated after a successful modification

#### Scenario: Update triggers ModifyDBInstanceSpec when cpu changes together with other spec params
- **GIVEN** an existing `tencentcloud_mongodb_instance` resource
- **WHEN** the user updates `cpu` together with `memory`, `volume`, or `node_num`
- **THEN** the system SHALL include the `cpu` value alongside the other changed spec parameters in a single `ModifyDBInstanceSpec` call
- **AND** the `Cpu` request field SHALL be set from the user-specified `cpu` value

#### Scenario: cpu omitted in update keeps existing behavior
- **GIVEN** an existing `tencentcloud_mongodb_instance` resource where `cpu` was not configured
- **WHEN** the user updates fields unrelated to spec modification (e.g., tags, project_id)
- **THEN** the system SHALL NOT call `ModifyDBInstanceSpec`
- **AND** the `cpu` field SHALL remain unset in the schema (populated only via Read)

#### Scenario: Service layer maps cpu to ModifyDBInstanceSpecRequest.Cpu
- **GIVEN** the `MongodbService.UpgradeInstance` service method
- **WHEN** `params["cpu"]` is present with an integer value
- **THEN** the system SHALL set `request.Cpu` to a `*int64` pointer constructed from the integer value
- **AND** the type conversion SHALL use the `int64` helper matching the `ModifyDBInstanceSpecRequest.Cpu` field type
- **WHEN** `params["cpu"]` is not present
- **THEN** the system SHALL leave `request.Cpu` unset (nil) so the cloud API defaults to the current CPU size

### Requirement: MongoDB Instance CPU Read Backfill
The resource Read operation SHALL populate the `cpu` field from the cloud API response so that Terraform state reflects the actual CPU core count of the instance.

#### Scenario: Read populates cpu from DescribeDBInstances
- **GIVEN** an existing `tencentcloud_mongodb_instance` resource
- **WHEN** the Read operation queries the instance via `DescribeDBInstances` (through `DescribeInstanceById`)
- **AND** the returned `InstanceDetail.CpuNum` is not nil
- **THEN** the system SHALL set the `cpu` field in state to `int(*instance.CpuNum)`

#### Scenario: Read handles nil CpuNum gracefully
- **GIVEN** an existing `tencentcloud_mongodb_instance` resource
- **WHEN** the Read operation queries the instance
- **AND** the returned `InstanceDetail.CpuNum` is nil
- **THEN** the system SHALL NOT call `d.Set("cpu", ...)` (skip setting the field)
- **AND** the Read operation SHALL NOT fail solely because `CpuNum` is nil

#### Scenario: CpuNum not added to mandatory nil check
- **GIVEN** the Read operation's `CheckNil` validation list
- **THEN** `CpuNum` SHALL NOT be added to the mandatory nil-check map
- **AND** existing instances that do not return `CpuNum` SHALL still be readable without error

### Requirement: CPU Parameter Testing
The resource SHALL have unit tests using gomonkey mocks (not Terraform test suites) covering the new `cpu` parameter behavior, runnable via `go test` with `-gcflags=all=-l`.

#### Scenario: Unit test for cpu change triggering spec modification
- **GIVEN** the `tencentcloud_mongodb_instance` resource update function
- **WHEN** a unit test simulates a `cpu` change in the schema
- **THEN** the test SHALL mock the `ModifyDBInstanceSpec` and `DescribeDBInstanceDeal` cloud APIs using gomonkey
- **AND** verify that the update flow calls `ModifyDBInstanceSpec` with the expected `Cpu` value
- **AND** verify the update completes without error

#### Scenario: Unit test for cpu read backfill
- **GIVEN** the `tencentcloud_mongodb_instance` resource read function
- **WHEN** a unit test mocks `DescribeDBInstances` to return an instance with `CpuNum` set
- **THEN** the test SHALL verify that `d.Set("cpu", ...)` is called with the correct CPU value
- **AND** verify the read completes without error

#### Scenario: Unit test for nil CpuNum read
- **GIVEN** the `tencentcloud_mongodb_instance` resource read function
- **WHEN** a unit test mocks `DescribeDBInstances` to return an instance with `CpuNum` nil
- **THEN** the test SHALL verify the read operation does not fail
- **AND** verify the `cpu` field is not set

### Requirement: CPU Parameter Documentation
The resource documentation SHALL be updated to describe the new `cpu` parameter, generated via the `make doc` command during the finalize phase.

#### Scenario: cpu field documented
- **GIVEN** the resource documentation file `resource_tc_mongodb_instance.md`
- **THEN** the documentation SHALL include the `cpu` parameter description
- **AND** the description SHALL state the unit is C and that supported specifications can be obtained via the `DescribeSpecInfo` API
- **AND** the documentation SHALL NOT include manual `Argument Reference` or `Attribute Reference` sections (auto-generated)

#### Scenario: Example usage includes cpu
- **GIVEN** the resource documentation
- **THEN** the example usage SHALL demonstrate specifying the `cpu` parameter in an update scenario
Loading
Loading