Skip to content

Commit

Permalink
azurerm_api_management - refactoring remaining api_management resourc…
Browse files Browse the repository at this point in the history
…es to use parsers (hashicorp#14148)

* refactoring api_management to use parsers

* refactoring api_management to use parsers
  • Loading branch information
catriona-m authored Nov 12, 2021
1 parent 7833565 commit e43775d
Show file tree
Hide file tree
Showing 20 changed files with 214 additions and 346 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ func resourceApiManagementApiOperationPolicy() *pluginsdk.Resource {
Read: resourceApiManagementAPIOperationPolicyRead,
Update: resourceApiManagementAPIOperationPolicyCreateUpdate,
Delete: resourceApiManagementAPIOperationPolicyDelete,
// TODO: replace this with an importer which validates the ID during import
Importer: pluginsdk.DefaultImporter(),
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ApiOperationPolicyID(id)
return err
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"testing"

"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2020-12-01/apimanagement"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -94,18 +94,14 @@ func TestAccApiManagementAPIOperationPolicy_rawXml(t *testing.T) {
}

func (ApiManagementApiOperationPolicyResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := azure.ParseAzureResourceID(state.ID)
id, err := parse.ApiOperationPolicyID(state.ID)
if err != nil {
return nil, err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
apiName := id.Path["apis"]
operationID := id.Path["operations"]

resp, err := clients.ApiManagement.ApiOperationPoliciesClient.Get(ctx, resourceGroup, serviceName, apiName, operationID, apimanagement.PolicyExportFormatXML)
resp, err := clients.ApiManagement.ApiOperationPoliciesClient.Get(ctx, id.ResourceGroup, id.ServiceName, id.ApiName, id.OperationName, apimanagement.PolicyExportFormatXML)
if err != nil {
return nil, fmt.Errorf("reading ApiManagementApi Operation Policy (%s): %+v", id, err)
return nil, fmt.Errorf("reading %s: %+v", *id, err)
}

return utils.Bool(resp.ID != nil), nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ func resourceApiManagementApiPolicy() *pluginsdk.Resource {
Read: resourceApiManagementAPIPolicyRead,
Update: resourceApiManagementAPIPolicyCreateUpdate,
Delete: resourceApiManagementAPIPolicyDelete,
// TODO: replace this with an importer which validates the ID during import
Importer: pluginsdk.DefaultImporter(),
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ApiPolicyID(id)
return err
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Expand Down Expand Up @@ -132,24 +134,21 @@ func resourceApiManagementAPIPolicyRead(d *pluginsdk.ResourceData, meta interfac
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.ServiceName
apiName := id.ApiName

resp, err := client.Get(ctx, resourceGroup, serviceName, apiName, apimanagement.PolicyExportFormatXML)
resp, err := client.Get(ctx, id.ResourceGroup, id.ServiceName, id.ApiName, apimanagement.PolicyExportFormatXML)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] API Policy (Resource Group %q / API Management Service %q / API %q) was not found - removing from state!", resourceGroup, serviceName, apiName)
log.Printf("[DEBUG] %s was not found - removing from state!", *id)
d.SetId("")
return nil
}

return fmt.Errorf("making Read request for API Policy (Resource Group %q / API Management Service %q / API %q): %+v", resourceGroup, serviceName, apiName, err)
return fmt.Errorf("making Read request for %s: %+v", *id, err)
}

d.Set("resource_group_name", resourceGroup)
d.Set("api_management_name", serviceName)
d.Set("api_name", apiName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("api_management_name", id.ServiceName)
d.Set("api_name", id.ApiName)

if properties := resp.PolicyContractProperties; properties != nil {
policyContent := ""
Expand All @@ -174,13 +173,10 @@ func resourceApiManagementAPIPolicyDelete(d *pluginsdk.ResourceData, meta interf
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.ServiceName
apiName := id.ApiName

if resp, err := client.Delete(ctx, resourceGroup, serviceName, apiName, ""); err != nil {
if resp, err := client.Delete(ctx, id.ResourceGroup, id.ServiceName, id.ApiName, ""); err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("deleting API Policy (Resource Group %q / API Management Service %q / API %q): %+v", resourceGroup, serviceName, apiName, err)
return fmt.Errorf("deleting %s: %+v", *id, err)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"testing"

"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2020-12-01/apimanagement"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -99,17 +99,14 @@ func TestAccApiManagementAPIPolicy_customPolicy(t *testing.T) {
}

func (ApiManagementApiPolicyResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := azure.ParseAzureResourceID(state.ID)
id, err := parse.ApiPolicyID(state.ID)
if err != nil {
return nil, err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
apiName := id.Path["apis"]

resp, err := clients.ApiManagement.ApiPoliciesClient.Get(ctx, resourceGroup, serviceName, apiName, apimanagement.PolicyExportFormatXML)
resp, err := clients.ApiManagement.ApiPoliciesClient.Get(ctx, id.ResourceGroup, id.ServiceName, id.ApiName, apimanagement.PolicyExportFormatXML)
if err != nil {
return nil, fmt.Errorf("reading ApiManagementApi Policy (%s): %+v", id, err)
return nil, fmt.Errorf("reading %s: %+v", *id, err)
}

return utils.Bool(resp.ID != nil), nil
Expand Down
83 changes: 34 additions & 49 deletions internal/services/apimanagement/api_management_api_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ func resourceApiManagementApi() *pluginsdk.Resource {
Read: resourceApiManagementApiRead,
Update: resourceApiManagementApiCreateUpdate,
Delete: resourceApiManagementApiDelete,
// TODO: replace this with an importer which validates the ID during import
Importer: pluginsdk.DefaultImporter(),
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ApiID(id)
return err
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Expand Down Expand Up @@ -266,15 +268,15 @@ func resourceApiManagementApi() *pluginsdk.Resource {

func resourceApiManagementApiCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ApiManagement.ApiClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

resourceGroup := d.Get("resource_group_name").(string)
serviceName := d.Get("api_management_name").(string)
name := d.Get("name").(string)
id := parse.NewApiID(subscriptionId, d.Get("resource_group_name").(string), d.Get("api_management_name").(string), d.Get("name").(string))

revision := d.Get("revision").(string)
path := d.Get("path").(string)
apiId := fmt.Sprintf("%s;rev=%s", name, revision)
apiId := fmt.Sprintf("%s;rev=%s", id.Name, revision)
version := d.Get("version").(string)
versionSetId := d.Get("version_set_id").(string)
displayName := d.Get("display_name").(string)
Expand All @@ -291,10 +293,10 @@ func resourceApiManagementApiCreateUpdate(d *pluginsdk.ResourceData, meta interf
}

if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, serviceName, apiId)
existing, err := client.Get(ctx, id.ResourceGroup, id.ServiceName, apiId)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing API %q (API Management Service %q / Resource Group %q): %s", name, serviceName, resourceGroup, err)
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
}
}

Expand Down Expand Up @@ -323,7 +325,7 @@ func resourceApiManagementApiCreateUpdate(d *pluginsdk.ResourceData, meta interf
contentFormat := importV["content_format"].(string)
contentValue := importV["content_value"].(string)

log.Printf("[DEBUG] Importing API Management API %q of type %q", name, contentFormat)
log.Printf("[DEBUG] Importing API Management API %q of type %q", id.Name, contentFormat)
apiParams := apimanagement.APICreateOrUpdateParameter{
APICreateOrUpdateProperties: &apimanagement.APICreateOrUpdateProperties{
APIType: apiType,
Expand All @@ -338,7 +340,7 @@ func resourceApiManagementApiCreateUpdate(d *pluginsdk.ResourceData, meta interf

// `wsdl_selector` is necessary under format `wsdl`
if len(wsdlSelectorVs) == 0 && contentFormat == string(apimanagement.Wsdl) {
return fmt.Errorf("`wsdl_selector` is required when content format is `wsdl` in API Management API %q", name)
return fmt.Errorf("`wsdl_selector` is required when content format is `wsdl` in API Management API %q", id.Name)
}

if len(wsdlSelectorVs) > 0 {
Expand All @@ -356,13 +358,13 @@ func resourceApiManagementApiCreateUpdate(d *pluginsdk.ResourceData, meta interf
apiParams.APICreateOrUpdateProperties.APIVersionSetID = utils.String(versionSetId)
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, apiId, apiParams, "")
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.ServiceName, apiId, apiParams, "")
if err != nil {
return fmt.Errorf("creating/updating API Management API %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting on creating/updating API Management API %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("waiting on creating/updating %s: %+v", id, err)
}
}

Expand Down Expand Up @@ -412,25 +414,16 @@ func resourceApiManagementApiCreateUpdate(d *pluginsdk.ResourceData, meta interf
params.APICreateOrUpdateProperties.APIVersionSetID = utils.String(versionSetId)
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, apiId, params, "")
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.ServiceName, apiId, params, "")
if err != nil {
return fmt.Errorf("creating/updating API Management API %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting on creating/updating API Management API %q (Resource Group %q): %+v", name, resourceGroup, err)
}

read, err := client.Get(ctx, resourceGroup, serviceName, apiId)
if err != nil {
return fmt.Errorf("retrieving API %q / Revision %q (API Management Service %q / Resource Group %q): %+v", name, revision, serviceName, resourceGroup, err)
return fmt.Errorf("waiting on creating/updating %s: %+v", id, err)
}

if read.ID == nil {
return fmt.Errorf("Cannot read ID for API %q / Revision %q (API Management Service %q / Resource Group %q)", name, revision, serviceName, resourceGroup)
}

d.SetId(*read.ID)
d.SetId(id.ID())
return resourceApiManagementApiRead(d, meta)
}

Expand All @@ -444,31 +437,27 @@ func resourceApiManagementApiRead(d *pluginsdk.ResourceData, meta interface{}) e
return err
}

resourceGroup := id.ResourceGroup
serviceName := id.ServiceName
apiid := id.Name

name := apiid
name := id.Name
revision := ""
if strings.Contains(apiid, ";") {
name = strings.Split(apiid, ";")[0]
revision = strings.Split(apiid, "=")[1]
if strings.Contains(id.Name, ";") {
name = strings.Split(id.Name, ";")[0]
revision = strings.Split(id.Name, "=")[1]
}

resp, err := client.Get(ctx, resourceGroup, serviceName, apiid)
resp, err := client.Get(ctx, id.ResourceGroup, id.ServiceName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] API %q Revision %q (API Management Service %q / Resource Group %q) does not exist - removing from state!", name, revision, serviceName, resourceGroup)
log.Printf("[DEBUG] API %q Revision %q (API Management Service %q / Resource Group %q) does not exist - removing from state!", name, revision, id.ServiceName, id.ResourceGroup)
d.SetId("")
return nil
}

return fmt.Errorf("retrieving API %q / Revision %q (API Management Service %q / Resource Group %q): %+v", name, revision, serviceName, resourceGroup, err)
return fmt.Errorf("retrieving API %q / Revision %q (API Management Service %q / Resource Group %q): %+v", name, revision, id.ServiceName, id.ResourceGroup, err)
}

d.Set("api_management_name", serviceName)
d.Set("api_management_name", id.ServiceName)
d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
d.Set("resource_group_name", id.ResourceGroup)

if props := resp.APIContractProperties; props != nil {
d.Set("description", props.Description)
Expand Down Expand Up @@ -515,21 +504,17 @@ func resourceApiManagementApiDelete(d *pluginsdk.ResourceData, meta interface{})
return err
}

resourceGroup := id.ResourceGroup
serviceName := id.ServiceName
apiid := id.Name

name := apiid
name := id.Name
revision := ""
if strings.Contains(apiid, ";") {
name = strings.Split(apiid, ";")[0]
revision = strings.Split(apiid, "=")[1]
if strings.Contains(id.Name, ";") {
name = strings.Split(id.Name, ";")[0]
revision = strings.Split(id.Name, "=")[1]
}

deleteRevisions := utils.Bool(true)
if resp, err := client.Delete(ctx, resourceGroup, serviceName, name, "", deleteRevisions); err != nil {
if resp, err := client.Delete(ctx, id.ResourceGroup, id.ServiceName, name, "", deleteRevisions); err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("deleting API %q / Revision %q (API Management Service %q / Resource Group %q): %s", name, revision, serviceName, resourceGroup, err)
return fmt.Errorf("deleting API %q / Revision %q (API Management Service %q / Resource Group %q): %s", name, revision, id.ServiceName, id.ResourceGroup, err)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -322,18 +322,14 @@ func TestAccApiManagementApi_createRevisionFromExistingRevision(t *testing.T) {
}

func (ApiManagementApiResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := azure.ParseAzureResourceID(state.ID)
id, err := parse.ApiID(state.ID)
if err != nil {
return nil, err
}

resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
apiid := id.Path["apis"]

resp, err := clients.ApiManagement.ApiClient.Get(ctx, resourceGroup, serviceName, apiid)
resp, err := clients.ApiManagement.ApiClient.Get(ctx, id.ResourceGroup, id.ServiceName, id.Name)
if err != nil {
return nil, fmt.Errorf("reading ApiManagementApi (%s): %+v", id, err)
return nil, fmt.Errorf("reading %s: %+v", *id, err)
}

return utils.Bool(resp.ID != nil), nil
Expand Down
Loading

0 comments on commit e43775d

Please sign in to comment.