forked from heroku/terraform-provider-heroku
/
resource_heroku_addon_migrate.go
53 lines (42 loc) · 1.48 KB
/
resource_heroku_addon_migrate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package heroku
import (
"context"
"fmt"
"github.com/hashicorp/terraform/terraform"
"github.com/heroku/heroku-go/v3"
"log"
)
func resourceHerokuAddonMigrate(v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
client := meta.(*Config).Api
log.Printf("[DEBUG] Current version of state file is: v%v", v)
switch v {
case 0:
log.Println("[INFO] Found Heroku Addon state v0; migrating to v1")
return migrateAddonIdsStateV0toV1(is, client)
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
}
}
func migrateAddonIdsStateV0toV1(is *terraform.InstanceState, client *heroku.Service) (*terraform.InstanceState, error) {
if is.Empty() || is.Attributes == nil {
log.Println("[DEBUG] Empty Heroku Addon State; nothing to migrate.")
return is, nil
}
log.Printf("[DEBUG] Addon Id before migration: %#v", is.ID)
log.Printf("[DEBUG] Addon Attributes before migration: %#v", is.Attributes)
currentAddonId := is.ID
addonAppId := is.Attributes["app"]
addon, err := client.AddOnInfoByApp(context.TODO(), addonAppId, currentAddonId)
if err != nil {
return nil, fmt.Errorf("Error retrieving addon: %s", err)
}
newAddonId := addon.ID
if currentAddonId != newAddonId {
log.Printf("[DEBUG] Setting addon id to %s", newAddonId)
is.Attributes["id"] = newAddonId
is.ID = newAddonId
}
log.Printf("[DEBUG] Addon Id after migration: %#v", is.ID)
log.Printf("[DEBUG] Addon Attributes after migration: %#v", is.Attributes)
return is, nil
}