|
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 resourceGithubRepositoryMigrateState(v int, is *terraform.InstanceState, meta any) (*terraform.InstanceState, error) { |
12 | | - switch v { |
13 | | - case 0: |
14 | | - log.Printf("[INFO] Found GitHub Repository State v0; migrating to v1") |
15 | | - return migrateGithubRepositoryStateV0toV1(is) |
16 | | - default: |
17 | | - return is, fmt.Errorf("unexpected schema version: %d", v) |
18 | | - } |
19 | | -} |
| 11 | +func resourceGithubRepositoryResourceV0() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Schema: map[string]*schema.Schema{ |
| 14 | + "full_name": { |
| 15 | + Type: schema.TypeString, |
| 16 | + Optional: true, |
| 17 | + Computed: true, |
| 18 | + ConflictsWith: []string{"name"}, |
| 19 | + }, |
| 20 | + "name": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Optional: true, |
| 23 | + Computed: true, |
| 24 | + ConflictsWith: []string{"full_name"}, |
| 25 | + }, |
| 26 | + "only_protected_branches": { |
| 27 | + Type: schema.TypeBool, |
| 28 | + Optional: true, |
| 29 | + Default: false, |
| 30 | + }, |
20 | 31 |
|
21 | | -func migrateGithubRepositoryStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) { |
22 | | - if is.Empty() { |
23 | | - log.Printf("[DEBUG] Empty InstanceState; nothing to migrate.") |
24 | | - return is, nil |
| 32 | + "description": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Default: nil, |
| 35 | + Optional: true, |
| 36 | + }, |
| 37 | + "homepage_url": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Default: "", |
| 40 | + Optional: true, |
| 41 | + }, |
| 42 | + "private": { |
| 43 | + Type: schema.TypeBool, |
| 44 | + Computed: true, |
| 45 | + }, |
| 46 | + "visibility": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + }, |
| 50 | + "has_issues": { |
| 51 | + Type: schema.TypeBool, |
| 52 | + Computed: true, |
| 53 | + }, |
| 54 | + "has_projects": { |
| 55 | + Type: schema.TypeBool, |
| 56 | + Computed: true, |
| 57 | + }, |
| 58 | + "has_downloads": { |
| 59 | + Type: schema.TypeBool, |
| 60 | + Computed: true, |
| 61 | + }, |
| 62 | + "has_wiki": { |
| 63 | + Type: schema.TypeBool, |
| 64 | + Computed: true, |
| 65 | + }, |
| 66 | + "allow_merge_commit": { |
| 67 | + Type: schema.TypeBool, |
| 68 | + Computed: true, |
| 69 | + }, |
| 70 | + "allow_squash_merge": { |
| 71 | + Type: schema.TypeBool, |
| 72 | + Computed: true, |
| 73 | + }, |
| 74 | + "allow_rebase_merge": { |
| 75 | + Type: schema.TypeBool, |
| 76 | + Computed: true, |
| 77 | + }, |
| 78 | + "allow_auto_merge": { |
| 79 | + Type: schema.TypeBool, |
| 80 | + Computed: true, |
| 81 | + }, |
| 82 | + "squash_merge_commit_title": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + }, |
| 86 | + "squash_merge_commit_message": { |
| 87 | + Type: schema.TypeString, |
| 88 | + Computed: true, |
| 89 | + }, |
| 90 | + "merge_commit_title": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Computed: true, |
| 93 | + }, |
| 94 | + "merge_commit_message": { |
| 95 | + Type: schema.TypeString, |
| 96 | + Computed: true, |
| 97 | + }, |
| 98 | + "default_branch": { |
| 99 | + Type: schema.TypeString, |
| 100 | + Computed: true, |
| 101 | + }, |
| 102 | + "archived": { |
| 103 | + Type: schema.TypeBool, |
| 104 | + Computed: true, |
| 105 | + }, |
| 106 | + "branches": { |
| 107 | + Type: schema.TypeList, |
| 108 | + Computed: true, |
| 109 | + Elem: &schema.Resource{ |
| 110 | + Schema: map[string]*schema.Schema{ |
| 111 | + "name": { |
| 112 | + Type: schema.TypeString, |
| 113 | + Computed: true, |
| 114 | + }, |
| 115 | + "protected": { |
| 116 | + Type: schema.TypeBool, |
| 117 | + Computed: true, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + "pages": { |
| 123 | + Type: schema.TypeList, |
| 124 | + Computed: true, |
| 125 | + Elem: &schema.Resource{ |
| 126 | + Schema: map[string]*schema.Schema{ |
| 127 | + "source": { |
| 128 | + Type: schema.TypeList, |
| 129 | + Computed: true, |
| 130 | + Elem: &schema.Resource{ |
| 131 | + Schema: map[string]*schema.Schema{ |
| 132 | + "branch": { |
| 133 | + Type: schema.TypeString, |
| 134 | + Computed: true, |
| 135 | + }, |
| 136 | + "path": { |
| 137 | + Type: schema.TypeString, |
| 138 | + Computed: true, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + "cname": { |
| 144 | + Type: schema.TypeString, |
| 145 | + Computed: true, |
| 146 | + }, |
| 147 | + "custom_404": { |
| 148 | + Type: schema.TypeBool, |
| 149 | + Computed: true, |
| 150 | + }, |
| 151 | + "html_url": { |
| 152 | + Type: schema.TypeString, |
| 153 | + Computed: true, |
| 154 | + }, |
| 155 | + "status": { |
| 156 | + Type: schema.TypeString, |
| 157 | + Computed: true, |
| 158 | + }, |
| 159 | + "url": { |
| 160 | + Type: schema.TypeString, |
| 161 | + Computed: true, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + "topics": { |
| 167 | + Type: schema.TypeList, |
| 168 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 169 | + Computed: true, |
| 170 | + }, |
| 171 | + "html_url": { |
| 172 | + Type: schema.TypeString, |
| 173 | + Computed: true, |
| 174 | + }, |
| 175 | + "ssh_clone_url": { |
| 176 | + Type: schema.TypeString, |
| 177 | + Computed: true, |
| 178 | + }, |
| 179 | + "svn_url": { |
| 180 | + Type: schema.TypeString, |
| 181 | + Computed: true, |
| 182 | + }, |
| 183 | + "git_clone_url": { |
| 184 | + Type: schema.TypeString, |
| 185 | + Computed: true, |
| 186 | + }, |
| 187 | + "http_clone_url": { |
| 188 | + Type: schema.TypeString, |
| 189 | + Computed: true, |
| 190 | + }, |
| 191 | + "node_id": { |
| 192 | + Type: schema.TypeString, |
| 193 | + Computed: true, |
| 194 | + }, |
| 195 | + "repo_id": { |
| 196 | + Type: schema.TypeInt, |
| 197 | + Computed: true, |
| 198 | + }, |
| 199 | + }, |
25 | 200 | } |
| 201 | +} |
26 | 202 |
|
27 | | - log.Printf("[DEBUG] GitHub Repository Attributes before migration: %#v", is.Attributes) |
| 203 | +func resourceGithubRepositoryInstanceStateUpgradeV0(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) { |
| 204 | + log.Printf("[DEBUG] GitHub Repository State before migration: %#v", rawState) |
28 | 205 |
|
29 | 206 | prefix := "branches." |
30 | 207 |
|
31 | | - for k := range is.Attributes { |
| 208 | + for k := range rawState { |
32 | 209 | if strings.HasPrefix(k, prefix) { |
33 | | - delete(is.Attributes, k) |
| 210 | + delete(rawState, k) |
34 | 211 | } |
35 | 212 | } |
36 | 213 |
|
37 | | - log.Printf("[DEBUG] GitHub Repository Attributes after State Migration: %#v", is.Attributes) |
38 | | - |
39 | | - return is, nil |
| 214 | + log.Printf("[DEBUG] GitHub Repository State after migration: %#v", rawState) |
| 215 | + return rawState, nil |
40 | 216 | } |
0 commit comments