Skip to content

Commit

Permalink
Update to work with latest OCR API spec
Browse files Browse the repository at this point in the history
  • Loading branch information
jleeh committed Dec 16, 2020
1 parent 059ad4e commit 252db18
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 123 deletions.
36 changes: 0 additions & 36 deletions chainlink/data_source_chainlink_wallet.go

This file was deleted.

47 changes: 47 additions & 0 deletions chainlink/data_source_eth_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package chainlink

import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
)

func DataSourceETHKey() *schema.Resource {
return &schema.Resource{
Read: resourceETHKeyRead,

Schema: mergeSchemaWithNodeProperties(map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"index": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
},
}),
}
}

func resourceETHKeyRead(d *schema.ResourceData, m interface{}) error {
c, err := NewClientFromModel(d, m)
if err != nil {
return err
}

keys, err := c.ReadETHKeys()
if err != nil {
return err
}

index, ok := d.Get("index").(int)
if !ok {
return fmt.Errorf("provided index of %s is not an integer", fmt.Sprint(index))
}
if index >= len(keys.Data) {
return fmt.Errorf("provided index of %d exceeds %d amount of keys returned", index, len(keys.Data))
}

d.SetId(keys.Data[index].Attributes.Address)
return nil
}
3 changes: 1 addition & 2 deletions chainlink/resource_chainlink_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func resourceBridgeTypeCreate(d *schema.ResourceData, m interface{}) error {
if err != nil {
return err
}
matcher := client.NewMatcher("bridge", name)
d.SetId(matcher.Id())
d.SetId(name)
return nil
}

Expand Down
3 changes: 1 addition & 2 deletions chainlink/resource_chainlink_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ func resourceSpecCreate(d *schema.ResourceData, m interface{}) error {
return err
}

matcher := client.NewMatcher("spec", id)
d.SetId(matcher.Id())
d.SetId(id)
if err := d.Set("spec_id", id); err != nil {
return err
}
Expand Down
28 changes: 15 additions & 13 deletions chainlink/resource_chainlink_spec_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ func ResourceChainlinkSpecV2() *schema.Resource {
Update: resourceSpecV2Update,

Schema: mergeSchemaWithNodeProperties(map[string]*schema.Schema{
"spec_id": {
Type: schema.TypeString,
Computed: true,
ForceNew: true,
},
"toml": {
Type: schema.TypeString,
Required: true,
Expand All @@ -34,19 +29,26 @@ func resourceSpecV2Create(d *schema.ResourceData, m interface{}) error {
}

toml := d.Get("toml").(string)
id, err := c.CreateSpecV2(toml)
spec, err := c.CreateSpecV2(toml)
if err != nil {
return err
}
matcher := client.NewMatcher("spec_v2", id)
d.SetId(matcher.Id())
if err := d.Set("spec_id", id); err != nil {
return err
}
d.SetId(spec.Data.ID)
return nil
}

func resourceSpecV2Read(_ *schema.ResourceData, _ interface{}) error {
func resourceSpecV2Read(d *schema.ResourceData, m interface{}) error {
c, err := NewClientFromModel(d, m)
if err != nil {
return err
}

if err := c.ReadSpecV2(d.Id()); err == client.ErrNotFound || err == client.ErrUnprocessableEntity {
d.SetId("")
return nil
} else if err != nil {
return err
}
return nil
}

Expand All @@ -63,5 +65,5 @@ func resourceSpecV2Delete(d *schema.ResourceData, m interface{}) error {
return err
}

return c.DeleteSpecV2(d.Get("spec_id").(string))
return c.DeleteSpecV2(d.Id())
}
61 changes: 30 additions & 31 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

var ErrNotFound = errors.New("unexpected response code, got 404")
var ErrUnprocessableEntity = errors.New("unexpected response code, got 422")

func NewChainlink(c *Config) (*Chainlink, error) {
cl := &Chainlink{Config: c}
Expand All @@ -22,21 +23,21 @@ func (c *Chainlink) CreateSpec(spec string) (string, error) {
return fmt.Sprint(specResp.Data["id"]), err
}

func (c *Chainlink) CreateSpecV2(spec string) (string, error) {
specV2Create := struct {
TOML string `json:"toml"`
}{
func (c *Chainlink) CreateSpecV2(spec string) (*SpecV2, error) {
specV2 := &SpecV2{}
_, err := c.do(http.MethodPost, "/v2/jobs", &SpecV2Form{
TOML: spec,
}
specV2Resp := struct {
JobID int32 `json:"jobID"`
}{}
_, err := c.do(http.MethodPost, "/v2/ocr/specs", specV2Create, &specV2Resp, http.StatusOK)
return fmt.Sprint(specV2Resp.JobID), err
}, &specV2, http.StatusOK)
return specV2, err
}

func (c *Chainlink) ReadSpecV2(id string) error {
_, err := c.do(http.MethodGet, fmt.Sprintf("/v2/jobs/%s", id), nil, nil, http.StatusOK)
return err
}

func (c *Chainlink) DeleteSpecV2(id string) error {
_, err := c.do(http.MethodDelete, fmt.Sprintf("/v2/ocr/specs/%s", id), nil, nil, http.StatusNoContent)
_, err := c.do(http.MethodDelete, fmt.Sprintf("/v2/jobs/%s", id), nil, nil, http.StatusNoContent)
return err
}

Expand Down Expand Up @@ -67,50 +68,46 @@ func (c *Chainlink) DeleteBridge(name string) error {
return err
}

func (c *Chainlink) ReadWallet() (string, error) {
walletObj := &ResponseArray{}
if _, err := c.do(http.MethodGet, "/v2/user/balances", nil, &walletObj, http.StatusOK); err != nil {
return "", err
} else if len(walletObj.Data) == 0 {
return "", fmt.Errorf("unexpected response back from Chainlink, no wallets were given")
}
return fmt.Sprint(walletObj.Data[0]["id"]), nil
}

func (c *Chainlink) CreateOCRKey() (*OCRKey, error) {
ocrKey := &OCRKey{}
_, err := c.do(http.MethodPost, "/v2/off_chain_reporting_keys", nil, ocrKey, http.StatusOK)
_, err := c.do(http.MethodPost, "/v2/keys/ocr", nil, ocrKey, http.StatusOK)
return ocrKey, err
}

func (c *Chainlink) ReadOCRKeys() (*OCRKeys, error) {
ocrKeys := &OCRKeys{}
_, err := c.do(http.MethodGet, "/v2/off_chain_reporting_keys", nil, ocrKeys, http.StatusOK)
_, err := c.do(http.MethodGet, "/v2/keys/ocr", nil, ocrKeys, http.StatusOK)
return ocrKeys, err
}

func (c *Chainlink) DeleteOCRKey(id string) error {
_, err := c.do(http.MethodDelete, fmt.Sprintf("/v2/off_chain_reporting_keys/%s", id), nil, nil, http.StatusOK)
_, err := c.do(http.MethodDelete, fmt.Sprintf("/v2/keys/ocr/%s", id), nil, nil, http.StatusOK)
return err
}

func (c *Chainlink) CreateP2PKey() (*P2PKey, error) {
p2pKey := &P2PKey{}
_, err := c.do(http.MethodPost, "/v2/p2p_keys", nil, p2pKey, http.StatusOK)
_, err := c.do(http.MethodPost, "/v2/keys/p2p", nil, p2pKey, http.StatusOK)
return p2pKey, err
}

func (c *Chainlink) ReadP2PKeys() (*P2PKeys, error) {
p2pKeys := &P2PKeys{}
_, err := c.do(http.MethodGet, "/v2/p2p_keys", nil, p2pKeys, http.StatusOK)
_, err := c.do(http.MethodGet, "/v2/keys/p2p", nil, p2pKeys, http.StatusOK)
return p2pKeys, err
}

func (c *Chainlink) DeleteP2PKey(id int) error {
_, err := c.do(http.MethodDelete, fmt.Sprintf("/v2/p2p_keys/%d", id), nil, nil, http.StatusOK)
_, err := c.do(http.MethodDelete, fmt.Sprintf("/v2/keys/p2p/%d", id), nil, nil, http.StatusOK)
return err
}

func (c *Chainlink) ReadETHKeys() (*ETHKeys, error) {
ethKeys := &ETHKeys{}
_, err := c.do(http.MethodGet, "/v2/keys/eth", nil, ethKeys, http.StatusOK)
return ethKeys, err
}

func (c *Chainlink) doRaw(
method,
endpoint string,
Expand All @@ -133,21 +130,23 @@ func (c *Chainlink) doRaw(
resp, err := client.Do(req)
if err != nil {
return resp, err
} else if obj == nil {
return resp, err
}

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error while reading response: %v\nURL: %s\nresponse received: %s", err, c.Config.URL, string(b))
}

if resp.StatusCode == 404 {
if resp.StatusCode == http.StatusNotFound {
return resp, ErrNotFound
} else if resp.StatusCode == http.StatusUnprocessableEntity {
return resp, ErrUnprocessableEntity
} else if resp.StatusCode != expectedStatusCode {
return resp, fmt.Errorf("unexpected response code, got %d, expected 200\nURL: %s\nresponse received: %s", resp.StatusCode, c.Config.URL, string(b))
}

if obj == nil {
return resp, err
}
err = json.Unmarshal(b, &obj)
if err != nil {
return nil, fmt.Errorf("error while unmarshaling response: %v\nURL: %s\nresponse received: %s", err, c.Config.URL, string(b))
Expand Down
55 changes: 28 additions & 27 deletions client/models.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package client

import (
"fmt"
"net/http"
"strings"
)

type Config struct {
Expand Down Expand Up @@ -81,6 +79,34 @@ type P2PKeyAttributes struct {
PublicKey string `json:"publicKey"`
}

type ETHKeys struct {
Data []ETHKeyData `json:"data"`
}

type ETHKey struct {
Data ETHKeyData `json:"data"`
}

type ETHKeyData struct {
Attributes ETHKeyAttributes `json:"attributes"`
}

type ETHKeyAttributes struct {
Address string `json:"address"`
}

type SpecV2Form struct {
TOML string `json:"toml"`
}

type SpecV2 struct {
Data SpecV2Data `json:"data"`
}

type SpecV2Data struct {
ID string `json:"id"`
}

type Client interface {
CreateBridge(addr, name, url string) error
ReadBridge(id string) (*BridgeType, error)
Expand All @@ -96,28 +122,3 @@ type Chainlink struct {
type DataIdentifier interface {
Id() string
}

type Matcher struct {
DataIdentifier
Object string
Data string
}

func NewMatcher(obj, data string) Matcher {
return Matcher{
Object: obj,
Data: data,
}
}

func NewMatcherFromID(id string) Matcher {
s := strings.Split(id, Delimiter())
return Matcher{
Object: s[0],
Data: s[1],
}
}

func (i *Matcher) Id() string {
return fmt.Sprintf("%s%s%s", i.Object, Delimiter(), i.Data)
}
5 changes: 0 additions & 5 deletions client/utils.go

This file was deleted.

Loading

0 comments on commit 252db18

Please sign in to comment.