From 1539cfb41d0e2c1e43c7fce6d5bafe1dbb40a580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20W=C3=BCrbach?= Date: Sun, 22 Jan 2017 17:17:54 +0100 Subject: [PATCH] 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/ --- client.go | 12 ++++++++++++ resource_sentry_key.go | 21 ++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 793e6775..bccb9f7f 100644 --- a/client.go +++ b/client.go @@ -223,6 +223,10 @@ type CreateKeyParams struct { Name string `json:"name"` } +type UpdateKeyParams struct { + Name string `json:"name"` +} + func (c *Client) GetKey(organizationSlug, projectSlug, keyID string) (*Key, *http.Response, error) { var key Key @@ -253,6 +257,14 @@ func (c *Client) CreateKey(organizationSlug, projectSlug string, params *CreateK return &key, resp, relevantError(err, apiErr) } +func (c *Client) UpdateKey(organizationSlug, projectSlug string, keyID string, params *UpdateKeyParams) (*Key, *http.Response, error) { + var key Key + apiErr := make(APIError) + path := fmt.Sprintf("0/projects/%s/%s/keys/%s/", organizationSlug, projectSlug, keyID) + resp, err := c.sling.New().Put(path).BodyJSON(params).Receive(&key, &apiErr) + return &key, resp, relevantError(err, apiErr) +} + func (c *Client) DeleteKey(organizationSlug, projectSlug, keyID string) (*http.Response, error) { apiErr := make(APIError) path := fmt.Sprintf("0/projects/%s/%s/keys/%s/", organizationSlug, projectSlug, keyID) diff --git a/resource_sentry_key.go b/resource_sentry_key.go index e6bf62cc..3da0e0d2 100644 --- a/resource_sentry_key.go +++ b/resource_sentry_key.go @@ -12,6 +12,7 @@ func resourceSentryKey() *schema.Resource { return &schema.Resource{ Create: resourceSentryKeyCreate, Read: resourceSentryKeyRead, + Update: resourceSentryKeyUpdate, Delete: resourceSentryKeyDelete, Importer: &schema.ResourceImporter{ State: resourceKeyImporter, @@ -33,7 +34,6 @@ func resourceSentryKey() *schema.Resource { "name": &schema.Schema{ Type: schema.TypeString, Required: true, - ForceNew: true, Description: "The name of the key", }, "dsn_secret": &schema.Schema{ @@ -94,6 +94,25 @@ func resourceSentryKeyRead(d *schema.ResourceData, meta interface{}) error { return nil } +func resourceSentryKeyUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*Client) + + id := d.Id() + org := d.Get("organization").(string) + project := d.Get("project").(string) + params := &UpdateKeyParams{ + Name: d.Get("name").(string), + } + + key, _, err := client.UpdateKey(org, project, id, params) + if err != nil { + return err + } + + d.SetId(key.ID) + return resourceSentryKeyRead(d, meta) +} + func resourceSentryKeyDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*Client)