/
resource_github_repository_tag_protection.go
126 lines (111 loc) · 3.52 KB
/
resource_github_repository_tag_protection.go
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
package github
import (
"context"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"github.com/google/go-github/v57/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGithubRepositoryTagProtection() *schema.Resource {
return &schema.Resource{
Create: resourceGithubRepositoryTagProtectionCreateOrUpdate,
Read: resourceGithubRepositoryTagProtectionRead,
Delete: resourceGithubRepositoryTagProtectionDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid ID specified: supplied ID must be written as <repository>/<tag_protection_id>")
}
if err := d.Set("repository", parts[0]); err != nil {
return nil, err
}
if err := d.Set("tag_protection_id", parts[1]); err != nil {
return nil, err
}
d.SetId(parts[1])
return []*schema.ResourceData{d}, nil
},
},
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the repository to add the tag protection to.",
},
"pattern": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The pattern of the tag to protect.",
},
"tag_protection_id": {
Type: schema.TypeInt,
Computed: true,
Description: "The ID of the tag protection.",
},
},
}
}
func resourceGithubRepositoryTagProtectionCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
ctx := context.Background()
owner := meta.(*Owner).name
repo := d.Get("repository").(string)
pattern := d.Get("pattern").(string)
log.Printf("[DEBUG] Creating tag protection for %s/%s with pattern %s", owner, repo, pattern)
tagProtection, _, err := client.Repositories.CreateTagProtection(ctx, owner, repo, pattern)
if err != nil {
return err
}
d.SetId(strconv.FormatInt(tagProtection.GetID(), 10))
return resourceGithubRepositoryTagProtectionRead(d, meta)
}
func resourceGithubRepositoryTagProtectionRead(d *schema.ResourceData, meta interface{}) error {
ctx := context.WithValue(context.Background(), ctxId, d.Id())
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repo := d.Get("repository").(string)
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return err
}
tag_protection, _, err := client.Repositories.ListTagProtection(ctx, owner, repo)
if err != nil {
if ghErr, ok := err.(*github.ErrorResponse); ok {
if ghErr.Response.StatusCode == http.StatusNotModified {
return nil
}
if ghErr.Response.StatusCode == http.StatusNotFound && d.IsNewResource() {
return nil
}
return err
}
return err
}
for _, tag := range tag_protection {
if tag.GetID() == id {
if err = d.Set("pattern", tag.GetPattern()); err != nil {
return err
}
}
}
return nil
}
func resourceGithubRepositoryTagProtectionDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
ctx := context.WithValue(context.Background(), ctxId, d.Id())
owner := meta.(*Owner).name
repo := d.Get("repository").(string)
tag_protection_id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return err
}
log.Printf("[DEBUG] Deleting tag protection for %s/%s with id %d", owner, repo, tag_protection_id)
_, error := client.Repositories.DeleteTagProtection(ctx, owner, repo, tag_protection_id)
return error
}