From 3f86218cced31faf2cac44a8b20f67c94d9a5c50 Mon Sep 17 00:00:00 2001 From: Jared Dickson Date: Thu, 10 Oct 2019 12:46:30 -0400 Subject: [PATCH 01/12] Allow setting Version and Version Set Id for app_service --- azurerm/resource_arm_api_management_api.go | 47 ++++++++++++++++------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/azurerm/resource_arm_api_management_api.go b/azurerm/resource_arm_api_management_api.go index 4f2c7efa6699..1390264f7b5e 100644 --- a/azurerm/resource_arm_api_management_api.go +++ b/azurerm/resource_arm_api_management_api.go @@ -170,12 +170,12 @@ func resourceArmApiManagementApi() *schema.Resource { "version": { Type: schema.TypeString, - Computed: true, + Optional: true, }, "version_set_id": { Type: schema.TypeString, - Computed: true, + Optional: true, }, }, } @@ -243,6 +243,9 @@ func resourceArmApiManagementApiCreateUpdate(d *schema.ResourceData, meta interf displayName := d.Get("display_name").(string) serviceUrl := d.Get("service_url").(string) + version := d.Get("version").(string) + versionSetId := d.Get("version_set_id").(string) + protocolsRaw := d.Get("protocols").(*schema.Set).List() protocols := expandApiManagementApiProtocols(protocolsRaw) @@ -257,16 +260,36 @@ func resourceArmApiManagementApiCreateUpdate(d *schema.ResourceData, meta interf apiType = apimanagement.APIType(apimanagement.SoapToRest) } - params := apimanagement.APICreateOrUpdateParameter{ - APICreateOrUpdateProperties: &apimanagement.APICreateOrUpdateProperties{ - APIType: apiType, - Description: utils.String(description), - DisplayName: utils.String(displayName), - Path: utils.String(path), - Protocols: protocols, - ServiceURL: utils.String(serviceUrl), - SubscriptionKeyParameterNames: subscriptionKeyParameterNames, - }, + log.Printf("[DEBUG] Jared was here: %q", versionSetId) + + var params apimanagement.APICreateOrUpdateParameter + if versionSetId != "" { + params = apimanagement.APICreateOrUpdateParameter{ + APICreateOrUpdateProperties: &apimanagement.APICreateOrUpdateProperties{ + APIType: apiType, + Description: utils.String(description), + DisplayName: utils.String(displayName), + Path: utils.String(path), + Protocols: protocols, + ServiceURL: utils.String(serviceUrl), + SubscriptionKeyParameterNames: subscriptionKeyParameterNames, + APIVersion: utils.String(version), + APIVersionSetID: utils.String(versionSetId), + }, + } + } else { + params = apimanagement.APICreateOrUpdateParameter{ + APICreateOrUpdateProperties: &apimanagement.APICreateOrUpdateProperties{ + APIType: apiType, + Description: utils.String(description), + DisplayName: utils.String(displayName), + Path: utils.String(path), + Protocols: protocols, + ServiceURL: utils.String(serviceUrl), + SubscriptionKeyParameterNames: subscriptionKeyParameterNames, + APIVersion: utils.String(version), + }, + } } if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, apiId, params, ""); err != nil { From 7a5f380ced7891d9dee3754a97b2f9091299448a Mon Sep 17 00:00:00 2001 From: Jared Dickson Date: Thu, 10 Oct 2019 14:33:19 -0400 Subject: [PATCH 02/12] Remove log statement --- azurerm/resource_arm_api_management_api.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/azurerm/resource_arm_api_management_api.go b/azurerm/resource_arm_api_management_api.go index 1390264f7b5e..c79b718d0922 100644 --- a/azurerm/resource_arm_api_management_api.go +++ b/azurerm/resource_arm_api_management_api.go @@ -260,8 +260,6 @@ func resourceArmApiManagementApiCreateUpdate(d *schema.ResourceData, meta interf apiType = apimanagement.APIType(apimanagement.SoapToRest) } - log.Printf("[DEBUG] Jared was here: %q", versionSetId) - var params apimanagement.APICreateOrUpdateParameter if versionSetId != "" { params = apimanagement.APICreateOrUpdateParameter{ From eff9b6a48cbe58bdfb9aa31483227e3fceec5c22 Mon Sep 17 00:00:00 2001 From: Jared Dickson Date: Tue, 15 Oct 2019 20:42:43 -0400 Subject: [PATCH 03/12] Simplify conditional block --- azurerm/resource_arm_api_management_api.go | 41 ++++++++-------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/azurerm/resource_arm_api_management_api.go b/azurerm/resource_arm_api_management_api.go index c79b718d0922..91bc0b9cc567 100644 --- a/azurerm/resource_arm_api_management_api.go +++ b/azurerm/resource_arm_api_management_api.go @@ -260,34 +260,21 @@ func resourceArmApiManagementApiCreateUpdate(d *schema.ResourceData, meta interf apiType = apimanagement.APIType(apimanagement.SoapToRest) } - var params apimanagement.APICreateOrUpdateParameter + params := apimanagement.APICreateOrUpdateParameter{ + APICreateOrUpdateProperties: &apimanagement.APICreateOrUpdateProperties{ + APIType: apiType, + Description: utils.String(description), + DisplayName: utils.String(displayName), + Path: utils.String(path), + Protocols: protocols, + ServiceURL: utils.String(serviceUrl), + SubscriptionKeyParameterNames: subscriptionKeyParameterNames, + APIVersion: utils.String(version), + }, + } + if versionSetId != "" { - params = apimanagement.APICreateOrUpdateParameter{ - APICreateOrUpdateProperties: &apimanagement.APICreateOrUpdateProperties{ - APIType: apiType, - Description: utils.String(description), - DisplayName: utils.String(displayName), - Path: utils.String(path), - Protocols: protocols, - ServiceURL: utils.String(serviceUrl), - SubscriptionKeyParameterNames: subscriptionKeyParameterNames, - APIVersion: utils.String(version), - APIVersionSetID: utils.String(versionSetId), - }, - } - } else { - params = apimanagement.APICreateOrUpdateParameter{ - APICreateOrUpdateProperties: &apimanagement.APICreateOrUpdateProperties{ - APIType: apiType, - Description: utils.String(description), - DisplayName: utils.String(displayName), - Path: utils.String(path), - Protocols: protocols, - ServiceURL: utils.String(serviceUrl), - SubscriptionKeyParameterNames: subscriptionKeyParameterNames, - APIVersion: utils.String(version), - }, - } + params.APICreateOrUpdateProperties.APIVersionSetID = utils.String(versionSetId) } if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, apiId, params, ""); err != nil { From bc7ff400c1c91ab6a46455ce719b751f64211acd Mon Sep 17 00:00:00 2001 From: Jared Dickson Date: Tue, 15 Oct 2019 21:07:04 -0400 Subject: [PATCH 04/12] Set the Read state for version and version_set_id --- azurerm/resource_arm_api_management_api.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/azurerm/resource_arm_api_management_api.go b/azurerm/resource_arm_api_management_api.go index 91bc0b9cc567..5d3f41c96004 100644 --- a/azurerm/resource_arm_api_management_api.go +++ b/azurerm/resource_arm_api_management_api.go @@ -315,6 +315,9 @@ func resourceArmApiManagementApiRead(d *schema.ResourceData, meta interface{}) e revision = strings.Split(apiid, "=")[1] } + version := id.Path["version"] + versionSetId := id.Path["version_set_id"] + resp, err := client.Get(ctx, resourceGroup, serviceName, apiid) if err != nil { if utils.ResponseWasNotFound(resp.Response) { @@ -329,6 +332,8 @@ func resourceArmApiManagementApiRead(d *schema.ResourceData, meta interface{}) e d.Set("api_management_name", serviceName) d.Set("name", name) d.Set("resource_group_name", resourceGroup) + d.Set("version", version) + d.Set("version_set_id", versionSetId) if props := resp.APIContractProperties; props != nil { d.Set("description", props.Description) @@ -339,8 +344,6 @@ func resourceArmApiManagementApiRead(d *schema.ResourceData, meta interface{}) e d.Set("service_url", props.ServiceURL) d.Set("revision", props.APIRevision) d.Set("soap_pass_through", string(props.APIType) == string(apimanagement.SoapPassThrough)) - d.Set("version", props.APIVersion) - d.Set("version_set_id", props.APIVersionSetID) if err := d.Set("protocols", flattenApiManagementApiProtocols(props.Protocols)); err != nil { return fmt.Errorf("Error setting `protocols`: %s", err) From 856409bc7df5ffa983defa91d7a8bc734d0ede52 Mon Sep 17 00:00:00 2001 From: Jared Dickson Date: Fri, 18 Oct 2019 13:16:56 -0400 Subject: [PATCH 05/12] Configure Read section --- azurerm/resource_arm_api_management_api.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/azurerm/resource_arm_api_management_api.go b/azurerm/resource_arm_api_management_api.go index 5d3f41c96004..91bc0b9cc567 100644 --- a/azurerm/resource_arm_api_management_api.go +++ b/azurerm/resource_arm_api_management_api.go @@ -315,9 +315,6 @@ func resourceArmApiManagementApiRead(d *schema.ResourceData, meta interface{}) e revision = strings.Split(apiid, "=")[1] } - version := id.Path["version"] - versionSetId := id.Path["version_set_id"] - resp, err := client.Get(ctx, resourceGroup, serviceName, apiid) if err != nil { if utils.ResponseWasNotFound(resp.Response) { @@ -332,8 +329,6 @@ func resourceArmApiManagementApiRead(d *schema.ResourceData, meta interface{}) e d.Set("api_management_name", serviceName) d.Set("name", name) d.Set("resource_group_name", resourceGroup) - d.Set("version", version) - d.Set("version_set_id", versionSetId) if props := resp.APIContractProperties; props != nil { d.Set("description", props.Description) @@ -344,6 +339,8 @@ func resourceArmApiManagementApiRead(d *schema.ResourceData, meta interface{}) e d.Set("service_url", props.ServiceURL) d.Set("revision", props.APIRevision) d.Set("soap_pass_through", string(props.APIType) == string(apimanagement.SoapPassThrough)) + d.Set("version", props.APIVersion) + d.Set("version_set_id", props.APIVersionSetID) if err := d.Set("protocols", flattenApiManagementApiProtocols(props.Protocols)); err != nil { return fmt.Errorf("Error setting `protocols`: %s", err) From 245e3d249b2dcbccdb77d16060f48082227ccbb4 Mon Sep 17 00:00:00 2001 From: Jared Dickson Date: Fri, 18 Oct 2019 13:17:09 -0400 Subject: [PATCH 06/12] Add validation --- azurerm/resource_arm_api_management_api.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/azurerm/resource_arm_api_management_api.go b/azurerm/resource_arm_api_management_api.go index 91bc0b9cc567..47ffdbd60d98 100644 --- a/azurerm/resource_arm_api_management_api.go +++ b/azurerm/resource_arm_api_management_api.go @@ -178,6 +178,17 @@ func resourceArmApiManagementApi() *schema.Resource { Optional: true, }, }, + + CustomizeDiff: func(diff *schema.ResourceDiff, v interface{}) error { + _, hasVersion := diff.GetOk("version") + _, hasVersionSetId := diff.GetOk("version_set_id") + + if hasVersion && !hasVersionSetId { + return fmt.Errorf("`version_set_id` is required when `version` is set") + } + + return nil + }, } } @@ -246,6 +257,10 @@ func resourceArmApiManagementApiCreateUpdate(d *schema.ResourceData, meta interf version := d.Get("version").(string) versionSetId := d.Get("version_set_id").(string) + if version != "" && versionSetId == "" { + return fmt.Errorf("Error setting `version` without the required `version_set_id`") + } + protocolsRaw := d.Get("protocols").(*schema.Set).List() protocols := expandApiManagementApiProtocols(protocolsRaw) From e3cdb66cef47ead6abacece0d7fcb8d74d525afc Mon Sep 17 00:00:00 2001 From: Jared Dickson Date: Fri, 18 Oct 2019 13:27:53 -0400 Subject: [PATCH 07/12] Update docs --- website/docs/d/api_management_api.html.markdown | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/website/docs/d/api_management_api.html.markdown b/website/docs/d/api_management_api.html.markdown index cf76684cf37b..e5501c69d7b5 100644 --- a/website/docs/d/api_management_api.html.markdown +++ b/website/docs/d/api_management_api.html.markdown @@ -35,6 +35,12 @@ output "api_management_api_id" { * `revision` - (Required) The Revision of the API Management API. +* `version` - (Optional) The Version number of this API, if this API is versioned. + +* `version_set_id` - (Optional) The ID of the Version Set which this API is associated with. + +-> **NOTE:** When `version` is set, `version_set_id` must also be specified + ## Attributes Reference * `id` - The ID of the API Management API. From 4eee91fe6f831fa25db3911c4f590d88ad5a2d99 Mon Sep 17 00:00:00 2001 From: Jared Dickson Date: Fri, 18 Oct 2019 13:46:30 -0400 Subject: [PATCH 08/12] Add new test --- .../resource_arm_api_management_api_test.go | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/azurerm/resource_arm_api_management_api_test.go b/azurerm/resource_arm_api_management_api_test.go index 28935295da31..423f13ca4687 100644 --- a/azurerm/resource_arm_api_management_api_test.go +++ b/azurerm/resource_arm_api_management_api_test.go @@ -94,6 +94,32 @@ func TestAccAzureRMApiManagementApi_wordRevision(t *testing.T) { }) } +func TestAccAzureRMApiManagementApi_version(t *testing.T) { + resourceName := "azurerm_api_management_api.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMApiManagementApiDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMApiManagementApi_versionSet(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMApiManagementApiExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "version", "v1"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func TestAccAzureRMApiManagementApi_requiresImport(t *testing.T) { if !features.ShouldResourcesBeImported() { t.Skip("Skipping since resources aren't required to be imported") @@ -495,6 +521,33 @@ resource "azurerm_api_management_api" "test" { `, template, rInt) } +func testAccAzureRMApiManagementApi_versionSet(rInt int, location string) string { + template := testAccAzureRMApiManagementApi_template(rInt, location) + return fmt.Sprintf(` +%s + +resource "azurerm_api_management_api_version_set" "test" { + name = "acctestAMAVS-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + api_management_name = "${azurerm_api_management.test.name}" + display_name = "Butter Parser" + versioning_scheme = "Segment" +} + +resource "azurerm_api_management_api" "test" { + name = "acctestapi-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + api_management_name = "${azurerm_api_management.test.name}" + display_name = "api1" + path = "api1" + protocols = ["https"] + revision = "1" + version = "v1" + version_set_id = "${azurerm_api_management_api_version_set.test.id}" +} +`, template, rInt) +} + func testAccAzureRMApiManagementApi_template(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { From e220f793d3b48077ffd7cb002549dfe6b6194ad4 Mon Sep 17 00:00:00 2001 From: Jared Dickson Date: Fri, 18 Oct 2019 14:03:36 -0400 Subject: [PATCH 09/12] Add missing test parameter --- azurerm/resource_arm_api_management_api_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/resource_arm_api_management_api_test.go b/azurerm/resource_arm_api_management_api_test.go index 423f13ca4687..a1097c61503f 100644 --- a/azurerm/resource_arm_api_management_api_test.go +++ b/azurerm/resource_arm_api_management_api_test.go @@ -545,7 +545,7 @@ resource "azurerm_api_management_api" "test" { version = "v1" version_set_id = "${azurerm_api_management_api_version_set.test.id}" } -`, template, rInt) +`, template, rInt, rInt) } func testAccAzureRMApiManagementApi_template(rInt int, location string) string { From 7373af6c3e43cda9051a7dbbf146aa9746236b8b Mon Sep 17 00:00:00 2001 From: Jared Dickson Date: Mon, 4 Nov 2019 15:32:28 -0500 Subject: [PATCH 10/12] Remove CustomizeDiff --- azurerm/resource_arm_api_management_api.go | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/azurerm/resource_arm_api_management_api.go b/azurerm/resource_arm_api_management_api.go index 47ffdbd60d98..fdf0c718eea5 100644 --- a/azurerm/resource_arm_api_management_api.go +++ b/azurerm/resource_arm_api_management_api.go @@ -178,17 +178,6 @@ func resourceArmApiManagementApi() *schema.Resource { Optional: true, }, }, - - CustomizeDiff: func(diff *schema.ResourceDiff, v interface{}) error { - _, hasVersion := diff.GetOk("version") - _, hasVersionSetId := diff.GetOk("version_set_id") - - if hasVersion && !hasVersionSetId { - return fmt.Errorf("`version_set_id` is required when `version` is set") - } - - return nil - }, } } From a5bcd74a5db9b8e7204e256ba7ae56bb5d29df9d Mon Sep 17 00:00:00 2001 From: Jared Dickson Date: Mon, 4 Nov 2019 15:48:22 -0500 Subject: [PATCH 11/12] Correct docs --- website/docs/d/api_management_api.html.markdown | 6 ------ website/docs/r/api_management_api.html.markdown | 6 ++++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/website/docs/d/api_management_api.html.markdown b/website/docs/d/api_management_api.html.markdown index e5501c69d7b5..cf76684cf37b 100644 --- a/website/docs/d/api_management_api.html.markdown +++ b/website/docs/d/api_management_api.html.markdown @@ -35,12 +35,6 @@ output "api_management_api_id" { * `revision` - (Required) The Revision of the API Management API. -* `version` - (Optional) The Version number of this API, if this API is versioned. - -* `version_set_id` - (Optional) The ID of the Version Set which this API is associated with. - --> **NOTE:** When `version` is set, `version_set_id` must also be specified - ## Attributes Reference * `id` - The ID of the API Management API. diff --git a/website/docs/r/api_management_api.html.markdown b/website/docs/r/api_management_api.html.markdown index 68f045a1b2f6..1180c17f75cf 100644 --- a/website/docs/r/api_management_api.html.markdown +++ b/website/docs/r/api_management_api.html.markdown @@ -74,6 +74,12 @@ The following arguments are supported: * `subscription_key_parameter_names` - (Optional) A `subscription_key_parameter_names` block as documented below. +* `version` - (Optional) The Version number of this API, if this API is versioned. + +* `version_set_id` - (Optional) The ID of the Version Set which this API is associated with. + +-> **NOTE:** When `version` is set, `version_set_id` must also be specified + --- A `import` block supports the following: From c3c2a07a765585ae5d6ee73aafb6ed9605aaf87f Mon Sep 17 00:00:00 2001 From: Jared Dickson Date: Mon, 4 Nov 2019 16:14:10 -0500 Subject: [PATCH 12/12] Add computed:true --- azurerm/resource_arm_api_management_api.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/azurerm/resource_arm_api_management_api.go b/azurerm/resource_arm_api_management_api.go index fdf0c718eea5..f84fde09ca23 100644 --- a/azurerm/resource_arm_api_management_api.go +++ b/azurerm/resource_arm_api_management_api.go @@ -170,11 +170,13 @@ func resourceArmApiManagementApi() *schema.Resource { "version": { Type: schema.TypeString, + Computed: true, Optional: true, }, "version_set_id": { Type: schema.TypeString, + Computed: true, Optional: true, }, },