Skip to content

Commit

Permalink
small refactor and addition of epoch data source
Browse files Browse the repository at this point in the history
  • Loading branch information
callensm committed Aug 30, 2021
1 parent 4f68bad commit f81985e
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ builds:
flags:
- -trimpath
ldflags:
- '-s -w -X main.version={{ .Version }} -X main.commit={{ .Commit }}'
- '-s -w -X main.version={{ .Version }}'
goos:
- freebsd
- windows
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
clean:
rm -rf vendor/

test: vendor
test:
TF_ACC=1 go test -v ./solana/...

vendor: clean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "solana_account_info Data Source - terraform-provider-solana"
page_title: "solana_account Data Source - terraform-provider-solana"
subcategory: ""
description: |-
---

# solana_account_info (Data Source)
# solana_account (Data Source)



Expand Down
30 changes: 30 additions & 0 deletions docs/data-sources/epoch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "solana_epoch Data Source - terraform-provider-solana"
subcategory: ""
description: |-
---

# solana_epoch (Data Source)





<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- **id** (String) The ID of this resource.

### Read-Only

- **absolute_slot** (Number) The current absolute slot in the epoch
- **block_height** (Number) The current block height
- **epoch** (Number) The current epoch count
- **slot_index** (Number) The current slot relative to the start of the current epoch
- **slots_in_epoch** (Number) The number of slots in this epoch


Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceAccountInfo() *schema.Resource {
func dataSourceAccount() *schema.Resource {
return &schema.Resource{
Read: dataSourceAccountInfoRead,
Read: dataSourceAccountRead,

Schema: map[string]*schema.Schema{
"public_key": {
Expand Down Expand Up @@ -42,7 +42,7 @@ func dataSourceAccountInfo() *schema.Resource {
}
}

func dataSourceAccountInfoRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceAccountRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*providerConfig).rpcClient

pub := d.Get("public_key").(string)
Expand All @@ -56,7 +56,7 @@ func dataSourceAccountInfoRead(d *schema.ResourceData, meta interface{}) error {
return err
}

d.SetId(fmt.Sprintf("account_info:%s", pub))
d.SetId(fmt.Sprintf("account:%s", pub))
d.Set("lamports", uint64(res.Value.Lamports))
d.Set("owner", res.Value.Owner.String())
d.Set("executable", res.Value.Executable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,44 @@ import (
)

const (
testAccountInfoDataConfig = `
testAccountDataConfig = `
provider "solana" {
endpoint = "https://api.testnet.solana.com"
}
data "solana_account_info" "test" {
data "solana_account" "test" {
public_key = "11111111111111111111111111111111"
}
`
)

func TestAccAccountInfoDataSource(t *testing.T) {
func TestAccAccountDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccountInfoDataConfig,
Config: testAccountDataConfig,
Check: resource.ComposeTestCheckFunc(
testAccountInfoDataSucceeds("data.solana_account_info.test"),
testAccountDataSucceeds("data.solana_account.test"),
),
},
},
})
}

func testAccountInfoDataSucceeds(name string) resource.TestCheckFunc {
func testAccountDataSucceeds(name string) resource.TestCheckFunc {
return func(state *terraform.State) error {
val, ok := state.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Account Info Failure: %s not found", name)
return fmt.Errorf("Account Failure: %s not found", name)
}

if val.Primary.ID == "" {
return fmt.Errorf("Account Failure: ID was not set")
}

if owner := val.Primary.Attributes["owner"]; owner != "NativeLoader1111111111111111111111111111111" {
return fmt.Errorf("Account Info Failure: unexpected owner received (%s)", owner)
return fmt.Errorf("Account Failure: unexpected owner received (%s)", owner)
}

return nil
Expand Down
60 changes: 60 additions & 0 deletions solana/data_source_epoch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package solana

import (
"context"
"fmt"

"github.com/gagliardetto/solana-go/rpc"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceEpoch() *schema.Resource {
return &schema.Resource{
Read: dataSourceEpochRead,

Schema: map[string]*schema.Schema{
"absolute_slot": {
Type: schema.TypeInt,
Description: "The current absolute slot in the epoch",
Computed: true,
},
"block_height": {
Type: schema.TypeInt,
Description: "The current block height",
Computed: true,
},
"epoch": {
Type: schema.TypeInt,
Description: "The current epoch count",
Computed: true,
},
"slot_index": {
Type: schema.TypeInt,
Description: "The current slot relative to the start of the current epoch",
Computed: true,
},
"slots_in_epoch": {
Type: schema.TypeInt,
Description: "The number of slots in this epoch",
Computed: true,
},
},
}
}

func dataSourceEpochRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*providerConfig).rpcClient

res, err := client.GetEpochInfo(context.Background(), rpc.CommitmentRecent)
if err != nil {
return err
}

d.SetId(fmt.Sprintf("epoch:%d", uint64(res.Epoch)))
d.Set("absolute_slot", uint64(res.AbsoluteSlot))
d.Set("block_height", uint64(res.TransactionCount))
d.Set("epoch", uint64(res.Epoch))
d.Set("slot_index", uint64(res.SlotIndex))
d.Set("slots_in_epoch", uint64(res.SlotsInEpoch))
return nil
}
48 changes: 48 additions & 0 deletions solana/data_source_epoch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package solana

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

const (
testEpocDataConfig = `
provider "solana" {
endpoint = "https://api.testnet.solana.com"
}
data "solana_epoch" "test" {}
`
)

func TestAccEpocDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testEpocDataConfig,
Check: resource.ComposeTestCheckFunc(
testEpocDataSucceeds("data.solana_epoch.test"),
),
},
},
})
}

func testEpocDataSucceeds(name string) resource.TestCheckFunc {
return func(state *terraform.State) error {
val, ok := state.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Epoch Failure: %s not found", name)
}

if val.Primary.ID == "" {
return fmt.Errorf("Epoch Failure: ID was not set")
}

return nil
}
}
5 changes: 3 additions & 2 deletions solana/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ func New() *schema.Provider {
ResourcesMap: map[string]*schema.Resource{},

DataSourcesMap: map[string]*schema.Resource{
"solana_account_info": dataSourceAccountInfo(),
"solana_balance": dataSourceBalance(),
"solana_account": dataSourceAccount(),
"solana_balance": dataSourceBalance(),
"solana_epoch": dataSourceEpoch(),
},
}
}
Expand Down

0 comments on commit f81985e

Please sign in to comment.