Skip to content

Commit

Permalink
Add Template credential CRUDL support (#33)
Browse files Browse the repository at this point in the history
* WIP : Add template-credentials support

* Update Template Credential structure to get all response fields

* Takes in account Marius's comments

* Comment change

* Change Comment + remove unused variable

* Update template_credentials.go

* Update template_credentials.go

Co-authored-by: Marius <[email protected]>
  • Loading branch information
CARRIERE Etienne and Acconut authored Aug 9, 2022
1 parent 1b18056 commit 2be7e08
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
75 changes: 75 additions & 0 deletions template_credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package transloadit

import (
"context"
)

// TemplateCredential contains details about a single template credential.
type TemplateCredential struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Content map[string]interface{} `json:"content"`
Created string `json:"created",omitempty`
Modified string `json:"modified",omitempty`
}

type templateCredentialResponseBody struct {
Credential TemplateCredential `json:"credential"`
OK string `json:"ok"`
Message string `json:"message"`
}

// TemplateCredentialList contains a list of template credentials.
type TemplateCredentialList struct {
TemplateCredential []TemplateCredential `json:"credentials"`
OK string `json:"ok"`
Message string `json:"message"`
}

// NewTemplateCredential returns a new TemplateCredential struct with initialized values. This
// template credential will not be saved to Transloadit. To do so, please use the
// Client.CreateTemplateCredential function.
func NewTemplateCredential() TemplateCredential {
return TemplateCredential{
Content: make(map[string]interface{}),
}
}

var templateCredentialPrefix = "template_credentials"

// CreateTemplateCredential will save the provided template credential struct to the server
// and return the ID of the new template credential.
func (client *Client) CreateTemplateCredential(ctx context.Context, templateCredential TemplateCredential) (string, error) {
content := map[string]interface{}{
"name": templateCredential.Name,
"type": templateCredential.Type,
"content": templateCredential.Content,
}
var response templateCredentialResponseBody
if err := client.request(ctx, "POST", templateCredentialPrefix, content, &response); err != nil {
return "", err
}
return response.Credential.ID, nil
}

// GetTemplateCredential will retrieve details about the template credential associated with the
// provided template credential ID.
func (client *Client) GetTemplateCredential(ctx context.Context, templateID string) (template TemplateCredential, err error) {
var response templateCredentialResponseBody
err = client.request(ctx, "GET", templateCredentialPrefix+"/"+templateID, nil, &response)
template = response.Credential
return template, err
}

// DeleteTemplateCredential will delete the template credential associated with the provided
// template ID.
func (client *Client) DeleteTemplateCredential(ctx context.Context, templateID string) error {
return client.request(ctx, "DELETE", templateCredentialPrefix+"/"+templateID, nil, nil)
}

// ListTemplateCredential will retrieve all templates credential matching the criteria.
func (client *Client) ListTemplateCredential(ctx context.Context, options *ListOptions) (list TemplateCredentialList, err error) {
err = client.listRequest(ctx, templateCredentialPrefix, options, &list)
return list, err
}
79 changes: 79 additions & 0 deletions template_credentials_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package transloadit

import (
"reflect"
"testing"
)

func TestTemplateCredentials(t *testing.T) {
t.Parallel()

client := setup(t)
templateCredentialName := generateTemplateName()

templateCredentialPost := NewTemplateCredential()
templateCredentialPost.Name = templateCredentialName
templateCredentialPost.Type = "s3"
templateCredentialContent := map[string]interface{}{
"key": "xyxy",
"secret": "xyxyxyxy",
"bucket": "mybucket.example.com",
"bucket_region": "us-east-1",
}
templateCredentialPost.Content = templateCredentialContent

// Step 1: Create a brand new templateCredential
id, err := client.CreateTemplateCredential(ctx, templateCredentialPost)
if err != nil {
t.Error(err)
}
if id == "" {
t.Error("no templateCredentialPost id returned")
}

// Step 2: Retrieve new templateCredential created and assert its properties
var templateCredential TemplateCredential
if templateCredential, err = client.GetTemplateCredential(ctx, id); err != nil {
t.Error(err)
}
checkTemplateCredential(t, templateCredential, templateCredentialName, templateCredentialContent)

// Step 3: List all Templated credentials and assume that the created templateCredential is present
list, err := client.ListTemplateCredential(ctx, nil)
if err != nil {
t.Error(err)
}
found := false
for _, cred := range list.TemplateCredential {
if cred.ID == id {
checkTemplateCredential(t, cred, templateCredentialName, templateCredentialContent)
found = true
}
}
if !found {
t.Errorf("Created TemplateCredential not found id=%s", id)
}

// Step 4: Delete test templateCredential
if err := client.DeleteTemplateCredential(ctx, id); err != nil {
t.Error(err)
}

// Step 5: Assert templateCredential has been deleted
_, err = client.GetTemplateCredential(ctx, id)
if err.(RequestError).Code != "TEMPLATE_CREDENTIALS_NOT_READ" {
t.Error("templateCredentialPost has not been deleted")
}
}

func checkTemplateCredential(t *testing.T, cred TemplateCredential, templateCredentialName string, expected map[string]interface{}) {
if cred.Name != templateCredentialName {
t.Error("wrong templateCredentialPost name")
}
if cred.Type != "s3" {
t.Error("wrong templateCredentialPost type")
}
if !reflect.DeepEqual(cred.Content, expected) {
t.Errorf("Different in content expected=%+v . In response : %+v", expected, cred.Content)
}
}

0 comments on commit 2be7e08

Please sign in to comment.