Skip to content

Commit

Permalink
Merge pull request #487 from hashicorp/bugfix/application-state-migra…
Browse files Browse the repository at this point in the history
…tion

Add state migration for azuread_application to handle group_membership_claims type change
  • Loading branch information
manicminer authored Jul 21, 2021
2 parents d3565cd + ee489c0 commit 99b15ba
Show file tree
Hide file tree
Showing 6 changed files with 754 additions and 164 deletions.
91 changes: 10 additions & 81 deletions internal/services/applications/application_password_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package applications
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"time"
Expand All @@ -15,6 +14,7 @@ import (

"github.com/hashicorp/terraform-provider-azuread/internal/clients"
"github.com/hashicorp/terraform-provider-azuread/internal/helpers"
"github.com/hashicorp/terraform-provider-azuread/internal/services/applications/migrations"
"github.com/hashicorp/terraform-provider-azuread/internal/services/applications/parse"
"github.com/hashicorp/terraform-provider-azuread/internal/tf"
"github.com/hashicorp/terraform-provider-azuread/internal/validate"
Expand All @@ -33,6 +33,15 @@ func applicationPasswordResource() *schema.Resource {
Delete: schema.DefaultTimeout(5 * time.Minute),
},

SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: migrations.ResourceApplicationPasswordInstanceResourceV0().CoreConfigSchema().ImpliedType(),
Upgrade: migrations.ResourceApplicationPasswordInstanceStateUpgradeV0,
Version: 0,
},
},

Schema: map[string]*schema.Schema{
"application_object_id": {
Description: "The object ID of the application for which this password should be created",
Expand Down Expand Up @@ -91,14 +100,6 @@ func applicationPasswordResource() *schema.Resource {
Sensitive: true,
},
},
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: resourceApplicationPasswordInstanceResourceV0().CoreConfigSchema().ImpliedType(),
Upgrade: resourceApplicationPasswordInstanceStateUpgradeV0,
Version: 0,
},
},
}
}

Expand Down Expand Up @@ -223,75 +224,3 @@ func applicationPasswordResourceDelete(ctx context.Context, d *schema.ResourceDa

return nil
}

func resourceApplicationPasswordInstanceResourceV0() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"application_object_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: validate.UUID,
},

"key_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateDiagFunc: validate.UUID,
},

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

"value": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Sensitive: true,
ValidateFunc: validation.StringLenBetween(1, 863),
},

"start_date": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.IsRFC3339Time,
},

"end_date": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ExactlyOneOf: []string{"end_date_relative"},
ValidateFunc: validation.IsRFC3339Time,
},

"end_date_relative": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ExactlyOneOf: []string{"end_date"},
ValidateDiagFunc: validate.NoEmptyStrings,
},
},
}
}

func resourceApplicationPasswordInstanceStateUpgradeV0(_ context.Context, rawState map[string]interface{}, _ interface{}) (map[string]interface{}, error) {
log.Println("[DEBUG] Migrating ID from v0 to v1 format")
newId, err := parse.OldPasswordID(rawState["id"].(string))
if err != nil {
return rawState, fmt.Errorf("generating new ID: %s", err)
}

rawState["id"] = newId.String()
return rawState, nil
}
11 changes: 11 additions & 0 deletions internal/services/applications/application_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/http"
"time"

"github.com/hashicorp/terraform-provider-azuread/internal/services/applications/migrations"

"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -46,6 +48,15 @@ func applicationResource() *schema.Resource {
return nil
}),

SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: migrations.ResourceApplicationInstanceResourceV0().CoreConfigSchema().ImpliedType(),
Upgrade: migrations.ResourceApplicationInstanceStateUpgradeV0,
Version: 0,
},
},

Schema: map[string]*schema.Schema{
"display_name": {
Description: "The display name for the application",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package migrations

import (
"context"
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-azuread/internal/services/applications/parse"
"github.com/hashicorp/terraform-provider-azuread/internal/validate"
)

func ResourceApplicationPasswordInstanceResourceV0() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"application_object_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: validate.UUID,
},

"key_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateDiagFunc: validate.UUID,
},

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

"value": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Sensitive: true,
ValidateFunc: validation.StringLenBetween(1, 863),
},

"start_date": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.IsRFC3339Time,
},

"end_date": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ExactlyOneOf: []string{"end_date_relative"},
ValidateFunc: validation.IsRFC3339Time,
},

"end_date_relative": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ExactlyOneOf: []string{"end_date"},
ValidateDiagFunc: validate.NoEmptyStrings,
},
},
}
}

func ResourceApplicationPasswordInstanceStateUpgradeV0(_ context.Context, rawState map[string]interface{}, _ interface{}) (map[string]interface{}, error) {
log.Println("[DEBUG] Migrating ID from v0 to v1 format")
newId, err := parse.OldPasswordID(rawState["id"].(string))
if err != nil {
return rawState, fmt.Errorf("generating new ID: %s", err)
}

rawState["id"] = newId.String()
return rawState, nil
}
Loading

0 comments on commit 99b15ba

Please sign in to comment.