Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
API Key Resource
Browse files Browse the repository at this point in the history
Adds API Key resource:

- Create
- Read
- Delete

Update has not yet been implemented.
  • Loading branch information
flovouin authored and Preskton committed Dec 23, 2019
1 parent f45ba02 commit 194507f
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 4 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Twilio Terraform Provider

The goal of this Terraform provider plugin is to make manging your Twilio account easier.
The goal of this Terraform provider plugin is to make managing your Twilio account easier.

Current features:

Expand All @@ -19,6 +19,9 @@ Current features:
- Create
- Update
- Delete
- `twilio_api_key`
- Create
- Delete

More coming eventually!

Expand All @@ -43,6 +46,10 @@ resource "twilio_subaccount" "woomy" {
friendly_name = "Woomy Subaccount #1"
}
resource "twilio_api_key" "woomy" {
friendly_name = "Woomy Key #1"
}
resource "twilio_phone_number" "area_code_test" {
// Find a number
country_code = "US"
Expand All @@ -55,7 +62,7 @@ resource "twilio_phone_number" "area_code_test" {
identity_sid = "IDXXXX" // Certain countries may require a validated identity!
trunk_sid = "XXXXX"
voice {
voice {
primary_url = "https://genoq.com/handlers/voice-primary"
primary_http_method = "POST"
fallback_url = "https://genoq.com/handlers/voice-fallback"
Expand Down Expand Up @@ -92,12 +99,12 @@ resource "twilio_phone_number" "search_test" {
primary_url = "https://genoq.com/handlers/sms-primary"
primary_http_method = "POST"
fallback_url = "https://genoq.com/handlers/sms-fallback"
fallback_http_method = "GET"
fallback_http_method = "GET"
}
voice {
receive_mode = "fax"
application_sid = "APXXXXX"
}
}
}
```
1 change: 1 addition & 0 deletions plugin/providers/twilio/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func providerResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"twilio_phone_number": resourceTwilioPhoneNumber(),
"twilio_subaccount": resourceTwilioSubaccount(),
"twilio_api_key": resourceTwilioApiKey(),
}
}

Expand Down
138 changes: 138 additions & 0 deletions plugin/providers/twilio/resource_twilio_api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package twilio

import (
"context"
"errors"
"fmt"
"net/url"

"github.com/hashicorp/terraform/helper/schema"

log "github.com/sirupsen/logrus"
)

func resourceTwilioApiKey() *schema.Resource {
return &schema.Resource{
Create: resourceTwilioApiKeyCreate,
Read: resourceTwilioApiKeyRead,
Update: resourceTwilioApiKeyUpdate,
Delete: resourceTwilioApiKeyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"sid": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"friendly_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"secret": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"date_created": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"date_updated": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}

func flattenKeyForCreate(d *schema.ResourceData) url.Values {
v := make(url.Values)

v.Add("FriendlyName", d.Get("friendly_name").(string))

return v
}

func resourceTwilioApiKeyCreate(d *schema.ResourceData, meta interface{}) error {
log.Debug("ENTER resourceTwilioApiKeyCreate")

client := meta.(*TerraformTwilioContext).client
context := context.TODO()

createParams := flattenKeyForCreate(d)

log.Debug("START client.Keys.Create")

createResult, err := client.Keys.Create(context, createParams)

if err != nil {
log.WithError(err).Error("client.Keys.Create failed")

return err
}

d.SetId(createResult.Sid)
d.Set("sid", createResult.Sid)
d.Set("secret", createResult.Secret)
d.Set("friendly_name", createResult.FriendlyName) // In the event that the name wasn't specified, Twilio generates one for you
d.Set("date_created", createResult.DateCreated)
d.Set("date_updated", createResult.DateUpdated)

log.Debug("END client.Keys.Create")

return nil
}

func resourceTwilioApiKeyRead(d *schema.ResourceData, meta interface{}) error {
log.Debug("ENTER resourceTwilioApiKeyRead")

client := meta.(*TerraformTwilioContext).client
context := context.TODO()

sid := d.Id()

log.Debug("START client.Keys.Get")

key, err := client.Keys.Get(context, sid)

d.Set("sid", key.Sid)
// Not updating the secret as Twilio only returns it on creation, not after
d.Set("friendly_name", key.FriendlyName) // In the event that the name wasn't specified, Twilio generates one for you
d.Set("date_created", key.DateCreated)
d.Set("date_updated", key.DateUpdated)

log.Debug("END client.Keys.Get")

if err != nil {
return fmt.Errorf("Failed to refresh key: %s", err.Error())
}

return nil
}

func resourceTwilioApiKeyUpdate(d *schema.ResourceData, meta interface{}) error {
return errors.New("Not implemented")
}

func resourceTwilioApiKeyDelete(d *schema.ResourceData, meta interface{}) error {
log.Debug("ENTER resourceTwilioApiKeyDelete")

client := meta.(*TerraformTwilioContext).client
context := context.TODO()

sid := d.Id()

log.Debug("START client.Keys.Delete")

err := client.Keys.Delete(context, sid)

log.Debug("END client.Accounts.Delete")

if err != nil {
return fmt.Errorf("Failed to delete key: %s", err.Error())
}

return nil
}

0 comments on commit 194507f

Please sign in to comment.