diff --git a/.goreleaser.yaml b/.goreleaser.yaml index eb8a99f..95d537c 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -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 diff --git a/Makefile b/Makefile index 54f9ef9..3600f84 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ clean: rm -rf vendor/ -test: vendor +test: TF_ACC=1 go test -v ./solana/... vendor: clean diff --git a/docs/data-sources/account_info.md b/docs/data-sources/account.md similarity index 85% rename from docs/data-sources/account_info.md rename to docs/data-sources/account.md index 64fa905..fc03025 100644 --- a/docs/data-sources/account_info.md +++ b/docs/data-sources/account.md @@ -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) diff --git a/docs/data-sources/epoch.md b/docs/data-sources/epoch.md new file mode 100644 index 0000000..34eba44 --- /dev/null +++ b/docs/data-sources/epoch.md @@ -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 + +### 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 + + diff --git a/solana/data_source_account_info.go b/solana/data_source_account.go similarity index 87% rename from solana/data_source_account_info.go rename to solana/data_source_account.go index 62819a6..e933279 100644 --- a/solana/data_source_account_info.go +++ b/solana/data_source_account.go @@ -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": { @@ -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) @@ -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) diff --git a/solana/data_source_account_info_test.go b/solana/data_source_account_test.go similarity index 59% rename from solana/data_source_account_info_test.go rename to solana/data_source_account_test.go index 832e3f2..9dc96dc 100644 --- a/solana/data_source_account_info_test.go +++ b/solana/data_source_account_test.go @@ -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 diff --git a/solana/data_source_epoch.go b/solana/data_source_epoch.go new file mode 100644 index 0000000..43d5ce4 --- /dev/null +++ b/solana/data_source_epoch.go @@ -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 +} diff --git a/solana/data_source_epoch_test.go b/solana/data_source_epoch_test.go new file mode 100644 index 0000000..228719a --- /dev/null +++ b/solana/data_source_epoch_test.go @@ -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 + } +} diff --git a/solana/provider.go b/solana/provider.go index 81145d1..d429c4e 100644 --- a/solana/provider.go +++ b/solana/provider.go @@ -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(), }, } }