forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_github_emu_group_mapping_migration.go
More file actions
121 lines (110 loc) · 3.5 KB
/
resource_github_emu_group_mapping_migration.go
File metadata and controls
121 lines (110 loc) · 3.5 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package github
import (
"context"
"fmt"
"net/http"
"strconv"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGithubEMUGroupMappingV0() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"team_slug": {
Type: schema.TypeString,
Required: true,
Description: "Slug of the GitHub team.",
},
"group_id": {
Type: schema.TypeInt,
Required: true,
Description: "Integer corresponding to the external group ID to be linked.",
},
"etag": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceGithubEMUGroupMappingStateUpgradeV0(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) {
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
tflog.Trace(ctx, "GitHub EMU Group Mapping State before migration", map[string]any{"state": rawState, "owner": orgName})
teamSlug := rawState["team_slug"].(string)
// We need to bypass the etag because we need to get the latest group
ctx = context.WithValue(ctx, ctxEtag, nil)
groupsList, resp, err := client.Teams.ListExternalGroupsForTeamBySlug(ctx, orgName, teamSlug)
if err != nil {
if resp != nil && (resp.StatusCode == http.StatusNotFound) {
// If the Group is not found, remove it from state
tflog.Info(ctx, "Removing EMU group mapping from state because team no longer exists in GitHub", map[string]any{
"resource_id": rawState["id"],
})
return nil, err
}
return nil, err
}
group := groupsList.Groups[0]
teamID, err := lookupTeamID(ctx, client, orgName, teamSlug)
if err != nil {
return nil, err
}
rawState["team_id"] = int(teamID)
resourceID, err := buildID(strconv.FormatInt(teamID, 10), teamSlug, strconv.FormatInt(group.GetGroupID(), 10))
if err != nil {
return nil, err
}
rawState["id"] = resourceID
tflog.Trace(ctx, "GitHub EMU Group Mapping State after migration", map[string]any{"state": rawState})
return rawState, nil
}
func resourceGithubEMUGroupMappingV1() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"team_id": {
Type: schema.TypeInt,
Computed: true,
Description: "ID of the GitHub team.",
},
"team_slug": {
Type: schema.TypeString,
Required: true,
Description: "Slug of the GitHub team.",
},
"group_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "Integer corresponding to the external group ID to be linked.",
},
"group_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the external group.",
},
"etag": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceGithubEMUGroupMappingStateUpgradeV1(ctx context.Context, rawState map[string]any, meta any) (map[string]any, error) {
tflog.Trace(ctx, "GitHub EMU Group Mapping State before migration v1 => v2", map[string]any{"state": rawState})
oldResourceID, ok := rawState["id"].(string)
if !ok {
return nil, fmt.Errorf("id is not a string")
}
teamID, _, groupID, err := parseID3(oldResourceID)
if err != nil {
return nil, fmt.Errorf("could not parse ID: %w", err)
}
resourceID, err := buildID(groupID, teamID)
if err != nil {
return nil, fmt.Errorf("could not build ID: %w", err)
}
rawState["id"] = resourceID
tflog.Trace(ctx, "GitHub EMU Group Mapping State after migration", map[string]any{"state": rawState})
return rawState, nil
}