Skip to content

Commit

Permalink
Merge pull request #93 from volcengine/feat/ecs
Browse files Browse the repository at this point in the history
Feat/ecs
  • Loading branch information
zpp12354321 authored Jun 12, 2023
2 parents a37ebfb + 768e86e commit 7631125
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 20 deletions.
2 changes: 2 additions & 0 deletions common/common_volcengine_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func (d *Dispatcher) Read(resourceService ResourceService, resourceDate *schema.
}
handlers := resourceService.WithResourceResponseHandlers(instance)
if len(handlers) == 0 {
resourceSpecial(resource, instance, nil)
_, err = ResponseToResourceData(resourceDate, resource, instance, nil)
return err
}
Expand All @@ -159,6 +160,7 @@ func (d *Dispatcher) Read(resourceService ResourceService, resourceDate *schema.
if err != nil {
return err
}
resourceSpecial(resource, data, convert)
_, err = ResponseToResourceData(resourceDate, resource, data, convert)
if err != nil {
return err
Expand Down
76 changes: 76 additions & 0 deletions common/common_volcengine_resource_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package common

import "github.com/hashicorp/terraform-plugin-sdk/helper/schema"

func resourceSpecial(resource *schema.Resource, data map[string]interface{}, convert map[string]ResponseConvert) {
if len(resource.Schema) > 0 {
mappings := map[string]string{}
//构造一下反向映射
if len(convert) > 0 {
for k, v := range convert {
if v.TargetField != "" {
mappings[v.TargetField] = k
}
}
}

for k, v := range resource.Schema {
var (
key string
isMapping bool
ok bool
val interface{}
next *schema.Resource
)
if key, ok = mappings[k]; ok {
isMapping = true
} else {
key = DownLineToHump(k)
}
switch v.Type {
case schema.TypeSet:
if val, ok = data[key]; !ok {
setSchemaSetSpecial(key, k, data, isMapping)
} else {
if val == nil {
setSchemaSetSpecial(key, k, data, isMapping)
} else {
break
}
}
//不存在转换映射的情况下 判断一下schemaKey是否存在 不存在则set
if !isMapping {
if val, ok = data[k]; !ok {
setSchemaSetSpecial(key, k, data, isMapping)
} else {
if val == nil {
setSchemaSetSpecial(key, k, data, isMapping)
}
}
}
break
default:
break
}
if v.Elem != nil {
if next, ok = v.Elem.(*schema.Resource); ok {
if val, ok = data[key]; ok {
if nextData, ok2 := val.(map[string]interface{}); ok2 {
resourceSpecial(next, nextData, convert)
data[key] = nextData
}
}
}

}
}
}
}

func setSchemaSetSpecial(targetKey string, sourceKey string, data map[string]interface{}, isMapping bool) {
data[targetKey] = new(schema.Set)
if !isMapping {
//不存在转换映射的情况下 自动补齐一个下划线类型的key上去
data[sourceKey] = new(schema.Set)
}
}
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.76"
TerraformProviderVersion = "0.0.77"
)
1 change: 1 addition & 0 deletions example/mongoDBInstance/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
resource "volcengine_mongodb_instance" "foo"{
zone_id="cn-beijing-a"
db_engine_version = "MongoDB_4_0"
instance_type="ReplicaSet"
node_spec="mongo.2c4g"
# mongos_node_spec="mongo.mongos.2c4g"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s *VolcengineScalingGroupService) ReadResources(m map[string]interface{})
return data, err
}
}
logger.Debug(logger.RespFormat, action, action, resp)
logger.Debug(logger.RespFormat, action, action, *resp)
results, err = ve.ObtainSdkValue("Result.ScalingGroups", *resp)
if err != nil {
return data, err
Expand Down Expand Up @@ -128,6 +128,9 @@ func (VolcengineScalingGroupService) WithResourceResponseHandlers(scalingGroup m
"MultiAZPolicy": {
TargetField: "multi_az_policy",
},
"DBInstanceIds": {
TargetField: "db_instance_ids",
},
}, nil
}
return []ve.ResourceResponseHandler{handler}
Expand Down Expand Up @@ -423,6 +426,9 @@ func (s *VolcengineScalingGroupService) DatasourceResources(*schema.ResourceData
"MultiAZPolicy": {
TargetField: "multi_az_policy",
},
"DBInstanceIds": {
TargetField: "db_instance_ids",
},
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ func ResourceVolcengineMongoDBInstance() *schema.Resource {
// ValidateFunc: validation.StringInSlice([]string{"MongoDB"}, false),
// Description: "The db engine,valid value contains `MongoDB`.",
// },
// "db_engine_version": {
// Type: schema.TypeString,
// Optional: true,
// Computed: true,
// ValidateFunc: validation.StringInSlice([]string{"MongoDB_4_0"}, false),
// Description: "The version of db engine,valid value contains `MongoDB_4_0`.",
// },
"db_engine_version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: "The version of db engine, valid value contains `MongoDB_4_0`, `MongoDB_5_0`.",
},
"node_spec": {
Type: schema.TypeString,
Required: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (s *VolcengineMongoDBInstanceService) WithResourceResponseHandlers(instance
handler := func() (map[string]interface{}, map[string]ve.ResponseConvert, error) {
return instance, map[string]ve.ResponseConvert{
"DBEngine": {
TargetField: "db_ending",
TargetField: "db_engine",
},
"DBEngineVersion": {
TargetField: "db_engine_version",
Expand Down Expand Up @@ -348,9 +348,9 @@ func (s *VolcengineMongoDBInstanceService) CreateResource(resourceData *schema.R
// "db_engine": {
// TargetField: "DBEngine",
// },
// "db_engine_version": {
// TargetField: "DBEngineVersion",
// },
"db_engine_version": {
TargetField: "DBEngineVersion",
},
"storage_space_gb": {
TargetField: "StorageSpaceGB",
},
Expand Down Expand Up @@ -558,6 +558,9 @@ func (s *VolcengineMongoDBInstanceService) RemoveResource(resourceData *schema.R
logger.Debug(logger.ReqFormat, call.Action, call.SdkParam)
return s.Client.UniversalClient.DoCall(getUniversalInfo(call.Action), call.SdkParam)
},
AfterCall: func(d *schema.ResourceData, client *ve.SdkClient, resp *map[string]interface{}, call ve.SdkCall) error {
return ve.CheckResourceUtilRemoved(d, s.ReadResource, 5*time.Minute)
},
CallError: func(d *schema.ResourceData, client *ve.SdkClient, call ve.SdkCall, baseErr error) error {
//出现错误后重试
return resource.Retry(15*time.Minute, func() *resource.RetryError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ func DataSourceVolcengineVkeVkeClusters() *schema.Resource {
"log_type": {
Type: schema.TypeString,
Computed: true,
Description: "The currently enabled log type, optional values: `Audit`.",
Description: "The currently enabled log type.",
},
"log_ttl": {
Type: schema.TypeInt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
ve "github.com/volcengine/terraform-provider-volcengine/common"
"github.com/volcengine/terraform-provider-volcengine/logger"
"github.com/volcengine/terraform-provider-volcengine/volcengine/vke/default_node_pool"
)

Expand All @@ -28,7 +27,7 @@ func ResourceVolcengineDefaultNodePoolBatchAttach() *schema.Resource {
}
ve.MergeDateSourceToResource(default_node_pool.ResourceVolcengineDefaultNodePool().Schema, &m)

logger.Debug(logger.RespFormat, "ATTACH_TEST", m)
// logger.Debug(logger.RespFormat, "ATTACH_TEST", m)

return &schema.Resource{
Create: resourceVolcengineDefaultNodePoolBatchAttachCreate,
Expand Down
2 changes: 1 addition & 1 deletion website/docs/d/vke_clusters.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ In addition to all arguments above, the following attributes are exported:
* `log_setups` - Cluster logging options.
* `enabled` - Whether to enable the log option, true means enable, false means not enable, the default is false. When Enabled is changed from false to true, a new Topic will be created.
* `log_ttl` - The storage time of logs in Log Service. After the specified log storage time is exceeded, the expired logs in this log topic will be automatically cleared. The unit is days, and the default is 30 days. The value range is 1 to 3650, specifying 3650 days means permanent storage.
* `log_type` - The currently enabled log type, optional values: `Audit`.
* `log_type` - The currently enabled log type.
* `name` - The name of the cluster.
* `node_statistics` - Statistics on the number of nodes corresponding to each master state in the cluster.
* `creating_count` - Phase=Creating total number of nodes.
Expand Down
8 changes: 5 additions & 3 deletions website/docs/r/mongodb_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ use 'terraform state rm ${resourceId}' to remove.
## Example Usage
```hcl
resource "volcengine_mongodb_instance" "foo" {
zone_id = "cn-beijing-a"
instance_type = "ReplicaSet"
node_spec = "mongo.2c4g"
zone_id = "cn-beijing-a"
db_engine_version = "MongoDB_4_0"
instance_type = "ReplicaSet"
node_spec = "mongo.2c4g"
# mongos_node_spec="mongo.mongos.2c4g"
# mongos_node_number = 3
# shard_number=3
Expand Down Expand Up @@ -49,6 +50,7 @@ The following arguments are supported:
* `subnet_id` - (Required, ForceNew) The subnet id of instance.
* `auto_renew` - (Optional) Whether to enable automatic renewal.
* `charge_type` - (Optional) The charge type of instance, valid value contains `Prepaid` or `PostPaid`.
* `db_engine_version` - (Optional, ForceNew) The version of db engine, valid value contains `MongoDB_4_0`, `MongoDB_5_0`.
* `instance_name` - (Optional) The instance name.
* `instance_type` - (Optional, ForceNew) The type of instance,the valid value contains `ReplicaSet` or `ShardedCluster`.
* `mongos_node_number` - (Optional) The mongos node number of shard cluster,value range is `2~23`, this parameter is required when `InstanceType` is `ShardedCluster`.
Expand Down

0 comments on commit 7631125

Please sign in to comment.