-
Notifications
You must be signed in to change notification settings - Fork 140
feat(tse): [115375235] support tse network access_control #2581
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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,3 @@ | ||
```release-note:new-resource | ||
tencentcloud_tse_cngw_network_access_control | ||
``` |
This file contains hidden or 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
This file contains hidden or 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
235 changes: 235 additions & 0 deletions
235
tencentcloud/services/tse/resource_tc_tse_cngw_network_access_control.go
This file contains hidden or 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,235 @@ | ||
package tse | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
tse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse/v20201207" | ||
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" | ||
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" | ||
) | ||
|
||
func ResourceTencentCloudTseCngwNetworkAccessControl() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceTencentCloudTseCngwNetworkAccessControlCreate, | ||
Read: resourceTencentCloudTseCngwNetworkAccessControlRead, | ||
Update: resourceTencentCloudTseCngwNetworkAccessControlUpdate, | ||
Delete: resourceTencentCloudTseCngwNetworkAccessControlDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"gateway_id": { | ||
Required: true, | ||
ForceNew: true, | ||
Type: schema.TypeString, | ||
Description: "gateway ID.", | ||
}, | ||
|
||
"group_id": { | ||
Required: true, | ||
ForceNew: true, | ||
Type: schema.TypeString, | ||
Description: "gateway group ID.", | ||
}, | ||
|
||
"network_id": { | ||
Required: true, | ||
ForceNew: true, | ||
Type: schema.TypeString, | ||
Description: "network id.", | ||
}, | ||
|
||
"access_control": { | ||
Type: schema.TypeList, | ||
MaxItems: 1, | ||
Optional: true, | ||
Description: "access control policy.", | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"mode": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
Description: "Access mode: `Whitelist`, `Blacklist`.", | ||
}, | ||
"cidr_white_list": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Description: "White list.", | ||
}, | ||
"cidr_black_list": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Description: "Black list.", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceTencentCloudTseCngwNetworkAccessControlCreate(d *schema.ResourceData, meta interface{}) error { | ||
defer tccommon.LogElapsed("resource.tencentcloud_tse_cngw_network_access_control.create")() | ||
defer tccommon.InconsistentCheck(d, meta)() | ||
|
||
var ( | ||
gatewayId string | ||
groupId string | ||
networkId string | ||
) | ||
if v, ok := d.GetOk("gateway_id"); ok { | ||
gatewayId = v.(string) | ||
} | ||
if v, ok := d.GetOk("group_id"); ok { | ||
groupId = v.(string) | ||
} | ||
if v, ok := d.GetOk("network_id"); ok { | ||
networkId = v.(string) | ||
} | ||
d.SetId(gatewayId + tccommon.FILED_SP + groupId + tccommon.FILED_SP + networkId) | ||
|
||
return resourceTencentCloudTseCngwNetworkAccessControlUpdate(d, meta) | ||
} | ||
|
||
func resourceTencentCloudTseCngwNetworkAccessControlRead(d *schema.ResourceData, meta interface{}) error { | ||
defer tccommon.LogElapsed("resource.tencentcloud_tse_cngw_network_access_control.read")() | ||
defer tccommon.InconsistentCheck(d, meta)() | ||
|
||
logId := tccommon.GetLogId(tccommon.ContextNil) | ||
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) | ||
|
||
service := TseService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} | ||
|
||
idSplit := strings.Split(d.Id(), tccommon.FILED_SP) | ||
if len(idSplit) != 3 { | ||
return fmt.Errorf("id is broken,%s", d.Id()) | ||
} | ||
gatewayId := idSplit[0] | ||
groupId := idSplit[1] | ||
networkId := idSplit[2] | ||
|
||
cngwNetwork, err := service.DescribeTseCngwNetworkById(ctx, gatewayId, groupId, networkId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if cngwNetwork == nil { | ||
d.SetId("") | ||
log.Printf("[WARN]%s resource `TseCngwNetworkAccessControl` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) | ||
return nil | ||
} | ||
|
||
_ = d.Set("gateway_id", gatewayId) | ||
_ = d.Set("group_id", groupId) | ||
_ = d.Set("network_id", networkId) | ||
|
||
if cngwNetwork.PublicNetwork != nil { | ||
internetConfig := cngwNetwork.PublicNetwork | ||
if internetConfig.AccessControl != nil { | ||
accessControlMap := map[string]interface{}{} | ||
|
||
accessControl := internetConfig.AccessControl | ||
if accessControl.Mode != nil { | ||
accessControlMap["mode"] = accessControl.Mode | ||
} | ||
if accessControl.Mode != nil { | ||
accessControlMap["cidr_white_list"] = accessControl.CidrWhiteList | ||
} | ||
if accessControl.Mode != nil { | ||
accessControlMap["cidr_black_list"] = accessControl.CidrBlackList | ||
} | ||
_ = d.Set("access_control", []interface{}{accessControlMap}) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceTencentCloudTseCngwNetworkAccessControlUpdate(d *schema.ResourceData, meta interface{}) error { | ||
defer tccommon.LogElapsed("resource.tencentcloud_tse_cngw_network_access_control.update")() | ||
defer tccommon.InconsistentCheck(d, meta)() | ||
|
||
logId := tccommon.GetLogId(tccommon.ContextNil) | ||
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) | ||
|
||
request := tse.NewModifyNetworkAccessStrategyRequest() | ||
|
||
idSplit := strings.Split(d.Id(), tccommon.FILED_SP) | ||
if len(idSplit) != 3 { | ||
return fmt.Errorf("id is broken,%s", d.Id()) | ||
} | ||
gatewayId := idSplit[0] | ||
groupId := idSplit[1] | ||
networkId := idSplit[2] | ||
|
||
service := TseService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} | ||
cngwNetwork, err := service.DescribeTseCngwNetworkById(ctx, gatewayId, groupId, networkId) | ||
if err != nil { | ||
return err | ||
} | ||
if cngwNetwork == nil { | ||
return fmt.Errorf("[WARN]%s resource `TseCngwNetworkAccessControl` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) | ||
} | ||
|
||
request.GatewayId = helper.String(gatewayId) | ||
request.GroupId = helper.String(groupId) | ||
// The interface only supports public network | ||
request.NetworkType = helper.String("Open") | ||
request.Vip = cngwNetwork.PublicNetwork.Vip | ||
|
||
if d.HasChange("access_control") { | ||
if dMap, ok := helper.InterfacesHeadMap(d, "access_control"); ok { | ||
accessControl := tse.NetworkAccessControl{} | ||
if v, ok := dMap["mode"]; ok { | ||
accessControl.Mode = helper.String(v.(string)) | ||
} | ||
if v, ok := dMap["cidr_white_list"]; ok { | ||
whitelist := v.([]interface{}) | ||
accessControl.CidrWhiteList = helper.InterfacesStringsPoint(whitelist) | ||
} | ||
if v, ok := dMap["cidr_black_list"]; ok { | ||
blacklist := v.([]interface{}) | ||
accessControl.CidrBlackList = helper.InterfacesStringsPoint(blacklist) | ||
} | ||
request.AccessControl = &accessControl | ||
} | ||
} | ||
|
||
err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { | ||
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTseClient().ModifyNetworkAccessStrategy(request) | ||
if e != nil { | ||
return tccommon.RetryError(e) | ||
} else { | ||
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
log.Printf("[CRITAL]%s update tse cngwNetworkAccessStrategy failed, reason:%+v", logId, err) | ||
return err | ||
} | ||
|
||
conf := tccommon.BuildStateChangeConf([]string{}, []string{"Open"}, 5*tccommon.ReadRetryTimeout, time.Second, service.TseCngwNetworkStateRefreshFunc(gatewayId, groupId, networkId, []string{})) | ||
|
||
if _, e := conf.WaitForState(); e != nil { | ||
return e | ||
} | ||
|
||
return resourceTencentCloudTseCngwNetworkAccessControlRead(d, meta) | ||
} | ||
|
||
func resourceTencentCloudTseCngwNetworkAccessControlDelete(d *schema.ResourceData, meta interface{}) error { | ||
defer tccommon.LogElapsed("resource.tencentcloud_tse_cngw_network_access_control.delete")() | ||
defer tccommon.InconsistentCheck(d, meta)() | ||
|
||
return nil | ||
} |
23 changes: 23 additions & 0 deletions
23
tencentcloud/services/tse/resource_tc_tse_cngw_network_access_control.md
This file contains hidden or 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,23 @@ | ||
Provides a resource to create a tse cngw_network_access_control | ||
|
||
Example Usage | ||
|
||
```hcl | ||
resource "tencentcloud_tse_cngw_network_access_control" "cngw_network_access_control" { | ||
gateway_id = "gateway-cf8c99c3" | ||
group_id = "group-a160d123" | ||
network_id = "network-372b1e84" | ||
access_control { | ||
mode = "Whitelist" | ||
cidr_white_list = ["1.1.1.0"] | ||
} | ||
} | ||
``` | ||
|
||
Import | ||
|
||
tse cngw_route_rate_limit can be imported using the id, e.g. | ||
|
||
``` | ||
terraform import tencentcloud_tse_cngw_network_access_control.cngw_network_access_control gatewayId#groupId#networkId | ||
``` |
48 changes: 48 additions & 0 deletions
48
tencentcloud/services/tse/resource_tc_tse_cngw_network_access_control_test.go
This file contains hidden or 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,48 @@ | ||
package tse_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest" | ||
) | ||
|
||
func TestAccTencentCloudNeedFixTseCngwNetworkAccessControlResource_basic(t *testing.T) { | ||
t.Parallel() | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
tcacctest.AccPreCheck(t) | ||
}, | ||
Providers: tcacctest.AccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccTseCngwNetworkAccessControl, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("tencentcloud_tse_cngw_network_access_control.cngw_network_access_control", "id"), | ||
resource.TestCheckResourceAttr("tencentcloud_tse_cngw_network_access_control.cngw_network_access_control", "access_control.#", "1"), | ||
resource.TestCheckResourceAttr("tencentcloud_tse_cngw_network_access_control.cngw_network_access_control", "access_control.0.mode", "Whitelist"), | ||
resource.TestCheckResourceAttr("tencentcloud_tse_cngw_network_access_control.cngw_network_access_control", "access_control.0.cidr_white_list.#", "1"), | ||
), | ||
}, | ||
{ | ||
ResourceName: "tencentcloud_tse_cngw_network_access_control.cngw_network_access_control", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccTseCngwNetworkAccessControl = ` | ||
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. 这里 lint 检查没有通过 |
||
|
||
resource "tencentcloud_tse_cngw_network_access_control" "cngw_network_access_control" { | ||
gateway_id = "gateway-cf1790c7" | ||
group_id = "group-d8d99615" | ||
network_id = "network-9cd9821f" | ||
access_control { | ||
mode = "Whitelist" | ||
cidr_white_list = ["1.1.1.0"] | ||
} | ||
} | ||
|
||
` |
58 changes: 58 additions & 0 deletions
58
website/docs/r/tse_cngw_network_access_control.html.markdown
This file contains hidden or 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,58 @@ | ||
--- | ||
subcategory: "Tencent Cloud Service Engine(TSE)" | ||
layout: "tencentcloud" | ||
page_title: "TencentCloud: tencentcloud_tse_cngw_network_access_control" | ||
sidebar_current: "docs-tencentcloud-resource-tse_cngw_network_access_control" | ||
description: |- | ||
Provides a resource to create a tse cngw_network_access_control | ||
--- | ||
|
||
# tencentcloud_tse_cngw_network_access_control | ||
|
||
Provides a resource to create a tse cngw_network_access_control | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "tencentcloud_tse_cngw_network_access_control" "cngw_network_access_control" { | ||
gateway_id = "gateway-cf8c99c3" | ||
group_id = "group-a160d123" | ||
network_id = "network-372b1e84" | ||
access_control { | ||
mode = "Whitelist" | ||
cidr_white_list = ["1.1.1.0"] | ||
} | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `gateway_id` - (Required, String, ForceNew) gateway ID. | ||
* `group_id` - (Required, String, ForceNew) gateway group ID. | ||
* `network_id` - (Required, String, ForceNew) network id. | ||
* `access_control` - (Optional, List) access control policy. | ||
|
||
The `access_control` object supports the following: | ||
|
||
* `cidr_black_list` - (Optional, List) Black list. | ||
* `cidr_white_list` - (Optional, List) White list. | ||
* `mode` - (Optional, String) Access mode: `Whitelist`, `Blacklist`. | ||
|
||
## Attributes Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - ID of the resource. | ||
|
||
|
||
|
||
## Import | ||
|
||
tse cngw_route_rate_limit can be imported using the id, e.g. | ||
|
||
``` | ||
terraform import tencentcloud_tse_cngw_network_access_control.cngw_network_access_control gatewayId#groupId#networkId | ||
``` | ||
|
This file contains hidden or 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.
测试用例修改资源的场景没有覆盖
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.
这个是个config
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.
config 应该也支持修改吧