From 6c978d6bcbfd4111c6f2ba7dc63b45c56b13c333 Mon Sep 17 00:00:00 2001 From: Matthew Frahry Date: Fri, 1 Mar 2019 14:10:55 -0700 Subject: [PATCH 1/5] Adding api management property resource --- azurerm/config.go | 5 + azurerm/provider.go | 1 + .../resource_arm_api_management_property.go | 166 +++++++++++++++ ...source_arm_api_management_property_test.go | 198 ++++++++++++++++++ website/azurerm.erb | 8 + .../r/api_management_property.html.markdown | 75 +++++++ 6 files changed, 453 insertions(+) create mode 100644 azurerm/resource_arm_api_management_property.go create mode 100644 azurerm/resource_arm_api_management_property_test.go create mode 100644 website/docs/r/api_management_property.html.markdown diff --git a/azurerm/config.go b/azurerm/config.go index 3f3a422c6038..fa1732738b9d 100644 --- a/azurerm/config.go +++ b/azurerm/config.go @@ -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 @@ -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 diff --git a/azurerm/provider.go b/azurerm/provider.go index ce0a57c84047..bd5cda9fc5f2 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -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(), diff --git a/azurerm/resource_arm_api_management_property.go b/azurerm/resource_arm_api_management_property.go new file mode 100644 index 000000000000..d85365fcd099 --- /dev/null +++ b/azurerm/resource_arm_api_management_property.go @@ -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) + } + 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 Group %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err) + } + } + + return nil +} diff --git a/azurerm/resource_arm_api_management_property_test.go b/azurerm/resource_arm_api_management_property_test.go new file mode 100644 index 000000000000..6c97187b3731 --- /dev/null +++ b/azurerm/resource_arm_api_management_property_test.go @@ -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 Group %q (Resource Group %q / API Management Service %q) does not exist", name, resourceGroup, serviceName) + } + return fmt.Errorf("Bad: Get on apiManagementGroupClient: %+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) +} diff --git a/website/azurerm.erb b/website/azurerm.erb index 7463396c0272..69833a7610fb 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -71,6 +71,10 @@ azurerm_api_management_product + > + azurerm_api_management_property + + > azurerm_api_management_user @@ -857,6 +861,10 @@ azurerm_eventgrid_domain + > + azurerm_eventgrid_event_subscription + + > azurerm_eventgrid_topic diff --git a/website/docs/r/api_management_property.html.markdown b/website/docs/r/api_management_property.html.markdown new file mode 100644 index 000000000000..b2f800c59102 --- /dev/null +++ b/website/docs/r/api_management_property.html.markdown @@ -0,0 +1,75 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_api_management_property" +sidebar_current: "docs-azurerm-resource-api-management-property-x" +description: |- + Manages an API Management Property. +--- + +# azurerm_api_management_property + +Manages an API Management Property. + + +## Example Usage + +```hcl +resource "azurerm_resource_group" "example" { + name = "example-resources" + location = "West US" +} + +resource "azurerm_api_management" "example" { + name = "example-apim" + location = "${azurerm_resource_group.example.location}" + resource_group_name = "${azurerm_resource_group.example.name}" + publisher_name = "pub1" + publisher_email = "pub1@email.com" + + sku { + name = "Developer" + capacity = 1 + } +} + +resource "azurerm_api_management_property" "example" { + name = "example-apimg" + resource_group_name = "${azurerm_resource_group.example.name}" + api_management_name = "${azurerm_api_management.example.name}" + display_name = "ExampleProperty" + value = "Example Value" +} +``` + + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the API Management Property. Changing this forces a new resource to be created. + +* `resource_group_name` - (Required) The name of the Resource Group in which the API Management Property should exist. Changing this forces a new resource to be created. + +* `api_management_name` - (Required) The name of the [API Management Service](api_management.html) in which the API Management Property should exist. Changing this forces a new resource to be created. + +* `display_name` - (Required) The display name of this API Management Property. + +* `value` - (Required) The value of this API Management Property. + +* `secret` - (Optional) Specifies whether the API Management Property is secret. Valid values are `true` or `false`. The default value is `false`. + +* `tags` - (Optional) A list of tags to be applied to the API Management Property. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The ID of the API Management Group. + +## Import + +API Management Groups can be imported using the `resource id`, e.g. + +```shell +terraform import azurerm_api_management_property.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.ApiManagement/service/example-apim/properties/example-apimp +``` From 1086fa8a80504200c2350eb3ee11506381730e55 Mon Sep 17 00:00:00 2001 From: Matthew Frahry Date: Fri, 1 Mar 2019 14:14:43 -0700 Subject: [PATCH 2/5] Fixing spelling --- azurerm/resource_arm_api_management_property.go | 2 +- azurerm/resource_arm_api_management_property_test.go | 4 ++-- website/docs/r/api_management_property.html.markdown | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/azurerm/resource_arm_api_management_property.go b/azurerm/resource_arm_api_management_property.go index d85365fcd099..9dfa4d149f5e 100644 --- a/azurerm/resource_arm_api_management_property.go +++ b/azurerm/resource_arm_api_management_property.go @@ -158,7 +158,7 @@ func resourceArmApiManagementPropertyDelete(d *schema.ResourceData, meta interfa if resp, err := client.Delete(ctx, resourceGroup, serviceName, name, ""); err != nil { if !utils.ResponseWasNotFound(resp) { - return fmt.Errorf("Error deleting Group %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err) + return fmt.Errorf("Error deleting Property %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err) } } diff --git a/azurerm/resource_arm_api_management_property_test.go b/azurerm/resource_arm_api_management_property_test.go index 6c97187b3731..b93d3aed3df4 100644 --- a/azurerm/resource_arm_api_management_property_test.go +++ b/azurerm/resource_arm_api_management_property_test.go @@ -121,9 +121,9 @@ func testCheckAzureRMAPIManagementPropertyExists(resourceName string) resource.T resp, err := client.Get(ctx, resourceGroup, serviceName, name) if err != nil { if utils.ResponseWasNotFound(resp.Response) { - return fmt.Errorf("Bad: API Management Group %q (Resource Group %q / API Management Service %q) does not exist", name, resourceGroup, serviceName) + 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 apiManagementGroupClient: %+v", err) + return fmt.Errorf("Bad: Get on apiManagementPropertyClient: %+v", err) } return nil diff --git a/website/docs/r/api_management_property.html.markdown b/website/docs/r/api_management_property.html.markdown index b2f800c59102..eda8d1662f9f 100644 --- a/website/docs/r/api_management_property.html.markdown +++ b/website/docs/r/api_management_property.html.markdown @@ -64,11 +64,11 @@ The following arguments are supported: In addition to all arguments above, the following attributes are exported: -* `id` - The ID of the API Management Group. +* `id` - The ID of the API Management Property. ## Import -API Management Groups can be imported using the `resource id`, e.g. +API Management Properties can be imported using the `resource id`, e.g. ```shell terraform import azurerm_api_management_property.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.ApiManagement/service/example-apim/properties/example-apimp From 06e0985c7940f9c1826a4d798e6fd196967fb239 Mon Sep 17 00:00:00 2001 From: kt Date: Mon, 4 Mar 2019 07:43:11 -0700 Subject: [PATCH 3/5] Update azurerm/resource_arm_api_management_property.go Co-Authored-By: mbfrahry --- azurerm/resource_arm_api_management_property.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/resource_arm_api_management_property.go b/azurerm/resource_arm_api_management_property.go index 9dfa4d149f5e..6730203d607c 100644 --- a/azurerm/resource_arm_api_management_property.go +++ b/azurerm/resource_arm_api_management_property.go @@ -100,7 +100,7 @@ func resourceArmApiManagementPropertyCreateUpdate(d *schema.ResourceData, meta i 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) + return fmt.Errorf("Cannot read ID for Property %q (Resource Group %q / API Management Service %q)", name, resourceGroup, serviceName) } d.SetId(*resp.ID) From 3647d7fa7f3d89324d86f256b4743e2834e3a647 Mon Sep 17 00:00:00 2001 From: Matthew Frahry Date: Mon, 4 Mar 2019 07:48:55 -0700 Subject: [PATCH 4/5] Update docs with note --- website/docs/r/api_management_property.html.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/docs/r/api_management_property.html.markdown b/website/docs/r/api_management_property.html.markdown index eda8d1662f9f..2ad9cc8a3692 100644 --- a/website/docs/r/api_management_property.html.markdown +++ b/website/docs/r/api_management_property.html.markdown @@ -58,6 +58,8 @@ The following arguments are supported: * `secret` - (Optional) Specifies whether the API Management Property is secret. Valid values are `true` or `false`. The default value is `false`. +~> **NOTE:** setting the field `secret` to `true` doesn't make this field sensitive in Terraform, instead it marks the value as secret and encrypts the value in Azure. + * `tags` - (Optional) A list of tags to be applied to the API Management Property. ## Attributes Reference From 54436bef131adda8bcfd0d550883c4c6c0241274 Mon Sep 17 00:00:00 2001 From: Matthew Frahry Date: Mon, 4 Mar 2019 08:33:12 -0700 Subject: [PATCH 5/5] moving property link --- website/azurerm.erb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/website/azurerm.erb b/website/azurerm.erb index 7890942943f7..8ca020169a6f 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -71,10 +71,6 @@ azurerm_api_management_product - > - azurerm_api_management_property - - > azurerm_api_management_user @@ -349,6 +345,10 @@ > azurerm_api_management_product_group + + > + azurerm_api_management_property +