-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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 #3066 from terraform-providers/f/api-management-pr…
…oduct-api New Resource: `azurerm_api_management_product_api`
- Loading branch information
Showing
9 changed files
with
382 additions
and
5 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,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
180
azurerm/resource_arm_api_management_product_api_test.go
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,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) | ||
} |
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.