Skip to content

Commit 1539cfb

Browse files
johanneswuerbachjianyuan
authored andcommitted
Update key names, instead of recreating them (#9)
Update the name of a key using: https://docs.sentry.io/api/projects/put-project-key-details/
1 parent 6c44592 commit 1539cfb

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ type CreateKeyParams struct {
223223
Name string `json:"name"`
224224
}
225225

226+
type UpdateKeyParams struct {
227+
Name string `json:"name"`
228+
}
229+
226230
func (c *Client) GetKey(organizationSlug, projectSlug, keyID string) (*Key, *http.Response, error) {
227231
var key Key
228232

@@ -253,6 +257,14 @@ func (c *Client) CreateKey(organizationSlug, projectSlug string, params *CreateK
253257
return &key, resp, relevantError(err, apiErr)
254258
}
255259

260+
func (c *Client) UpdateKey(organizationSlug, projectSlug string, keyID string, params *UpdateKeyParams) (*Key, *http.Response, error) {
261+
var key Key
262+
apiErr := make(APIError)
263+
path := fmt.Sprintf("0/projects/%s/%s/keys/%s/", organizationSlug, projectSlug, keyID)
264+
resp, err := c.sling.New().Put(path).BodyJSON(params).Receive(&key, &apiErr)
265+
return &key, resp, relevantError(err, apiErr)
266+
}
267+
256268
func (c *Client) DeleteKey(organizationSlug, projectSlug, keyID string) (*http.Response, error) {
257269
apiErr := make(APIError)
258270
path := fmt.Sprintf("0/projects/%s/%s/keys/%s/", organizationSlug, projectSlug, keyID)

resource_sentry_key.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func resourceSentryKey() *schema.Resource {
1212
return &schema.Resource{
1313
Create: resourceSentryKeyCreate,
1414
Read: resourceSentryKeyRead,
15+
Update: resourceSentryKeyUpdate,
1516
Delete: resourceSentryKeyDelete,
1617
Importer: &schema.ResourceImporter{
1718
State: resourceKeyImporter,
@@ -33,7 +34,6 @@ func resourceSentryKey() *schema.Resource {
3334
"name": &schema.Schema{
3435
Type: schema.TypeString,
3536
Required: true,
36-
ForceNew: true,
3737
Description: "The name of the key",
3838
},
3939
"dsn_secret": &schema.Schema{
@@ -94,6 +94,25 @@ func resourceSentryKeyRead(d *schema.ResourceData, meta interface{}) error {
9494
return nil
9595
}
9696

97+
func resourceSentryKeyUpdate(d *schema.ResourceData, meta interface{}) error {
98+
client := meta.(*Client)
99+
100+
id := d.Id()
101+
org := d.Get("organization").(string)
102+
project := d.Get("project").(string)
103+
params := &UpdateKeyParams{
104+
Name: d.Get("name").(string),
105+
}
106+
107+
key, _, err := client.UpdateKey(org, project, id, params)
108+
if err != nil {
109+
return err
110+
}
111+
112+
d.SetId(key.ID)
113+
return resourceSentryKeyRead(d, meta)
114+
}
115+
97116
func resourceSentryKeyDelete(d *schema.ResourceData, meta interface{}) error {
98117
client := meta.(*Client)
99118

0 commit comments

Comments
 (0)