-
Notifications
You must be signed in to change notification settings - Fork 961
Expand file tree
/
Copy pathresource_github_actions_repository_access_level.go
More file actions
89 lines (74 loc) · 2.94 KB
/
resource_github_actions_repository_access_level.go
File metadata and controls
89 lines (74 loc) · 2.94 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
package github
import (
"context"
"github.com/google/go-github/v85/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func resourceGithubActionsRepositoryAccessLevel() *schema.Resource {
return &schema.Resource{
Create: resourceGithubActionsRepositoryAccessLevelCreateOrUpdate,
Read: resourceGithubActionsRepositoryAccessLevelRead,
Update: resourceGithubActionsRepositoryAccessLevelCreateOrUpdate,
Delete: resourceGithubActionsRepositoryAccessLevelDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"access_level": {
Type: schema.TypeString,
Required: true,
Description: "Where the actions or reusable workflows of the repository may be used. Possible values are 'none', 'user', 'organization', or 'enterprise'.",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"none", "user", "organization", "enterprise"}, false)),
},
"repository": {
Type: schema.TypeString,
Required: true,
Description: "The GitHub repository.",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringLenBetween(1, 100)),
},
},
}
}
func resourceGithubActionsRepositoryAccessLevelCreateOrUpdate(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repoName := d.Get("repository").(string)
ctx := context.Background()
if !d.IsNewResource() {
ctx = context.WithValue(ctx, ctxId, d.Id())
}
accessLevel := d.Get("access_level").(string)
actionAccessLevel := github.RepositoryActionsAccessLevel{
AccessLevel: new(accessLevel),
}
_, err := client.Repositories.EditActionsAccessLevel(ctx, owner, repoName, actionAccessLevel)
if err != nil {
return err
}
d.SetId(repoName)
return resourceGithubActionsRepositoryAccessLevelRead(d, meta)
}
func resourceGithubActionsRepositoryAccessLevelRead(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repoName := d.Id()
ctx := context.WithValue(context.Background(), ctxId, repoName)
actionAccessLevel, _, err := client.Repositories.GetActionsAccessLevel(ctx, owner, repoName)
if err != nil {
return deleteResourceOn404AndSwallow304OtherwiseReturnError(err, d, "repository (%s/%s) actions access level", owner, repoName)
}
_ = d.Set("access_level", actionAccessLevel.GetAccessLevel())
return nil
}
func resourceGithubActionsRepositoryAccessLevelDelete(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repoName := d.Id()
ctx := context.WithValue(context.Background(), ctxId, repoName)
actionAccessLevel := github.RepositoryActionsAccessLevel{
AccessLevel: new("none"),
}
_, err := client.Repositories.EditActionsAccessLevel(ctx, owner, repoName, actionAccessLevel)
return err
}