Skip to content

Commit

Permalink
resource/alicloud_ess_notification: add attribute of time_zone.
Browse files Browse the repository at this point in the history
  • Loading branch information
fuliu-zln committed Dec 25, 2024
1 parent 1f22af0 commit 8997e09
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 168 deletions.
99 changes: 61 additions & 38 deletions alicloud/resource_alicloud_ess_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package alicloud

import (
"fmt"
"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"strings"

"github.com/aliyun/alibaba-cloud-sdk-go/services/ess"
Expand Down Expand Up @@ -35,17 +38,24 @@ func resourceAlicloudEssNotification() *schema.Resource {
Required: true,
ForceNew: true,
},
"time_zone": {
Type: schema.TypeString,
Optional: true,
},
},
}
}

func resourceAlicloudEssNotificationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)

request := ess.CreateCreateNotificationConfigurationRequest()
request.RegionId = client.RegionId
request.ScalingGroupId = d.Get("scaling_group_id").(string)
request.NotificationArn = d.Get("notification_arn").(string)
var response map[string]interface{}
action := "CreateNotificationConfiguration"
request := make(map[string]interface{})
request["RegionId"] = client.RegionId
conn, err := client.NewEssClient()
request["ScalingGroupId"] = d.Get("scaling_group_id").(string)
request["NotificationArn"] = d.Get("notification_arn").(string)
request["TimeZone"] = d.Get("time_zone").(string)
if v, ok := d.GetOk("notification_types"); ok {
notificationTypes := make([]string, 0)
notificationTypeList := v.(*schema.Set).List()
Expand All @@ -55,18 +65,20 @@ func resourceAlicloudEssNotificationCreate(d *schema.ResourceData, meta interfac
}
}
if len(notificationTypes) > 0 {
request.NotificationType = &notificationTypes
request["NotificationType"] = &notificationTypes
}
}

raw, err := client.WithEssClient(func(essClient *ess.Client) (interface{}, error) {
return essClient.CreateNotificationConfiguration(request)
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2014-08-28"), StringPointer("AK"), nil, request, &runtime)
if err != nil {
return resource.RetryableError(err)
}
return nil
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_ess_notification", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
d.SetId(fmt.Sprintf("%s:%s", request.ScalingGroupId, request.NotificationArn))
addDebug(action, response, request)
d.SetId(fmt.Sprintf("%s:%s", request["ScalingGroupId"], request["NotificationArn"]))
return resourceAlicloudEssNotificationRead(d, meta)
}

Expand All @@ -81,39 +93,50 @@ func resourceAlicloudEssNotificationRead(d *schema.ResourceData, meta interface{
}
return WrapError(err)
}
d.Set("scaling_group_id", object.ScalingGroupId)
d.Set("notification_arn", object.NotificationArn)
d.Set("notification_types", object.NotificationTypes.NotificationType)
d.Set("scaling_group_id", object["ScalingGroupId"])
d.Set("notification_arn", object["NotificationArn"])
d.Set("time_zone", object["TimeZone"])
notificationTypes, _ := jsonpath.Get("$.NotificationTypes", object)
notificationType, _ := jsonpath.Get("$.NotificationType", notificationTypes)
d.Set("notification_types", notificationType)
return nil
}

func resourceAlicloudEssNotificationUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := ess.CreateModifyNotificationConfigurationRequest()
request.RegionId = client.RegionId
var response map[string]interface{}
action := "ModifyNotificationConfiguration"
request := make(map[string]interface{})
request["RegionId"] = client.RegionId
conn, err := client.NewEssClient()
request["RegionId"] = client.RegionId
parts := strings.SplitN(d.Id(), ":", 2)
request.ScalingGroupId = parts[0]
request.NotificationArn = parts[1]
if d.HasChange("notification_types") {
v := d.Get("notification_types")
notificationTypes := make([]string, 0)
notificationTypeList := v.(*schema.Set).List()
if len(notificationTypeList) > 0 {
for _, n := range notificationTypeList {
notificationTypes = append(notificationTypes, n.(string))
}
}
if len(notificationTypes) > 0 {
request.NotificationType = &notificationTypes
request["ScalingGroupId"] = parts[0]
request["NotificationArn"] = parts[1]
v := d.Get("notification_types")
notificationTypes := make([]string, 0)
notificationTypeList := v.(*schema.Set).List()
if len(notificationTypeList) > 0 {
for _, n := range notificationTypeList {
notificationTypes = append(notificationTypes, n.(string))
}
}
raw, err := client.WithEssClient(func(essClient *ess.Client) (interface{}, error) {
return essClient.ModifyNotificationConfiguration(request)
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
if len(notificationTypes) > 0 {
request["NotificationType"] = &notificationTypes
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
if d.HasChange("time_zone") {
request["TimeZone"] = d.Get("time_zone").(string)
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2014-08-28"), StringPointer("AK"), nil, request, &runtime)
if err != nil {
return resource.RetryableError(err)
}
return nil
})
addDebug(action, response, request)
return resourceAlicloudEssNotificationRead(d, meta)
}

Expand Down
Loading

0 comments on commit 8997e09

Please sign in to comment.