-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resource to manage sentry plugins (#5)
- Loading branch information
1 parent
90aa787
commit 8e39cc8
Showing
4 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceSentryPlugin() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceSentryPluginCreate, | ||
Read: resourceSentryPluginRead, | ||
Update: resourceSentryPluginUpdate, | ||
Delete: resourceSentryPluginDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: resourceSentryPluginImporter, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"organization": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The slug of the organization the project belongs to", | ||
}, | ||
"project": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The slug of the project to create the plugin for", | ||
}, | ||
"plugin": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The id of the plugin", | ||
}, | ||
"config": &schema.Schema{ | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
Description: "Plugin config", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceSentryPluginCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Client) | ||
|
||
plugin := d.Get("plugin").(string) | ||
org := d.Get("organization").(string) | ||
project := d.Get("project").(string) | ||
|
||
log.Printf("%v, %v, %v", plugin, org, project) | ||
|
||
if _, err := client.EnablePlugin(org, project, plugin); err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(plugin) | ||
|
||
params := d.Get("config").(map[string]interface{}) | ||
if _, _, err := client.UpdatePlugin(org, project, plugin, params); err != nil { | ||
return err | ||
} | ||
|
||
return resourceSentryPluginRead(d, meta) | ||
} | ||
|
||
func resourceSentryPluginRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Client) | ||
|
||
id := d.Id() | ||
org := d.Get("organization").(string) | ||
project := d.Get("project").(string) | ||
|
||
plugin, _, err := client.GetPlugin(org, project, id) | ||
if err != nil { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
d.SetId(plugin.ID) | ||
|
||
pluginConfig := make(map[string]string) | ||
for _, entry := range plugin.Config { | ||
pluginConfig[entry.Name] = entry.Value | ||
} | ||
|
||
config := make(map[string]string) | ||
for k := range d.Get("config").(map[string]interface{}) { | ||
config[k] = pluginConfig[k] | ||
} | ||
|
||
d.Set("config", config) | ||
|
||
return nil | ||
} | ||
|
||
func resourceSentryPluginUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Client) | ||
|
||
id := d.Id() | ||
org := d.Get("organization").(string) | ||
project := d.Get("project").(string) | ||
|
||
params := d.Get("config").(map[string]interface{}) | ||
if _, _, err := client.UpdatePlugin(org, project, id, params); err != nil { | ||
return err | ||
} | ||
|
||
return resourceSentryPluginRead(d, meta) | ||
} | ||
|
||
func resourceSentryPluginDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Client) | ||
|
||
id := d.Id() | ||
org := d.Get("organization").(string) | ||
project := d.Get("project").(string) | ||
|
||
_, err := client.DisablePlugin(org, project, id) | ||
return err | ||
} | ||
|
||
func resourceSentryPluginImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
addrID := d.Id() | ||
|
||
log.Printf("[DEBUG] Importing key using ADDR ID %s", addrID) | ||
|
||
parts := strings.Split(addrID, "/") | ||
|
||
if len(parts) != 3 { | ||
return nil, errors.New("Project import requires an ADDR ID of the following schema org-slug/project-slug/plugin-id") | ||
} | ||
|
||
d.Set("organization", parts[0]) | ||
d.Set("project", parts[1]) | ||
d.SetId(parts[2]) | ||
|
||
return []*schema.ResourceData{d}, nil | ||
} |