Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Resource/Data Source: azurerm_api_management_product #2953

Merged
merged 8 commits into from
Feb 27, 2019
13 changes: 9 additions & 4 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ type ArmClient struct {
redisPatchSchedulesClient redis.PatchSchedulesClient

// API Management
apiManagementServiceClient apimanagement.ServiceClient
apiManagementProductsClient apimanagement.ProductClient
apiManagementServiceClient apimanagement.ServiceClient

// Application Insights
appInsightsClient appinsights.ComponentsClient
Expand Down Expand Up @@ -487,9 +488,13 @@ func getArmClient(c *authentication.Config, skipProviderRegistration bool, partn
}

func (c *ArmClient) registerApiManagementServiceClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
ams := apimanagement.NewServiceClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&ams.Client, auth)
c.apiManagementServiceClient = ams
serviceClient := apimanagement.NewServiceClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&serviceClient.Client, auth)
c.apiManagementServiceClient = serviceClient

productsClient := apimanagement.NewProductClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&productsClient.Client, auth)
c.apiManagementProductsClient = productsClient
}

func (c *ArmClient) registerAppInsightsClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
Expand Down
8 changes: 2 additions & 6 deletions azurerm/data_source_api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"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/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand All @@ -15,11 +15,7 @@ func dataSourceApiManagementService() *schema.Resource {
Read: dataSourceApiManagementRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.ApiManagementServiceName,
},
"name": azure.SchemaApiManagementDataSourceName(),

"resource_group_name": resourceGroupNameForDataSourceSchema(),

Expand Down
90 changes: 90 additions & 0 deletions azurerm/data_source_api_management_product.go
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
}
76 changes: 76 additions & 0 deletions azurerm/data_source_api_management_product_test.go
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)
}
40 changes: 40 additions & 0 deletions azurerm/helpers/azure/api_management.go
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,
}
}
11 changes: 11 additions & 0 deletions azurerm/helpers/validate/api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import (
"regexp"
)

func ApiManagementProductName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

// from the portal: `The field may contain only numbers, letters, and dash (-) sign when preceded and followed by number or a letter.`
if matched := regexp.MustCompile(`(^[a-zA-Z0-9])([a-zA-Z0-9-]{1,78})([a-zA-Z0-9]$)`).Match([]byte(value)); !matched {
errors = append(errors, fmt.Errorf("%q may only contain alphanumeric characters and dashes up to 80 characters in length", k))
}

return warnings, errors
}

func ApiManagementServiceName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

Expand Down
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func Provider() terraform.ResourceProvider {

DataSourcesMap: map[string]*schema.Resource{
"azurerm_api_management": dataSourceApiManagementService(),
"azurerm_api_management_product": dataSourceApiManagementProduct(),
"azurerm_app_service_plan": dataSourceAppServicePlan(),
"azurerm_app_service": dataSourceArmAppService(),
"azurerm_application_insights": dataSourceArmApplicationInsights(),
Expand Down Expand Up @@ -164,6 +165,7 @@ func Provider() terraform.ResourceProvider {

ResourcesMap: map[string]*schema.Resource{
"azurerm_api_management": resourceArmApiManagementService(),
"azurerm_api_management_product": resourceArmApiManagementProduct(),
"azurerm_app_service_active_slot": resourceArmAppServiceActiveSlot(),
"azurerm_app_service_custom_hostname_binding": resourceArmAppServiceCustomHostnameBinding(),
"azurerm_app_service_plan": resourceArmAppServicePlan(),
Expand Down
7 changes: 1 addition & 6 deletions azurerm/resource_arm_api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ func resourceArmApiManagementService() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ApiManagementServiceName,
},
"name": azure.SchemaApiManagementName(),

"resource_group_name": resourceGroupNameSchema(),

Expand Down
Loading