-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_repo.go
177 lines (158 loc) · 4.1 KB
/
resource_repo.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SPDX-FileCopyrightText: 2024 Dominik Wombacher <[email protected]>
// SPDX-FileCopyrightText: 2019 The SourceHut API Contributors
//
// SPDX-License-Identifier: BSD-2-Clause
package main
import (
"net/http"
"strconv"
"time"
"git.sr.ht/~wombelix/sourcehut-go/git"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
// Resource Name
repoName = "sourcehut_repository"
// Schema keys
nameKey = "name"
descKey = "description"
visiKey = "visibility"
subjectKey = "subject"
)
// repoSchema returns a schema that is used by both the repo resource and the
// repo datasource.
func repoSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
nameKey: {
Type: schema.TypeString,
Required: true,
Description: "The name of the repository.",
},
descKey: {
Type: schema.TypeString,
Optional: true,
Description: "A description of the repository.",
},
visiKey: {
Type: schema.TypeString,
Optional: true,
Default: "public",
Description: `The visibility of the repository ("public", "unlisted", or "private").`,
},
createdKey: {
Type: schema.TypeString,
Computed: true,
Description: "The date on which the repo was created in RFC3339 format.",
},
createdTimestampKey: {
Type: schema.TypeInt,
Computed: true,
Description: "The date on which the repo was created as a unix timestamp.",
},
subjectKey: {
Type: schema.TypeString,
Computed: true,
Description: "The message subject.",
},
}
}
func resourceRepo() *schema.Resource {
return &schema.Resource{
Create: resourceRepoCreate,
Read: resourceRepoRead,
Delete: resourceRepoDelete,
Update: resourceRepoUpdate,
Importer: &schema.ResourceImporter{
State: resourceRepoImport,
},
Schema: repoSchema(),
}
}
func resourceRepoCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(config)
visibility := git.RepoVisibility(d.Get(visiKey).(string))
repo, err := config.gitClient.NewRepo(
d.Get(nameKey).(string),
d.Get(descKey).(string),
visibility,
)
if err != nil {
return err
}
return setRepo(d, repo)
}
func resourceRepoRead(d *schema.ResourceData, meta interface{}) error {
return repoRead(d, meta, false)
}
func repoRead(d *schema.ResourceData, meta interface{}, importing bool) error {
config := meta.(config)
name := d.Id()
if !importing {
name = d.Get(nameKey).(string)
}
repo, err := config.gitClient.Repo("", name)
var statusCode int
if e, ok := err.(interface {
StatusCode() int
}); ok {
statusCode = e.StatusCode()
}
// If the error resulted from a 404, mark the resource as deleted.
if statusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return err
}
return setRepo(d, repo)
}
func resourceRepoDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(config)
return config.gitClient.DeleteRepo(d.Get(nameKey).(string))
}
func resourceRepoUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(config)
oldName, newName := d.GetChange(nameKey)
repo := &git.Repo{
Description: d.Get(descKey).(string),
Name: newName.(string),
Visibility: git.RepoVisibility(d.Get(visiKey).(string)),
}
return config.gitClient.UpdateRepo(oldName.(string), repo)
}
func resourceRepoImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
err := repoRead(d, meta, true)
if err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}
func setRepo(d *schema.ResourceData, repo *git.Repo) error {
d.SetId(strconv.FormatInt(repo.ID, 10))
//err := d.Set(idKey, repo.ID)
//if err != nil {
// return err
//}
err := d.Set(createdKey, repo.Created.Format(time.RFC3339))
if err != nil {
return err
}
err = d.Set(createdTimestampKey, repo.Created.Unix())
if err != nil {
return err
}
err = d.Set(subjectKey, repo.Subject)
if err != nil {
return err
}
err = d.Set(descKey, repo.Description)
if err != nil {
return err
}
err = d.Set(visiKey, string(repo.Visibility))
if err != nil {
return err
}
return d.Set(nameKey, repo.Name)
}