-
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 #2986 from terraform-providers/f-api-management-pr…
…operty New resource: `azurerm_api_management_property`
- Loading branch information
Showing
6 changed files
with
455 additions
and
0 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,166 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2018-01-01/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/helpers/tf" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmApiManagementProperty() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmApiManagementPropertyCreateUpdate, | ||
Read: resourceArmApiManagementPropertyRead, | ||
Update: resourceArmApiManagementPropertyCreateUpdate, | ||
Delete: resourceArmApiManagementPropertyDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": azure.SchemaApiManagementChildName(), | ||
|
||
"resource_group_name": resourceGroupNameSchema(), | ||
|
||
"api_management_name": azure.SchemaApiManagementName(), | ||
|
||
"display_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.NoEmptyStrings, | ||
}, | ||
|
||
"value": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.NoEmptyStrings, | ||
}, | ||
|
||
"secret": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
|
||
"tags": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmApiManagementPropertyCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).apiManagementPropertyClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
serviceName := d.Get("api_management_name").(string) | ||
|
||
if requireResourcesToBeImported && d.IsNewResource() { | ||
existing, err := client.Get(ctx, resourceGroup, serviceName, name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("Error checking for presence of existing Property %q (API Management Service %q / Resource Group %q): %s", name, serviceName, resourceGroup, err) | ||
} | ||
} | ||
|
||
if existing.ID != nil && *existing.ID != "" { | ||
return tf.ImportAsExistsError("azurerm_api_management_property", *existing.ID) | ||
} | ||
} | ||
|
||
parameters := apimanagement.PropertyContract{ | ||
PropertyContractProperties: &apimanagement.PropertyContractProperties{ | ||
DisplayName: utils.String(d.Get("display_name").(string)), | ||
Secret: utils.Bool(d.Get("secret").(bool)), | ||
Value: utils.String(d.Get("value").(string)), | ||
}, | ||
} | ||
|
||
if tags, ok := d.GetOk("tags"); ok { | ||
parameters.PropertyContractProperties.Tags = utils.ExpandStringArray(tags.([]interface{})) | ||
} | ||
|
||
if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, name, parameters, ""); err != nil { | ||
return fmt.Errorf("Error creating or updating Property %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err) | ||
} | ||
|
||
resp, err := client.Get(ctx, resourceGroup, serviceName, name) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving Property %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err) | ||
} | ||
if resp.ID == nil { | ||
return fmt.Errorf("Cannot read ID for Property %q (Resource Group %q / API Management Service %q)", name, resourceGroup, serviceName) | ||
} | ||
d.SetId(*resp.ID) | ||
|
||
return resourceArmApiManagementPropertyRead(d, meta) | ||
} | ||
|
||
func resourceArmApiManagementPropertyRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).apiManagementPropertyClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
serviceName := id.Path["service"] | ||
name := id.Path["properties"] | ||
|
||
resp, err := client.Get(ctx, resourceGroup, serviceName, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[DEBUG] Property %q (Resource Group %q / API Management Service %q) was not found - removing from state!", name, resourceGroup, serviceName) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Error making Read request for Property %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err) | ||
} | ||
|
||
d.Set("name", resp.Name) | ||
d.Set("resource_group_name", resourceGroup) | ||
d.Set("api_management_name", serviceName) | ||
|
||
if properties := resp.PropertyContractProperties; properties != nil { | ||
d.Set("display_name", properties.DisplayName) | ||
d.Set("secret", properties.Secret) | ||
d.Set("value", properties.Value) | ||
d.Set("tags", properties.Tags) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmApiManagementPropertyDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).apiManagementPropertyClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
serviceName := id.Path["service"] | ||
name := id.Path["properties"] | ||
|
||
if resp, err := client.Delete(ctx, resourceGroup, serviceName, name, ""); err != nil { | ||
if !utils.ResponseWasNotFound(resp) { | ||
return fmt.Errorf("Error deleting Property %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err) | ||
} | ||
} | ||
|
||
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,198 @@ | ||
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 TestAccAzureRMAPIManagementProperty_basic(t *testing.T) { | ||
resourceName := "azurerm_api_management_property.test" | ||
ri := tf.AccRandTimeInt() | ||
config := testAccAzureRMAPIManagementProperty_basic(ri, testLocation()) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMAPIManagementPropertyDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMAPIManagementPropertyExists(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "display_name", fmt.Sprintf("TestProperty%d", ri)), | ||
resource.TestCheckResourceAttr(resourceName, "value", "Test Value"), | ||
resource.TestCheckResourceAttr(resourceName, "tags.0", "tag1"), | ||
resource.TestCheckResourceAttr(resourceName, "tags.1", "tag2"), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMAPIManagementProperty_update(t *testing.T) { | ||
resourceName := "azurerm_api_management_property.test" | ||
ri := tf.AccRandTimeInt() | ||
config := testAccAzureRMAPIManagementProperty_basic(ri, testLocation()) | ||
config2 := testAccAzureRMAPIManagementProperty_update(ri, testLocation()) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMAPIManagementPropertyDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMAPIManagementPropertyExists(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "display_name", fmt.Sprintf("TestProperty%d", ri)), | ||
resource.TestCheckResourceAttr(resourceName, "value", "Test Value"), | ||
resource.TestCheckResourceAttr(resourceName, "tags.0", "tag1"), | ||
resource.TestCheckResourceAttr(resourceName, "tags.1", "tag2"), | ||
), | ||
}, | ||
{ | ||
Config: config2, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMAPIManagementPropertyExists(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "display_name", fmt.Sprintf("TestProperty2%d", ri)), | ||
resource.TestCheckResourceAttr(resourceName, "value", "Test Value2"), | ||
resource.TestCheckResourceAttr(resourceName, "secret", "true"), | ||
resource.TestCheckResourceAttr(resourceName, "tags.0", "tag3"), | ||
resource.TestCheckResourceAttr(resourceName, "tags.1", "tag4"), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testCheckAzureRMAPIManagementPropertyDestroy(s *terraform.State) error { | ||
client := testAccProvider.Meta().(*ArmClient).apiManagementPropertyClient | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "azurerm_api_management_property" { | ||
continue | ||
} | ||
|
||
name := rs.Primary.Attributes["name"] | ||
resourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
serviceName := rs.Primary.Attributes["api_management_name"] | ||
|
||
ctx := testAccProvider.Meta().(*ArmClient).StopContext | ||
resp, err := client.Get(ctx, resourceGroup, serviceName, name) | ||
|
||
if err != nil { | ||
if !utils.ResponseWasNotFound(resp.Response) { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
return nil | ||
} | ||
|
||
func testCheckAzureRMAPIManagementPropertyExists(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) | ||
} | ||
|
||
name := rs.Primary.Attributes["name"] | ||
resourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
serviceName := rs.Primary.Attributes["api_management_name"] | ||
|
||
client := testAccProvider.Meta().(*ArmClient).apiManagementPropertyClient | ||
ctx := testAccProvider.Meta().(*ArmClient).StopContext | ||
resp, err := client.Get(ctx, resourceGroup, serviceName, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Bad: API Management Property %q (Resource Group %q / API Management Service %q) does not exist", name, resourceGroup, serviceName) | ||
} | ||
return fmt.Errorf("Bad: Get on apiManagementPropertyClient: %+v", err) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
/* | ||
*/ | ||
|
||
func testAccAzureRMAPIManagementProperty_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_property" "test" { | ||
name = "acctestAMProperty-%d" | ||
resource_group_name = "${azurerm_api_management.test.resource_group_name}" | ||
api_management_name = "${azurerm_api_management.test.name}" | ||
display_name = "TestProperty%d" | ||
value = "Test Value" | ||
tags = ["tag1", "tag2"] | ||
} | ||
`, rInt, location, rInt, rInt, rInt) | ||
} | ||
|
||
func testAccAzureRMAPIManagementProperty_update(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_property" "test" { | ||
name = "acctestAMProperty-%d" | ||
resource_group_name = "${azurerm_api_management.test.resource_group_name}" | ||
api_management_name = "${azurerm_api_management.test.name}" | ||
display_name = "TestProperty2%d" | ||
value = "Test Value2" | ||
secret = true | ||
tags = ["tag3", "tag4"] | ||
} | ||
`, rInt, location, rInt, rInt, rInt) | ||
} |
Oops, something went wrong.