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_api - change id format so specific revisions can be managed by Terraform #23031

Merged
merged 14 commits into from
Jan 19, 2024
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
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,
},
}
wsdlSelectorVs := importV["wsdl_selector"].([]interface{})
Expand All @@ -433,6 +438,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 @@ -474,7 +483,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 @@ -490,6 +498,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 @@ -516,21 +529,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 @@ -595,12 +606,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
Loading