Skip to content

support emr import #2461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2461.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_emr_cluster: support import
```
256 changes: 243 additions & 13 deletions tencentcloud/services/emr/resource_tc_emr_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
svccdb "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/cdb"
svctag "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tag"

Expand All @@ -22,11 +23,14 @@ func ResourceTencentCloudEmrCluster() *schema.Resource {
Read: resourceTencentCloudEmrClusterRead,
Delete: resourceTencentCloudEmrClusterDelete,
Update: resourceTencentCloudEmrClusterUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"display_strategy": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Optional: true,
Deprecated: "It will be deprecated in later versions.",
Description: "Display strategy of EMR instance.",
},
"product_id": {
Expand Down Expand Up @@ -54,7 +58,7 @@ func ResourceTencentCloudEmrCluster() *schema.Resource {
Description: "The private net config of EMR instance.",
},
"softwares": {
Type: schema.TypeList,
Type: schema.TypeSet,
Required: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Expand All @@ -71,22 +75,26 @@ func ResourceTencentCloudEmrCluster() *schema.Resource {
"task_resource_spec": buildResourceSpecSchema(),
"master_count": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The number of master node.",
},
"core_count": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The number of core node.",
},
"task_count": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The number of core node.",
},
"common_resource_spec": buildResourceSpecSchema(),
"common_count": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
ForceNew: true,
Description: "The number of common node.",
Expand Down Expand Up @@ -116,25 +124,50 @@ func ResourceTencentCloudEmrCluster() *schema.Resource {
Description: "The pay mode of instance. 0 represent POSTPAID_BY_HOUR, 1 represent PREPAID.",
},
"placement": {
Type: schema.TypeMap,
Required: true,
ForceNew: true,
Type: schema.TypeMap,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"placement", "placement_info"},
Deprecated: "It will be deprecated in later versions. Use `placement_info` instead.",
Description: "The location of the instance.",
},
"placement_info": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
ExactlyOneOf: []string{"placement", "placement_info"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"zone": {
Type: schema.TypeString,
Required: true,
Description: "Zone.",
},
"project_id": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "Project id.",
},
},
},
Description: "The location of the instance.",
},
"time_span": {
Type: schema.TypeInt,
Required: true,
Optional: true,
Description: "The length of time the instance was purchased. Use with TimeUnit.When TimeUnit is s, the parameter can only be filled in at 3600, representing a metered instance.\nWhen TimeUnit is m, the number filled in by this parameter indicates the length of purchase of the monthly instance of the package year, such as 1 for one month of purchase.",
},
"time_unit": {
Type: schema.TypeString,
Required: true,
Optional: true,
Description: "The unit of time in which the instance was purchased. When PayMode is 0, TimeUnit can only take values of s(second). When PayMode is 1, TimeUnit can only take the value m(month).",
},
"login_settings": {
Type: schema.TypeMap,
Required: true,
ForceNew: true,
Optional: true,
Sensitive: true,
Description: "Instance login settings.",
},
"extend_fs_field": {
Expand Down Expand Up @@ -178,6 +211,14 @@ func resourceTencentCloudEmrClusterUpdate(d *schema.ResourceData, meta interface
defer tccommon.LogElapsed("resource.tencentcloud_emr_cluster.update")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)

immutableFields := []string{"placement", "placement_info", "display_strategy", "login_settings"}
for _, f := range immutableFields {
if d.HasChange(f) {
return fmt.Errorf("cannot update argument `%s`", f)
}
}

emrService := EMRService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
Expand Down Expand Up @@ -253,13 +294,20 @@ func resourceTencentCloudEmrClusterUpdate(d *schema.ResourceData, meta interface
if err != nil {
return err
}
return nil
return resourceTencentCloudEmrClusterRead(d, meta)
}

func resourceTencentCloudEmrClusterCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_emr_cluster.create")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
immutableFields := []string{"time_unit", "time_span", "login_settings"}
for _, f := range immutableFields {
if _, ok := d.GetOkExists(f); !ok {
return fmt.Errorf("Argument `%s` must be set", f)
}
}

emrService := EMRService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
Expand All @@ -272,6 +320,8 @@ func resourceTencentCloudEmrClusterCreate(d *schema.ResourceData, meta interface
var displayStrategy string
if v, ok := d.GetOk("display_strategy"); ok {
displayStrategy = v.(string)
} else {
displayStrategy = "clusterList"
}
err = resource.Retry(10*tccommon.ReadRetryTimeout, func() *resource.RetryError {
clusters, err := emrService.DescribeInstancesById(ctx, instanceId, displayStrategy)
Expand Down Expand Up @@ -299,7 +349,7 @@ func resourceTencentCloudEmrClusterCreate(d *schema.ResourceData, meta interface
return err
}

return nil
return resourceTencentCloudEmrClusterRead(d, meta)
}

func resourceTencentCloudEmrClusterDelete(d *schema.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -376,8 +426,9 @@ func resourceTencentCloudEmrClusterRead(d *schema.ResourceData, meta interface{}
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
instanceId := d.Id()
var instance *emr.ClusterInstancesInfo
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
_, err := emrService.DescribeInstancesById(ctx, instanceId, DisplayStrategyIsclusterList)
result, err := emrService.DescribeInstancesById(ctx, instanceId, DisplayStrategyIsclusterList)

if e, ok := err.(*errors.TencentCloudSDKError); ok {
if e.GetCode() == "InternalError.ClusterNotFound" {
Expand All @@ -388,12 +439,191 @@ func resourceTencentCloudEmrClusterRead(d *schema.ResourceData, meta interface{}
if err != nil {
return resource.RetryableError(err)
}

if len(result) > 0 {
instance = result[0]
}

return nil
})

if err != nil {
return err
}

_ = d.Set("instance_id", instanceId)
if instance != nil {
_ = d.Set("product_id", instance.ProductId)
_ = d.Set("vpc_settings", map[string]interface{}{
"vpc_id": *instance.UniqVpcId,
"subnet_id": *instance.UniqSubnetId,
})
if instance.Config != nil {
if instance.Config.SoftInfo != nil {
_ = d.Set("softwares", helper.PStrings(instance.Config.SoftInfo))
}

if instance.Config.SupportHA != nil {
if *instance.Config.SupportHA {
_ = d.Set("support_ha", 1)
} else {
_ = d.Set("support_ha", 0)
}
}

if instance.Config.SecurityGroup != nil {
_ = d.Set("sg_id", instance.Config.SecurityGroup)
}
resourceSpec := make(map[string]interface{})

var masterCount int64
if instance.Config.MasterNodeSize != nil {
masterCount = *instance.Config.MasterNodeSize
resourceSpec["master_count"] = masterCount
}
if masterCount != 0 && instance.Config.MasterResource != nil {
masterResource := instance.Config.MasterResource
masterResourceSpec := make(map[string]interface{})
if masterResource.MemSize != nil {
masterResourceSpec["mem_size"] = *masterResource.MemSize
}
if masterResource.Cpu != nil {
masterResourceSpec["cpu"] = *masterResource.Cpu
}
if masterResource.DiskSize != nil {
masterResourceSpec["disk_size"] = *masterResource.DiskSize
}
if masterResource.DiskType != nil {
masterResourceSpec["disk_type"] = *masterResource.DiskType
}
if masterResource.Spec != nil {
masterResourceSpec["spec"] = *masterResource.Spec
}
if masterResource.StorageType != nil {
masterResourceSpec["storage_type"] = *masterResource.StorageType
}
if masterResource.RootSize != nil {
masterResourceSpec["root_size"] = *masterResource.RootSize
}
resourceSpec["master_resource_spec"] = []interface{}{masterResourceSpec}
}

var coreCount int64
if instance.Config.CoreNodeSize != nil {
coreCount = *instance.Config.CoreNodeSize
resourceSpec["core_count"] = coreCount
}
if coreCount != 0 && instance.Config.CoreResource != nil {
coreResource := instance.Config.CoreResource
coreResourceSpec := make(map[string]interface{})
if coreResource.MemSize != nil {
coreResourceSpec["mem_size"] = *coreResource.MemSize
}
if coreResource.Cpu != nil {
coreResourceSpec["cpu"] = *coreResource.Cpu
}
if coreResource.DiskSize != nil {
coreResourceSpec["disk_size"] = *coreResource.DiskSize
}
if coreResource.DiskType != nil {
coreResourceSpec["disk_type"] = *coreResource.DiskType
}
if coreResource.Spec != nil {
coreResourceSpec["spec"] = *coreResource.Spec
}
if coreResource.StorageType != nil {
coreResourceSpec["storage_type"] = *coreResource.StorageType
}
if coreResource.RootSize != nil {
coreResourceSpec["root_size"] = *coreResource.RootSize
}
resourceSpec["core_resource_spec"] = []interface{}{coreResourceSpec}
}

var taskCount int64
if instance.Config.TaskNodeSize != nil {
taskCount = *instance.Config.TaskNodeSize
resourceSpec["task_count"] = taskCount
}
if taskCount != 0 && instance.Config.TaskResource != nil {
taskResource := instance.Config.TaskResource
taskResourceSpec := make(map[string]interface{})
if taskResource.MemSize != nil {
taskResourceSpec["mem_size"] = *taskResource.MemSize
}
if taskResource.Cpu != nil {
taskResourceSpec["cpu"] = *taskResource.Cpu
}
if taskResource.DiskSize != nil {
taskResourceSpec["disk_size"] = *taskResource.DiskSize
}
if taskResource.DiskType != nil {
taskResourceSpec["disk_type"] = *taskResource.DiskType
}
if taskResource.Spec != nil {
taskResourceSpec["spec"] = *taskResource.Spec
}
if taskResource.StorageType != nil {
taskResourceSpec["storage_type"] = *taskResource.StorageType
}
if taskResource.RootSize != nil {
taskResourceSpec["root_size"] = *taskResource.RootSize
}
resourceSpec["task_resource_spec"] = []interface{}{taskResourceSpec}
}

var commonCount int64
if instance.Config.ComNodeSize != nil {
commonCount = *instance.Config.ComNodeSize
resourceSpec["common_count"] = commonCount
}
if commonCount != 0 && instance.Config.ComResource != nil {
comResource := instance.Config.ComResource
comResourceSpec := make(map[string]interface{})
if comResource.MemSize != nil {
comResourceSpec["mem_size"] = *comResource.MemSize
}
if comResource.Cpu != nil {
comResourceSpec["cpu"] = *comResource.Cpu
}
if comResource.DiskSize != nil {
comResourceSpec["disk_size"] = *comResource.DiskSize
}
if comResource.DiskType != nil {
comResourceSpec["disk_type"] = *comResource.DiskType
}
if comResource.Spec != nil {
comResourceSpec["spec"] = *comResource.Spec
}
if comResource.StorageType != nil {
comResourceSpec["storage_type"] = *comResource.StorageType
}
if comResource.RootSize != nil {
comResourceSpec["root_size"] = *comResource.RootSize
}
resourceSpec["common_resource_spec"] = []interface{}{comResourceSpec}
}

_ = d.Set("resource_spec", []interface{}{resourceSpec})
}

_ = d.Set("instance_name", instance.ClusterName)
_ = d.Set("pay_mode", instance.ChargeType)
placement := map[string]interface{}{
"zone": *instance.Zone,
"project_id": *instance.ProjectId,
}
_ = d.Set("placement", map[string]interface{}{
"zone": *instance.Zone,
})
_ = d.Set("placement_info", []interface{}{placement})
if instance.MasterIp != nil {
_ = d.Set("need_master_wan", "NEED_MASTER_WAN")
} else {
_ = d.Set("need_master_wan", "NOT_NEED_MASTER_WAN")
}
}

tagService := svctag.NewTagService(meta.(tccommon.ProviderMeta).GetAPIV3Conn())
region := meta.(tccommon.ProviderMeta).GetAPIV3Conn().Region
tags, err := tagService.DescribeResourceTags(ctx, "emr", "emr-instance", region, d.Id())
Expand Down
Loading