-
Notifications
You must be signed in to change notification settings - Fork 965
Expand file tree
/
Copy pathresource_github_actions_runner_group_repository_access.go
More file actions
127 lines (100 loc) · 4.11 KB
/
resource_github_actions_runner_group_repository_access.go
File metadata and controls
127 lines (100 loc) · 4.11 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
122
123
124
125
126
127
package github
import (
"context"
"fmt"
"log"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGithubActionsRunnerGroupRepositoryAccess() *schema.Resource {
return &schema.Resource{
Description: "Manages the access of a repository to an organization runner group.",
CreateContext: resourceGithubActionsRunnerGroupRepositoryAccessCreate,
ReadContext: resourceGithubActionsRunnerGroupRepositoryAccessRead,
// Omitting update function since this resource expresses a simple membership in the set of repositories with access to the runner group, it either exists or doesn't
// UpdateContext: resourceGithubActionsRunnerGroupRepositoryAccessUpdate,
DeleteContext: resourceGithubActionsRunnerGroupRepositoryAccessDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceGithubActionsRunnerGroupRepositoryAccessImport,
},
Schema: map[string]*schema.Schema{
"runner_group_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "The ID of the runner group to grant the repository access on",
},
"repository_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "The ID of the repository to grant access to the runner group",
},
},
}
}
func resourceGithubActionsRunnerGroupRepositoryAccessCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
runnerGroupId := d.Get("runner_group_id").(int)
repositoryId := d.Get("repository_id").(int)
_, err := client.Actions.AddRepositoryAccessRunnerGroup(ctx, orgName, int64(runnerGroupId), int64(repositoryId))
if err != nil {
return diag.FromErr(err)
}
d.SetId(fmt.Sprintf("%d/%d", runnerGroupId, repositoryId))
return resourceGithubActionsRunnerGroupRepositoryAccessRead(ctx, d, meta)
}
func resourceGithubActionsRunnerGroupRepositoryAccessRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
runnerGroupId := d.Get("runner_group_id").(int)
repositoryId := d.Get("repository_id").(int)
for repo, err := range client.Actions.ListRepositoryAccessRunnerGroupIter(ctx, orgName, int64(runnerGroupId), nil) {
if err != nil {
return diag.FromErr(err)
}
if *repo.ID == int64(repositoryId) {
// Resource matches the state in github exactly (repo has access), no need for further modifications
return nil
}
}
// We reached the end of the list without a match for our desired repository access
log.Printf("[INFO] Removing access of repository with id %d to runner group %s/%d from state because access no longer exists in GitHub", repositoryId, orgName, runnerGroupId)
d.SetId("")
return nil
}
func resourceGithubActionsRunnerGroupRepositoryAccessDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
runnerGroupId := d.Get("runner_group_id").(int)
repositoryId := d.Get("repository_id").(int)
_, err := client.Actions.RemoveRepositoryAccessRunnerGroup(ctx, orgName, int64(runnerGroupId), int64(repositoryId))
return diag.FromErr(err)
}
func resourceGithubActionsRunnerGroupRepositoryAccessImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
id := d.Id()
parts := strings.Split(id, "/")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid import specified: supplied import must be written as <runner_group_id>/<repository_id>")
}
runnerGroupID, err := strconv.Atoi(parts[0])
if err != nil {
return nil, fmt.Errorf("runner_group_id in id must be convertible to an int: %w", err)
}
err = d.Set("runner_group_id", runnerGroupID)
if err != nil {
return nil, err
}
repositoryId, err := strconv.Atoi(parts[1])
if err != nil {
return nil, fmt.Errorf("runner_id in id must be convertible to an int: %w", err)
}
err = d.Set("repository_id", repositoryId)
if err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}