Skip to content

Commit

Permalink
r/api_management_api: additional validation / making revision a string
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Mar 15, 2019
1 parent d4f836d commit 34e10fa
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 55 deletions.
20 changes: 6 additions & 14 deletions azurerm/data_source_api_management_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package azurerm

import (
"fmt"
"strconv"

"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2018-01-01/apimanagement"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -27,8 +26,9 @@ func dataSourceApiManagementApi() *schema.Resource {
"resource_group_name": resourceGroupNameForDataSourceSchema(),

"revision": {
Type: schema.TypeInt,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.ApiManagementChildName,
},

"description": {
Expand Down Expand Up @@ -111,9 +111,9 @@ func dataSourceApiManagementApiRead(d *schema.ResourceData, meta interface{}) er
resourceGroup := d.Get("resource_group_name").(string)
serviceName := d.Get("api_management_name").(string)
name := d.Get("name").(string)
revision := d.Get("revision").(int)
revision := d.Get("revision").(string)

apiId := fmt.Sprintf("%s;rev=%d", name, revision)
apiId := fmt.Sprintf("%s;rev=%s", name, revision)
resp, err := client.Get(ctx, resourceGroup, serviceName, apiId)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
Expand All @@ -135,20 +135,12 @@ func dataSourceApiManagementApiRead(d *schema.ResourceData, meta interface{}) er
d.Set("is_current", props.IsCurrent)
d.Set("is_online", props.IsOnline)
d.Set("path", props.Path)
d.Set("revision", props.APIRevision)
d.Set("service_url", props.ServiceURL)
d.Set("soap_pass_through", string(props.APIType) == string(apimanagement.SoapPassThrough))
d.Set("version", props.APIVersion)
d.Set("version_set_id", props.APIVersionSetID)

if apiR := props.APIRevision; apiR != nil {
i, err := strconv.Atoi(*apiR)
if err != nil {
return fmt.Errorf("Error casting %q to an integer: %s", *apiR, err)
}

d.Set("revision", i)
}

if err := d.Set("protocols", flattenApiManagementApiDataSourceProtocols(props.Protocols)); err != nil {
return fmt.Errorf("Error setting `protocols`: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion azurerm/data_source_api_management_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestAccDataSourceAzureRMApiManagementApi_complete(t *testing.T) {
resource.TestCheckResourceAttr(dataSourceName, "display_name", "Butter Parser"),
resource.TestCheckResourceAttr(dataSourceName, "path", "butter-parser"),
resource.TestCheckResourceAttr(dataSourceName, "protocols.#", "2"),
resource.TestCheckResourceAttr(dataSourceName, "description", "What is my purpose? You pass butter."),
resource.TestCheckResourceAttr(dataSourceName, "description", "What is my purpose? You parse butter."),
resource.TestCheckResourceAttr(dataSourceName, "service_url", "https://example.com/foo/bar"),
resource.TestCheckResourceAttr(dataSourceName, "soap_pass_through", "false"),
resource.TestCheckResourceAttr(dataSourceName, "subscription_key_parameter_names.0.header", "X-Butter-Robot-API-Key"),
Expand Down
60 changes: 29 additions & 31 deletions azurerm/resource_arm_api_management_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package azurerm
import (
"fmt"
"log"
"strconv"
"strings"

"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2018-01-01/apimanagement"
Expand Down Expand Up @@ -38,8 +37,9 @@ func resourceArmApiManagementApi() *schema.Resource {
"resource_group_name": resourceGroupNameSchema(),

"display_name": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},

"path": {
Expand All @@ -61,9 +61,10 @@ func resourceArmApiManagementApi() *schema.Resource {
},

"revision": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ApiManagementChildName,
},

// Optional
Expand All @@ -79,9 +80,11 @@ func resourceArmApiManagementApi() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"content_value": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},

"content_format": {
Type: schema.TypeString,
Required: true,
Expand All @@ -102,13 +105,15 @@ func resourceArmApiManagementApi() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},

"endpoint_name": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},
},
},
Expand All @@ -131,12 +136,14 @@ func resourceArmApiManagementApi() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"header": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},
"query": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},
},
},
Expand Down Expand Up @@ -179,9 +186,9 @@ func resourceArmApiManagementApiCreateUpdate(d *schema.ResourceData, meta interf
resourceGroup := d.Get("resource_group_name").(string)
serviceName := d.Get("api_management_name").(string)
name := d.Get("name").(string)
revision := int32(d.Get("revision").(int))
revision := d.Get("revision").(string)
path := d.Get("path").(string)
apiId := fmt.Sprintf("%s;rev=%d", name, revision)
apiId := fmt.Sprintf("%s;rev=%s", name, revision)

if requireResourcesToBeImported && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, serviceName, apiId)
Expand Down Expand Up @@ -259,18 +266,17 @@ func resourceArmApiManagementApiCreateUpdate(d *schema.ResourceData, meta interf
},
}

revisionStr := strconv.Itoa(int(revision))
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, revisionStr, serviceName, resourceGroup, err)
return fmt.Errorf("Error creating/updating API %q / Revision %q (API Management Service %q / Resource Group %q): %+v", name, revision, serviceName, resourceGroup, err)
}

read, err := client.Get(ctx, resourceGroup, serviceName, apiId)
if err != nil {
return fmt.Errorf("Error retrieving API %q / Revision %q (API Management Service %q / Resource Group %q): %+v", name, revisionStr, serviceName, resourceGroup, err)
return fmt.Errorf("Error retrieving API %q / Revision %q (API Management Service %q / Resource Group %q): %+v", name, revision, serviceName, resourceGroup, err)
}

if read.ID == nil {
return fmt.Errorf("Cannot read ID for API %q / Revision %q (API Management Service %q / Resource Group %q)", name, revisionStr, serviceName, resourceGroup)
return fmt.Errorf("Cannot read ID for API %q / Revision %q (API Management Service %q / Resource Group %q)", name, revision, serviceName, resourceGroup)
}

d.SetId(*read.ID)
Expand Down Expand Up @@ -319,19 +325,11 @@ func resourceArmApiManagementApiRead(d *schema.ResourceData, meta interface{}) e
d.Set("is_online", props.IsOnline)
d.Set("path", props.Path)
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 apiR := props.APIRevision; apiR != nil {
i, err := strconv.Atoi(*apiR)
if err != nil {
return fmt.Errorf("Error casting %q to an integer: %s", *apiR, err)
}

d.Set("revision", i)
}

if err := d.Set("protocols", flattenApiManagementApiProtocols(props.Protocols)); err != nil {
return fmt.Errorf("Error setting `protocols`: %s", err)
}
Expand Down
55 changes: 49 additions & 6 deletions azurerm/resource_arm_api_management_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ func TestAccAzureRMApiManagementApi_basic(t *testing.T) {
})
}

func TestAccAzureRMApiManagementApi_wordRevision(t *testing.T) {
resourceName := "azurerm_api_management_api.test"
ri := acctest.RandInt()
location := testLocation()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMApiManagementApiDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMApiManagementApi_wordRevision(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMApiManagementApiExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "revision", "one-point-oh"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMApiManagementApi_requiresImport(t *testing.T) {
if !requireResourcesToBeImported {
t.Skip("Skipping since resources aren't required to be imported")
Expand Down Expand Up @@ -291,7 +317,24 @@ resource "azurerm_api_management_api" "test" {
display_name = "api1"
path = "api1"
protocols = ["https"]
revision = 1
revision = "1"
}
`, template, rInt)
}

func testAccAzureRMApiManagementApi_wordRevision(rInt int, location string) string {
template := testAccAzureRMApiManagementApi_template(rInt, location)
return fmt.Sprintf(`
%s
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 = "one-point-oh"
}
`, template, rInt)
}
Expand All @@ -308,7 +351,7 @@ resource "azurerm_api_management_api" "test" {
display_name = "api1"
path = "api1"
protocols = ["https"]
revision = 1
revision = "1"
soap_pass_through = true
}
`, template, rInt)
Expand Down Expand Up @@ -343,7 +386,7 @@ resource "azurerm_api_management_api" "test" {
display_name = "api1"
path = "api1"
protocols = ["https"]
revision = 1
revision = "1"
import {
content_value = "${file("testdata/api_management_api_swagger.json")}"
Expand All @@ -365,7 +408,7 @@ resource "azurerm_api_management_api" "test" {
display_name = "api1"
path = "api1"
protocols = ["https"]
revision = 1
revision = "1"
import {
content_value = "${file("testdata/api_management_api_wsdl.xml")}"
Expand All @@ -392,8 +435,8 @@ resource "azurerm_api_management_api" "test" {
display_name = "Butter Parser"
path = "butter-parser"
protocols = ["https", "http"]
revision = 3
description = "What is my purpose? You pass butter."
revision = "3"
description = "What is my purpose? You parse butter."
service_url = "https://example.com/foo/bar"
subscription_key_parameter_names {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/d/api_management_api.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ output "api_management_api_id" {

* `resource_group_name` - (Required) The Name of the Resource Group in which the API Management Service exists.

* `revision` - (Required) The Revision number of the API Management API.
* `revision` - (Required) The Revision of the API Management API.

## Attributes Reference

Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/api_management_api.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ resource "azurerm_api_management_api" "test" {
name = "example-api"
resource_group_name = "${azurerm_resource_group.test.name}"
api_management_name = "${azurerm_api_management.test.name}"
revision = 1
revision = "1"
display_name = "Example API"
path = "example"
protocols = ["https"]
Expand All @@ -57,7 +57,7 @@ The following arguments are supported:

* `resource_group_name` - (Required) The Name of the Resource Group where the API Management API exists. Changing this forces a new resource to be created.

* `revision` - (Required) The Revision number used for this API.
* `revision` - (Required) The Revision which used for this API.

* `display_name` - (Required) The display name of the API.

Expand Down

0 comments on commit 34e10fa

Please sign in to comment.