Skip to content

Commit

Permalink
Merge pull request #68 from volcengine/feat/rds
Browse files Browse the repository at this point in the history
Feat/rds
  • Loading branch information
xuyaming0800 authored Mar 3, 2023
2 parents af47f4c + ad2d16d commit 4c7da54
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 14 deletions.
64 changes: 61 additions & 3 deletions common/common_volcengine_tos_client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package common

import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net"
"os"
"strconv"
"strings"

"github.com/volcengine/volcengine-go-sdk/volcengine/client"
"github.com/volcengine/volcengine-go-sdk/volcengine/client/metadata"
Expand Down Expand Up @@ -96,6 +101,9 @@ func (u *Tos) newTosClient(domain string) *client.Client {
}

func (u *Tos) DoTosCall(info TosInfo, input *map[string]interface{}) (output *map[string]interface{}, err error) {
defer func() {

}()
c := u.newTosClient(info.Domain)
trueInput := make(map[string]interface{})
var httpPath string
Expand Down Expand Up @@ -125,8 +133,9 @@ func (u *Tos) DoTosCall(info TosInfo, input *map[string]interface{}) (output *ma
}

if info.ContentPath != "" && (op.HTTPMethod == "PUT" || op.HTTPMethod == "POST") {
content, _ := ioutil.ReadFile(info.ContentPath)
req.SetBufferBody(content)
content, _ := os.Open(info.ContentPath)
req.Body = content
req.HTTPRequest.Header.Set("Content-Length", strconv.FormatInt(u.tryResolveLength(content), 10))
}

if len(info.Header) > 0 {
Expand All @@ -138,3 +147,52 @@ func (u *Tos) DoTosCall(info TosInfo, input *map[string]interface{}) (output *ma
err = req.Send()
return output, err
}

func (u *Tos) tryResolveLength(reader io.Reader) int64 {
switch v := reader.(type) {
case *bytes.Buffer:
return int64(v.Len())
case *bytes.Reader:
return int64(v.Len())
case *strings.Reader:
return int64(v.Len())
case *os.File:
length, err := fileUnreadLength(v)
if err != nil {
return -1
}
return length
case *io.LimitedReader:
return v.N
case *net.Buffers:
if v != nil {
length := int64(0)
for _, p := range *v {
length += int64(len(p))
}
return length
}
return 0
default:
return -1
}
}

func fileUnreadLength(file *os.File) (int64, error) {
offset, err := file.Seek(0, io.SeekCurrent)
if err != nil {
return 0, err
}

stat, err := file.Stat()
if err != nil {
return 0, err
}

size := stat.Size()
if offset > size || offset < 0 {
return 0, fmt.Errorf("unexpected file size and(or) offset")
}

return size - offset, nil
}
2 changes: 1 addition & 1 deletion common/common_volcengine_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package common

const (
TerraformProviderName = "terraform-provider-volcengine"
TerraformProviderVersion = "0.0.57"
TerraformProviderVersion = "0.0.58"
)
3 changes: 3 additions & 0 deletions volcengine/nat/nat_gateway/service_volcengine_nat_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ func (s *VolcengineNatGatewayService) CreateResource(resourceData *schema.Resour
Call: ve.SdkCall{
Action: "CreateNatGateway",
ConvertMode: ve.RequestConvertAll,
LockId: func(d *schema.ResourceData) string {
return d.Get("vpc_id").(string)
},
Convert: map[string]ve.RequestConvert{
"billing_type": {
TargetField: "BillingType",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func ResourceVolcengineRdsInstance() *schema.Resource {
Optional: true,
Computed: true,
ForceNew: true,
Description: "Whether to automatically renew in prepaid scenarios.\nAutorenew_Enable\nAutorenew_Disable (default).",
Description: "Whether to automatically renew in prepaid scenarios.",
},
"period_unit": {
Type: schema.TypeString,
Expand Down
5 changes: 5 additions & 0 deletions volcengine/tos/object/resource_volcengine_tos_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func ResourceVolcengineTosObject() *schema.Resource {
//ForceNew: true,
Description: "The file path for upload.",
},
"content_md5": {
Type: schema.TypeString,
Optional: true,
Description: "The file md5 sum (32-bit hexadecimal string) for upload.",
},
"encryption": {
Type: schema.TypeString,
Optional: true,
Expand Down
39 changes: 32 additions & 7 deletions volcengine/tos/object/service_volcengine_tos_object.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package object

import (
"encoding/base64"
"encoding/hex"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -92,6 +94,9 @@ func (s *VolcengineTosObjectService) ReadResource(resourceData *schema.ResourceD
if header.Get("X-Tos-Server-Side-Encryption") != "" {
data["Encryption"] = header.Get("X-Tos-Server-Side-Encryption")
}
if header.Get("x-tos-meta-content-md5") != "" {
data["ContentMd5"] = strings.Replace(header.Get("x-tos-meta-content-md5"), "\"", "", -1)
}

if header.Get("X-Tos-Version-Id") != "" {
action = "ListObjects"
Expand Down Expand Up @@ -233,7 +238,7 @@ func (s *VolcengineTosObjectService) CreateResource(resourceData *schema.Resourc
func (s *VolcengineTosObjectService) ModifyResource(data *schema.ResourceData, resource *schema.Resource) []ve.Callback {
var callbacks []ve.Callback

if data.HasChange("file_path") {
if data.HasChange("file_path") || data.HasChanges("content_md5") {
callbacks = append(callbacks, s.createOrReplaceObject(data, resource, true))
callbacks = append(callbacks, s.createOrUpdateObjectAcl(data, resource, true))
} else {
Expand Down Expand Up @@ -449,12 +454,13 @@ func (s *VolcengineTosObjectService) createOrUpdateObjectAcl(resourceData *schem
//},
},
}
if isUpdate {
callback.Call.Refresh = &ve.StateRefresh{
Target: []string{"Success"},
Timeout: resourceData.Timeout(schema.TimeoutCreate),
}
}
//如果出现acl缓存的问题 这里再打开 暂时去掉 不再等待60s
//if isUpdate && !resourceData.HasChange("file_path") && !resourceData.HasChanges("content_md5") {
// callback.Call.Refresh = &ve.StateRefresh{
// Target: []string{"Success"},
// Timeout: resourceData.Timeout(schema.TimeoutCreate),
// }
//}
return callback
}

Expand Down Expand Up @@ -506,12 +512,25 @@ func (s *VolcengineTosObjectService) createOrReplaceObject(resourceData *schema.
},
ForceGet: isUpdate,
},
"content_md5": {
ConvertType: ve.ConvertDefault,
TargetField: "Content-MD5",
Convert: func(data *schema.ResourceData, i interface{}) interface{} {
b, _ := hex.DecodeString(i.(string))
return base64.StdEncoding.EncodeToString(b)
},
SpecialParam: &ve.SpecialParam{
Type: ve.HeaderParam,
},
ForceGet: isUpdate,
},
"file_path": {
ConvertType: ve.ConvertDefault,
TargetField: "file-path",
SpecialParam: &ve.SpecialParam{
Type: ve.FilePathParam,
},
ForceGet: isUpdate,
},
"encryption": {
ConvertType: ve.ConvertDefault,
Expand All @@ -522,6 +541,12 @@ func (s *VolcengineTosObjectService) createOrReplaceObject(resourceData *schema.
ForceGet: isUpdate,
},
},
BeforeCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (bool, error) {
if _, ok := (*call.SdkParam)[ve.TosHeader].(map[string]string)["Content-MD5"]; ok {
(*call.SdkParam)[ve.TosHeader].(map[string]string)["x-tos-meta-content-md5"] = d.Get("content_md5").(string)
}
return true, nil
},
ExecuteCall: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall) (*map[string]interface{}, error) {
logger.Debug(logger.RespFormat, call.Action, call.SdkParam)
//创建Object
Expand Down
2 changes: 0 additions & 2 deletions website/docs/r/rds_instance_v2.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ The `charge_info` object supports the following:
PostPaid - Pay-As-You-Go
PrePaid - Yearly and monthly (default).
* `auto_renew` - (Optional, ForceNew) Whether to automatically renew in prepaid scenarios.
Autorenew_Enable
Autorenew_Disable (default).
* `period_unit` - (Optional, ForceNew) The purchase cycle in the prepaid scenario.
Month - monthly subscription (default)
Year - Package year.
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/tos_object.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The following arguments are supported:
* `file_path` - (Required) The file path for upload.
* `object_name` - (Required, ForceNew) The name of the object.
* `account_acl` - (Optional) The user set of grant full control.
* `content_md5` - (Optional) The file md5 sum (32-bit hexadecimal string) for upload.
* `content_type` - (Optional, ForceNew) The content type of the object.
* `encryption` - (Optional, ForceNew) The encryption of the object.Valid value is AES256.
* `public_acl` - (Optional) The public acl control of object.Valid value is private|public-read|public-read-write|authenticated-read|bucket-owner-read.
Expand Down

0 comments on commit 4c7da54

Please sign in to comment.