-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update go-sdk to 1.5.0 * Add template Credential resource * Add documentation * go fmt * go mod tidy * Fix name + add example * Fixes documentation * Bumps go-sdk to 1.5.1 * Add update of the resource * go fmt * Update template_credential.md * Update main.tf * Update resource_template_credential.go Co-authored-by: Marius <[email protected]>
- Loading branch information
Showing
15 changed files
with
444 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
--- | ||
layout: "transloadit" | ||
page_title: "Transloadit: template credential" | ||
description: |- | ||
Manages Transloadit Template Credential | ||
--- | ||
|
||
# Resource: transloadit_template_credential | ||
|
||
Manages Transloadit Templates Credential | ||
For additional details please refer to the [Transloadit documentation](https://transloadit.com/docs/topics/template-credentials/). | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
provider "transloadit" { | ||
} | ||
resource "transloadit_template_credential" "s3" { | ||
name = "S3_CREDENTIALS" | ||
type = "s3" | ||
content = <<EOT | ||
{ | ||
"bucket" : "mybucket", | ||
"bucket_region" : "us-east-1", | ||
"key" : "mykey", | ||
"secret" : "mysecret" | ||
} | ||
EOT | ||
} | ||
resource "transloadit_template" "my-terraform-template" { | ||
name = "my-terraform-template" | ||
template = <<EOT | ||
{ | ||
"steps": { | ||
":original": { | ||
"robot": "/upload/handle" | ||
}, | ||
[... different steps ...] | ||
"exported": { | ||
"use": [ ":original", "encoded", "thumbed"], | ||
"credentials": "${transloadit_template_credential.s3.name}", | ||
"robot": "/s3/store" | ||
} | ||
} | ||
} | ||
EOT | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
- `name` - (Required) Name of the Template credential to be added | ||
- `type` - (Required) Type of Template credential to be added | ||
- `content` - (Required) JSON of the template credential. Go to the [documentation](https://transloadit.com/docs/transcoding/) for information about the parameters needed. | ||
Examples: | ||
- s3 credentials: https://transloadit.com/docs/transcoding/file-importing/s3-import/#credentials | ||
- backblaze: https://transloadit.com/docs/transcoding/file-importing/backblaze-import/#credentials | ||
- sftp: https://transloadit.com/docs/transcoding/file-importing/sftp-import/#credentials | ||
|
||
## Attributes Reference | ||
|
||
- `id` - The Template credential ID reference from Transloadit, e.g. `908ab54f2c4b479184db637709320c85` | ||
- `name` - The Template credential slug reference from Transloadit, e.g. `S3_CREDENTIALS` | ||
- `type` - The Template credential type | ||
- `content` - The Template credential content | ||
- `created` - Creation date of the Template credential | ||
- `modified` - Last modified date of the Template credential | ||
|
||
## Update | ||
|
||
`name` and `content` argument can be updated without recreating template credential. | ||
Change of `type` will trigger a delete/creation of the resource | ||
|
||
## Import | ||
|
||
Template credential can be imported using the `id`, e.g. | ||
|
||
```bash | ||
$ terraform import transloadit_template_credential.my_template 908ab54f2c4b479184db637709320c85 | ||
``` | ||
|
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,42 @@ | ||
resource "transloadit_template_credential" "s3" { | ||
name = "MY_S3_CREDENTIALS" | ||
type = "s3" | ||
content = <<EOT | ||
{ | ||
"bucket" : "mybucket", | ||
"bucket_region" : "us-east-1", | ||
"key" : "mykey", | ||
"secret" : "mysecret" | ||
} | ||
EOT | ||
} | ||
|
||
resource "transloadit_template" "my-terraform-template" { | ||
name = "my-terraform-template" | ||
template = <<EOT | ||
{ | ||
"steps": { | ||
":original": { | ||
"robot": "/upload/handle" | ||
}, | ||
"encoded": { | ||
"use": ":original", | ||
"robot": "/video/encode", | ||
"preset": "ipad-high", | ||
"ffmpeg_stack": "v3.3.3" | ||
}, | ||
"thumbed": { | ||
"use": ":original", | ||
"robot": "/video/thumbs", | ||
"count": 4, | ||
"ffmpeg_stack": "v3.3.3" | ||
}, | ||
"exported": { | ||
"use": [ ":original", "encoded", "thumbed"], | ||
"credentials": "${transloadit_template_credential.s3.name}", | ||
"robot": "/s3/store" | ||
} | ||
} | ||
} | ||
EOT | ||
} |
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,11 @@ | ||
terraform { | ||
required_providers { | ||
transloadit = { | ||
source = "transloadit/transloadit" | ||
} | ||
} | ||
} | ||
|
||
provider "transloadit" { | ||
} | ||
|
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,125 @@ | ||
package transloadit | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
transloadit "github.com/transloadit/go-sdk" | ||
) | ||
|
||
func resourceTemplateCredential() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceTemplateCredentialCreate, | ||
Read: resourceTemplateCredentialRead, | ||
Delete: resourceTemplateCredentialDelete, | ||
Update: resourceTemplateCredentialUpdate, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: false, | ||
Description: "Template credential name", | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "Template credential type", | ||
}, | ||
"content": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: false, | ||
Description: "Template credential content in JSON", | ||
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { | ||
bool, _ := AreEqualJSON(old, new) | ||
return bool | ||
}, | ||
}, | ||
"created": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"modified": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceTemplateCredentialCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*transloadit.Client) | ||
templateCredentialContent, err := jsonStringToTemplateCredentialContent(d.Get("content").(string)) | ||
if err != nil { | ||
return err | ||
} | ||
payload := transloadit.TemplateCredential{ | ||
Name: d.Get("name").(string), | ||
Type: d.Get("type").(string), | ||
Content: *templateCredentialContent, | ||
} | ||
templateId, err := client.CreateTemplateCredential(context.Background(), payload) | ||
if err != nil { | ||
return err | ||
} | ||
d.SetId(templateId) | ||
return resourceTemplateCredentialRead(d, meta) | ||
} | ||
|
||
func resourceTemplateCredentialRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*transloadit.Client) | ||
templateCredential, err := client.GetTemplateCredential(context.Background(), d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
toSave := BatchSet{ | ||
"name": templateCredential.Name, | ||
"type": templateCredential.Type, | ||
"created": templateCredential.Created, | ||
"modified": templateCredential.Modified, | ||
} | ||
result, err := json.Marshal(templateCredential.Content) | ||
if err != nil { | ||
return fmt.Errorf("Content field can't be marshalled to json +%v", templateCredential.Content) | ||
} | ||
toSave["content"] = string(result) | ||
return saveToState(d, toSave) | ||
} | ||
|
||
func resourceTemplateCredentialDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*transloadit.Client) | ||
err := client.DeleteTemplateCredential(context.Background(), d.Id()) | ||
return err | ||
} | ||
|
||
func resourceTemplateCredentialUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*transloadit.Client) | ||
templateCredentialContent, err := jsonStringToTemplateCredentialContent(d.Get("content").(string)) | ||
if err != nil { | ||
return err | ||
} | ||
payload := transloadit.TemplateCredential{ | ||
Name: d.Get("name").(string), | ||
Type: d.Get("type").(string), | ||
Content: *templateCredentialContent, | ||
} | ||
err = client.UpdateTemplateCredential(context.Background(), d.Id(), payload) | ||
return err | ||
} | ||
|
||
func jsonStringToTemplateCredentialContent(src string) (*map[string]interface{}, error) { | ||
var content map[string]interface{} | ||
err := json.Unmarshal([]byte(src), &content) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &content, nil | ||
} |
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,63 @@ | ||
package transloadit | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccTemplateCredential_basic(t *testing.T) { | ||
|
||
randInt := rand.Int() % 1000 | ||
template1_name := fmt.Sprintf("templatecredentialbasic%d", randInt) | ||
template2_name := fmt.Sprintf("templatecredentialbasic%d", randInt+1) | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: nil, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: fmt.Sprintf(testAccTemplateCredential_basic_1, template1_name, template2_name), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckExistingResource("transloadit_template_credential.s3"), | ||
testAccCheckExistingResource("transloadit_template_credential.backblaze"), | ||
resource.TestCheckResourceAttr("transloadit_template_credential.s3", "name", template1_name), | ||
resource.TestCheckResourceAttr("transloadit_template_credential.s3", "type", "s3"), | ||
resource.TestCheckResourceAttr("transloadit_template_credential.s3", "content", "{\"bucket\":\"mybucket\",\"bucket_region\":\"us-east-1\",\"key\":\"mykey\",\"secret\":\"mysecret\"}"), | ||
resource.TestCheckResourceAttr("transloadit_template_credential.backblaze", "name", template2_name), | ||
resource.TestCheckResourceAttr("transloadit_template_credential.backblaze", "type", "backblaze"), | ||
resource.TestCheckResourceAttr("transloadit_template_credential.backblaze", "content", "{\"app_key\":\"mykey\",\"app_key_id\":\"mykeyid\",\"bucket\":\"mybucket\"}"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccTemplateCredential_basic_1 = ` | ||
resource "transloadit_template_credential" "s3" { | ||
name = "%s" | ||
type = "s3" | ||
content = <<EOT | ||
{ | ||
"bucket" : "mybucket", | ||
"bucket_region" : "us-east-1", | ||
"key" : "mykey", | ||
"secret" : "mysecret" | ||
} | ||
EOT | ||
} | ||
resource "transloadit_template_credential" "backblaze" { | ||
name = "%s" | ||
type = "backblaze" | ||
content = <<EOT | ||
{ | ||
"bucket" : "mybucket", | ||
"app_key_id" : "mykeyid", | ||
"app_key" : "mykey" | ||
} | ||
EOT | ||
} | ||
` |
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
Oops, something went wrong.