|
| 1 | +/* |
| 2 | +Provides a resource to create a vpn_gateway_ccn_routes |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_vpn_gateway_ccn_routes" "vpn_gateway_ccn_routes" { |
| 8 | + destination_cidr_block = "192.168.1.0/24" |
| 9 | + route_id = "vpnr-akdy0757" |
| 10 | + status = "DISABLE" |
| 11 | + vpn_gateway_id = "vpngw-lie1a4u7" |
| 12 | +} |
| 13 | +
|
| 14 | +``` |
| 15 | +
|
| 16 | +Import |
| 17 | +
|
| 18 | +vpc vpn_gateway_ccn_routes can be imported using the id, e.g. |
| 19 | +
|
| 20 | +``` |
| 21 | +terraform import tencentcloud_vpn_gateway_ccn_routes.vpn_gateway_ccn_routes vpn_gateway_id#ccn_routes_id |
| 22 | +``` |
| 23 | +*/ |
| 24 | +package tencentcloud |
| 25 | + |
| 26 | +import ( |
| 27 | + "context" |
| 28 | + "fmt" |
| 29 | + "log" |
| 30 | + "strings" |
| 31 | + |
| 32 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 33 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 34 | + vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" |
| 35 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 36 | +) |
| 37 | + |
| 38 | +func resourceTencentCloudVpnGatewayCcnRoutes() *schema.Resource { |
| 39 | + return &schema.Resource{ |
| 40 | + Create: resourceTencentCloudVpnGatewayCcnRoutesCreate, |
| 41 | + Read: resourceTencentCloudVpnGatewayCcnRoutesRead, |
| 42 | + Update: resourceTencentCloudVpnGatewayCcnRoutesUpdate, |
| 43 | + Delete: resourceTencentCloudVpnGatewayCcnRoutesDelete, |
| 44 | + Importer: &schema.ResourceImporter{ |
| 45 | + State: schema.ImportStatePassthrough, |
| 46 | + }, |
| 47 | + Schema: map[string]*schema.Schema{ |
| 48 | + "vpn_gateway_id": { |
| 49 | + Required: true, |
| 50 | + ForceNew: true, |
| 51 | + Type: schema.TypeString, |
| 52 | + Description: "VPN GATEWAY INSTANCE ID.", |
| 53 | + }, |
| 54 | + "route_id": { |
| 55 | + Type: schema.TypeString, |
| 56 | + ForceNew: true, |
| 57 | + Required: true, |
| 58 | + Description: "Route Id.", |
| 59 | + }, |
| 60 | + "status": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Required: true, |
| 63 | + Description: "Whether routing information is enabled. `ENABLE`: Enable Route, `DISABLE`: Disable Route.", |
| 64 | + }, |
| 65 | + "destination_cidr_block": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Required: true, |
| 68 | + ForceNew: true, |
| 69 | + Description: "Routing CIDR.", |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func resourceTencentCloudVpnGatewayCcnRoutesCreate(d *schema.ResourceData, meta interface{}) error { |
| 76 | + defer logElapsed("resource.tencentcloud_vpn_gateway_ccn_routes.create")() |
| 77 | + defer inconsistentCheck(d, meta)() |
| 78 | + |
| 79 | + var ( |
| 80 | + vpnGwId string |
| 81 | + routeId string |
| 82 | + ) |
| 83 | + |
| 84 | + if v, ok := d.GetOk("vpn_gateway_id"); ok { |
| 85 | + vpnGwId = v.(string) |
| 86 | + } |
| 87 | + |
| 88 | + if v, ok := d.GetOk("route_id"); ok { |
| 89 | + routeId = v.(string) |
| 90 | + } |
| 91 | + |
| 92 | + d.SetId(vpnGwId + FILED_SP + routeId) |
| 93 | + |
| 94 | + return resourceTencentCloudVpnGatewayCcnRoutesUpdate(d, meta) |
| 95 | +} |
| 96 | + |
| 97 | +func resourceTencentCloudVpnGatewayCcnRoutesRead(d *schema.ResourceData, meta interface{}) error { |
| 98 | + defer logElapsed("resource.tencentcloud_vpn_gateway_ccn_routes.read")() |
| 99 | + defer inconsistentCheck(d, meta)() |
| 100 | + |
| 101 | + logId := getLogId(contextNil) |
| 102 | + |
| 103 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 104 | + |
| 105 | + service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 106 | + |
| 107 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 108 | + if len(idSplit) != 2 { |
| 109 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 110 | + } |
| 111 | + vpnGatewayId := idSplit[0] |
| 112 | + routeId := idSplit[1] |
| 113 | + |
| 114 | + vpnGatewayCcnRoutes, err := service.DescribeVpcVpnGatewayCcnRoutesById(ctx, vpnGatewayId, routeId) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + if vpnGatewayCcnRoutes == nil { |
| 120 | + d.SetId("") |
| 121 | + log.Printf("[WARN]%s resource `VpcVpnGatewayCcnRoutes` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 122 | + return nil |
| 123 | + } |
| 124 | + |
| 125 | + _ = d.Set("vpn_gateway_id", vpnGatewayId) |
| 126 | + _ = d.Set("route_id", vpnGatewayCcnRoutes.RouteId) |
| 127 | + _ = d.Set("status", vpnGatewayCcnRoutes.Status) |
| 128 | + _ = d.Set("destination_cidr_block", vpnGatewayCcnRoutes.DestinationCidrBlock) |
| 129 | + |
| 130 | + return nil |
| 131 | +} |
| 132 | + |
| 133 | +func resourceTencentCloudVpnGatewayCcnRoutesUpdate(d *schema.ResourceData, meta interface{}) error { |
| 134 | + defer logElapsed("resource.tencentcloud_vpn_gateway_ccn_routes.update")() |
| 135 | + defer inconsistentCheck(d, meta)() |
| 136 | + |
| 137 | + logId := getLogId(contextNil) |
| 138 | + |
| 139 | + request := vpc.NewModifyVpnGatewayCcnRoutesRequest() |
| 140 | + |
| 141 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 142 | + if len(idSplit) != 2 { |
| 143 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 144 | + } |
| 145 | + vpnGatewayId := idSplit[0] |
| 146 | + routeId := idSplit[1] |
| 147 | + |
| 148 | + request.VpnGatewayId = &vpnGatewayId |
| 149 | + route := vpc.VpngwCcnRoutes{} |
| 150 | + route.RouteId = &routeId |
| 151 | + route.Status = helper.String(d.Get("status").(string)) |
| 152 | + route.DestinationCidrBlock = helper.String(d.Get("destination_cidr_block").(string)) |
| 153 | + request.Routes = append(request.Routes, &route) |
| 154 | + |
| 155 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 156 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().ModifyVpnGatewayCcnRoutes(request) |
| 157 | + if e != nil { |
| 158 | + return retryError(e) |
| 159 | + } else { |
| 160 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 161 | + } |
| 162 | + return nil |
| 163 | + }) |
| 164 | + if err != nil { |
| 165 | + log.Printf("[CRITAL]%s update vpnGatewayCcnRoutes failed, reason:%+v", logId, err) |
| 166 | + return err |
| 167 | + } |
| 168 | + |
| 169 | + return resourceTencentCloudVpnGatewayCcnRoutesRead(d, meta) |
| 170 | +} |
| 171 | + |
| 172 | +func resourceTencentCloudVpnGatewayCcnRoutesDelete(d *schema.ResourceData, meta interface{}) error { |
| 173 | + defer logElapsed("resource.tencentcloud_vpn_gateway_ccn_routes.delete")() |
| 174 | + defer inconsistentCheck(d, meta)() |
| 175 | + |
| 176 | + return nil |
| 177 | +} |
0 commit comments