|
1 | 1 | package github |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
| 4 | + "context" |
5 | 5 | "log" |
6 | 6 | "strings" |
7 | 7 |
|
8 | | - "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
9 | 9 | ) |
10 | 10 |
|
11 | | -func resourceGithubWebhookMigrateState(v int, is *terraform.InstanceState, meta any) (*terraform.InstanceState, error) { |
12 | | - switch v { |
13 | | - case 0: |
14 | | - log.Printf("[INFO] Found GitHub Webhook State v0; migrating to v1") |
15 | | - return migrateGithubWebhookStateV0toV1(is) |
16 | | - default: |
17 | | - return is, fmt.Errorf("unexpected schema version: %d", v) |
| 11 | +func resourceGithubRepositoryWebhookResourceV0() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Schema: map[string]*schema.Schema{ |
| 14 | + "name": { |
| 15 | + Type: schema.TypeString, |
| 16 | + Required: true, |
| 17 | + ForceNew: true, |
| 18 | + }, |
| 19 | + "repository": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + ForceNew: true, |
| 23 | + }, |
| 24 | + "events": { |
| 25 | + Type: schema.TypeSet, |
| 26 | + Required: true, |
| 27 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 28 | + Set: schema.HashString, |
| 29 | + }, |
| 30 | + "configuration": { |
| 31 | + Type: schema.TypeMap, |
| 32 | + Optional: true, |
| 33 | + }, |
| 34 | + "url": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Computed: true, |
| 37 | + }, |
| 38 | + "active": { |
| 39 | + Type: schema.TypeBool, |
| 40 | + Optional: true, |
| 41 | + Default: true, |
| 42 | + }, |
| 43 | + }, |
18 | 44 | } |
19 | 45 | } |
20 | 46 |
|
21 | | -func migrateGithubWebhookStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) { |
22 | | - if is.Empty() { |
23 | | - log.Printf("[DEBUG] Empty InstanceState; nothing to migrate.") |
24 | | - return is, nil |
25 | | - } |
26 | | - |
27 | | - log.Printf("[DEBUG] GitHub Webhook Attributes before migration: %#v", is.Attributes) |
| 47 | +func resourceGithubRepositoryWebhookInstanceStateUpgradeV0(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) { |
| 48 | + log.Printf("[DEBUG] GitHub Repository Webhook State before migration: %#v", rawState) |
28 | 49 |
|
29 | 50 | prefix := "configuration." |
30 | | - |
31 | | - delete(is.Attributes, prefix+"%") |
| 51 | + delete(rawState, prefix+"%") |
32 | 52 |
|
33 | 53 | // Read & delete old keys |
34 | | - oldKeys := make(map[string]string) |
35 | | - for k, v := range is.Attributes { |
| 54 | + oldKeys := make(map[string]any) |
| 55 | + for k, v := range rawState { |
36 | 56 | if strings.HasPrefix(k, prefix) { |
37 | 57 | oldKeys[k] = v |
38 | 58 |
|
39 | 59 | // Delete old keys |
40 | | - delete(is.Attributes, k) |
| 60 | + delete(rawState, k) |
41 | 61 | } |
42 | 62 | } |
43 | 63 |
|
44 | 64 | // Write new keys |
45 | 65 | for k, v := range oldKeys { |
46 | 66 | newKey := "configuration.0." + strings.TrimPrefix(k, prefix) |
47 | | - is.Attributes[newKey] = v |
| 67 | + rawState[newKey] = v |
48 | 68 | } |
49 | 69 |
|
50 | | - is.Attributes[prefix+"#"] = "1" |
51 | | - log.Printf("[DEBUG] GitHub Webhook Attributes after State Migration: %#v", is.Attributes) |
| 70 | + rawState[prefix+"#"] = "1" |
| 71 | + |
| 72 | + log.Printf("[DEBUG] GitHub Repository Webhook State after migration: %#v", rawState) |
52 | 73 |
|
53 | | - return is, nil |
| 74 | + return rawState, nil |
54 | 75 | } |
0 commit comments