Skip to content

Commit

Permalink
resource/alicloud_ebs_enterprise_snapshot_policy: adds new features.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenHanZhang committed Jan 12, 2024
1 parent 867feda commit c22cb7b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 77 deletions.
79 changes: 19 additions & 60 deletions alicloud/resource_alicloud_ebs_enterprise_snapshot_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func resourceAliCloudEbsEnterpriseSnapshotPolicy() *schema.Resource {
"cross_region_copy_info": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -115,7 +114,6 @@ func resourceAliCloudEbsEnterpriseSnapshotPolicy() *schema.Resource {
"special_retain_rules": {
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Expand Down Expand Up @@ -205,30 +203,24 @@ func resourceAliCloudEbsEnterpriseSnapshotPolicyCreate(d *schema.ResourceData, m
if nodeNative != "" {
objectDataLocalMap["CronExpression"] = nodeNative
}
request["Schedule"], err = convertMaptoJsonString(objectDataLocalMap)
if err != nil {
return WrapError(err)
}
request["Schedule"] = objectDataLocalMap
}

objectDataLocalMap1 := make(map[string]interface{})
if v := d.Get("retain_rule"); !IsNil(v) {
nodeNative1, _ := jsonpath.Get("$[0].number", d.Get("retain_rule"))
if nodeNative1 != "" && nodeNative1.(int) > 0 {
if nodeNative1 != "" {
objectDataLocalMap1["Number"] = nodeNative1
}
nodeNative2, _ := jsonpath.Get("$[0].time_interval", d.Get("retain_rule"))
if nodeNative2 != "" && nodeNative2.(int) > 0 {
if nodeNative2 != "" {
objectDataLocalMap1["TimeInterval"] = nodeNative2
}
nodeNative3, _ := jsonpath.Get("$[0].time_unit", d.Get("retain_rule"))
if nodeNative3 != "" {
objectDataLocalMap1["TimeUnit"] = nodeNative3
}
request["RetainRule"], err = convertMaptoJsonString(objectDataLocalMap1)
if err != nil {
return WrapError(err)
}
request["RetainRule"] = objectDataLocalMap1
}

objectDataLocalMap2 := make(map[string]interface{})
Expand All @@ -237,10 +229,7 @@ func resourceAliCloudEbsEnterpriseSnapshotPolicyCreate(d *schema.ResourceData, m
if nodeNative4 != "" {
objectDataLocalMap2["EnableImmediateAccess"] = nodeNative4
}
request["StorageRule"], err = convertMaptoJsonString(objectDataLocalMap2)
if err != nil {
return WrapError(err)
}
request["StorageRule"] = objectDataLocalMap2
}

objectDataLocalMap3 := make(map[string]interface{})
Expand All @@ -259,30 +248,20 @@ func resourceAliCloudEbsEnterpriseSnapshotPolicyCreate(d *schema.ResourceData, m
dataLoopTmp := dataLoop.(map[string]interface{})
dataLoopMap := make(map[string]interface{})
dataLoopMap["RegionId"] = dataLoopTmp["region_id"]
if dataLoopTmp["retain_days"].(int) > 0 {
dataLoopMap["RetainDays"] = dataLoopTmp["retain_days"]
}
dataLoopMap["RetainDays"] = dataLoopTmp["retain_days"]
localMaps = append(localMaps, dataLoopMap)
}
objectDataLocalMap3["Regions"] = localMaps
}
request["CrossRegionCopyInfo"], err = convertMaptoJsonString(objectDataLocalMap3)
if err != nil {
return WrapError(err)
}
request["CrossRegionCopyInfo"] = objectDataLocalMap3
}

if v, ok := d.GetOk("status"); ok {
request["State"] = v
}
if _, ok := d.GetOk("tags"); ok {
added, _ := parsingTags(d)
count := 1
for key, value := range added {
request[fmt.Sprintf("Tag.%d.Key", count)] = key
request[fmt.Sprintf("Tag.%d.Value", count)] = value
count++
}
if v, ok := d.GetOk("tags"); ok {
tagsMap := ConvertTags(v.(map[string]interface{}))
request["Tags"] = tagsMap
}

objectDataLocalMap4 := make(map[string]interface{})
Expand All @@ -307,10 +286,7 @@ func resourceAliCloudEbsEnterpriseSnapshotPolicyCreate(d *schema.ResourceData, m
}
objectDataLocalMap4["Rules"] = localMaps1
}
request["SpecialRetainRules"], err = convertMaptoJsonString(objectDataLocalMap4)
if err != nil {
return WrapError(err)
}
request["SpecialRetainRules"] = objectDataLocalMap4
}

runtime := util.RuntimeOptions{}
Expand Down Expand Up @@ -491,10 +467,7 @@ func resourceAliCloudEbsEnterpriseSnapshotPolicyUpdate(d *schema.ResourceData, m
if nodeNative != "" {
objectDataLocalMap["CronExpression"] = nodeNative
}
request["Schedule"], err = convertMaptoJsonString(objectDataLocalMap)
if err != nil {
return WrapError(err)
}
request["Schedule"] = objectDataLocalMap
}

if d.HasChange("retain_rule") {
Expand All @@ -503,21 +476,18 @@ func resourceAliCloudEbsEnterpriseSnapshotPolicyUpdate(d *schema.ResourceData, m
objectDataLocalMap1 := make(map[string]interface{})
if v := d.Get("retain_rule"); !IsNil(v) {
nodeNative1, _ := jsonpath.Get("$[0].number", v)
if nodeNative1 != "" && nodeNative1.(int) > 0 {
if nodeNative1 != "" {
objectDataLocalMap1["Number"] = nodeNative1
}
nodeNative2, _ := jsonpath.Get("$[0].time_interval", v)
if nodeNative2 != "" && nodeNative2.(int) > 0 {
if nodeNative2 != "" {
objectDataLocalMap1["TimeInterval"] = nodeNative2
}
nodeNative3, _ := jsonpath.Get("$[0].time_unit", v)
if nodeNative3 != "" {
objectDataLocalMap1["TimeUnit"] = nodeNative3
}
request["RetainRule"], err = convertMaptoJsonString(objectDataLocalMap1)
if err != nil {
return WrapError(err)
}
request["RetainRule"] = objectDataLocalMap1
}

if d.HasChange("storage_rule") {
Expand All @@ -528,10 +498,7 @@ func resourceAliCloudEbsEnterpriseSnapshotPolicyUpdate(d *schema.ResourceData, m
if nodeNative4 != "" {
objectDataLocalMap2["EnableImmediateAccess"] = nodeNative4
}
request["StorageRule"], err = convertMaptoJsonString(objectDataLocalMap2)
if err != nil {
return WrapError(err)
}
request["StorageRule"] = objectDataLocalMap2
}
}

Expand All @@ -553,17 +520,12 @@ func resourceAliCloudEbsEnterpriseSnapshotPolicyUpdate(d *schema.ResourceData, m
dataLoopTmp := dataLoop.(map[string]interface{})
dataLoopMap := make(map[string]interface{})
dataLoopMap["RegionId"] = dataLoopTmp["region_id"]
if dataLoopTmp["retain_days"].(int) > 0 {
dataLoopMap["RetainDays"] = dataLoopTmp["retain_days"]
}
dataLoopMap["RetainDays"] = dataLoopTmp["retain_days"]
localMaps = append(localMaps, dataLoopMap)
}
objectDataLocalMap3["Regions"] = localMaps
}
request["CrossRegionCopyInfo"], err = convertMaptoJsonString(objectDataLocalMap3)
if err != nil {
return WrapError(err)
}
request["CrossRegionCopyInfo"] = objectDataLocalMap3
}
}

Expand Down Expand Up @@ -596,10 +558,7 @@ func resourceAliCloudEbsEnterpriseSnapshotPolicyUpdate(d *schema.ResourceData, m
}
objectDataLocalMap4["Rules"] = localMaps1
}
request["SpecialRetainRules"], err = convertMaptoJsonString(objectDataLocalMap4)
if err != nil {
return WrapError(err)
}
request["SpecialRetainRules"] = objectDataLocalMap4
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func resourceAliCloudEbsEnterpriseSnapshotPolicyAttachmentCreate(d *schema.Resou
return WrapErrorf(err, DefaultErrorMsg, "alicloud_ebs_enterprise_snapshot_policy_attachment", action, AlibabaCloudSdkGoERROR)
}

d.SetId(fmt.Sprintf("%v:%v", query["PolicyId"], query["DiskTargets.1"]))
DiskTargets, _ := jsonpath.Get("DiskTargets[0]", request)
d.SetId(fmt.Sprintf("%v:%v", request["PolicyId"], DiskTargets))

return resourceAliCloudEbsEnterpriseSnapshotPolicyAttachmentRead(d, meta)
}
Expand All @@ -104,7 +105,7 @@ func resourceAliCloudEbsEnterpriseSnapshotPolicyAttachmentRead(d *schema.Resourc
d.Set("policy_id", objectRaw["PolicyId"])

diskIds1Raw, _ := jsonpath.Get("$.DiskIds", objectRaw)
d.Set("disk_id", diskIds1Raw.([]interface{})[0])
d.Set("disk_id", diskIds1Raw["DiskIdsChild"])

Check failure on line 108 in alicloud/resource_alicloud_ebs_enterprise_snapshot_policy_attachment.go

View workflow job for this annotation

GitHub Actions / runner / errcheck

[errcheck] reported by reviewdog 🐶 invalid operation: cannot index diskIds1Raw (variable of type interface{}) (typecheck) Raw Output: alicloud/resource_alicloud_ebs_enterprise_snapshot_policy_attachment.go:108:30: invalid operation: cannot index diskIds1Raw (variable of type interface{}) (typecheck) package alicloud

Check failure on line 108 in alicloud/resource_alicloud_ebs_enterprise_snapshot_policy_attachment.go

View workflow job for this annotation

GitHub Actions / runner / errcheck

[errcheck] reported by reviewdog 🐶 invalid operation: cannot index diskIds1Raw (variable of type interface{})) (typecheck) Raw Output: alicloud/resource_alicloud_ebs_enterprise_snapshot_policy_attachment.go:108:30: invalid operation: cannot index diskIds1Raw (variable of type interface{})) (typecheck) "github.com/aliyun/terraform-provider-alicloud/alicloud" ^

Check failure on line 108 in alicloud/resource_alicloud_ebs_enterprise_snapshot_policy_attachment.go

View workflow job for this annotation

GitHub Actions / runner / errcheck

[errcheck] reported by reviewdog 🐶 invalid operation: cannot index diskIds1Raw (variable of type interface{})) (typecheck) Raw Output: alicloud/resource_alicloud_ebs_enterprise_snapshot_policy_attachment.go:108:30: invalid operation: cannot index diskIds1Raw (variable of type interface{})) (typecheck) "github.com/aliyun/terraform-provider-alicloud/alicloud" ^

Check failure on line 108 in alicloud/resource_alicloud_ebs_enterprise_snapshot_policy_attachment.go

View workflow job for this annotation

GitHub Actions / TestingCoverageRate

invalid operation: cannot index diskIds1Raw (variable of type interface{})

Check failure on line 108 in alicloud/resource_alicloud_ebs_enterprise_snapshot_policy_attachment.go

View workflow job for this annotation

GitHub Actions / Compile

invalid operation: cannot index diskIds1Raw (variable of type interface{})

Check failure on line 108 in alicloud/resource_alicloud_ebs_enterprise_snapshot_policy_attachment.go

View workflow job for this annotation

GitHub Actions / Consistency

invalid operation: cannot index diskIds1Raw (variable of type interface{})

Check failure on line 108 in alicloud/resource_alicloud_ebs_enterprise_snapshot_policy_attachment.go

View workflow job for this annotation

GitHub Actions / Consistency

invalid operation: cannot index diskIds1Raw (variable of type interface{})

parts := strings.Split(d.Id(), ":")
d.Set("policy_id", parts[0])
Expand Down
6 changes: 6 additions & 0 deletions alicloud/service_alicloud_ebs_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ func (s *EbsServiceV2) SetResourceTags(d *schema.ResourceData, resourceType stri
}

// SetResourceTags >>> tag function encapsulated.

// DescribeEbsEnterpriseSnapshotPolicyAttachment <<< Encapsulated get interface for Ebs EnterpriseSnapshotPolicyAttachment.

func (s *EbsServiceV2) DescribeEbsEnterpriseSnapshotPolicyAttachment(id string) (object map[string]interface{}, err error) {
Expand Down Expand Up @@ -419,6 +420,11 @@ func (s *EbsServiceV2) DescribeEbsEnterpriseSnapshotPolicyAttachment(id string)
return object, WrapErrorf(Error(GetNotFoundMessage("EnterpriseSnapshotPolicyAttachment", id)), NotFoundMsg, response)
}

currentStatus := v.([]interface{})[0].(map[string]interface{})["DiskIdsChild"]
if currentStatus == "" {
return object, WrapErrorf(Error(GetNotFoundMessage("EnterpriseSnapshotPolicyAttachment", id)), NotFoundMsg, response)
}

return v.([]interface{})[0].(map[string]interface{}), nil
}

Expand Down
17 changes: 4 additions & 13 deletions website/docs/r/ebs_enterprise_snapshot_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,18 @@ provider "alicloud" {
region = "cn-hangzhou"
}
resource "alicloud_ecs_disk" "defaultJkW46o" {
category = "cloud_essd"
description = "esp-attachment-test"
zone_id = "cn-hangzhou-i"
performance_level = "PL1"
size = "20"
disk_name = var.name
}
resource "alicloud_ebs_enterprise_snapshot_policy" "defaultPE3jjR" {
status = "DISABLED"
resource "alicloud_ebs_enterprise_snapshot_policy" "default" {
status = "ENABLED"
desc = "DESC"
schedule {
cron_expression = "0 0 0 1 * ?"
cron_expression = "0 0 1 * * ?"
}
enterprise_snapshot_policy_name = var.name
target_type = "DISK"
retain_rule {
time_interval = "120"
time_unit = "DAYS"
number = "1"
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ provider "alicloud" {
resource "alicloud_ecs_disk" "defaultJkW46o" {
category = "cloud_essd"
description = "esp-attachment-test"
zone_id = "cn-hangzhou-i"
zone_id = "cn-heyuan-b"
performance_level = "PL1"
size = "20"
disk_name = var.name
}
resource "alicloud_ebs_enterprise_snapshot_policy" "defaultPE3jjR" {
Expand All @@ -53,7 +54,7 @@ resource "alicloud_ebs_enterprise_snapshot_policy" "defaultPE3jjR" {
resource "alicloud_ebs_enterprise_snapshot_policy_attachment" "default" {
policy_id = alicloud_ebs_enterprise_snapshot_policy.defaultPE3jjR.id
policy_id = alicloud_ebs_enterprise_snapshot_policy.defaultPE3jjR.policy_id
disk_id = alicloud_ecs_disk.defaultJkW46o.id
}
```
Expand Down

0 comments on commit c22cb7b

Please sign in to comment.