Skip to content
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

Lalibaba #2098

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a2f5975
int to string
juhuahaoteng Jan 7, 2020
c024d5d
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng Jan 7, 2020
33efe2e
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng Jan 8, 2020
f3968ac
parameters explain fault
juhuahaoteng Jan 8, 2020
a6f2bb9
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng Jan 8, 2020
dec339e
image markdown fault
juhuahaoteng Jan 8, 2020
98d35da
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng Jan 8, 2020
2fe83c5
go.mod
juhuahaoteng Jan 9, 2020
6ff4701
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng Jan 9, 2020
3906980
go.sum
juhuahaoteng Jan 13, 2020
5ea74a7
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng Jan 13, 2020
6f22f22
ess_alarm_first
juhuahaoteng Jan 13, 2020
f909227
ess_alarm_second
juhuahaoteng Jan 13, 2020
75da931
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng Jan 14, 2020
15c29dc
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng Jan 15, 2020
6a7f496
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng Jan 15, 2020
6b9930b
datasource_ess_alarms update
juhuahaoteng Jan 15, 2020
f680f66
datasource_ess_alarms and it's test
juhuahaoteng Jan 15, 2020
ba18dbc
datasource_ess_alarms update
juhuahaoteng Jan 15, 2020
e498772
Update datasource_ess_alarm.go
juhuahaoteng Jan 15, 2020
2d81bba
update data_source_alicloud_ess_alarms
juhuahaoteng Jan 15, 2020
43ae5e2
update data_source_alicloud_ess_alarms.go
juhuahaoteng Jan 15, 2020
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
272 changes: 272 additions & 0 deletions alicloud/data_source_alicloud_ess_alarms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
package alicloud

import (
"regexp"

"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ess"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)

func dataSourceAlicloudEssAlarms() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudEssAlarmsRead,
Schema: map[string]*schema.Schema{
"scaling_group_id": {
Type: schema.TypeString,
Optional: true,
},
"name_regex": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.ValidateRegexp,
},
"ids": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
"enable": {
Type: schema.TypeBool,
Computed: true,
Optional: true,
},
"metric_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"system", "custom"}, false),
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

增加state过滤参数

"states": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个可以是state或者states,但是后面的代码都没有用到这个参数

Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"OK", "ALARM", "INSUFFICIENT_DATA"}, false),
},
"alarms": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"enable": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"alarm_actions": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Required: true,
MaxItems: 5,
MinItems: 1,
},
"scaling_group_id": {
Type: schema.TypeString,
Required: true,
},
"metric_type": {
Type: schema.TypeString,
Optional: true,
Default: System,
ValidateFunc: validation.StringInSlice([]string{
string(System),
string(Custom),
}, false),
},
"metric_name": {
Type: schema.TypeString,
Required: true,
},
"period": {
Type: schema.TypeInt,
Optional: true,
Default: FiveMinite,
ValidateFunc: validation.IntInSlice([]int{
int(OneMinite),
int(TwoMinite),
int(FiveMinite),
int(FifteenMinite),
}),
},
"statistics": {
Type: schema.TypeString,
Optional: true,
Default: Average,
ValidateFunc: validation.StringInSlice([]string{
string(Average),
string(Minimum),
string(Maximum),
}, false),
},
"threshold": {
Type: schema.TypeString,
Required: true,
},
"comparison_operator": {
Type: schema.TypeString,
Optional: true,
Default: Gte,
ValidateFunc: validation.StringInSlice([]string{
string(Gt),
string(Gte),
string(Lt),
string(Lte),
}, false),
},
"evaluation_count": {
Type: schema.TypeInt,
Optional: true,
Default: 3,
ValidateFunc: validation.IntAtLeast(0),
},
"cloud_monitor_group_id": {
Type: schema.TypeInt,
Optional: true,
},
"dimensions": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceAlicloudEssAlarmsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := ess.CreateDescribeAlarmsRequest()
request.RegionId = client.RegionId
request.PageSize = requests.NewInteger(PageSizeLarge)
request.PageNumber = requests.NewInteger(1)

if scalingGroupId, ok := d.GetOk("scaling_group_id"); ok && scalingGroupId.(string) != "" {
request.ScalingGroupId = scalingGroupId.(string)
}
if isenable, ok := d.GetOk("IsEnable"); ok && isenable.(requests.Boolean) != "" {
request.IsEnable = isenable.(requests.Boolean)
}
if metric_type, ok := d.GetOk("Metric_type"); ok && metric_type.(string) != "" {
request.MetricType = metric_type.(string)
}
var allAlarms []ess.Alarm
for {
raw, err := client.WithEssClient(func(essClient *ess.Client) (interface{}, error) {
return essClient.DescribeAlarms(request)
})
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_ess_alarms", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
response := raw.(*ess.DescribeAlarmsResponse)
if len(response.AlarmList.Alarm) < 1 {
break
}
allAlarms = append(allAlarms, response.AlarmList.Alarm...)
if len(response.AlarmList.Alarm) < PageSizeLarge {
break
}
if page, err := getNextpageNumber(request.PageNumber); err != nil {
return WrapError(err)
} else {
request.PageNumber = page
}
}
var filteredAlarms = make([]ess.Alarm, 0)

nameRegex, okNameRegex := d.GetOk("name_regex")
idsMap := make(map[string]string)
ids, okIds := d.GetOk("ids")
if okIds {
for _, i := range ids.([]interface{}) {
idsMap[i.(string)] = i.(string)
}
}

if okNameRegex || okIds {
for _, alarm := range allAlarms {
if okNameRegex && nameRegex != "" {
var r = regexp.MustCompile(nameRegex.(string))
if r != nil && !r.MatchString(alarm.AlarmTaskName) {
continue
}
}
if okIds && len(idsMap) > 0 {
if _, ok := idsMap[alarm.AlarmTaskId]; !ok {
continue
}
}
filteredAlarms = append(filteredAlarms, alarm)
}
} else {
filteredAlarms = allAlarms
}

return alarmsDescriptionAttribute(d, filteredAlarms, meta)
}

func alarmsDescriptionAttribute(d *schema.ResourceData, alarms []ess.Alarm, meta interface{}) error {
var ids []string
var names []string
var s = make([]map[string]interface{}, 0)
for _, alarm := range alarms {
mapping := map[string]interface{}{
"state": alarm.State,
"task_id": alarm.AlarmTaskId,
"name": alarm.Name,
"scaling_group_id": alarm.ScalingGroupId,
"metric_name": alarm.MetricName,
"description": alarm.Description,
"enable": alarm.Enable,
"alarm_actions": alarm.AlarmActions,
"metric_type": alarm.MetricType,
"period": alarm.Period,
"statistics": alarm.Statistics,
"threshold": alarm.Threshold,
"comparison_operator": alarm.ComparisonOperator,
"evaluation_count": alarm.EvaluationCount,
"dimensions": alarm.Dimensions,
}
ids = append(ids, alarm.AlarmTaskId)
names = append(names, alarm.Name)
s = append(s, mapping)
}
d.SetId(dataResourceIdHash(ids))

if err := d.Set("alarms", s); err != nil {
return WrapError(err)
}

if err := d.Set("ids", ids); err != nil {
return WrapError(err)
}

if err := d.Set("names", names); err != nil {
return WrapError(err)
}

if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
}
return nil
}
Loading