Skip to content

Commit

Permalink
Add support for notification configuration resource
Browse files Browse the repository at this point in the history
  • Loading branch information
lafentres committed Aug 13, 2019
1 parent 2ff06c9 commit 1418928
Show file tree
Hide file tree
Showing 4 changed files with 653 additions and 13 deletions.
27 changes: 14 additions & 13 deletions tfe/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,20 @@ func Provider() terraform.ResourceProvider {
},

ResourcesMap: map[string]*schema.Resource{
"tfe_oauth_client": resourceTFEOAuthClient(),
"tfe_organization": resourceTFEOrganization(),
"tfe_organization_token": resourceTFEOrganizationToken(),
"tfe_policy_set": resourceTFEPolicySet(),
"tfe_sentinel_policy": resourceTFESentinelPolicy(),
"tfe_ssh_key": resourceTFESSHKey(),
"tfe_team": resourceTFETeam(),
"tfe_team_access": resourceTFETeamAccess(),
"tfe_team_member": resourceTFETeamMember(),
"tfe_team_members": resourceTFETeamMembers(),
"tfe_team_token": resourceTFETeamToken(),
"tfe_workspace": resourceTFEWorkspace(),
"tfe_variable": resourceTFEVariable(),
"tfe_notification_configuration": resourceTFENotificationConfiguration(),
"tfe_oauth_client": resourceTFEOAuthClient(),
"tfe_organization": resourceTFEOrganization(),
"tfe_organization_token": resourceTFEOrganizationToken(),
"tfe_policy_set": resourceTFEPolicySet(),
"tfe_sentinel_policy": resourceTFESentinelPolicy(),
"tfe_ssh_key": resourceTFESSHKey(),
"tfe_team": resourceTFETeam(),
"tfe_team_access": resourceTFETeamAccess(),
"tfe_team_member": resourceTFETeamMember(),
"tfe_team_members": resourceTFETeamMembers(),
"tfe_team_token": resourceTFETeamToken(),
"tfe_workspace": resourceTFEWorkspace(),
"tfe_variable": resourceTFEVariable(),
},

ConfigureFunc: providerConfigure,
Expand Down
205 changes: 205 additions & 0 deletions tfe/resource_tfe_notification_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package tfe

import (
"fmt"
"log"

tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceTFENotificationConfiguration() *schema.Resource {
return &schema.Resource{
Create: resourceTFENotificationConfigurationCreate,
Read: resourceTFENotificationConfigurationRead,
Update: resourceTFENotificationConfigurationUpdate,
Delete: resourceTFENotificationConfigurationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"destination_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(
[]string{
string(tfe.NotificationDestinationTypeGeneric),
string(tfe.NotificationDestinationTypeSlack),
},
false,
),
},

"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"token": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},

"triggers": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(
[]string{
string(tfe.NotificationTriggerCreated),
string(tfe.NotificationTriggerPlanning),
string(tfe.NotificationTriggerNeedsAttention),
string(tfe.NotificationTriggerApplying),
string(tfe.NotificationTriggerCompleted),
string(tfe.NotificationTriggerErrored),
},
false,
),
},
},

"url": {
Type: schema.TypeString,
Required: true,
},

"workspace_external_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceTFENotificationConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)

// Get workspace
workspaceID := d.Get("workspace_external_id").(string)

// Get attributes
destinationType := tfe.NotificationDestinationType(d.Get("destination_type").(string))
enabled := d.Get("enabled").(bool)
name := d.Get("name").(string)
token := d.Get("token").(string)
url := d.Get("url").(string)

// Throw error if token is set with destinationType of slack
if token != "" && destinationType == tfe.NotificationDestinationTypeSlack {
return fmt.Errorf("Token cannot be set with destination_type of %s", destinationType)
}

// Create a new options struct
options := tfe.NotificationConfigurationCreateOptions{
DestinationType: tfe.NotificationDestination(destinationType),
Enabled: tfe.Bool(enabled),
Name: tfe.String(name),
Token: tfe.String(token),
URL: tfe.String(url),
}

// Add triggers set to the options struct
for _, trigger := range d.Get("triggers").(*schema.Set).List() {
options.Triggers = append(options.Triggers, trigger.(string))
}

log.Printf("[DEBUG] Create notification configuration: %s", name)
notificationConfiguration, err := tfeClient.NotificationConfigurations.Create(ctx, workspaceID, options)
if err != nil {
return fmt.Errorf("Error creating notification configuration %s: %v", name, err)
}

d.SetId(notificationConfiguration.ID)

return resourceTFENotificationConfigurationRead(d, meta)
}

func resourceTFENotificationConfigurationRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)

log.Printf("[DEBUG] Read notification configuration: %s", d.Id())
notificationConfiguration, err := tfeClient.NotificationConfigurations.Read(ctx, d.Id())
if err != nil {
if err == tfe.ErrResourceNotFound {
log.Printf("[DEBUG] Notification configuration %s no longer exists", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading notification configuration %s: %v", d.Id(), err)
}

// Update config
d.Set("destination_type", notificationConfiguration.DestinationType)
d.Set("enabled", notificationConfiguration.Enabled)
d.Set("name", notificationConfiguration.Name)
// Don't set token here, as it is write only
// and setting it here would make it blank
d.Set("triggers", notificationConfiguration.Triggers)
d.Set("url", notificationConfiguration.URL)

return nil
}

func resourceTFENotificationConfigurationUpdate(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)

// Get attributes
destinationType := tfe.NotificationDestinationType(d.Get("destination_type").(string))
enabled := d.Get("enabled").(bool)
name := d.Get("name").(string)
token := d.Get("token").(string)
url := d.Get("url").(string)

// Throw error if token is set with destinationType of slack
if token != "" && destinationType == tfe.NotificationDestinationTypeSlack {
return fmt.Errorf("Token cannot be set with destination_type of %s", destinationType)
}

// Create a new options struct
options := tfe.NotificationConfigurationUpdateOptions{
Enabled: tfe.Bool(enabled),
Name: tfe.String(name),
Token: tfe.String(token),
URL: tfe.String(url),
}

// Add triggers set to the options struct
for _, trigger := range d.Get("triggers").(*schema.Set).List() {
options.Triggers = append(options.Triggers, trigger.(string))
}

log.Printf("[DEBUG] Update notification configuration: %s", d.Id())
_, err := tfeClient.NotificationConfigurations.Update(ctx, d.Id(), options)
if err != nil {
return fmt.Errorf("Error updating notification configuration %s: %v", d.Id(), err)
}

return resourceTFENotificationConfigurationRead(d, meta)
}

func resourceTFENotificationConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)

log.Printf("[DEBUG] Delete notification configuration: %s", d.Id())
err := tfeClient.NotificationConfigurations.Delete(ctx, d.Id())
if err != nil {
if err == tfe.ErrResourceNotFound {
return nil
}
return fmt.Errorf("Error deleting notification configuration %s: %v", d.Id(), err)
}

return nil
}
Loading

0 comments on commit 1418928

Please sign in to comment.