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

New Resource: alicloud_tag_associated_rule #8409

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions alicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,7 @@ func Provider() terraform.ResourceProvider {
"alicloud_esa_network_optimization": resourceAliCloudEsaNetworkOptimization(),
"alicloud_esa_origin_rule": resourceAliCloudEsaOriginRule(),
"alicloud_esa_https_application_configuration": resourceAliCloudEsaHttpsApplicationConfiguration(),
"alicloud_tag_associated_rule": resourceAliCloudTagAssociatedRule(),
"alicloud_esa_compression_rule": resourceAliCloudEsaCompressionRule(),
"alicloud_esa_https_basic_configuration": resourceAliCloudEsaHttpsBasicConfiguration(),
"alicloud_vpc_ipam_ipam_resource_discovery": resourceAliCloudVpcIpamIpamResourceDiscovery(),
Expand Down
220 changes: 220 additions & 0 deletions alicloud/resource_alicloud_tag_associated_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you!
package alicloud

import (
"encoding/json"
"fmt"
"log"
"time"

"github.com/PaesslerAG/jsonpath"
"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/tidwall/sjson"
)

func resourceAliCloudTagAssociatedRule() *schema.Resource {
return &schema.Resource{
Create: resourceAliCloudTagAssociatedRuleCreate,
Read: resourceAliCloudTagAssociatedRuleRead,
Update: resourceAliCloudTagAssociatedRuleUpdate,
Delete: resourceAliCloudTagAssociatedRuleDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"associated_setting_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"status": {
Type: schema.TypeString,
Required: true,
},
"tag_keys": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func resourceAliCloudTagAssociatedRuleCreate(d *schema.ResourceData, meta interface{}) error {

client := meta.(*connectivity.AliyunClient)

action := "CreateAssociatedResourceRules"
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
var err error
request = make(map[string]interface{})
request["RegionId"] = client.RegionId

objectDataLocalMap := make(map[string]interface{})

if v, ok := d.GetOk("tag_keys"); ok {
tagKeys1, _ := jsonpath.Get("$", v)
if tagKeys1 != nil && tagKeys1 != "" {
objectDataLocalMap["TagKeys"] = tagKeys1
}
}

if v, ok := d.GetOk("status"); ok {
objectDataLocalMap["Status"] = v
}

if v, ok := d.GetOk("associated_setting_name"); ok {
objectDataLocalMap["SettingName"] = v
}

CreateRulesListMap := make([]interface{}, 0)
CreateRulesListMap = append(CreateRulesListMap, objectDataLocalMap)
request["CreateRulesList"] = CreateRulesListMap

jsonString := convertObjectToJsonString(request)
jsonString, _ = sjson.Set(jsonString, "CreateRulesList.0.SettingName", d.Get("associated_setting_name"))
_ = json.Unmarshal([]byte(jsonString), &request)

wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = client.RpcPost("Tag", "2018-08-28", action, query, request, true)
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, DefaultErrorMsg, "alicloud_tag_associated_rule", action, AlibabaCloudSdkGoERROR)
}

id, _ := jsonpath.Get("CreateRulesList[0].SettingName", request)
d.SetId(fmt.Sprint(id))

return resourceAliCloudTagAssociatedRuleRead(d, meta)
}

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

objectRaw, err := tagServiceV2.DescribeTagAssociatedRule(d.Id())
if err != nil {
if !d.IsNewResource() && NotFoundError(err) {
log.Printf("[DEBUG] Resource alicloud_tag_associated_rule DescribeTagAssociatedRule Failed!!! %s", err)
d.SetId("")
return nil
}
return WrapError(err)
}

d.Set("status", objectRaw["Status"])
d.Set("associated_setting_name", objectRaw["SettingName"])

tagKeysRaw := make([]interface{}, 0)
if objectRaw["TagKeys"] != nil {
tagKeysRaw = objectRaw["TagKeys"].([]interface{})
}

d.Set("tag_keys", tagKeysRaw)

return nil
}

func resourceAliCloudTagAssociatedRuleUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var request map[string]interface{}
var response map[string]interface{}
var query map[string]interface{}
update := false

var err error
action := "UpdateAssociatedResourceRule"
request = make(map[string]interface{})
query = make(map[string]interface{})
request["SettingName"] = d.Id()
request["RegionId"] = client.RegionId
if d.HasChange("tag_keys") {
update = true
}
if v, ok := d.GetOk("tag_keys"); ok {
tagKeysMapsArray := v.([]interface{})
request["TagKeys"] = tagKeysMapsArray
}

if d.HasChange("status") {
update = true
}
request["Status"] = d.Get("status")
if update {
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = client.RpcPost("Tag", "2018-08-28", action, query, request, true)
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, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
}

return resourceAliCloudTagAssociatedRuleRead(d, meta)
}

func resourceAliCloudTagAssociatedRuleDelete(d *schema.ResourceData, meta interface{}) error {

client := meta.(*connectivity.AliyunClient)
action := "DeleteAssociatedResourceRule"
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
var err error
request = make(map[string]interface{})
request["SettingName"] = d.Id()
request["RegionId"] = client.RegionId

wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = client.RpcPost("Tag", "2018-08-28", action, query, request, true)

if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)

if err != nil {
if IsExpectedErrors(err, []string{"InvalidSettingName.NotFound"}) || NotFoundError(err) {
return nil
}
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}

return nil
}
Loading
Loading