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_group #2809

Merged
merged 11 commits into from
Feb 27, 2019
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ type ArmClient struct {
redisPatchSchedulesClient redis.PatchSchedulesClient

// API Management
apiManagementGroupClient apimanagement.GroupClient
apiManagementProductsClient apimanagement.ProductClient
apiManagementServiceClient apimanagement.ServiceClient

Expand Down Expand Up @@ -488,6 +489,10 @@ func getArmClient(c *authentication.Config, skipProviderRegistration bool, partn
}

func (c *ArmClient) registerApiManagementServiceClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
groupsClient := apimanagement.NewGroupClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&groupsClient.Client, auth)
c.apiManagementGroupClient = groupsClient

serviceClient := apimanagement.NewServiceClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&serviceClient.Client, auth)
c.apiManagementServiceClient = serviceClient
Expand Down
79 changes: 79 additions & 0 deletions azurerm/data_source_api_management_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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/utils"
)

func dataSourceApiManagementGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceApiManagementGroupRead,

Schema: map[string]*schema.Schema{
"name": azure.SchemaApiManagementChildDataSourceName(),

"resource_group_name": resourceGroupNameForDataSourceSchema(),

"api_management_name": azure.SchemaApiManagementDataSourceName(),

"display_name": {
Type: schema.TypeString,
Computed: true,
},

"description": {
Type: schema.TypeString,
Computed: true,
},

"external_id": {
Type: schema.TypeString,
Computed: true,
},

"type": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

resourceGroup := d.Get("resource_group_name").(string)
serviceName := d.Get("api_management_name").(string)
name := d.Get("name").(string)

resp, err := client.Get(ctx, resourceGroup, serviceName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Group %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 Group %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err)
}

d.SetId(*resp.ID)

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("api_management_name", serviceName)

if properties := resp.GroupContractProperties; properties != nil {
d.Set("display_name", properties.DisplayName)
d.Set("description", properties.Description)
d.Set("external_id", properties.ExternalID)
d.Set("type", string(properties.Type))
}

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

import (
"fmt"
"testing"

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

func TestAccDataSourceAzureRMApiManagementGroup_basic(t *testing.T) {
dataSourceName := "data.azurerm_api_management_group.test"
rInt := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceApiManagementGroup_basic(rInt, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "display_name", "Test Group"),
resource.TestCheckResourceAttr(dataSourceName, "description", ""),
resource.TestCheckResourceAttr(dataSourceName, "external_id", ""),
resource.TestCheckResourceAttr(dataSourceName, "type", "custom"),
),
},
},
})
}

func testAccDataSourceApiManagementGroup_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_group" "test" {
name = "acctestAMGroup-%d"
resource_group_name = "${azurerm_resource_group.test.name}"
api_management_name = "${azurerm_api_management.test.name}"
display_name = "Test Group"
}

data "azurerm_api_management_group" "test" {
name = "${azurerm_api_management_group.test.name}"
api_management_name = "${azurerm_api_management_group.test.api_management_name}"
resource_group_name = "${azurerm_api_management_group.test.resource_group_name}"
}
`, rInt, location, rInt, rInt)
}
2 changes: 1 addition & 1 deletion azurerm/data_source_api_management_product.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func dataSourceApiManagementProduct() *schema.Resource {
Read: dataSourceApiManagementProductRead,

Schema: map[string]*schema.Schema{
"product_id": azure.SchemaApiManagementProductDataSourceName(),
"product_id": azure.SchemaApiManagementChildDataSourceName(),
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved

"api_management_name": azure.SchemaApiManagementDataSourceName(),

Expand Down
12 changes: 8 additions & 4 deletions azurerm/helpers/azure/api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@ func SchemaApiManagementDataSourceName() *schema.Schema {
}
}

func SchemaApiManagementProductName() *schema.Schema {
// SchemaApiManagementChildName returns the Schema for the identifier
// used by resources within nested under the API Management Service resource
func SchemaApiManagementChildName() *schema.Schema {
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
return &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ApiManagementProductName,
ValidateFunc: validate.ApiManagementChildName,
}
}

func SchemaApiManagementProductDataSourceName() *schema.Schema {
// SchemaApiManagementChildDataSourceName returns the Schema for the identifier
// used by resources within nested under the API Management Service resource
func SchemaApiManagementChildDataSourceName() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.ApiManagementProductName,
ValidateFunc: validate.ApiManagementChildName,
}
}
2 changes: 1 addition & 1 deletion azurerm/helpers/validate/api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"regexp"
)

func ApiManagementProductName(v interface{}, k string) (warnings []string, errors []error) {
func ApiManagementChildName(v interface{}, k string) (warnings []string, errors []error) {
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
value := v.(string)

// from the portal: `The field may contain only numbers, letters, and dash (-) sign when preceded and followed by number or a letter.`
Expand Down
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func Provider() terraform.ResourceProvider {

DataSourcesMap: map[string]*schema.Resource{
"azurerm_api_management": dataSourceApiManagementService(),
"azurerm_api_management_group": dataSourceApiManagementGroup(),
"azurerm_api_management_product": dataSourceApiManagementProduct(),
"azurerm_app_service_plan": dataSourceAppServicePlan(),
"azurerm_app_service": dataSourceArmAppService(),
Expand Down Expand Up @@ -165,6 +166,7 @@ func Provider() terraform.ResourceProvider {

ResourcesMap: map[string]*schema.Resource{
"azurerm_api_management": resourceArmApiManagementService(),
"azurerm_api_management_group": resourceArmApiManagementGroup(),
"azurerm_api_management_product": resourceArmApiManagementProduct(),
"azurerm_app_service_active_slot": resourceArmAppServiceActiveSlot(),
"azurerm_app_service_custom_hostname_binding": resourceArmAppServiceCustomHostnameBinding(),
Expand Down
157 changes: 157 additions & 0 deletions azurerm/resource_arm_api_management_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
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/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmApiManagementGroup() *schema.Resource {
return &schema.Resource{
Create: resourceArmApiManagementGroupCreateUpdate,
Read: resourceArmApiManagementGroupRead,
Update: resourceArmApiManagementGroupCreateUpdate,
Delete: resourceArmApiManagementGroupDelete,
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,
},

"description": {
Type: schema.TypeString,
Optional: true,
},

"external_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: string(apimanagement.Custom),
ValidateFunc: validation.StringInSlice([]string{
string(apimanagement.Custom),
string(apimanagement.External),
}, false),
},
},
}
}

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

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
serviceName := d.Get("api_management_name").(string)

displayName := d.Get("display_name").(string)
description := d.Get("description").(string)
externalID := d.Get("external_id").(string)
groupType := d.Get("type").(string)

parameters := apimanagement.GroupCreateParameters{
GroupCreateParametersProperties: &apimanagement.GroupCreateParametersProperties{
DisplayName: utils.String(displayName),
Description: utils.String(description),
ExternalID: utils.String(externalID),
Type: apimanagement.GroupType(groupType),
},
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, name, parameters, ""); err != nil {
return fmt.Errorf("Error creating or updating Group %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 Group %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 resourceArmApiManagementGroupRead(d, meta)
}

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

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

resp, err := client.Get(ctx, resourceGroup, serviceName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Group %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 Group %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.GroupContractProperties; properties != nil {
d.Set("display_name", properties.DisplayName)
d.Set("description", properties.Description)
d.Set("external_id", properties.ExternalID)
d.Set("type", string(properties.Type))
}

return nil
}

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

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

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