Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from wandera/onepass_note_resources
Browse files Browse the repository at this point in the history
Add onepass note resource and datasource
  • Loading branch information
r2ronoha authored Jun 26, 2020
2 parents 2c77587 + 4d34f75 commit b100a6f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
10 changes: 10 additions & 0 deletions cfssl/data_item_secure_note.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cfssl

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

func dataSourceItemSecureNote() *schema.Resource {
return &schema.Resource{
Read: resourceItemSecureNoteRead,
Schema: resourceItemSecureNote().Schema,
}
}
6 changes: 4 additions & 2 deletions cfssl/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ func Provider() terraform.ResourceProvider {
"cfssl_full_chain_cert": resourceFullChain(),
"cfssl_full_chain_multiple_cert": resourceFullChainMultipleCert(),
"cfssl_full_chain_multiple_cert_onepass": resourceFullChainMultipleCertOnePassCaKey(),
"cfssl_onepassword_item_secure_note": resourceItemSecureNote(),
},
DataSourcesMap: map[string]*schema.Resource{
"cfssl_onepassword_item_secure_note": dataSourceItemSecureNote(),
},

DataSourcesMap: map[string]*schema.Resource{},

ConfigureFunc: providerConfigure,
}
Expand Down
88 changes: 88 additions & 0 deletions cfssl/resource_item_secure_note.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cfssl

import (
"errors"

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

func resourceItemSecureNote() *schema.Resource {
return &schema.Resource{
Read: resourceItemSecureNoteRead,
Create: resourceItemSecureNoteCreate,
Delete: resourceItemDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
err := resourceItemSecureNoteRead(d, meta)
return []*schema.ResourceData{d}, err
},
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"tags": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"vault": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"notes": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}

func resourceItemSecureNoteRead(d *schema.ResourceData, meta interface{}) error {
m := meta.(*Meta)
vaultID := d.Get("vault").(string)
v, err := m.onePassClient.ReadItem(getID(d), vaultID)
if err != nil {
return err
}
if v.Template != Category2Template(SecureNoteCategory) {
return errors.New("item is not from " + string(SecureNoteCategory))
}

d.SetId(v.UUID)
if err := d.Set("name", v.Overview.Title); err != nil {
return err
}
if err := d.Set("tags", v.Overview.Tags); err != nil {
return err
}
if err := d.Set("vault", v.Vault); err != nil {
return err
}
return d.Set("notes", v.Details.Notes)
}

func resourceItemSecureNoteCreate(d *schema.ResourceData, meta interface{}) error {
item := &Item{
Vault: d.Get("vault").(string),
Template: Category2Template(SecureNoteCategory),
Details: Details{
Notes: d.Get("notes").(string),
},
Overview: Overview{
Title: d.Get("name").(string),
},
}
m := meta.(*Meta)
err := m.onePassClient.CreateItem(item)
if err != nil {
return err
}
d.SetId(item.UUID)
return nil
}

0 comments on commit b100a6f

Please sign in to comment.