Skip to content

Commit

Permalink
Merge pull request #10 from linkpoolio/chore/improvements
Browse files Browse the repository at this point in the history
Update to work with latest OCR API spec
  • Loading branch information
jleeh authored Dec 16, 2020
2 parents f6e7483 + 063bbd9 commit 4bad92f
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 137 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
18 changes: 3 additions & 15 deletions chainlink/resource_chainlink_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ func ResourceChainlinkSpec() *schema.Resource {
Update: resourceSpecUpdate,

Schema: mergeSchemaWithNodeProperties(map[string]*schema.Schema{
"spec_id": {
Type: schema.TypeString,
Computed: true,
ForceNew: true,
},
"json": {
Type: schema.TypeString,
Required: true,
Expand All @@ -40,11 +35,7 @@ func resourceSpecCreate(d *schema.ResourceData, m interface{}) error {
return err
}

matcher := client.NewMatcher("spec", id)
d.SetId(matcher.Id())
if err := d.Set("spec_id", id); err != nil {
return err
}
d.SetId(id)
return nil
}

Expand All @@ -54,15 +45,12 @@ func resourceSpecRead(d *schema.ResourceData, m interface{}) error {
return err
}

spec, err := c.ReadSpec(d.Get("spec_id").(string))

_, err = c.ReadSpec(d.Id())
if err == client.ErrNotFound {
d.SetId("")
} else if err != nil {
d.SetId("")
return err
} else if err := d.Set("spec_id", spec.Data["id"]); err != nil {
return err
}
return nil
}
Expand All @@ -80,5 +68,5 @@ func resourceSpecDelete(d *schema.ResourceData, m interface{}) error {
return err
}

return c.DeleteSpec(d.Get("spec_id").(string))
return c.DeleteSpec(d.Id())
}
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
Loading

0 comments on commit 4bad92f

Please sign in to comment.