Skip to content

Conversation

shinny-chengzhi
Copy link
Collaborator

@shinny-chengzhi shinny-chengzhi commented Apr 18, 2025

Summary by CodeRabbit

  • 新功能
    • 路由表条目的 next_hub 字段现支持填写实例ID或网卡挂载ID,自动解析并关联到对应的主私有IP。
  • 修复
    • 优化了弹性公网IP(EIP)绑定网卡时的状态判断,提升了兼容性和稳定性。

Copy link

coderabbitai bot commented Apr 18, 2025

📝 Walkthrough

Walkthrough

本次更改主要集中在腾讯云相关的 Terraform 资源的实现中。对弹性公网 IP(EIP)绑定状态的判断条件进行了扩展,允许更多的绑定状态被视为成功。同时,路由表条目资源的 next_hub 字段逻辑被增强,现在支持既可以是实例 ID,也可以是网卡附加 ID,并实现了在创建和读取时根据实际情况进行解析和映射的机制。相关的查询和错误处理逻辑也相应调整,以适配新的字段解析方式。

Changes

文件/路径 变更摘要
tencentcloud/resource_tc_eip_association.go 扩展了 EIP 绑定状态的判断条件,新增支持 EIP_STATUS_BIND 作为成功状态,影响 EIP 绑定后的重试逻辑。
tencentcloud/resource_tc_route_table_entry.go 增强 next_hub 字段逻辑,支持实例 ID 和网卡附加 ID,增加了相应的解析、查询和反向映射处理及错误处理。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Terraform
    participant TencentCloudAPI

    User->>Terraform: 配置 route_table_entry (next_hub)
    Terraform->>TencentCloudAPI: 查询 next_hub(实例ID或网卡ID+附加ID)
    alt next_hub 是网卡+附加ID
        TencentCloudAPI->>Terraform: 返回主私有IP
    else next_hub 是实例ID
        TencentCloudAPI->>Terraform: 返回实例私有IP
    end
    Terraform->>TencentCloudAPI: 创建路由表条目(使用私有IP)
    TencentCloudAPI-->>Terraform: 返回创建结果
    Terraform-->>User: 路由表条目创建完成
Loading

Poem

兔子跳跃云端间,
绑定 EIP 状态添,
路由 next_hub 新解读,
实例网卡皆可选。
云路畅通无阻碍,
代码更新更灵巧,
兔耳随风乐逍遥! 🐰✨

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.

Warning

Review ran into problems

🔥 Problems

Errors were encountered while retrieving linked issues.

Errors (1)
  • JIRA integration encountered authorization issues. Please disconnect and reconnect the integration in the CodeRabbit UI.
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🔭 Outside diff range comments (1)
tencentcloud/resource_tc_route_table_entry.go (1)

266-317: ⚠️ Potential issue

字段名拼写错误导致编译失败

新增的读取逻辑多次使用了 v.nextBub 字段(276、294、312 行),而此前结构体中应为 nextHub(或其它正确拼写)。若代码中确无 nextBub 字段,将直接引发编译错误。请确认并统一字段命名。

-        filter["private-ip-address"] = v.nextBub
+        filter["private-ip-address"] = v.nextHub
...
-        interfaces, errRet = vpcService.DescribeEniByFilters(ctx, &info.vpcId, nil, nil, nil, nil, nil, &v.nextBub, nil)
+        interfaces, errRet = vpcService.DescribeEniByFilters(ctx, &info.vpcId, nil, nil, nil, nil, nil, &v.nextHub, nil)
...
-        return resource.NonRetryableError(fmt.Errorf("cannot find exact instance by ip %s in vpc %s", v.nextBub, info.vpcId))
+        return resource.NonRetryableError(fmt.Errorf("cannot find exact instance by ip %s in vpc %s", v.nextHub, info.vpcId))
🧹 Nitpick comments (1)
tencentcloud/resource_tc_route_table_entry.go (1)

161-170: eniUsed 语义不清易读性一般

strings.Cut 返回的第三个布尔值含义是“是否切割成功”,这里代表 存在 “+” 即 ENI 场景。用 eniUsed 容易让人误解为 “是否使用 ENI”,但其实际含义恰好相反 —— false 表示实例 ID。建议改名为 isEnihasAttachment, 并在后续分支中正向使用,可提升可读性与后期维护。

-        eniId, _, eniUsed := strings.Cut(nextHub, "+")
+        eniId, _, isEni := strings.Cut(nextHub, "+")
...
-        if !eniUsed {
+        if !isEni {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 13b4e3c and 1c00865.

📒 Files selected for processing (2)
  • tencentcloud/resource_tc_eip_association.go (1 hunks)
  • tencentcloud/resource_tc_route_table_entry.go (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tencentcloud/resource_tc_eip_association.go (1)
tencentcloud/extension_vpc.go (2)
  • EIP_STATUS_BIND_ENI (45-45)
  • EIP_STATUS_BIND (41-41)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: docs
  • GitHub Check: golangci-lint
  • GitHub Check: tfprovider-lint
  • GitHub Check: unit-tests

Comment on lines +190 to 205
if len(instances) == 1 {
nextHub = *(instances[0].PrivateIpAddresses[0]) // 此处为实例主网卡的第一个ip,当前cdk主网卡只有一个内网ip
} else if len(interfaces) == 1 {
for _, ip := range interfaces[0].PrivateIpAddressSet {
if ip.Primary != nil && *ip.Primary && ip.PrivateIpAddress != nil {
nextHub = *ip.PrivateIpAddress
interfaces = nil
break
}
}
if interfaces != nil {
return fmt.Errorf("cannot find primary ip address of interface %s", nextHub)
}
} else {
return fmt.Errorf("cannot find exact instance by id %s", nextHub)
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

通过将 interfaces 置为 nil 标记逻辑不直观

在找到主 IP 后通过 interfaces = nil 来区分是否命中,随后再用 interfaces != nil 判断会产生“写 ‑ 判”耦合,阅读成本高,也容易在后续修改时踩坑。建议改为显式布尔标志:

-            for _, ip := range interfaces[0].PrivateIpAddressSet {
-                if ip.Primary != nil && *ip.Primary && ip.PrivateIpAddress != nil {
-                    nextHub = *ip.PrivateIpAddress
-                    interfaces = nil
-                    break
-                }
-            }
-            if interfaces != nil {
+            foundPrimary := false
+            for _, ip := range interfaces[0].PrivateIpAddressSet {
+                if ip.Primary != nil && *ip.Primary && ip.PrivateIpAddress != nil {
+                    nextHub = *ip.PrivateIpAddress
+                    foundPrimary = true
+                    break
+                }
+            }
+            if !foundPrimary {

这样既直观又避免对同一变量承担两种含义。

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if len(instances) == 1 {
nextHub = *(instances[0].PrivateIpAddresses[0]) // 此处为实例主网卡的第一个ip,当前cdk主网卡只有一个内网ip
} else if len(interfaces) == 1 {
for _, ip := range interfaces[0].PrivateIpAddressSet {
if ip.Primary != nil && *ip.Primary && ip.PrivateIpAddress != nil {
nextHub = *ip.PrivateIpAddress
interfaces = nil
break
}
}
if interfaces != nil {
return fmt.Errorf("cannot find primary ip address of interface %s", nextHub)
}
} else {
return fmt.Errorf("cannot find exact instance by id %s", nextHub)
}
if len(instances) == 1 {
nextHub = *(instances[0].PrivateIpAddresses[0]) // 此处为实例主网卡的第一个ip,当前cdk主网卡只有一个内网ip
} else if len(interfaces) == 1 {
- for _, ip := range interfaces[0].PrivateIpAddressSet {
- if ip.Primary != nil && *ip.Primary && ip.PrivateIpAddress != nil {
- nextHub = *ip.PrivateIpAddress
- interfaces = nil
- break
- }
- }
- if interfaces != nil {
- return fmt.Errorf("cannot find primary ip address of interface %s", nextHub)
- }
+ foundPrimary := false
+ for _, ip := range interfaces[0].PrivateIpAddressSet {
+ if ip.Primary != nil && *ip.Primary && ip.PrivateIpAddress != nil {
+ nextHub = *ip.PrivateIpAddress
+ foundPrimary = true
+ break
+ }
+ }
+ if !foundPrimary {
+ return fmt.Errorf("cannot find primary ip address of interface %s", nextHub)
+ }
} else {
return fmt.Errorf("cannot find exact instance by id %s", nextHub)
}

@shinny-chengzhi shinny-chengzhi merged commit 1c00865 into master May 8, 2025
4 of 6 checks passed
@shinny-chengzhi shinny-chengzhi deleted the route-entry-add-nic-support branch May 8, 2025 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants