-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For resources with the "Name" identifier
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |