Skip to content

Commit

Permalink
New Resource: alicloud_arms_alert_robot, New Data Source: alicloud_ar…
Browse files Browse the repository at this point in the history
…ms_alert_robots
  • Loading branch information
icloudnote committed Nov 29, 2024
1 parent d4b804a commit 37f98d9
Show file tree
Hide file tree
Showing 8 changed files with 1,165 additions and 0 deletions.
201 changes: 201 additions & 0 deletions alicloud/data_source_alicloud_arms_alert_robots.go
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 alicloud/data_source_alicloud_arms_alert_robots_test.go
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)
}
2 changes: 2 additions & 0 deletions alicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ func Provider() terraform.ResourceProvider {
"alicloud_hbr_vaults": dataSourceAlicloudHbrVaults(),
"alicloud_ssl_certificates_service_certificates": dataSourceAliCloudSslCertificatesServiceCertificates(),
"alicloud_arms_alert_contacts": dataSourceAlicloudArmsAlertContacts(),
"alicloud_arms_alert_robots": dataSourceAlicloudArmsAlertRobots(),
"alicloud_event_bridge_rules": dataSourceAlicloudEventBridgeRules(),
"alicloud_cloud_firewall_control_policies": dataSourceAliCloudCloudFirewallControlPolicies(),
"alicloud_sae_namespaces": dataSourceAlicloudSaeNamespaces(),
Expand Down Expand Up @@ -1418,6 +1419,7 @@ func Provider() terraform.ResourceProvider {
"alicloud_hbr_vault": resourceAliCloudHbrVault(),
"alicloud_ssl_certificates_service_certificate": resourceAliCloudSslCertificatesServiceCertificate(),
"alicloud_arms_alert_contact": resourceAlicloudArmsAlertContact(),
"alicloud_arms_alert_robot": resourceAlicloudArmsAlertRobot(),
"alicloud_event_bridge_slr": resourceAlicloudEventBridgeServiceLinkedRole(),
"alicloud_event_bridge_rule": resourceAliCloudEventBridgeRule(),
"alicloud_cloud_firewall_control_policy": resourceAliCloudCloudFirewallControlPolicy(),
Expand Down
Loading

0 comments on commit 37f98d9

Please sign in to comment.