Skip to content

Commit

Permalink
azurerm_api_management_api - change id format so specific `revisi…
Browse files Browse the repository at this point in the history
…on`s can be managed by Terraform (#23031)
  • Loading branch information
mbfrahry authored Jan 19, 2024
1 parent 6ea3e01 commit d16fbb1
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 26 deletions.
7 changes: 2 additions & 5 deletions internal/services/apimanagement/api_management_api_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ func resourceApiManagementApiReleaseCreateUpdate(d *pluginsdk.ResourceData, meta
return err
}

apiName := getApiName(apiId.ApiId)

id := apirelease.NewReleaseID(subscriptionId, apiId.ResourceGroupName, apiId.ServiceName, apiName, name)
id := apirelease.NewReleaseID(subscriptionId, apiId.ResourceGroupName, apiId.ServiceName, apiId.ApiId, name)
ifMatch := "*"

if d.IsNewResource() {
Expand Down Expand Up @@ -131,8 +129,7 @@ func resourceApiManagementApiReleaseRead(d *pluginsdk.ResourceData, meta interfa
d.Set("name", id.ReleaseId)
if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
apiName := getApiName(id.ApiId)
d.Set("api_id", api.NewApiID(subscriptionId, id.ResourceGroupName, id.ServiceName, apiName).ID())
d.Set("api_id", api.NewApiID(subscriptionId, id.ResourceGroupName, id.ServiceName, id.ApiId).ID())
d.Set("notes", pointer.From(props.Notes))
}
}
Expand Down
48 changes: 28 additions & 20 deletions internal/services/apimanagement/api_management_api_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/schemaz"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand All @@ -41,6 +42,11 @@ func resourceApiManagementApi() *pluginsdk.Resource {
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},

SchemaVersion: 1,
StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{
0: migration.ApiV0ToV1{},
}),

Schema: map[string]*pluginsdk.Schema{
"name": schemaz.SchemaApiManagementApiName(),

Expand Down Expand Up @@ -351,8 +357,6 @@ func resourceApiManagementApiCreateUpdate(d *pluginsdk.ResourceData, meta interf
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id := api.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", d.Get("name").(string), revision)
Expand All @@ -363,6 +367,8 @@ func resourceApiManagementApiCreateUpdate(d *pluginsdk.ResourceData, meta interf
protocols := expandApiManagementApiProtocols(protocolsRaw)
sourceApiId := d.Get("source_api_id").(string)

id := api.NewApiID(subscriptionId, d.Get("resource_group_name").(string), d.Get("api_management_name").(string), apiId)

if version != "" && versionSetId == "" {
return fmt.Errorf("setting `version` without the required `version_set_id`")
}
Expand Down Expand Up @@ -412,12 +418,11 @@ func resourceApiManagementApiCreateUpdate(d *pluginsdk.ResourceData, meta interf
log.Printf("[DEBUG] Importing API Management API %q of type %q", id.ApiId, contentFormat)
apiParams := api.ApiCreateOrUpdateParameter{
Properties: &api.ApiCreateOrUpdateProperties{
Type: pointer.To(apiType),
ApiType: pointer.To(soapApiType),
Format: pointer.To(api.ContentFormat(contentFormat)),
Value: pointer.To(contentValue),
Path: path,
ApiVersion: pointer.To(version),
Type: pointer.To(apiType),
ApiType: pointer.To(soapApiType),
Format: pointer.To(api.ContentFormat(contentFormat)),
Value: pointer.To(contentValue),
Path: path,
},
}

Expand All @@ -438,6 +443,10 @@ func resourceApiManagementApiCreateUpdate(d *pluginsdk.ResourceData, meta interf
}
}

if version != "" {
apiParams.Properties.ApiVersion = pointer.To(version)
}

if versionSetId != "" {
apiParams.Properties.ApiVersionSetId = pointer.To(versionSetId)
}
Expand Down Expand Up @@ -478,7 +487,6 @@ func resourceApiManagementApiCreateUpdate(d *pluginsdk.ResourceData, meta interf
Protocols: protocols,
ServiceUrl: pointer.To(serviceUrl),
SubscriptionKeyParameterNames: subscriptionKeyParameterNames,
ApiVersion: pointer.To(version),
SubscriptionRequired: &subscriptionRequired,
AuthenticationSettings: authenticationSettings,
ApiRevisionDescription: pointer.To(d.Get("revision_description").(string)),
Expand All @@ -494,6 +502,11 @@ func resourceApiManagementApiCreateUpdate(d *pluginsdk.ResourceData, meta interf
if displayName != "" {
params.Properties.DisplayName = pointer.To(displayName)
}

if version != "" {
params.Properties.ApiVersion = pointer.To(version)
}

if versionSetId != "" {
params.Properties.ApiVersionSetId = pointer.To(versionSetId)
}
Expand All @@ -520,21 +533,19 @@ func resourceApiManagementApiRead(d *pluginsdk.ResourceData, meta interface{}) e
return err
}

name := getApiName(id.ApiId)
newId := api.NewApiID(id.SubscriptionId, id.ResourceGroupName, id.ServiceName, name)
resp, err := client.Get(ctx, newId)
resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[INFO] %s does not exist - removing from state", newId)
log.Printf("[INFO] %s does not exist - removing from state", id)
d.SetId("")
return nil
}

return fmt.Errorf("retrieving %s: %+v", newId, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}

d.Set("api_management_name", id.ServiceName)
d.Set("name", name)
d.Set("name", getApiName(id.ApiId))
d.Set("resource_group_name", id.ResourceGroupName)

if model := resp.Model; model != nil {
Expand Down Expand Up @@ -599,12 +610,9 @@ func resourceApiManagementApiDelete(d *pluginsdk.ResourceData, meta interface{})
return err
}

name := getApiName(id.ApiId)

newId := api.NewApiID(id.SubscriptionId, id.ResourceGroupName, id.ServiceName, name)
if resp, err := client.Delete(ctx, newId, api.DeleteOperationOptions{DeleteRevisions: pointer.To(true)}); err != nil {
if resp, err := client.Delete(ctx, *id, api.DefaultDeleteOperationOptions()); err != nil {
if !response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("deleting %s: %+v", newId, err)
return fmt.Errorf("deleting %s: %+v", id, err)
}
}

Expand Down
Loading

0 comments on commit d16fbb1

Please sign in to comment.