Skip to content

Commit

Permalink
Merge pull request #24 from volcengine/feat/vke_nodepool
Browse files Browse the repository at this point in the history
Feat/vke nodepool
  • Loading branch information
xuyaming0800 authored Sep 20, 2022
2 parents f7be110 + c117dd8 commit 070e98a
Show file tree
Hide file tree
Showing 36 changed files with 2,847 additions and 95 deletions.
44 changes: 29 additions & 15 deletions common/common_volcengine_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ type Callback struct {
}

type SdkCall struct {
Action string
BeforeCall BeforeCallFunc
ExecuteCall ExecuteCallFunc
CallError CallErrorFunc
AfterCall AfterCallFunc
Convert map[string]RequestConvert
ConvertMode RequestConvertMode
SdkParam *map[string]interface{}
RequestIdField string
Refresh *StateRefresh
ExtraRefresh map[ResourceService]*StateRefresh
ContentType RequestContentType
LockId LockId
Action string
BeforeCall BeforeCallFunc
ExecuteCall ExecuteCallFunc
CallError CallErrorFunc
AfterCall AfterCallFunc
Convert map[string]RequestConvert
ConvertMode RequestConvertMode
SdkParam *map[string]interface{}
RequestIdField string
Refresh *StateRefresh
ExtraRefresh map[ResourceService]*StateRefresh
ContentType RequestContentType
LockId LockId
ServiceCategory ServiceCategory
}

type StateRefresh struct {
Expand Down Expand Up @@ -69,7 +70,7 @@ func (c *SdkCall) InitWriteCall(resourceData *schema.ResourceData, resource *sch
return err
}

func sortAndStartTransJson(source map[string]interface{}) map[string]interface{} {
func SortAndStartTransJson(source map[string]interface{}) map[string]interface{} {
target := make(map[string]interface{})
var a []string
for k := range source {
Expand Down Expand Up @@ -162,6 +163,19 @@ func CallProcess(calls []SdkCall, d *schema.ResourceData, client *SdkClient, ser
resp *map[string]interface{}
)
doExecute := true

switch fn.ServiceCategory {
case ServiceTos:
var trans map[string]interface{}
trans, err = convertToTosParams(fn.Convert, *fn.SdkParam)
if err != nil {
return err
}
fn.SdkParam = &trans
case DefaultServiceCategory:
break
}

if fn.BeforeCall != nil {
doExecute, err = fn.BeforeCall(d, client, fn)
}
Expand All @@ -170,7 +184,7 @@ func CallProcess(calls []SdkCall, d *schema.ResourceData, client *SdkClient, ser
case ContentTypeDefault:
break
case ContentTypeJson:
jsonParam := sortAndStartTransJson(*fn.SdkParam)
jsonParam := SortAndStartTransJson(*fn.SdkParam)
fn.SdkParam = &jsonParam
break
}
Expand Down
10 changes: 5 additions & 5 deletions common/common_volcengine_callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ func TestSortAndStartTransJson1(t *testing.T) {
"ClusterId": "12345",
},
}
resp := sortAndStartTransJson(req)
resp := SortAndStartTransJson(req)
assert.Equal(t, resp, target)
}

func TestSortAndStartTransJson2(t *testing.T) {
req := sortAndStartTransJson(map[string]interface{}{
req := SortAndStartTransJson(map[string]interface{}{
"Filter.Ids.1": "id123",
"Filter.Ids.2": "id456",
})
Expand All @@ -30,12 +30,12 @@ func TestSortAndStartTransJson2(t *testing.T) {
"Ids": []interface{}{"id123", "id456"},
},
}
resp := sortAndStartTransJson(req)
resp := SortAndStartTransJson(req)
assert.Equal(t, resp, target)
}

func TestSortAndStartTransJson3(t *testing.T) {
req := sortAndStartTransJson(map[string]interface{}{
req := SortAndStartTransJson(map[string]interface{}{
"Filter.ClusterId": "12345",
"Filter.Ids.1": "id123",
"Filter.Ids.2": "id456",
Expand All @@ -61,7 +61,7 @@ func TestSortAndStartTransJson3(t *testing.T) {
},
},
}
resp := sortAndStartTransJson(req)
resp := SortAndStartTransJson(req)
assert.Equal(t, resp, target)

str := `{"Filter":{"ClusterId":"12345","Ids":["id123","id456"],"Nets":[{"Subnet":"subnet1"},{"Subnet":"subnet2"},{"Subnet":"subnet3"}]}}`
Expand Down
1 change: 1 addition & 0 deletions common/common_volcengine_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ type SdkClient struct {
RdsClient *rdsmysql.RDSMYSQL
RdsClientV2 *rdsmysqlv2.RDSMYSQLV2
UniversalClient *Universal
TosClient *Tos
}
1 change: 1 addition & 0 deletions common/common_volcengine_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (c *Config) Client() (*SdkClient, error) {
client.RdsClient = rdsmysql.New(sess)
client.RdsClientV2 = rdsmysqlv2.New(sess)
client.UniversalClient = NewUniversalClient(sess)
client.TosClient = NewTosClient(sess)

InitLocks()
InitSyncLimit()
Expand Down
17 changes: 17 additions & 0 deletions common/common_volcengine_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,20 @@ const (
ContentTypeDefault RequestContentType = iota
ContentTypeJson
)

type ServiceCategory int

const (
DefaultServiceCategory ServiceCategory = iota
ServiceTos
)

type SpecialParamType int

const (
DomainParam SpecialParamType = iota
HeaderParam
PathParam
UrlParam
FilePathParam
)
39 changes: 27 additions & 12 deletions common/common_volcengine_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type RequestConvert struct {
TargetField string
NextLevelConvert map[string]RequestConvert
StartIndex int
SpecialParam *SpecialParam
}
type SpecialParam struct {
Type SpecialParamType
Index int
}

var supportRequestConvertType = map[RequestContentType]map[RequestConvertType]bool{
Expand Down Expand Up @@ -237,7 +242,7 @@ func ResourceDateToRequest(d *schema.ResourceData, resource *schema.Resource, is
return req, err
}

func Convert(d *schema.ResourceData, k string, v interface{}, t RequestConvert, index int, req *map[string]interface{}, chain string, forceGet bool, contentType RequestContentType, schemaChain string) (err error) {
func Convert(d *schema.ResourceData, k string, v interface{}, t RequestConvert, index int, req *map[string]interface{}, chain string, forceGet bool, contentType RequestContentType, schemaChain string, setIndex []int) (err error) {
if !checkRequestConvertTypeSupport(contentType, t.ConvertType) {
return fmt.Errorf("Can not support the RequestContentType [%v] when RequestContentType is [%v] ", t.ConvertType, contentType)
}
Expand All @@ -253,19 +258,19 @@ func Convert(d *schema.ResourceData, k string, v interface{}, t RequestConvert,
err = RequestConvertWithN(v, k, t, req, chain)
break
case ConvertListN:
err = RequestConvertListN(v, k, t, req, chain, d, forceGet, false, contentType, schemaChain)
err = RequestConvertListN(v, k, t, req, chain, d, forceGet, false, contentType, schemaChain, setIndex)
break
case ConvertListUnique:
err = RequestConvertListN(v, k, t, req, chain, d, forceGet, true, contentType, schemaChain)
err = RequestConvertListN(v, k, t, req, chain, d, forceGet, true, contentType, schemaChain, setIndex)
break
case ConvertJsonObject: //equal ConvertListUnique
err = RequestConvertListN(v, k, t, req, chain, d, forceGet, true, contentType, schemaChain)
err = RequestConvertListN(v, k, t, req, chain, d, forceGet, true, contentType, schemaChain, setIndex)
break
case ConvertJsonArray: //equal ConvertWithN
err = RequestConvertWithN(v, k, t, req, chain)
break
case ConvertJsonObjectArray: //equal ConvertListN
err = RequestConvertListN(v, k, t, req, chain, d, forceGet, false, contentType, schemaChain)
err = RequestConvertListN(v, k, t, req, chain, d, forceGet, false, contentType, schemaChain, setIndex)
break
//case ConvertWithFilter:
// index, err = RequestConvertWithFilter(v, k, t, index, req)
Expand Down Expand Up @@ -302,7 +307,7 @@ func RequestCreateConvert(d *schema.ResourceData, k string, t RequestConvert, in
}
}
if ok {
err = Convert(d, k, v, t, index, req, "", forceGet, contentType, "")
err = Convert(d, k, v, t, index, req, "", forceGet, contentType, "", nil)
}
return index, err
}
Expand Down Expand Up @@ -365,7 +370,7 @@ func RequestConvertWithN(v interface{}, k string, t RequestConvert, req *map[str
return nil
}

func RequestConvertListN(v interface{}, k string, t RequestConvert, req *map[string]interface{}, chain string, d *schema.ResourceData, forceGet bool, single bool, contentType RequestContentType, schemaChain string) error {
func RequestConvertListN(v interface{}, k string, t RequestConvert, req *map[string]interface{}, chain string, d *schema.ResourceData, forceGet bool, single bool, contentType RequestContentType, schemaChain string, indexes []int) error {
var (
err error
isSet bool
Expand All @@ -389,7 +394,13 @@ func RequestConvertListN(v interface{}, k string, t RequestConvert, req *map[str
if t.NextLevelConvert != nil && t.NextLevelConvert[k2].ForceGet {
flag = true
} else {
schemaKey := fmt.Sprintf("%s.%d.%s", schemaChain+k, index, k2)
var schemaKey string
if len(indexes) > 0 {
schemaKey = fmt.Sprintf("%s.%d.%s", schemaChain+k, indexes[index], k2)
} else {
schemaKey = fmt.Sprintf("%s.%d.%s", schemaChain+k, index, k2)
}

if forceGet {
if t.ForceGet || (d.HasChange(schemaKey) && !d.IsNewResource()) {
flag = true
Expand All @@ -411,9 +422,9 @@ func RequestConvertListN(v interface{}, k string, t RequestConvert, req *map[str
switch reflect.TypeOf(v2).Kind() {
case reflect.Slice:
if t.NextLevelConvert[k2].Convert != nil {
err = Convert(d, k2, t.NextLevelConvert[k2].Convert(d, v2), t.NextLevelConvert[k2], 0, req, k3, t.NextLevelConvert[k2].ForceGet, contentType, k4)
err = Convert(d, k2, t.NextLevelConvert[k2].Convert(d, v2), t.NextLevelConvert[k2], 0, req, k3, t.NextLevelConvert[k2].ForceGet, contentType, k4, nil)
} else {
err = Convert(d, k2, v2, t.NextLevelConvert[k2], 0, req, k3, t.NextLevelConvert[k2].ForceGet, contentType, k4)
err = Convert(d, k2, v2, t.NextLevelConvert[k2], 0, req, k3, t.NextLevelConvert[k2].ForceGet, contentType, k4, nil)
}

if err != nil {
Expand All @@ -422,10 +433,14 @@ func RequestConvertListN(v interface{}, k string, t RequestConvert, req *map[str
break
case reflect.Ptr:
if _v2, ok2 := v2.(*schema.Set); ok2 {
var setIndex []int
for _, mmm := range _v2.List() {
setIndex = append(setIndex, _v2.F(mmm))
}
if t.NextLevelConvert[k2].Convert != nil {
err = Convert(d, k2, t.NextLevelConvert[k2].Convert(d, _v2.List()), t.NextLevelConvert[k2], 0, req, k3, t.NextLevelConvert[k2].ForceGet, contentType, k4)
err = Convert(d, k2, t.NextLevelConvert[k2].Convert(d, _v2.List()), t.NextLevelConvert[k2], 0, req, k3, t.NextLevelConvert[k2].ForceGet, contentType, k4, setIndex)
} else {
err = Convert(d, k2, _v2.List(), t.NextLevelConvert[k2], 0, req, k3, t.NextLevelConvert[k2].ForceGet, contentType, k4)
err = Convert(d, k2, _v2.List(), t.NextLevelConvert[k2], 0, req, k3, t.NextLevelConvert[k2].ForceGet, contentType, k4, setIndex)
}
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions common/common_volcengine_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type DataSourceInfo struct {
CollectField string
ContentType RequestContentType
ExtraData ExtraData
ServiceCategory ServiceCategory
}

func DataSourceToRequest(d *schema.ResourceData, r *schema.Resource, info DataSourceInfo) (req map[string]interface{}, err error) {
Expand Down
11 changes: 10 additions & 1 deletion common/common_volcengine_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,16 @@ func (d *Dispatcher) Data(resourceService ResourceService, resourceDate *schema.
return err
}
if info.ContentType == ContentTypeJson {
condition = sortAndStartTransJson(condition)
condition = SortAndStartTransJson(condition)
}
switch info.ServiceCategory {
case ServiceTos:
condition, err = convertToTosParams(info.RequestConverts, condition)
if err != nil {
return err
}
default:
break
}
collection, err = resourceService.ReadResources(condition)
if err != nil {
Expand Down
Loading

0 comments on commit 070e98a

Please sign in to comment.