Skip to content
This repository was archived by the owner on Oct 6, 2024. It is now read-only.

Commit 5cd9842

Browse files
committed
feat: add nodeinfo data source
1 parent 20d5a26 commit 5cd9842

File tree

7 files changed

+223
-3
lines changed

7 files changed

+223
-3
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
terraform {
3+
required_version = "~>1.4"
4+
5+
required_providers {
6+
turing-pi-bmc = {
7+
source = "jlec.de/dev/turing-pi-bmc"
8+
version = ">0"
9+
}
10+
}
11+
}
12+
13+
provider "turing-pi-bmc" {
14+
endpoint = "10.100.100.231"
15+
}
16+
17+
data "turing-pi-bmc_nodeinfo" "example" {
18+
}
19+
20+
output "nodeinfo" {
21+
value = data.turing-pi-bmc_nodeinfo.example
22+
}

internal/api/model.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,14 @@ type SDCard struct {
99
type SDCardResponse struct {
1010
Response []SDCard `json:"response"`
1111
}
12+
13+
type NodeInfo struct {
14+
Node1 string `json:"node1"`
15+
Node2 string `json:"node2"`
16+
Node3 string `json:"node3"`
17+
Node4 string `json:"node4"`
18+
}
19+
20+
type NodeInfoResponse struct {
21+
Response []NodeInfo `json:"response"`
22+
}

internal/api/nodeinfo.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package turingpi
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
// GetNodeInfo - Returns Turing Pi BMC SD card status.
10+
func (c *Client) GetNodeInfo() (NodeInfo, error) {
11+
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s?opt=get&type=nodeinfo", c.ApiURI), nil)
12+
if err != nil {
13+
return NodeInfo{}, err
14+
}
15+
16+
body, err := c.doRequest(req)
17+
if err != nil {
18+
return NodeInfo{}, err
19+
}
20+
21+
sdCardResponse := NodeInfoResponse{}
22+
err = json.Unmarshal(body, &sdCardResponse)
23+
24+
if err != nil {
25+
return NodeInfo{}, err
26+
}
27+
28+
return sdCardResponse.Response[0], nil
29+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/datasource"
8+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/hashicorp/terraform-plugin-log/tflog"
11+
turingpi "github.com/jlec/terraform-provider-turing-pi-bmc/internal/api"
12+
)
13+
14+
// Ensure provider defined types fully satisfy framework interfaces.
15+
// Ensure the implementation satisfies the expected interfaces.
16+
var (
17+
_ datasource.DataSource = &nodeInfoDataSource{}
18+
_ datasource.DataSourceWithConfigure = &nodeInfoDataSource{}
19+
)
20+
21+
func NewnodeInfoDataSource() datasource.DataSource {
22+
return &nodeInfoDataSource{}
23+
}
24+
25+
// NewNodeInfoDataSource is a helper function to simplify the provider implementation.
26+
func NewNodeInfoDataSource() datasource.DataSource {
27+
return &nodeInfoDataSource{}
28+
}
29+
30+
// nodeInfoDataSource defines the data source implementation.
31+
type nodeInfoDataSource struct {
32+
client *turingpi.Client
33+
}
34+
35+
// nodeInfoDataSourceModel describes the data source data model.
36+
type nodeInfoDataSourceModel struct {
37+
ID types.String `tfsdk:"id"`
38+
Node1 types.String `tfsdk:"node1"`
39+
Node2 types.String `tfsdk:"node2"`
40+
Node3 types.String `tfsdk:"node3"`
41+
Node4 types.String `tfsdk:"node4"`
42+
}
43+
44+
func (d *nodeInfoDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
45+
resp.TypeName = req.ProviderTypeName + "_nodeinfo"
46+
}
47+
48+
func (d *nodeInfoDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
49+
resp.Schema = schema.Schema{
50+
// This description is used by the documentation generator and the language server.
51+
MarkdownDescription: "Turing PI NodeInfo Data Source",
52+
53+
Attributes: map[string]schema.Attribute{
54+
"id": schema.StringAttribute{
55+
Description: "ID",
56+
Computed: true,
57+
},
58+
"node1": schema.StringAttribute{
59+
MarkdownDescription: "Information for Node 1",
60+
Computed: true,
61+
},
62+
"node2": schema.StringAttribute{
63+
MarkdownDescription: "Information for Node 2",
64+
Computed: true,
65+
},
66+
"node3": schema.StringAttribute{
67+
MarkdownDescription: "Information for Node 3",
68+
Computed: true,
69+
},
70+
"node4": schema.StringAttribute{
71+
MarkdownDescription: "Information for Node 4",
72+
Computed: true,
73+
},
74+
},
75+
}
76+
}
77+
78+
// FIXME: RO attribute.
79+
func (d *nodeInfoDataSource) Configure(
80+
ctx context.Context,
81+
req datasource.ConfigureRequest,
82+
resp *datasource.ConfigureResponse,
83+
) {
84+
var ok bool
85+
// Prevent panic if the provider has not been configured.
86+
if req.ProviderData == nil {
87+
return
88+
}
89+
90+
d.client, ok = req.ProviderData.(*turingpi.Client)
91+
if !ok {
92+
resp.Diagnostics.AddError("Client Error", "failed to get client")
93+
}
94+
}
95+
96+
func (d *nodeInfoDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
97+
var data nodeInfoDataSourceModel
98+
99+
// Read Terraform configuration data into the model
100+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
101+
102+
if resp.Diagnostics.HasError() {
103+
return
104+
}
105+
106+
// If applicable, this is a great opportunity to initialize any necessary
107+
// provider client data and make a call using it.
108+
nodeInfo, err := d.client.GetNodeInfo()
109+
if err != nil {
110+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read nodeinfo data, got error: %s", err))
111+
112+
return
113+
}
114+
115+
// For the purposes of this example code, hardcoding a response value to
116+
// save into the Terraform state.
117+
data.ID = types.StringValue("nodeinfo")
118+
data.Node1 = types.StringValue(nodeInfo.Node1)
119+
data.Node2 = types.StringValue(nodeInfo.Node2)
120+
data.Node3 = types.StringValue(nodeInfo.Node3)
121+
data.Node4 = types.StringValue(nodeInfo.Node4)
122+
123+
// Write logs using the tflog package
124+
// Documentation: https://terraform.io/plugin/log
125+
tflog.Trace(ctx, "read a data source")
126+
127+
// Save data into Terraform state
128+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
129+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package provider_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
)
8+
9+
const testAccNodeInfoDataSourceConfig = `
10+
data "turing-pi-bmc_nodeinfo" "test" {
11+
}
12+
`
13+
14+
func TestAccNodeInfoDataSource(t *testing.T) {
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheck(t) },
17+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
18+
Steps: []resource.TestStep{
19+
// Read testing
20+
{
21+
Config: providerConfig + testAccNodeInfoDataSourceConfig,
22+
Check: resource.ComposeAggregateTestCheckFunc(
23+
resource.TestCheckResourceAttr("data.turing-pi-bmc_nodeinfo.test", "node1", "unknown"),
24+
),
25+
},
26+
},
27+
})
28+
}

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func (p *TuringPiBMCProvider) Resources(ctx context.Context) []func() resource.R
8484
func (p *TuringPiBMCProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
8585
return []func() datasource.DataSource{
8686
NewSDCardDataSource,
87+
NewNodeInfoDataSource,
8788
}
8889
}
8990

internal/provider/sdcard_data_source_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import (
66
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
77
)
88

9-
const testAccSdCardDataSourceConfig = `
9+
const testAccSDCardDataSourceConfig = `
1010
data "turing-pi-bmc_sdcard" "test" {
1111
}
1212
`
1313

14-
func TestAccSdCardDataSource(t *testing.T) {
14+
func TestAccSDCardDataSource(t *testing.T) {
1515
resource.Test(t, resource.TestCase{
1616
PreCheck: func() { testAccPreCheck(t) },
1717
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
1818
Steps: []resource.TestStep{
1919
// Read testing
2020
{
21-
Config: providerConfig + testAccSdCardDataSourceConfig,
21+
Config: providerConfig + testAccSDCardDataSourceConfig,
2222
Check: resource.ComposeAggregateTestCheckFunc(
2323
resource.TestCheckResourceAttr("data.turing-pi-bmc_sdcard.test", "total", "0"),
2424
),

0 commit comments

Comments
 (0)