-
Notifications
You must be signed in to change notification settings - Fork 553
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
juhuahaoteng
wants to merge
22
commits into
aliyun:master
Choose a base branch
from
juhuahaoteng:Lalibaba
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lalibaba #2098
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a2f5975
int to string
juhuahaoteng c024d5d
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng 33efe2e
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng f3968ac
parameters explain fault
juhuahaoteng a6f2bb9
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng dec339e
image markdown fault
juhuahaoteng 98d35da
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng 2fe83c5
go.mod
juhuahaoteng 6ff4701
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng 3906980
go.sum
juhuahaoteng 5ea74a7
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng 6f22f22
ess_alarm_first
juhuahaoteng f909227
ess_alarm_second
juhuahaoteng 75da931
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng 15c29dc
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng 6a7f496
Merge branch 'master' of https://github.com/terraform-providers/terra…
juhuahaoteng 6b9930b
datasource_ess_alarms update
juhuahaoteng f680f66
datasource_ess_alarms and it's test
juhuahaoteng ba18dbc
datasource_ess_alarms update
juhuahaoteng e498772
Update datasource_ess_alarm.go
juhuahaoteng 2d81bba
update data_source_alicloud_ess_alarms
juhuahaoteng 43ae5e2
update data_source_alicloud_ess_alarms.go
juhuahaoteng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,265 @@ | ||
package alicloud | ||
|
||
import ( | ||
"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" | ||
"regexp" | ||
) | ||
|
||
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, | ||
}, | ||
"IsEnable": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"Metric_type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
ValidateFunc: validation.StringInSlice([]string{"system", "custom"}, false), | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 增加state过滤参数 |
||
"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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
注意命名规则 不要用大写和驼峰方式的命名