Skip to content

Commit

Permalink
azurerm_api_management_api - the version and `version_set_i… (#4592)
Browse files Browse the repository at this point in the history
Should fix #4297
  • Loading branch information
kraihn authored and katbyte committed Nov 8, 2019
1 parent 129d217 commit 92decd7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
14 changes: 14 additions & 0 deletions azurerm/resource_arm_api_management_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,13 @@ func resourceArmApiManagementApi() *schema.Resource {
"version": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},

"version_set_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
},
}
Expand Down Expand Up @@ -251,6 +253,13 @@ 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)

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)

Expand All @@ -274,9 +283,14 @@ func resourceArmApiManagementApiCreateUpdate(d *schema.ResourceData, meta interf
Protocols: protocols,
ServiceURL: utils.String(serviceUrl),
SubscriptionKeyParameterNames: subscriptionKeyParameterNames,
APIVersion: utils.String(version),
},
}

if versionSetId != "" {
params.APICreateOrUpdateProperties.APIVersionSetID = utils.String(versionSetId)
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, apiId, params, ""); err != nil {
return fmt.Errorf("Error creating/updating API %q / Revision %q (API Management Service %q / Resource Group %q): %+v", name, revision, serviceName, resourceGroup, err)
}
Expand Down
53 changes: 53 additions & 0 deletions azurerm/resource_arm_api_management_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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, rInt)
}

func testAccAzureRMApiManagementApi_template(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down
6 changes: 6 additions & 0 deletions website/docs/r/api_management_api.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,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:
Expand Down

0 comments on commit 92decd7

Please sign in to comment.