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

azurerm_api_management fix to pass all acctests #7603

Merged
merged 15 commits into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 32 additions & 9 deletions azurerm/internal/services/apimanagement/api_management_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (

"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2019-12-01/apimanagement"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-plugin-sdk/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"

"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"
Expand Down Expand Up @@ -46,10 +46,10 @@ func resourceArmApiManagementService() *schema.Resource {
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(60 * time.Minute),
Create: schema.DefaultTimeout(3 * time.Hour),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(60 * time.Minute),
Delete: schema.DefaultTimeout(60 * time.Minute),
Update: schema.DefaultTimeout(3 * time.Hour),
Delete: schema.DefaultTimeout(3 * time.Hour),
},

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -125,7 +125,6 @@ func resourceArmApiManagementService() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Default: string(apimanagement.VirtualNetworkTypeNone),
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(apimanagement.VirtualNetworkTypeNone),
string(apimanagement.VirtualNetworkTypeExternal),
Expand All @@ -142,7 +141,6 @@ func resourceArmApiManagementService() *schema.Resource {
"subnet_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},
},
Expand Down Expand Up @@ -301,7 +299,6 @@ func resourceArmApiManagementService() *schema.Resource {
"hostname_configuration": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -469,6 +466,21 @@ func resourceArmApiManagementService() *schema.Resource {

"tags": tags.Schema(),
},

// we can only change `virtual_network_type` from None to Internal Or External, Else the subnet can not be destroyed cause “InUseSubnetCannotBeDeleted” for 3 hours
// we can not change the subnet from subnet1 to subnet2 either, Else the subnet1 can not be destroyed cause “InUseSubnetCannotBeDeleted” for 3 hours
// Issue: https://github.com/Azure/azure-rest-api-specs/issues/10395
CustomizeDiff: customdiff.All(
customdiff.ForceNewIfChange("virtual_network_type", func(old, new, meta interface{}) bool {
return !(old.(string) == string(apimanagement.VirtualNetworkTypeNone) &&
(new.(string) == string(apimanagement.VirtualNetworkTypeInternal) ||
new.(string) == string(apimanagement.VirtualNetworkTypeExternal)))
}),

customdiff.ForceNewIfChange("virtual_network_configuration", func(old, new, meta interface{}) bool {
return !(len(old.([]interface{})) == 0 && len(new.([]interface{})) > 0)
}),
),
}
}

Expand Down Expand Up @@ -618,6 +630,7 @@ func resourceArmApiManagementServiceCreateUpdate(d *schema.ResourceData, meta in

func resourceArmApiManagementServiceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ApiManagement.ServiceClient
environment := meta.(*clients.Client).Account.Environment
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand Down Expand Up @@ -694,7 +707,8 @@ func resourceArmApiManagementServiceRead(d *schema.ResourceData, meta interface{
return fmt.Errorf("setting `protocols`: %+v", err)
}

hostnameConfigs := flattenApiManagementHostnameConfigurations(props.HostnameConfigurations, d)
apimHostNameSuffix := environment.APIManagementHostNameSuffix
hostnameConfigs := flattenApiManagementHostnameConfigurations(props.HostnameConfigurations, d, name, apimHostNameSuffix)
if err := d.Set("hostname_configuration", hostnameConfigs); err != nil {
return fmt.Errorf("setting `hostname_configuration`: %+v", err)
}
Expand Down Expand Up @@ -864,7 +878,7 @@ func expandApiManagementCommonHostnameConfiguration(input map[string]interface{}
return output
}

func flattenApiManagementHostnameConfigurations(input *[]apimanagement.HostnameConfiguration, d *schema.ResourceData) []interface{} {
func flattenApiManagementHostnameConfigurations(input *[]apimanagement.HostnameConfiguration, d *schema.ResourceData, name, apimHostNameSuffix string) []interface{} {
results := make([]interface{}, 0)
if input == nil {
return results
Expand All @@ -883,6 +897,11 @@ func flattenApiManagementHostnameConfigurations(input *[]apimanagement.HostnameC
output["host_name"] = *config.HostName
}

// There'll always be a default custom domain with hostName "apim_name.azure-api.net" and Type "Proxy", which should be ignored
if *config.HostName == strings.ToLower(name)+"."+apimHostNameSuffix && config.Type == apimanagement.HostnameTypeProxy {
continue
}

if config.NegotiateClientCertificate != nil {
output["negotiate_client_certificate"] = *config.NegotiateClientCertificate
}
Expand Down Expand Up @@ -929,6 +948,10 @@ func flattenApiManagementHostnameConfigurations(input *[]apimanagement.HostnameC
}
}

if len(managementResults) == 0 && len(portalResults) == 0 && len(developerPortalResults) == 0 && len(proxyResults) == 0 && len(scmResults) == 0 {
return []interface{}{}
}

return []interface{}{
map[string]interface{}{
"management": managementResults,
Expand Down
Loading