Skip to content

Commit

Permalink
feat/csip
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenEarth committed Feb 5, 2024
2 parents 5edd558 + bea7f20 commit 3f0f423
Show file tree
Hide file tree
Showing 54 changed files with 9,080 additions and 96 deletions.
3 changes: 3 additions & 0 deletions .changelog/2502.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_mysql_instance: Optimize the availability zone problem when modifying the configuration after active/standby switchover.
```
4 changes: 4 additions & 0 deletions .changelog/2509.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```release-note:enhancement
resource/tencentcloud_instance: fix private ip release problem
```

3 changes: 3 additions & 0 deletions .changelog/2512.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_clb_attachments: support param `eni_ip`
```
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 1.81.72 (January 30, 2024)

ENHANCEMENTS:

* resource/tencentcloud_ccn_attachment: support tke cluster addon modify ([#2507](https://github.com/tencentcloudstack/terraform-provider-tencentcloud/pull/2507))
* resource/tencentcloud_instance: fix private ip release problem ([#2509](https://github.com/tencentcloudstack/terraform-provider-tencentcloud/pull/2509))
* resource/tencentcloud_mysql_instance: Optimize the availability zone problem when modifying the configuration after active/standby switchover. ([#2502](https://github.com/tencentcloudstack/terraform-provider-tencentcloud/pull/2502))

## 1.81.71 (January 26, 2024)

ENHANCEMENTS:
Expand Down
5 changes: 3 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.0.847
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.544
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.711
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.853
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.857
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.624
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp v1.0.762
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.692
Expand Down Expand Up @@ -107,8 +107,9 @@ require (

require (
github.com/hashicorp/go-uuid v1.0.3
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing v1.0.856
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdwpg v1.0.772
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.853
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.857
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dasb v1.0.798
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/oceanus v1.0.831
github.com/wI2L/jsondiff v0.3.0
Expand Down
15 changes: 8 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/bi v1.0.770 h1:5FWt9F+a
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/bi v1.0.770/go.mod h1:zuWcAJVIXbHbOR7i6SKK0wNlvPLt5Su4ijOZGMJbUi8=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/bi v1.0.824 h1:DVKvZ6h+qd7tadUrCjVAkCCmE3TsbK2ZmwGd3AJcpWc=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/bi v1.0.824/go.mod h1:DvBpDX/qdJG4KKLeULmRvhAjPYiw8za0HeTSu2y/lFw=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing v1.0.856 h1:zJBb69FBBNZko4PbDScqj9wogPatzNYEXAJ862IBoMQ=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/billing v1.0.856/go.mod h1:P+VGibeVh8pW4N85x9ee0xzXsZoT9jzjHYD8GaAiyG4=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.0.760 h1:Ky9dRsTL2HXKWUrTFpQFZWQ1TrM+o+P35kczR7thalo=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam v1.0.760/go.mod h1:AAfdrxknvUedvigxbbzKQLxN+1EG5NPbytpiqmfuFvU=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cat v1.0.760 h1:oGY4IigfIw0iQKh3/cOY29KBeEeFbvJft69e0beyfdI=
Expand Down Expand Up @@ -954,14 +956,15 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.845 h1:fiiV
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.845/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.847 h1:ITZmxAWfbr5yikJ4T30yVYMW3jpa/oTmNbPnw/h1Vq0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.847/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.849 h1:ieRAJU2lnAcaDK25W29C9R7iyR5IR4E+5DujRfu5wls=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.849/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.853 h1:TNYjF1jDLLNTirAkq7zRT9iF9xC2ZjgwpXsVSEBQvgQ=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.853/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.849 h1:Q48wD8VMYTjasH0yAqCIoSkiIwtge3Gzem5pmJx9h54=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.849/go.mod h1:lTyS8mn+Au4aWFFHFsTmyAFYW5mxylw366QBJ0040eU=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.856 h1:4PaaKxPjh0dmRzzz0CRZSZlbvzQIPcg4TE1Ibz0Cdlk=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.856/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.857 h1:6TxCHz3zSG67cLDDu8w3TkFm/FwiyiAWBhcidGXWH/0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.857/go.mod h1:r5r4xbfxSaeR04b166HGsBa/R4U3SueirEUpXGuw+Q0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.853 h1:Ofgedv4BWvDYX1Ff/5H2wYShan8mC1OtaHQJq+rG0pE=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.853/go.mod h1:iDYmWmDEfR74RzWU5kO1UWFsWu/c0pMtEtnoWRMvaJQ=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.857 h1:ANnIsWqF7OjfH0Bvnnj3zniFs/7JAhHKaRQTJo1pIiA=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/csip v1.0.857/go.mod h1:d+OvJbz/yKr64+Scu00s/PoHtmnJBaOcFLom79zJ0jg=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.624 h1:nEZqsoqt1pEoaP9JjkHQy3/H00suCfzlHW1qOm2nYD8=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.624/go.mod h1:+TXSVyeKwt1IhZRqKPbTREteBcP+K07Q846/ilNzLWA=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp v1.0.762 h1:2egy69SP/wPsmnfozcQVZ6tUY6F6N/TpEe/7xtXrc/8=
Expand Down Expand Up @@ -1004,8 +1007,6 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb v1.0.672 h1:sR3
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb v1.0.672/go.mod h1:o3rBKhtfIJfw4BG22M0CQVLQAc0WqIsbyRI/EW52Ka0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.0.651 h1:Ohb58H6gIlTcd73CF+goUlEdNJ2CYkYUHTYxV/M4UL4=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.0.651/go.mod h1:HLqsSgUnwgkE+XecQajekinjz1B7S+1U1J66puH4jpQ=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.839 h1:eRIkvwwyw7Guh3Ayr3J5tKSWfNX9WQBQ5zzNpBYoBHQ=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.839/go.mod h1:4OoBA087NHVdyeRJFy/SML7kIJmt54DtNkNlJWZp8oE=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.844 h1:ZR7FSnv4k1j0BpJHg6JDbJ6YJ2YfLhX2xVIUcuweKm4=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.844/go.mod h1:a7PfDWl0B3QV+g99asQHumDNScEEMl+7lkXP0KXt8Xg=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mps v1.0.777 h1:SoH/KkqkEUw8iDmQDZCw5Saf319Ceyt3v8Vqodo8DiY=
Expand Down
185 changes: 185 additions & 0 deletions tencentcloud/common/cloud_common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package common

import (
"encoding/json"
"log"
"strconv"
"strings"
"time"

cls "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls/v20201016"

cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/connectivity"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)

const (
// DefaultSearchLogStartTimestamp sync logs start time 2023-11-07 16:41:00
DefaultSearchLogStartTimestamp = 1699346460000

DefaultTopicId = "aef50d54-b17d-4782-8618-a7873203ec29"

QueryGrammarRule = " AND "
)

// ResourceAccountInfo 资源账户信息
type ResourceAccountInfo struct {
ResourceType string // 资源类型
ResourceName string // 资源名称
AccountId string // 主账号ID
PrincipalId string // 用户ID
UserName string // 用户名
}

// GetResourceCreatorAccountInfo get resource creator user info
func GetResourceCreatorAccountInfo(client *connectivity.TencentCloudClient, resourceCreateAction string, resources []*ResourceInstance) map[string]*ResourceAccountInfo {
resourceIdToSubAccountInfoMap := make(map[string]*ResourceAccountInfo)
if resourceCreateAction == "" {
return resourceIdToSubAccountInfoMap
}

request := cls.NewSearchLogRequest()
request.From = helper.IntInt64(DefaultSearchLogStartTimestamp)
request.To = helper.Int64(CurrentTimeMillisecond())
request.TopicId = helper.String(DefaultTopicId)

for _, r := range resources {
query := resourceCreateAction + QueryGrammarRule
if r.Id != "" {
query = query + r.Id
} else if r.Name != "" {
query = query + r.Name
} else {
continue
}
request.Query = helper.String(query)

response, err := client.UseClsClient().SearchLog(request)
if err != nil {
log.Printf("[CRITAL] search resource[%v] log data error: %v", r.Id, err.Error())
return resourceIdToSubAccountInfoMap
}
if response == nil || response.Response == nil {
log.Printf("[CRITAL] search resource[%v] log data response is nil", r.Id)
return resourceIdToSubAccountInfoMap
}
if len(response.Response.Results) == 0 {
log.Printf("[CRITAL] search resource[%v] log data response results is empty", r.Id)
return resourceIdToSubAccountInfoMap
}

result := response.Response.Results[0]
if result != nil {
var jsonData string
if len(*result.LogJson) > 2 {
jsonData = *result.LogJson
} else if len(*result.RawLog) > 2 {
jsonData = *result.RawLog
} else {
continue
}

resourceAccountInfo := ParseLogJsonData(jsonData)
if resourceAccountInfo.PrincipalId == resourceAccountInfo.UserName &&
resourceAccountInfo.PrincipalId != resourceAccountInfo.AccountId {
userName := GetSubAccountUserName(client, resourceAccountInfo.PrincipalId)
resourceAccountInfo.UserName = userName
}
resourceIdToSubAccountInfoMap[r.Id] = resourceAccountInfo
}
}

return resourceIdToSubAccountInfoMap
}

// GetSubAccountUserName get sub account user name
func GetSubAccountUserName(client *connectivity.TencentCloudClient, uin string) string {
uinNum, err := strconv.ParseUint(uin, 10, 64)
if err != nil {
log.Printf("[CRITAL] parse uin[%v] to uint64 type error: %v", uin, err.Error())
return ""
}

request := cam.NewDescribeSubAccountsRequest()

uinArray := []*uint64{helper.Uint64(uinNum)}
request.FilterSubAccountUin = uinArray

response, err := client.UseCamClient().DescribeSubAccounts(request)
if err != nil {
log.Printf("[CRITAL] get sub account[%v] data error: %v", uin, err.Error())
return ""
}
if response == nil || response.Response == nil {
log.Printf("[CRITAL] get sub account[%v] data response is nil", uin)
return ""
}

name := response.Response.SubAccounts[0].Name
return *name
}

// CurrentTimeMillisecond get the current millisecond timestamp
func CurrentTimeMillisecond() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}

func ParseLogJsonData(jsonData string) *ResourceAccountInfo {
if jsonData == "" {
return nil
}

var data map[string]interface{}
err := json.Unmarshal([]byte(jsonData), &data)
if err != nil {
log.Printf("[CRITAL] parse log json data[%v] error: %v", jsonData, err.Error())
return nil
}

resourceType := ""
if v, ok := data["resourceType"]; ok {
resourceType = v.(string)
}
resourceName := ""
if v, ok := data["resourceName"]; ok {
resourceName = v.(string)
if resourceName != "" {
resourceName = strings.Split(resourceName, "/")[0]
}
}
accountId, principalId, userName := parseUserIdentityFields(data)

return &ResourceAccountInfo{
ResourceType: resourceType,
ResourceName: resourceName,
AccountId: accountId,
PrincipalId: principalId,
UserName: userName,
}
}

func parseUserIdentityFields(data map[string]interface{}) (accountId, principalId, userName string) {
if v, ok := data["userIdentity.accountId"]; ok {
accountId = v.(string)
}
if v, ok := data["userIdentity.principalId"]; ok {
principalId = v.(string)
}
if v, ok := data["userIdentity.userName"]; ok {
userName = v.(string)
}
if v, ok := data["userIdentity"]; ok {
switch v := v.(type) {
case string:
var userIdentity map[string]string
err := json.Unmarshal([]byte(v), &userIdentity)
if err == nil {
accountId = userIdentity["accountId"]
principalId = userIdentity["principalId"]
userName = userIdentity["userName"]
}
}
}
return
}
90 changes: 90 additions & 0 deletions tencentcloud/common/file_common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package common

import (
"encoding/csv"
"log"
"os"
"path/filepath"
"time"
)

const (
SweeperResourceScanDir = "../../../tmp/resource_scan/"
SweeperNonKeepResourceScanDir = "../../../tmp/non_keep_resource_scan/"
)

var ResourceScanHeader = []string{"资源类型", "资源名称", "实例ID", "实例名称", "分类", "创建时长(天)", "创建者用户ID", "创建者用户名"}
var NonKeepResourceScanHeader = []string{"ResourceType", "ResourceName", "InstanceId", "InstanceName", "PrincipalId", "UserName"}

// WriteCsvFileData write data to csv file
func WriteCsvFileData(dirPath string, header []string, data [][]string) error {
log.Printf("[INFO] write csv file data[%v] to path[%v] start", len(data), dirPath)

count := 0
defer func() {
log.Printf("[INFO] write csv file data to path[%v] success count[%v]", dirPath, count)
}()

if len(data) == 0 {
return nil
}

err := os.MkdirAll(dirPath, 0755)
if err != nil {
log.Printf("[CRITAL] create directory %s error: %v", dirPath, err.Error())
return err
}

currentDate := time.Now().Format("20060102")
filePath := filepath.Join(dirPath, currentDate+".csv")

_, err = os.Stat(filePath)
if os.IsNotExist(err) {
err = GenerateCsvFile(filePath, header)
if err != nil {
log.Printf("[CRITAL] generate csv file error: %v", err.Error())
return err
}
}

file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Printf("[CRITAL] open csv file error: %v", err.Error())
return err
}
defer file.Close()

writer := csv.NewWriter(file)

for _, row := range data {
err = writer.Write(row)
if err != nil {
log.Printf("[CRITAL] write data[%v] to csv file error: %v", row, err.Error())
return err
}
count++
}
writer.Flush()

return nil
}

// GenerateCsvFile generate when csv file does not exist
func GenerateCsvFile(filePath string, header []string) error {
file, err := os.Create(filePath)
if err != nil {
log.Printf("[CRITAL] create csv file error: %v", err.Error())
return err
}
defer file.Close()

writer := csv.NewWriter(file)
err = writer.Write(header)
if err != nil {
log.Printf("[CRITAL] write header to csv file error: %v", err.Error())
return err
}
writer.Flush()

return nil
}
Loading

0 comments on commit 3f0f423

Please sign in to comment.