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_cen_traffic_marking_policy
;
New Datasource `alicloud_cen_traffic_marking_policies`;
- Loading branch information
1 parent
21befa0
commit 215c77e
Showing
9 changed files
with
1,188 additions
and
0 deletions.
There are no files selected for viewing
218 changes: 218 additions & 0 deletions
218
alicloud/data_source_alicloud_cen_traffic_marking_policies.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,218 @@ | ||
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 dataSourceAlicloudCenTrafficMarkingPolicies() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAlicloudCenTrafficMarkingPoliciesRead, | ||
Schema: map[string]*schema.Schema{ | ||
"status": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringInSlice([]string{"Active", "Creating", "Deleting", "Updating"}, false), | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"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, | ||
}, | ||
"transit_router_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"output_file": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"policies": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"marking_dscp": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"priority": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"traffic_marking_policy_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"transit_router_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"traffic_marking_policy_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAlicloudCenTrafficMarkingPoliciesRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*connectivity.AliyunClient) | ||
|
||
action := "ListTrafficMarkingPolicies" | ||
request := make(map[string]interface{}) | ||
if v, ok := d.GetOk("description"); ok { | ||
request["TrafficMarkingPolicyDescription"] = v | ||
} | ||
request["TransitRouterId"] = d.Get("transit_router_id") | ||
request["MaxResults"] = PageSizeLarge | ||
var objects []map[string]interface{} | ||
var trafficMarkingPolicyNameRegex *regexp.Regexp | ||
if v, ok := d.GetOk("name_regex"); ok { | ||
r, err := regexp.Compile(v.(string)) | ||
if err != nil { | ||
return WrapError(err) | ||
} | ||
trafficMarkingPolicyNameRegex = 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) | ||
} | ||
} | ||
status, statusOk := d.GetOk("status") | ||
var response map[string]interface{} | ||
conn, err := client.NewCbnClient() | ||
if err != nil { | ||
return WrapError(err) | ||
} | ||
for { | ||
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("2017-09-12"), 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_cen_traffic_marking_policies", action, AlibabaCloudSdkGoERROR) | ||
} | ||
resp, err := jsonpath.Get("$.TrafficMarkingPolicies", response) | ||
if err != nil { | ||
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.TrafficMarkingPolicies", response) | ||
} | ||
result, _ := resp.([]interface{}) | ||
for _, v := range result { | ||
item := v.(map[string]interface{}) | ||
if trafficMarkingPolicyNameRegex != nil && !trafficMarkingPolicyNameRegex.MatchString(fmt.Sprint(item["TrafficMarkingPolicyName"])) { | ||
continue | ||
} | ||
if len(idsMap) > 0 { | ||
if _, ok := idsMap[fmt.Sprint(request["TransitRouterId"], ":", item["TrafficMarkingPolicyId"])]; !ok { | ||
continue | ||
} | ||
} | ||
if statusOk && status.(string) != "" && status.(string) != item["TrafficMarkingPolicyStatus"].(string) { | ||
continue | ||
} | ||
objects = append(objects, item) | ||
} | ||
if nextToken, ok := response["NextToken"].(string); ok && nextToken != "" { | ||
request["NextToken"] = nextToken | ||
} else { | ||
break | ||
} | ||
} | ||
ids := make([]string, 0) | ||
names := make([]interface{}, 0) | ||
s := make([]map[string]interface{}, 0) | ||
for _, object := range objects { | ||
mapping := map[string]interface{}{ | ||
"description": object["TrafficMarkingPolicyDescription"], | ||
"marking_dscp": formatInt(object["MarkingDscp"]), | ||
"priority": formatInt(object["Priority"]), | ||
"status": object["TrafficMarkingPolicyStatus"], | ||
"transit_router_id": request["TransitRouterId"], | ||
"id": fmt.Sprint(request["TransitRouterId"], ":", object["TrafficMarkingPolicyId"]), | ||
"traffic_marking_policy_id": fmt.Sprint(object["TrafficMarkingPolicyId"]), | ||
"traffic_marking_policy_name": object["TrafficMarkingPolicyName"], | ||
} | ||
ids = append(ids, fmt.Sprint(mapping["id"])) | ||
names = append(names, object["TrafficMarkingPolicyName"]) | ||
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("policies", s); err != nil { | ||
return WrapError(err) | ||
} | ||
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" { | ||
writeToFile(output.(string), s) | ||
} | ||
|
||
return nil | ||
} |
136 changes: 136 additions & 0 deletions
136
alicloud/data_source_alicloud_cen_traffic_marking_policies_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,136 @@ | ||
package alicloud | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
) | ||
|
||
func TestAccAlicloudCenTrafficMarkingPoliciesDataSource(t *testing.T) { | ||
rand := acctest.RandIntRange(1000000, 9999999) | ||
nameRegexConf := dataSourceTestAccConfig{ | ||
existConfig: testAccCheckAlicloudCenTrafficMarkingPoliciesSourceConfig(rand, map[string]string{ | ||
"name_regex": `"${alicloud_cen_traffic_marking_policy.default.traffic_marking_policy_name}"`, | ||
}), | ||
fakeConfig: testAccCheckAlicloudCenTrafficMarkingPoliciesSourceConfig(rand, map[string]string{ | ||
"name_regex": `"${alicloud_cen_traffic_marking_policy.default.traffic_marking_policy_name}_fake"`, | ||
}), | ||
} | ||
|
||
idsConf := dataSourceTestAccConfig{ | ||
existConfig: testAccCheckAlicloudCenTrafficMarkingPoliciesSourceConfig(rand, map[string]string{ | ||
"ids": `["${alicloud_cen_traffic_marking_policy.default.id}"]`, | ||
}), | ||
fakeConfig: testAccCheckAlicloudCenTrafficMarkingPoliciesSourceConfig(rand, map[string]string{ | ||
"ids": `["${alicloud_cen_traffic_marking_policy.default.id}_fake"]`, | ||
}), | ||
} | ||
|
||
descriptionConf := dataSourceTestAccConfig{ | ||
existConfig: testAccCheckAlicloudCenTrafficMarkingPoliciesSourceConfig(rand, map[string]string{ | ||
"ids": `["${alicloud_cen_traffic_marking_policy.default.id}"]`, | ||
"description": `"${alicloud_cen_traffic_marking_policy.default.description}"`, | ||
}), | ||
fakeConfig: testAccCheckAlicloudCenTrafficMarkingPoliciesSourceConfig(rand, map[string]string{ | ||
"ids": `["${alicloud_cen_traffic_marking_policy.default.id}"]`, | ||
"description": `"${alicloud_cen_traffic_marking_policy.default.description}_fake"`, | ||
}), | ||
} | ||
|
||
statusConf := dataSourceTestAccConfig{ | ||
existConfig: testAccCheckAlicloudCenTrafficMarkingPoliciesSourceConfig(rand, map[string]string{ | ||
"ids": `["${alicloud_cen_traffic_marking_policy.default.id}"]`, | ||
"status": `"Active"`, | ||
}), | ||
fakeConfig: testAccCheckAlicloudCenTrafficMarkingPoliciesSourceConfig(rand, map[string]string{ | ||
"ids": `["${alicloud_cen_traffic_marking_policy.default.id}"]`, | ||
"status": `"Creating"`, | ||
}), | ||
} | ||
|
||
allConf := dataSourceTestAccConfig{ | ||
existConfig: testAccCheckAlicloudCenTrafficMarkingPoliciesSourceConfig(rand, map[string]string{ | ||
"ids": `["${alicloud_cen_traffic_marking_policy.default.id}"]`, | ||
"name_regex": `"${alicloud_cen_traffic_marking_policy.default.traffic_marking_policy_name}"`, | ||
"description": `"${alicloud_cen_traffic_marking_policy.default.description}"`, | ||
"status": `"Active"`, | ||
}), | ||
fakeConfig: testAccCheckAlicloudCenTrafficMarkingPoliciesSourceConfig(rand, map[string]string{ | ||
"ids": `["${alicloud_cen_traffic_marking_policy.default.id}_fake"]`, | ||
"name_regex": `"${alicloud_cen_traffic_marking_policy.default.traffic_marking_policy_name}_fake"`, | ||
"description": `"${alicloud_cen_traffic_marking_policy.default.description}_fake"`, | ||
"status": `"Creating"`, | ||
}), | ||
} | ||
|
||
var existCenTrafficMarkingPoliciesMapFunc = func(rand int) map[string]string { | ||
return map[string]string{ | ||
"ids.#": "1", | ||
"policies.#": "1", | ||
"policies.0.priority": "1", | ||
"policies.0.description": fmt.Sprintf("tf-testAccCenTrafficMarkingPolicies%d", rand), | ||
"policies.0.traffic_marking_policy_name": fmt.Sprintf("tf-testAccCenTrafficMarkingPolicies%d", rand), | ||
"policies.0.traffic_marking_policy_id": CHECKSET, | ||
"policies.0.marking_dscp": "1", | ||
"policies.0.transit_router_id": CHECKSET, | ||
"policies.0.status": "Active", | ||
"policies.0.id": CHECKSET, | ||
} | ||
} | ||
|
||
var fakeCenTrafficMarkingPoliciesMapFunc = func(rand int) map[string]string { | ||
return map[string]string{ | ||
"ids.#": "0", | ||
"policies.#": "0", | ||
} | ||
} | ||
|
||
var cenTrafficMarkingPoliciesCheckInfo = dataSourceAttr{ | ||
resourceId: "data.alicloud_cen_traffic_marking_policies.default", | ||
existMapFunc: existCenTrafficMarkingPoliciesMapFunc, | ||
fakeMapFunc: fakeCenTrafficMarkingPoliciesMapFunc, | ||
} | ||
preCheck := func() { | ||
testAccPreCheck(t) | ||
} | ||
|
||
cenTrafficMarkingPoliciesCheckInfo.dataSourceTestCheckWithPreCheck(t, rand, preCheck, nameRegexConf, idsConf, statusConf, descriptionConf, allConf) | ||
|
||
} | ||
|
||
func testAccCheckAlicloudCenTrafficMarkingPoliciesSourceConfig(rand int, attrMap map[string]string) string { | ||
var pairs []string | ||
for k, v := range attrMap { | ||
pairs = append(pairs, k+" = "+v) | ||
} | ||
config := fmt.Sprintf(` | ||
variable "name" { | ||
default = "tf-testAccCenTrafficMarkingPolicies%d" | ||
} | ||
resource "alicloud_cen_instance" "default" { | ||
cen_instance_name = var.name | ||
} | ||
resource "alicloud_cen_transit_router" "default" { | ||
cen_id = alicloud_cen_instance.default.id | ||
transit_router_name = var.name | ||
} | ||
resource "alicloud_cen_traffic_marking_policy" "default" { | ||
marking_dscp = 1 | ||
priority = 1 | ||
traffic_marking_policy_name = var.name | ||
description = var.name | ||
transit_router_id = alicloud_cen_transit_router.default.transit_router_id | ||
} | ||
data "alicloud_cen_traffic_marking_policies" "default" { | ||
transit_router_id = alicloud_cen_transit_router.default.transit_router_id | ||
%s | ||
} | ||
`, rand, strings.Join(pairs, "\n ")) | ||
return config | ||
} |
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.