|
| 1 | +package tse |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + tse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse/v20201207" |
| 13 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 14 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 15 | +) |
| 16 | + |
| 17 | +func ResourceTencentCloudTseCngwNetworkAccessControl() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Create: resourceTencentCloudTseCngwNetworkAccessControlCreate, |
| 20 | + Read: resourceTencentCloudTseCngwNetworkAccessControlRead, |
| 21 | + Update: resourceTencentCloudTseCngwNetworkAccessControlUpdate, |
| 22 | + Delete: resourceTencentCloudTseCngwNetworkAccessControlDelete, |
| 23 | + Importer: &schema.ResourceImporter{ |
| 24 | + State: schema.ImportStatePassthrough, |
| 25 | + }, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "gateway_id": { |
| 28 | + Required: true, |
| 29 | + ForceNew: true, |
| 30 | + Type: schema.TypeString, |
| 31 | + Description: "gateway ID.", |
| 32 | + }, |
| 33 | + |
| 34 | + "group_id": { |
| 35 | + Required: true, |
| 36 | + ForceNew: true, |
| 37 | + Type: schema.TypeString, |
| 38 | + Description: "gateway group ID.", |
| 39 | + }, |
| 40 | + |
| 41 | + "network_id": { |
| 42 | + Required: true, |
| 43 | + ForceNew: true, |
| 44 | + Type: schema.TypeString, |
| 45 | + Description: "network id.", |
| 46 | + }, |
| 47 | + |
| 48 | + "access_control": { |
| 49 | + Type: schema.TypeList, |
| 50 | + MaxItems: 1, |
| 51 | + Optional: true, |
| 52 | + Description: "access control policy.", |
| 53 | + Elem: &schema.Resource{ |
| 54 | + Schema: map[string]*schema.Schema{ |
| 55 | + "mode": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Optional: true, |
| 58 | + Computed: true, |
| 59 | + Description: "Access mode: `Whitelist`, `Blacklist`.", |
| 60 | + }, |
| 61 | + "cidr_white_list": { |
| 62 | + Type: schema.TypeList, |
| 63 | + Optional: true, |
| 64 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 65 | + Description: "White list.", |
| 66 | + }, |
| 67 | + "cidr_black_list": { |
| 68 | + Type: schema.TypeList, |
| 69 | + Optional: true, |
| 70 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 71 | + Description: "Black list.", |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func resourceTencentCloudTseCngwNetworkAccessControlCreate(d *schema.ResourceData, meta interface{}) error { |
| 81 | + defer tccommon.LogElapsed("resource.tencentcloud_tse_cngw_network_access_control.create")() |
| 82 | + defer tccommon.InconsistentCheck(d, meta)() |
| 83 | + |
| 84 | + var ( |
| 85 | + gatewayId string |
| 86 | + groupId string |
| 87 | + networkId string |
| 88 | + ) |
| 89 | + if v, ok := d.GetOk("gateway_id"); ok { |
| 90 | + gatewayId = v.(string) |
| 91 | + } |
| 92 | + if v, ok := d.GetOk("group_id"); ok { |
| 93 | + groupId = v.(string) |
| 94 | + } |
| 95 | + if v, ok := d.GetOk("network_id"); ok { |
| 96 | + networkId = v.(string) |
| 97 | + } |
| 98 | + d.SetId(gatewayId + tccommon.FILED_SP + groupId + tccommon.FILED_SP + networkId) |
| 99 | + |
| 100 | + return resourceTencentCloudTseCngwNetworkAccessControlUpdate(d, meta) |
| 101 | +} |
| 102 | + |
| 103 | +func resourceTencentCloudTseCngwNetworkAccessControlRead(d *schema.ResourceData, meta interface{}) error { |
| 104 | + defer tccommon.LogElapsed("resource.tencentcloud_tse_cngw_network_access_control.read")() |
| 105 | + defer tccommon.InconsistentCheck(d, meta)() |
| 106 | + |
| 107 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 108 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 109 | + |
| 110 | + service := TseService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 111 | + |
| 112 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 113 | + if len(idSplit) != 3 { |
| 114 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 115 | + } |
| 116 | + gatewayId := idSplit[0] |
| 117 | + groupId := idSplit[1] |
| 118 | + networkId := idSplit[2] |
| 119 | + |
| 120 | + cngwNetwork, err := service.DescribeTseCngwNetworkById(ctx, gatewayId, groupId, networkId) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + if cngwNetwork == nil { |
| 126 | + d.SetId("") |
| 127 | + log.Printf("[WARN]%s resource `TseCngwNetworkAccessControl` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 128 | + return nil |
| 129 | + } |
| 130 | + |
| 131 | + _ = d.Set("gateway_id", gatewayId) |
| 132 | + _ = d.Set("group_id", groupId) |
| 133 | + _ = d.Set("network_id", networkId) |
| 134 | + |
| 135 | + if cngwNetwork.PublicNetwork != nil { |
| 136 | + internetConfig := cngwNetwork.PublicNetwork |
| 137 | + if internetConfig.AccessControl != nil { |
| 138 | + accessControlMap := map[string]interface{}{} |
| 139 | + |
| 140 | + accessControl := internetConfig.AccessControl |
| 141 | + if accessControl.Mode != nil { |
| 142 | + accessControlMap["mode"] = accessControl.Mode |
| 143 | + } |
| 144 | + if accessControl.Mode != nil { |
| 145 | + accessControlMap["cidr_white_list"] = accessControl.CidrWhiteList |
| 146 | + } |
| 147 | + if accessControl.Mode != nil { |
| 148 | + accessControlMap["cidr_black_list"] = accessControl.CidrBlackList |
| 149 | + } |
| 150 | + _ = d.Set("access_control", []interface{}{accessControlMap}) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return nil |
| 155 | +} |
| 156 | + |
| 157 | +func resourceTencentCloudTseCngwNetworkAccessControlUpdate(d *schema.ResourceData, meta interface{}) error { |
| 158 | + defer tccommon.LogElapsed("resource.tencentcloud_tse_cngw_network_access_control.update")() |
| 159 | + defer tccommon.InconsistentCheck(d, meta)() |
| 160 | + |
| 161 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 162 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 163 | + |
| 164 | + request := tse.NewModifyNetworkAccessStrategyRequest() |
| 165 | + |
| 166 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 167 | + if len(idSplit) != 3 { |
| 168 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 169 | + } |
| 170 | + gatewayId := idSplit[0] |
| 171 | + groupId := idSplit[1] |
| 172 | + networkId := idSplit[2] |
| 173 | + |
| 174 | + service := TseService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 175 | + cngwNetwork, err := service.DescribeTseCngwNetworkById(ctx, gatewayId, groupId, networkId) |
| 176 | + if err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + if cngwNetwork == nil { |
| 180 | + return fmt.Errorf("[WARN]%s resource `TseCngwNetworkAccessControl` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 181 | + } |
| 182 | + |
| 183 | + request.GatewayId = helper.String(gatewayId) |
| 184 | + request.GroupId = helper.String(groupId) |
| 185 | + // The interface only supports public network |
| 186 | + request.NetworkType = helper.String("Open") |
| 187 | + request.Vip = cngwNetwork.PublicNetwork.Vip |
| 188 | + |
| 189 | + if d.HasChange("access_control") { |
| 190 | + if dMap, ok := helper.InterfacesHeadMap(d, "access_control"); ok { |
| 191 | + accessControl := tse.NetworkAccessControl{} |
| 192 | + if v, ok := dMap["mode"]; ok { |
| 193 | + accessControl.Mode = helper.String(v.(string)) |
| 194 | + } |
| 195 | + if v, ok := dMap["cidr_white_list"]; ok { |
| 196 | + whitelist := v.([]interface{}) |
| 197 | + accessControl.CidrWhiteList = helper.InterfacesStringsPoint(whitelist) |
| 198 | + } |
| 199 | + if v, ok := dMap["cidr_black_list"]; ok { |
| 200 | + blacklist := v.([]interface{}) |
| 201 | + accessControl.CidrBlackList = helper.InterfacesStringsPoint(blacklist) |
| 202 | + } |
| 203 | + request.AccessControl = &accessControl |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 208 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTseClient().ModifyNetworkAccessStrategy(request) |
| 209 | + if e != nil { |
| 210 | + return tccommon.RetryError(e) |
| 211 | + } else { |
| 212 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 213 | + } |
| 214 | + return nil |
| 215 | + }) |
| 216 | + if err != nil { |
| 217 | + log.Printf("[CRITAL]%s update tse cngwNetworkAccessStrategy failed, reason:%+v", logId, err) |
| 218 | + return err |
| 219 | + } |
| 220 | + |
| 221 | + conf := tccommon.BuildStateChangeConf([]string{}, []string{"Open"}, 5*tccommon.ReadRetryTimeout, time.Second, service.TseCngwNetworkStateRefreshFunc(gatewayId, groupId, networkId, []string{})) |
| 222 | + |
| 223 | + if _, e := conf.WaitForState(); e != nil { |
| 224 | + return e |
| 225 | + } |
| 226 | + |
| 227 | + return resourceTencentCloudTseCngwNetworkAccessControlRead(d, meta) |
| 228 | +} |
| 229 | + |
| 230 | +func resourceTencentCloudTseCngwNetworkAccessControlDelete(d *schema.ResourceData, meta interface{}) error { |
| 231 | + defer tccommon.LogElapsed("resource.tencentcloud_tse_cngw_network_access_control.delete")() |
| 232 | + defer tccommon.InconsistentCheck(d, meta)() |
| 233 | + |
| 234 | + return nil |
| 235 | +} |
0 commit comments