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: azurerm_api_management_property #2986

Merged
merged 7 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ type ArmClient struct {
apiManagementGroupClient apimanagement.GroupClient
apiManagementGroupUsersClient apimanagement.GroupUserClient
apiManagementProductsClient apimanagement.ProductClient
apiManagementPropertyClient apimanagement.PropertyClient
apiManagementServiceClient apimanagement.ServiceClient
apiManagementUsersClient apimanagement.UserClient

Expand Down Expand Up @@ -507,6 +508,10 @@ func (c *ArmClient) registerApiManagementServiceClients(endpoint, subscriptionId
c.configureClient(&productsClient.Client, auth)
c.apiManagementProductsClient = productsClient

propertiesClient := apimanagement.NewPropertyClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&propertiesClient.Client, auth)
c.apiManagementPropertyClient = propertiesClient

usersClient := apimanagement.NewUserClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&usersClient.Client, auth)
c.apiManagementUsersClient = usersClient
Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_api_management_group": resourceArmApiManagementGroup(),
"azurerm_api_management_group_user": resourceArmApiManagementGroupUser(),
"azurerm_api_management_product": resourceArmApiManagementProduct(),
"azurerm_api_management_property": resourceArmApiManagementProperty(),
"azurerm_api_management_user": resourceArmApiManagementUser(),
"azurerm_app_service_active_slot": resourceArmAppServiceActiveSlot(),
"azurerm_app_service_custom_hostname_binding": resourceArmAppServiceCustomHostnameBinding(),
Expand Down
166 changes: 166 additions & 0 deletions azurerm/resource_arm_api_management_property.go
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 Group %q (Resource Group %q / API Management Service %q)", name, resourceGroup, serviceName)
mbfrahry marked this conversation as resolved.
Show resolved Hide resolved
}
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
}
198 changes: 198 additions & 0 deletions azurerm/resource_arm_api_management_property_test.go
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 = "[email protected]"

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 = "[email protected]"

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)
}
Loading