Skip to content

Commit 4f1056b

Browse files
authored
Merge pull request #9726 from learnerjohn/feat/api-gateway-existing-fixes
resource/alicloud_api_gateway_api: Fix backend_id read issue; resource/alicloud_api_gateway_vpc_access: Add vpc_access_id
2 parents 063ad8f + 150519c commit 4f1056b

5 files changed

Lines changed: 205 additions & 1 deletion

alicloud/resource_alicloud_api_gateway_api.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,14 @@ func resourceAliyunApigatewayApi() *schema.Resource {
352352
Optional: true,
353353
Computed: true,
354354
},
355+
"backend_id": {
356+
Type: schema.TypeString,
357+
Optional: true,
358+
},
359+
"backend_enabled": {
360+
Type: schema.TypeBool,
361+
Optional: true,
362+
},
355363
},
356364
}
357365
}
@@ -456,6 +464,15 @@ func resourceAliyunApigatewayApiRead(d *schema.ResourceData, meta interface{}) e
456464

457465
d.Set("system_parameters", convertApiGatewayApiSystemParamsResponse(objectRaw["SystemParameters"]))
458466

467+
if backendConfig, ok := objectRaw["BackendConfig"].(map[string]interface{}); ok {
468+
if v, ok := backendConfig["BackendId"].(string); ok && v != "" {
469+
d.Set("backend_id", v)
470+
}
471+
}
472+
if v, ok := objectRaw["BackendEnable"]; ok {
473+
d.Set("backend_enabled", v)
474+
}
475+
459476
return nil
460477
}
461478

@@ -497,7 +514,7 @@ func resourceAliyunApigatewayApiUpdate(d *schema.ResourceData, meta interface{})
497514
}
498515
request.RequestConfig = paramConfig
499516

500-
if d.HasChanges("service_type", "http_service_config", "http_vpc_service_config", "mock_service_config", "fc_service_config") {
517+
if d.HasChanges("service_type", "http_service_config", "http_vpc_service_config", "mock_service_config", "fc_service_config", "backend_id", "backend_enabled") {
501518
update = true
502519
}
503520
serviceConfig, err := serviceConfigToJsonStr(d)
@@ -524,6 +541,14 @@ func resourceAliyunApigatewayApiUpdate(d *schema.ResourceData, meta interface{})
524541
request.AllowSignatureMethod = AllowSignatureMethod
525542
request.WebSocketApiType = WebSocketApiType
526543

544+
backendEnabled := d.Get("backend_enabled").(bool)
545+
request.QueryParams["BackendEnable"] = strconv.FormatBool(backendEnabled)
546+
if backendEnabled {
547+
if v, ok := d.GetOk("backend_id"); ok && v.(string) != "" {
548+
request.QueryParams["BackendId"] = v.(string)
549+
}
550+
}
551+
527552
raw, err := client.WithCloudApiClient(func(cloudApiClient *cloudapi.Client) (interface{}, error) {
528553
return cloudApiClient.ModifyApi(request)
529554
})
@@ -646,6 +671,14 @@ func buildAliyunApiArgs(d *schema.ResourceData, meta interface{}) (*cloudapi.Cre
646671
request.AllowSignatureMethod = AllowSignatureMethod
647672
request.WebSocketApiType = WebSocketApiType
648673

674+
backendEnabled := d.Get("backend_enabled").(bool)
675+
request.QueryParams["BackendEnable"] = strconv.FormatBool(backendEnabled)
676+
if backendEnabled {
677+
if v, ok := d.GetOk("backend_id"); ok && v.(string) != "" {
678+
request.QueryParams["BackendId"] = v.(string)
679+
}
680+
}
681+
649682
return request, WrapError(err)
650683
}
651684

alicloud/resource_alicloud_api_gateway_api_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,32 @@ func resourceApigatewayApiConfigDependence_vpc(name string) string {
13711371
`, name, ApigatewayVpcAccessConfigDependence)
13721372
}
13731373

1374+
func resourceapigatewayapiconfigdependenceBackend(name string) string {
1375+
return fmt.Sprintf(`
1376+
1377+
variable "name" {
1378+
default = "%s"
1379+
}
1380+
1381+
resource "alicloud_api_gateway_group" "default" {
1382+
name = "${var.name}"
1383+
description = "tf_testAcc_api group description"
1384+
}
1385+
1386+
resource "alicloud_api_gateway_backend" "default" {
1387+
backend_name = "${var.name}_backend"
1388+
backend_type = "HTTP"
1389+
description = var.name
1390+
}
1391+
1392+
resource "alicloud_api_gateway_backend" "update" {
1393+
backend_name = "${var.name}_backend_update"
1394+
backend_type = "HTTP"
1395+
description = var.name
1396+
}
1397+
`, name)
1398+
}
1399+
13741400
var apiGatewayApiMap = map[string]string{
13751401
"name": CHECKSET,
13761402
"group_id": CHECKSET,
@@ -1383,3 +1409,95 @@ var apiGatewayApiMap = map[string]string{
13831409
"service_type": CHECKSET,
13841410
"api_id": CHECKSET,
13851411
}
1412+
1413+
func TestAccAliCloudApigatewayApi_backend(t *testing.T) {
1414+
var api *cloudapi.DescribeApiResponse
1415+
resourceId := "alicloud_api_gateway_api.default"
1416+
ra := resourceAttrInit(resourceId, apiGatewayApiMap)
1417+
serviceFunc := func() interface{} {
1418+
return &CloudApiService{testAccProvider.Meta().(*connectivity.AliyunClient)}
1419+
}
1420+
rc := resourceCheckInit(resourceId, &api, serviceFunc)
1421+
rac := resourceAttrCheckInit(rc, ra)
1422+
testAccCheck := rac.resourceAttrMapUpdateSet()
1423+
rand := acctest.RandIntRange(1000000, 9999999)
1424+
name := fmt.Sprintf("tf_testAccApiGatewayApi_%d", rand)
1425+
testAccConfig := resourceTestAccConfigFunc(resourceId, name, resourceapigatewayapiconfigdependenceBackend)
1426+
1427+
resource.Test(t, resource.TestCase{
1428+
PreCheck: func() {
1429+
testAccPreCheck(t)
1430+
},
1431+
IDRefreshName: resourceId,
1432+
Providers: testAccProviders,
1433+
CheckDestroy: rac.checkResourceDestroy(),
1434+
Steps: []resource.TestStep{
1435+
{
1436+
Config: testAccConfig(map[string]interface{}{
1437+
"name": "${alicloud_api_gateway_group.default.name}",
1438+
"group_id": "${alicloud_api_gateway_group.default.id}",
1439+
"description": "tf_testAcc_api backend_id",
1440+
"auth_type": "ANONYMOUS",
1441+
"request_config": []map[string]string{{
1442+
"protocol": "HTTP",
1443+
"method": "GET",
1444+
"path": "/test/path",
1445+
"mode": "MAPPING",
1446+
}},
1447+
"service_type": "HTTP",
1448+
"http_service_config": []map[string]string{{
1449+
"address": "",
1450+
"method": "GET",
1451+
"path": "/web/cloudapi",
1452+
"timeout": "20",
1453+
}},
1454+
"backend_id": "${alicloud_api_gateway_backend.default.id}",
1455+
"backend_enabled": "true",
1456+
}),
1457+
Check: resource.ComposeTestCheckFunc(
1458+
testAccCheck(map[string]string{
1459+
"name": name,
1460+
"description": "tf_testAcc_api backend_id",
1461+
"auth_type": "ANONYMOUS",
1462+
"service_type": "HTTP",
1463+
"backend_id": CHECKSET,
1464+
"backend_enabled": "true",
1465+
}),
1466+
),
1467+
},
1468+
{
1469+
Config: testAccConfig(map[string]interface{}{
1470+
"backend_id": "${alicloud_api_gateway_backend.update.id}",
1471+
}),
1472+
Check: resource.ComposeTestCheckFunc(
1473+
testAccCheck(map[string]string{
1474+
"backend_id": CHECKSET,
1475+
}),
1476+
),
1477+
},
1478+
{
1479+
ResourceName: resourceId,
1480+
ImportState: true,
1481+
ImportStateVerify: true,
1482+
ImportStateVerifyIgnore: []string{},
1483+
},
1484+
{
1485+
Config: testAccConfig(map[string]interface{}{
1486+
"backend_enabled": "false",
1487+
"http_service_config": []map[string]string{{
1488+
"address": "http://apigateway-backend.alicloudapi.com",
1489+
"method": "GET",
1490+
"path": "/web/cloudapi",
1491+
"timeout": "20",
1492+
}},
1493+
}),
1494+
Check: resource.ComposeTestCheckFunc(
1495+
testAccCheck(map[string]string{
1496+
"backend_enabled": "false",
1497+
"http_service_config.0.address": "http://apigateway-backend.alicloudapi.com",
1498+
}),
1499+
),
1500+
},
1501+
},
1502+
})
1503+
}

alicloud/resource_alicloud_api_gateway_vpc_access.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ func resourceAliCloudApiGatewayVpcAccess() *schema.Resource {
4040
Required: true,
4141
ForceNew: true,
4242
},
43+
"vpc_access_id": {
44+
Type: schema.TypeString,
45+
Computed: true,
46+
},
4347
},
4448
}
4549
}
@@ -97,6 +101,9 @@ func resourceAliCloudApiGatewayVpcAccessRead(d *schema.ResourceData, meta interf
97101
d.Set("vpc_id", object["VpcId"])
98102
d.Set("instance_id", object["InstanceId"])
99103
d.Set("port", object["Port"])
104+
if v, ok := object["VpcAccessId"]; ok {
105+
d.Set("vpc_access_id", v)
106+
}
100107

101108
return nil
102109
}

website/docs/r/api_gateway_api.html.markdown

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,49 @@ resource "alicloud_api_gateway_api" "example" {
7979
8080
```
8181

82+
Backend Usage
83+
84+
```terraform
85+
resource "alicloud_api_gateway_group" "example" {
86+
name = "tf-example"
87+
description = "tf-example"
88+
base_path = "/"
89+
}
90+
91+
resource "alicloud_api_gateway_backend" "example" {
92+
backend_name = "tf-example-backend"
93+
backend_type = "HTTP"
94+
description = "tf-example"
95+
}
96+
97+
resource "alicloud_api_gateway_api" "example" {
98+
group_id = alicloud_api_gateway_group.example.id
99+
name = "tf-example"
100+
description = "tf-example"
101+
auth_type = "APP"
102+
force_nonce_check = false
103+
104+
request_config {
105+
protocol = "HTTP"
106+
method = "GET"
107+
path = "/example/path"
108+
mode = "MAPPING"
109+
}
110+
111+
service_type = "HTTP"
112+
113+
http_service_config {
114+
address = ""
115+
method = "GET"
116+
path = "/web/cloudapi"
117+
timeout = 12
118+
}
119+
120+
backend_id = alicloud_api_gateway_backend.example.id
121+
backend_enabled = true
122+
}
123+
```
124+
82125
📚 Need more examples? [VIEW MORE EXAMPLES](https://api.aliyun.com/terraform?activeTab=sample&source=Sample&sourcePath=OfficialSample:alicloud_api_gateway_api&spm=docs.r.api_gateway_api.example&intl_lang=EN_US)
83126

84127
## Argument Reference
@@ -100,6 +143,8 @@ The following arguments are supported:
100143
* `system_parameters` - (Optional, List) system_parameters defines the system parameters of the api. See [`system_parameters`](#system_parameters) below.
101144
* `stage_names` - (Optional, Type: list) Stages that the api need to be deployed. Valid value: `RELEASE`,`PRE`,`TEST`.
102145
* `force_nonce_check` - (Optional, Type: bool, Available in v1.140+) Whether to prevent API replay attack. Default value: `false`.
146+
* `backend_id` - (Optional, Available since v1.279.0) The ID of the API Gateway Backend. When specified, the API references an existing backend created by `alicloud_api_gateway_backend`.
147+
* `backend_enabled` - (Optional, Available since v1.279.0) Specifies whether to enable the backend service. When set to `true`, the `backend_id` will be sent to the API.
103148

104149
### `request_config`
105150

website/docs/r/api_gateway_vpc_access.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ The following arguments are supported:
107107
The following attributes are exported:
108108

109109
* `id` - The resource ID in terraform of Vpc Access. It formats as `<name>:<vpc_id>:<instance_id>:<port>`.
110+
* `vpc_access_id` - The ID of the Vpc Access.
110111

111112
## Import
112113

0 commit comments

Comments
 (0)