Skip to content

Commit

Permalink
Add Update Operation to templateCredential (#36)
Browse files Browse the repository at this point in the history
Also some fix on function signature (change only variable name)
  • Loading branch information
CARRIERE Etienne authored Aug 11, 2022
1 parent e24cc16 commit e882637
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
22 changes: 16 additions & 6 deletions template_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,31 @@ func (client *Client) CreateTemplateCredential(ctx context.Context, templateCred

// 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) {
func (client *Client) GetTemplateCredential(ctx context.Context, templateCredentialID string) (TemplateCredential, error) {
var response templateCredentialResponseBody
err = client.request(ctx, "GET", templateCredentialPrefix+"/"+templateID, nil, &response)
template = response.Credential
return template, err
err := client.request(ctx, "GET", templateCredentialPrefix+"/"+templateCredentialID, nil, &response)
return response.Credential, 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)
func (client *Client) DeleteTemplateCredential(ctx context.Context, templateCredentialID string) error {
return client.request(ctx, "DELETE", templateCredentialPrefix+"/"+templateCredentialID, 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
}

// UpdateTemplateCredential will update the template credential associated with the provided
// template credential ID to match the new name and new content.
func (client *Client) UpdateTemplateCredential(ctx context.Context, templateCredentialID string, templateCredential TemplateCredential) error {
content := map[string]interface{}{
"name": templateCredential.Name,
"type": templateCredential.Type,
"content": templateCredential.Content,
}
return client.request(ctx, "PUT", templateCredentialPrefix+"/"+templateCredentialID, content, nil)
}
26 changes: 24 additions & 2 deletions template_credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,35 @@ func TestTemplateCredentials(t *testing.T) {
if !found {
t.Errorf("Created TemplateCredential not found id=%s", id)
}
// Step 4 : Update the Template credential
newTemplateCredentialPost := NewTemplateCredential()
newtemplateCredentialName := templateCredentialName + "updated"
newTemplateCredentialPost.Name = newtemplateCredentialName
newTemplateCredentialPost.Type = "backblaze"
newtemplateCredentialContent := map[string]interface{}{
"bucket": "mybucket",
"app_key_id": "mykeyid",
"app_key": "mykey",
}
newTemplateCredentialPost.Content = newtemplateCredentialContent
err = client.UpdateTemplateCredential(ctx, id, newTemplateCredentialPost)
if err != nil {
t.Error(err)
}

// Step 5 : Check the updated Template credential
var newTemplateCredential TemplateCredential
if newTemplateCredential, err = client.GetTemplateCredential(ctx, id); err != nil {
t.Error(err)
}
checkTemplateCredential(t, newTemplateCredential, newtemplateCredentialName, newtemplateCredentialContent)

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

// Step 5: Assert templateCredential has been deleted
// Step 7: 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")
Expand Down

0 comments on commit e882637

Please sign in to comment.