-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource: alicloud_arms_alert_robot, New Data Source: alicloud_ar…
…ms_alert_robots
- Loading branch information
1 parent
d4b804a
commit 478c16a
Showing
8 changed files
with
1,165 additions
and
0 deletions.
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,201 @@ | ||
package alicloud | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/PaesslerAG/jsonpath" | ||
util "github.com/alibabacloud-go/tea-utils/service" | ||
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
) | ||
|
||
func dataSourceAlicloudArmsAlertRobots() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAlicloudArmsAlertRobotsRead, | ||
Schema: map[string]*schema.Schema{ | ||
"ids": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
ForceNew: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Computed: true, | ||
}, | ||
"name_regex": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.ValidateRegexp, | ||
ForceNew: true, | ||
}, | ||
"names": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Computed: true, | ||
}, | ||
"alert_robot_name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"robot_type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"output_file": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"robots": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"robot_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"robot_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"robot_type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"robot_addr": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"daily_noc": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"daily_noc_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"create_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAlicloudArmsAlertRobotsRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*connectivity.AliyunClient) | ||
|
||
action := "DescribeIMRobots" | ||
request := make(map[string]interface{}) | ||
if v, ok := d.GetOk("alert_robot_name"); ok { | ||
request["RobotName"] = v | ||
} | ||
request["Page"] = 1 | ||
request["Size"] = PageSizeXLarge | ||
var objects []map[string]interface{} | ||
var alertContactRobotNameRegex *regexp.Regexp | ||
if v, ok := d.GetOk("name_regex"); ok { | ||
r, err := regexp.Compile(v.(string)) | ||
if err != nil { | ||
return WrapError(err) | ||
} | ||
alertContactRobotNameRegex = r | ||
} | ||
|
||
idsMap := make(map[string]string) | ||
if v, ok := d.GetOk("ids"); ok { | ||
for _, vv := range v.([]interface{}) { | ||
if vv == nil { | ||
continue | ||
} | ||
idsMap[vv.(string)] = vv.(string) | ||
} | ||
} | ||
var response map[string]interface{} | ||
conn, err := client.NewArmsClient() | ||
if err != nil { | ||
return WrapError(err) | ||
} | ||
runtime := util.RuntimeOptions{} | ||
runtime.SetAutoretry(true) | ||
wait := incrementalWait(3*time.Second, 3*time.Second) | ||
err = resource.Retry(5*time.Minute, func() *resource.RetryError { | ||
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-08-08"), StringPointer("AK"), nil, request, &runtime) | ||
if err != nil { | ||
if NeedRetry(err) { | ||
wait() | ||
return resource.RetryableError(err) | ||
} | ||
return resource.NonRetryableError(err) | ||
} | ||
return nil | ||
}) | ||
addDebug(action, response, request) | ||
if err != nil { | ||
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_arms_alert_robots", action, AlibabaCloudSdkGoERROR) | ||
} | ||
resp, err := jsonpath.Get("$.PageBean.AlertIMRobots", response) | ||
if err != nil { | ||
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.PageBean.AlertIMRobots", response) | ||
} | ||
result, _ := resp.([]interface{}) | ||
for _, v := range result { | ||
item := v.(map[string]interface{}) | ||
if alertContactRobotNameRegex != nil && !alertContactRobotNameRegex.MatchString(fmt.Sprint(item["RobotName"])) { | ||
continue | ||
} | ||
if len(idsMap) > 0 { | ||
if _, ok := idsMap[fmt.Sprint(item["RobotId"])]; !ok { | ||
continue | ||
} | ||
} | ||
objects = append(objects, item) | ||
} | ||
ids := make([]string, 0) | ||
names := make([]interface{}, 0) | ||
s := make([]map[string]interface{}, 0) | ||
for _, object := range objects { | ||
mapping := map[string]interface{}{ | ||
"id": fmt.Sprint(object["RobotId"]), | ||
"robot_type": object["Type"], | ||
"robot_id": fmt.Sprint(object["RobotId"]), | ||
"robot_name": object["RobotName"], | ||
"robot_addr": object["RobotAddr"], | ||
"daily_noc": fmt.Sprint(object["DailyNoc"]), | ||
"daily_noc_time": object["DailyNocTime"], | ||
"create_time": fmt.Sprint(object["CreateTime"]), | ||
} | ||
ids = append(ids, fmt.Sprint(mapping["id"])) | ||
names = append(names, object["RobotName"]) | ||
s = append(s, mapping) | ||
} | ||
|
||
d.SetId(dataResourceIdHash(ids)) | ||
if err := d.Set("ids", ids); err != nil { | ||
return WrapError(err) | ||
} | ||
|
||
if err := d.Set("names", names); err != nil { | ||
return WrapError(err) | ||
} | ||
|
||
if err := d.Set("robots", s); err != nil { | ||
return WrapError(err) | ||
} | ||
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" { | ||
writeToFile(output.(string), s) | ||
} | ||
|
||
return nil | ||
} |
106 changes: 106 additions & 0 deletions
106
alicloud/data_source_alicloud_arms_alert_robots_test.go
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,106 @@ | ||
package alicloud | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
) | ||
|
||
func TestAccAlicloudARMSAlertRobotsDataSource(t *testing.T) { | ||
rand := acctest.RandInt() | ||
resourceId := "data.alicloud_arms_alert_robots.default" | ||
name := fmt.Sprintf("tf-testacc-ArmsAlertRobots%v", rand) | ||
|
||
testAccConfig := dataSourceTestAccConfigFunc(resourceId, name, dataSourceArmsAlertRobotsConfigDependence) | ||
|
||
nameRegexConf := dataSourceTestAccConfig{ | ||
existConfig: testAccConfig(map[string]interface{}{ | ||
"name_regex": "${alicloud_arms_alert_robot.default.alert_robot_name}", | ||
}), | ||
fakeConfig: testAccConfig(map[string]interface{}{ | ||
"name_regex": "fake_tf-testacc*", | ||
}), | ||
} | ||
idsConf := dataSourceTestAccConfig{ | ||
existConfig: testAccConfig(map[string]interface{}{ | ||
"ids": []string{"${alicloud_arms_alert_robot.default.id}"}, | ||
}), | ||
fakeConfig: testAccConfig(map[string]interface{}{ | ||
"ids": []string{"${alicloud_arms_alert_robot.default.id}_fake"}, | ||
}), | ||
} | ||
robotNameConf := dataSourceTestAccConfig{ | ||
existConfig: testAccConfig(map[string]interface{}{ | ||
"alert_robot_name": "${alicloud_arms_alert_robot.default.alert_robot_name}", | ||
}), | ||
fakeConfig: testAccConfig(map[string]interface{}{ | ||
"alert_robot_name": "${alicloud_arms_alert_robot.default.alert_robot_name}-fake", | ||
}), | ||
} | ||
allConf := dataSourceTestAccConfig{ | ||
existConfig: testAccConfig(map[string]interface{}{ | ||
"robot_type": "${alicloud_arms_alert_robot.default.robot_type}", | ||
"name_regex": "${alicloud_arms_alert_robot.default.alert_robot_name}", | ||
"ids": []string{"${alicloud_arms_alert_robot.default.id}"}, | ||
"alert_robot_name": "${alicloud_arms_alert_robot.default.alert_robot_name}", | ||
}), | ||
fakeConfig: testAccConfig(map[string]interface{}{ | ||
"robot_type": "${alicloud_arms_alert_robot.default.robot_type}", | ||
"name_regex": "${alicloud_arms_alert_robot.default.alert_robot_name}", | ||
"ids": []string{"${alicloud_arms_alert_robot.default.id}"}, | ||
"alert_robot_name": "${alicloud_arms_alert_robot.default.alert_robot_name}-fake", | ||
}), | ||
} | ||
existArmsAlertRobotsMapFunc := func(rand int) map[string]string { | ||
return map[string]string{ | ||
"ids.#": "1", | ||
"names.#": "1", | ||
"robots.#": "1", | ||
"robots.0.id": CHECKSET, | ||
"robots.0.robot_name": name, | ||
"robots.0.robot_type": "wechat", | ||
"robots.0.robot_id": CHECKSET, | ||
"robots.0.robot_addr": "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1c704e23", | ||
"robots.0.daily_noc": "true", | ||
"robots.0.daily_noc_time": "09:30,17:00", | ||
"robots.0.create_time": CHECKSET, | ||
} | ||
} | ||
|
||
fakeArmsAlertRobotsMapFunc := func(rand int) map[string]string { | ||
return map[string]string{ | ||
"robots.#": "0", | ||
"names.#": "0", | ||
"ids.#": "0", | ||
} | ||
} | ||
|
||
ArmsAlertRobotsCheckInfo := dataSourceAttr{ | ||
resourceId: resourceId, | ||
existMapFunc: existArmsAlertRobotsMapFunc, | ||
fakeMapFunc: fakeArmsAlertRobotsMapFunc, | ||
} | ||
preCheck := func() { | ||
testAccPreCheck(t) | ||
testAccPreCheckWithRegions(t, true, connectivity.ARMSSupportRegions) | ||
} | ||
ArmsAlertRobotsCheckInfo.dataSourceTestCheckWithPreCheck(t, rand, preCheck, nameRegexConf, idsConf, robotNameConf, allConf) | ||
} | ||
|
||
func dataSourceArmsAlertRobotsConfigDependence(name string) string { | ||
return fmt.Sprintf(` | ||
variable "name" { | ||
default = "%v" | ||
} | ||
resource "alicloud_arms_alert_robot" "default" { | ||
alert_robot_name = var.name | ||
robot_type = "wechat" | ||
robot_addr = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1c704e23" | ||
daily_noc = "true" | ||
daily_noc_time = "09:30,17:00" | ||
} | ||
`, name) | ||
} |
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
Oops, something went wrong.