Skip to content

Commit

Permalink
Merge pull request #3066 from terraform-providers/f/api-management-pr…
Browse files Browse the repository at this point in the history
…oduct-api

New Resource: `azurerm_api_management_product_api`
  • Loading branch information
tombuildsstuff authored Mar 19, 2019
2 parents b9774b2 + 1500308 commit 5b65bd2
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 5 deletions.
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type ArmClient struct {
apiManagementGroupClient apimanagement.GroupClient
apiManagementGroupUsersClient apimanagement.GroupUserClient
apiManagementProductsClient apimanagement.ProductClient
apiManagementProductApisClient apimanagement.ProductAPIClient
apiManagementProductGroupsClient apimanagement.ProductGroupClient
apiManagementPropertyClient apimanagement.PropertyClient
apiManagementServiceClient apimanagement.ServiceClient
Expand Down Expand Up @@ -515,6 +516,10 @@ func (c *ArmClient) registerApiManagementServiceClients(endpoint, subscriptionId
c.configureClient(&productsClient.Client, auth)
c.apiManagementProductsClient = productsClient

productApisClient := apimanagement.NewProductAPIClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&productApisClient.Client, auth)
c.apiManagementProductApisClient = productApisClient

productGroupsClient := apimanagement.NewProductGroupClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&productGroupsClient.Client, auth)
c.apiManagementProductGroupsClient = productGroupsClient
Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_api_management_group": resourceArmApiManagementGroup(),
"azurerm_api_management_group_user": resourceArmApiManagementGroupUser(),
"azurerm_api_management_product": resourceArmApiManagementProduct(),
"azurerm_api_management_product_api": resourceArmApiManagementProductApi(),
"azurerm_api_management_product_group": resourceArmApiManagementProductGroup(),
"azurerm_api_management_property": resourceArmApiManagementProperty(),
"azurerm_api_management_user": resourceArmApiManagementUser(),
Expand Down
121 changes: 121 additions & 0 deletions azurerm/resource_arm_api_management_product_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package azurerm

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmApiManagementProductApi() *schema.Resource {
return &schema.Resource{
Create: resourceArmApiManagementProductApiCreate,
Read: resourceArmApiManagementProductApiRead,
Delete: resourceArmApiManagementProductApiDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"api_name": azure.SchemaApiManagementChildName(),

"product_id": azure.SchemaApiManagementChildName(),

"resource_group_name": resourceGroupNameSchema(),

"api_management_name": azure.SchemaApiManagementName(),
},
}
}

func resourceArmApiManagementProductApiCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiManagementProductApisClient
ctx := meta.(*ArmClient).StopContext

resourceGroup := d.Get("resource_group_name").(string)
serviceName := d.Get("api_management_name").(string)
apiName := d.Get("api_name").(string)
productId := d.Get("product_id").(string)

if requireResourcesToBeImported {
resp, err := client.CheckEntityExists(ctx, resourceGroup, serviceName, productId, apiName)
if err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error checking for present of existing API %q / Product %q (API Management Service %q / Resource Group %q): %+v", apiName, productId, serviceName, resourceGroup, err)
}
}

if !utils.ResponseWasNotFound(resp) {
subscriptionId := meta.(*ArmClient).subscriptionId
resourceId := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ApiManagement/service/%s/products/%s/apis/%s", subscriptionId, resourceGroup, serviceName, productId, apiName)
return tf.ImportAsExistsError("azurerm_api_management_product_api", resourceId)
}
}

resp, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, productId, apiName)
if err != nil {
return fmt.Errorf("Error adding API %q to Product %q (API Management Service %q / Resource Group %q): %+v", apiName, productId, serviceName, resourceGroup, err)
}

// there's no Read so this is best-effort
d.SetId(*resp.ID)

return resourceArmApiManagementProductApiRead(d, meta)
}

func resourceArmApiManagementProductApiRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiManagementProductApisClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
productId := id.Path["products"]
apiName := id.Path["apis"]

resp, err := client.CheckEntityExists(ctx, resourceGroup, serviceName, productId, apiName)
if err != nil {
if utils.ResponseWasNotFound(resp) {
log.Printf("[DEBUG] API %q was not found in Product %q (API Management Service %q / Resource Group %q) was not found - removing from state!", apiName, productId, serviceName, resourceGroup)
d.SetId("")
return nil
}

return fmt.Errorf("Error retrieving API %q / Product %q (API Management Service %q / Resource Group %q): %+v", apiName, productId, serviceName, resourceGroup, err)
}

d.Set("api_name", apiName)
d.Set("product_id", productId)
d.Set("resource_group_name", resourceGroup)
d.Set("api_management_name", serviceName)

return nil
}

func resourceArmApiManagementProductApiDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiManagementProductApisClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
productId := id.Path["products"]
apiName := id.Path["apis"]

if resp, err := client.Delete(ctx, resourceGroup, serviceName, productId, apiName); err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error removing API %q from Product %q (API Management Service %q / Resource Group %q): %+v", apiName, productId, serviceName, resourceGroup, err)
}
}

return nil
}
180 changes: 180 additions & 0 deletions azurerm/resource_arm_api_management_product_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func TestAccAzureRMAPIManagementProductApi_basic(t *testing.T) {
resourceName := "azurerm_api_management_product_api.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAPIManagementProductApiDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMAPIManagementProductApi_basic(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAPIManagementProductApiExists(resourceName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMAPIManagementProductApi_requiresImport(t *testing.T) {
if !requireResourcesToBeImported {
t.Skip("Skipping since resources aren't required to be imported")
return
}

resourceName := "azurerm_api_management_product_api.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAPIManagementProductApiDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMAPIManagementProductApi_basic(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAPIManagementProductGroupExists(resourceName),
),
},
{
Config: testAccAzureRMAPIManagementProductApi_requiresImport(ri, location),
ExpectError: testRequiresImportError("azurerm_api_management_product_api"),
},
},
})
}

func testCheckAzureRMAPIManagementProductApiDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*ArmClient).apiManagementProductApisClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_api_management_product_api" {
continue
}

apiName := rs.Primary.Attributes["api_name"]
productId := rs.Primary.Attributes["product_id"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
serviceName := rs.Primary.Attributes["api_management_name"]

ctx := testAccProvider.Meta().(*ArmClient).StopContext
resp, err := client.CheckEntityExists(ctx, resourceGroup, serviceName, productId, apiName)
if err != nil {
if !utils.ResponseWasNotFound(resp) {
return err
}
}

return nil
}
return nil
}

func testCheckAzureRMAPIManagementProductApiExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}

apiName := rs.Primary.Attributes["api_name"]
productId := rs.Primary.Attributes["product_id"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
serviceName := rs.Primary.Attributes["api_management_name"]

client := testAccProvider.Meta().(*ArmClient).apiManagementProductApisClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext
resp, err := client.CheckEntityExists(ctx, resourceGroup, serviceName, productId, apiName)
if err != nil {
if utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Bad: API %q / Product %q (API Management Service %q / Resource Group %q) does not exist", apiName, productId, serviceName, resourceGroup)
}
return fmt.Errorf("Bad: Get on apiManagementProductApisClient: %+v", err)
}

return nil
}
}

func testAccAzureRMAPIManagementProductApi_basic(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_api_management" "test" {
name = "acctestAM-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
publisher_name = "pub1"
publisher_email = "pub1@email.com"
sku {
name = "Developer"
capacity = 1
}
}
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 = false
published = true
}
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"
}
resource "azurerm_api_management_product_api" "test" {
product_id = "${azurerm_api_management_product.test.product_id}"
api_name = "${azurerm_api_management_api.test.name}"
api_management_name = "${azurerm_api_management.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, rInt, location, rInt, rInt)
}

func testAccAzureRMAPIManagementProductApi_requiresImport(rInt int, location string) string {
template := testAccAzureRMAPIManagementProductApi_basic(rInt, location)
return fmt.Sprintf(`
%s
resource "azurerm_api_management_product_api" "import" {
api_name = "${azurerm_api_management_product_api.test.api_name}"
product_id = "${azurerm_api_management_product_api.test.product_id}"
api_management_name = "${azurerm_api_management_product_api.test.api_management_name}"
resource_group_name = "${azurerm_api_management_product_api.test.resource_group_name}"
}
`, template)
}
2 changes: 1 addition & 1 deletion azurerm/resource_arm_api_management_product_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestAccAzureRMAPIManagementProductGroup_requiresImport(t *testing.T) {
}

func testCheckAzureRMAPIManagementProductGroupDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*ArmClient).apiManagementGroupUsersClient
client := testAccProvider.Meta().(*ArmClient).apiManagementProductGroupsClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_api_management_product_group" {
continue
Expand Down
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@
<a href="/docs/providers/azurerm/r/api_management_product.html">azurerm_api_management_product</a>
</li>

<li<%= sidebar_current("docs-azurerm-resource-api-management-product-api") %>>
<a href="/docs/providers/azurerm/r/api_management_product_api.html">azurerm_api_management_product_api</a>
</li>

<li<%= sidebar_current("docs-azurerm-resource-api-management-product-group") %>>
<a href="/docs/providers/azurerm/r/api_management_product_group.html">azurerm_api_management_product_group</a>
</li>
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 @@ -17,7 +17,7 @@ data "azurerm_api_management_api" "test" {
name = "search-api"
api_management_name = "search-api-management"
resource_group_name = "search-service"
revision = 2
revision = "2"
}
output "api_management_api_id" {
Expand Down
Loading

0 comments on commit 5b65bd2

Please sign in to comment.