Skip to content

Commit

Permalink
Add state migration function
Browse files Browse the repository at this point in the history
For resources with the "Name" identifier
  • Loading branch information
vaerh committed Nov 6, 2023
1 parent d5e8f85 commit 9d7dd8d
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions routeros/provider_resource_state_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package routeros

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func stateMigrationNameToId(resourcePath string) schema.StateUpgradeFunc {
return func(ctx context.Context, rawState map[string]interface{}, m interface{}) (map[string]interface{}, error) {
ColorizedMessage(ctx, INFO, fmt.Sprintf("ID attribute before migration: %#v", rawState["id"]))

if rawState["id"] != nil {
res, err := ReadItems(&ItemId{Name, rawState["id"].(string)}, resourcePath, m.(Client))
if err != nil {
return nil, err
}

// Resource not found.
if len(*res) == 0 {
rawState["id"] = ""
ColorizedMessage(ctx, WARN, "No resource found, but the scheme has been updated.",
map[string]interface{}{"path": resourcePath, "id": rawState["id"]})
return rawState, nil
}

rawState["id"] = (*res)[0].GetID(Id)
}

ColorizedMessage(ctx, INFO, fmt.Sprintf("ID attribute after migration: %#v", rawState["id"]))

return rawState, nil
}
}

0 comments on commit 9d7dd8d

Please sign in to comment.