-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2953 from terraform-providers/f/api-management-pr…
…oduct New Resource/Data Source: `azurerm_api_management_product`
- Loading branch information
Showing
14 changed files
with
982 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/preview/apimanagement/mgmt/2018-06-01-preview/apimanagement" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func dataSourceApiManagementProduct() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceApiManagementProductRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"product_id": azure.SchemaApiManagementProductDataSourceName(), | ||
|
||
"api_management_name": azure.SchemaApiManagementDataSourceName(), | ||
|
||
"resource_group_name": resourceGroupNameSchema(), | ||
|
||
"display_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"subscription_required": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
|
||
"approval_required": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
|
||
"published": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
|
||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"terms": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"subscriptions_limit": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
func dataSourceApiManagementProductRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).apiManagementProductsClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
resourceGroup := d.Get("resource_group_name").(string) | ||
serviceName := d.Get("api_management_name").(string) | ||
productId := d.Get("product_id").(string) | ||
|
||
resp, err := client.Get(ctx, resourceGroup, serviceName, productId) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Product %q was not found in API Management Service %q / Resource Group %q", productId, serviceName, resourceGroup) | ||
} | ||
|
||
return fmt.Errorf("Error making Read request on Product %q (API Management Service %q / Resource Group %q): %+v", productId, serviceName, resourceGroup, err) | ||
} | ||
|
||
d.SetId(*resp.ID) | ||
|
||
if props := resp.ProductContractProperties; props != nil { | ||
d.Set("approval_required", props.ApprovalRequired) | ||
d.Set("description", props.Description) | ||
d.Set("display_name", props.DisplayName) | ||
d.Set("published", props.State == apimanagement.Published) | ||
d.Set("subscriptions_limit", props.SubscriptionsLimit) | ||
d.Set("subscription_required", props.SubscriptionRequired) | ||
d.Set("terms", props.Terms) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" | ||
) | ||
|
||
func TestAccDataSourceAzureRMApiManagementProduct_basic(t *testing.T) { | ||
dataSourceName := "data.azurerm_api_management_product.test" | ||
rInt := tf.AccRandTimeInt() | ||
location := testLocation() | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceApiManagementProduct_basic(rInt, location), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(dataSourceName, "product_id", "test-product"), | ||
resource.TestCheckResourceAttr(dataSourceName, "display_name", "Test Product"), | ||
resource.TestCheckResourceAttr(dataSourceName, "subscription_required", "true"), | ||
resource.TestCheckResourceAttr(dataSourceName, "approval_required", "true"), | ||
resource.TestCheckResourceAttr(dataSourceName, "published", "true"), | ||
resource.TestCheckResourceAttr(dataSourceName, "description", "This is an example description"), | ||
resource.TestCheckResourceAttr(dataSourceName, "terms", "These are some example terms and conditions"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceApiManagementProduct_basic(rInt int, location string) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "amtestRG-%d" | ||
location = "%s" | ||
} | ||
resource "azurerm_api_management" "test" { | ||
name = "acctestAM-%d" | ||
publisher_name = "pub1" | ||
publisher_email = "pub1@email.com" | ||
sku { | ||
name = "Developer" | ||
capacity = 1 | ||
} | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
} | ||
resource "azurerm_api_management_product" "test" { | ||
product_id = "test-product" | ||
api_management_name = "${azurerm_api_management.test.name}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
display_name = "Test Product" | ||
subscription_required = true | ||
approval_required = true | ||
published = true | ||
description = "This is an example description" | ||
terms = "These are some example terms and conditions" | ||
} | ||
data "azurerm_api_management_product" "test" { | ||
product_id = "${azurerm_api_management_product.test.product_id}" | ||
api_management_name = "${azurerm_api_management_product.test.api_management_name}" | ||
resource_group_name = "${azurerm_api_management_product.test.resource_group_name}" | ||
} | ||
`, rInt, location, rInt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package azure | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" | ||
) | ||
|
||
func SchemaApiManagementName() *schema.Schema { | ||
return &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.ApiManagementServiceName, | ||
} | ||
} | ||
|
||
func SchemaApiManagementDataSourceName() *schema.Schema { | ||
return &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.ApiManagementServiceName, | ||
} | ||
} | ||
|
||
func SchemaApiManagementProductName() *schema.Schema { | ||
return &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.ApiManagementProductName, | ||
} | ||
} | ||
|
||
func SchemaApiManagementProductDataSourceName() *schema.Schema { | ||
return &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.ApiManagementProductName, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.