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

Commit f34002b

Browse files
committed
docs: starting
1 parent fb02ba0 commit f34002b

File tree

11 files changed

+249
-39
lines changed

11 files changed

+249
-39
lines changed

Taskfile.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ tasks:
186186
cmds:
187187
- go get github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
188188
- go generate ./...
189+
- cmd: pre-commit run -a end-of-file-fixer
190+
ignore_error: true
191+
- cmd: pre-commit run -a trailing-whitespace
192+
ignore_error: true
193+
- cmd: pre-commit run -a terraform_docs
194+
ignore_error: true
189195

190196
#############################################################################
191197
#

deployment/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@
33
TF provider for Turing PI BMC
44

55
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
6+
## Requirements
7+
8+
| Name | Version |
9+
|------|---------|
10+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.4.0 |
11+
12+
## Providers
13+
14+
No providers.
15+
16+
## Modules
17+
18+
No modules.
19+
20+
## Resources
21+
22+
No resources.
23+
24+
## Inputs
25+
26+
No inputs.
27+
28+
## Outputs
29+
30+
No outputs.
631
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
732

833
## License

docs/data-sources/usb.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ Turing PI Usb Data Source
1818
### Read-Only
1919

2020
- `id` (String) ID
21-
- `mode` (Number) USB mode
21+
- `mode` (String) USB mode
2222
- `node` (Number) Node using USB

docs/index.md

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
11
---
22
# generated by https://github.com/hashicorp/terraform-plugin-docs
3-
page_title: "turing-pi-bmc Provider"
4-
subcategory: ""
3+
page_title: "Provider: TURING-PI-BMC"
54
description: |-
6-
5+
The turing-pi-bmc provider provides direct access to the TuringPi 2 BMC API.
76
---
87

9-
# turing-pi-bmc Provider
8+
# TURING-PI-BMC Provider
9+
10+
This providers implements the Turing Pi 2 BMC API.
11+
12+
[help.turingpi.com/hc/en-us/articles/8795098568477-BMC-API](https://help.turingpi.com/hc/en-us/articles/8795098568477-BMC-API)
1013

14+
Use the navigation to the left to read about the available resources.
15+
16+
## Example Provider Usage
17+
18+
```terraform
19+
terraform {
20+
required_version = "~>1.4"
21+
22+
required_providers {
23+
turing-pi-bmc = {
24+
source = "jlec.de/dev/turing-pi-bmc"
25+
version = ">0"
26+
}
27+
}
28+
}
1129
30+
provider "turing-pi-bmc" {
31+
endpoint = "turingpi"
32+
}
33+
```
1234

13-
## Example Usage
35+
### Using the data source
1436

1537
```terraform
1638
terraform {
@@ -27,6 +49,35 @@ terraform {
2749
provider "turing-pi-bmc" {
2850
endpoint = "turingpi"
2951
}
52+
53+
data "turing-pi-bmc_nodeinfo" "example" {
54+
}
55+
56+
output "nodeinfo" {
57+
value = data.turing-pi-bmc_nodeinfo.example
58+
}
59+
```
60+
61+
### Using the USB resource
62+
63+
```terraform
64+
terraform {
65+
required_providers {
66+
turing-pi-bmc = {
67+
source = "jlec.de/dev/turing-pi-bmc"
68+
version = ">0"
69+
}
70+
}
71+
}
72+
73+
provider "turing-pi-bmc" {
74+
endpoint = "turingpi"
75+
}
76+
77+
resource "turing-pi-bmc_usb" "example" {
78+
node = 4
79+
mode = 0
80+
}
3081
```
3182

3283
<!-- schema generated by tfplugindocs -->
@@ -35,3 +86,7 @@ provider "turing-pi-bmc" {
3586
### Required
3687

3788
- `endpoint` (String) Turing Pi BMC endpoint
89+
90+
## Limitations
91+
92+
None so far

docs/resources/usb.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,43 @@
33
page_title: "turing-pi-bmc_usb Resource - terraform-provider-turing-pi-bmc"
44
subcategory: ""
55
description: |-
6-
Turing PI Usb Resource
6+
Interface to the USB CM4 port in the Turing PI 2 board. It allows switching between the host and device mode as well as mapping to one of the 4 nodes.
77
---
88

99
# turing-pi-bmc_usb (Resource)
1010

11-
Turing PI Usb Resource
11+
Interface to the USB CM4 port in the Turing PI 2 board. It allows switching between the host and device mode as well as mapping to one of the 4 nodes.
1212

13+
## Example Usage
1314

15+
```terraform
16+
terraform {
17+
required_providers {
18+
turing-pi-bmc = {
19+
source = "jlec.de/dev/turing-pi-bmc"
20+
version = ">0"
21+
}
22+
}
23+
}
24+
25+
provider "turing-pi-bmc" {
26+
endpoint = "turingpi"
27+
}
28+
29+
resource "turing-pi-bmc_usb" "example" {
30+
node = 4
31+
mode = 0
32+
}
33+
```
1434

1535
<!-- schema generated by tfplugindocs -->
1636
## Schema
1737

1838
### Required
1939

20-
- `mode` (Number) USB mode
21-
- `node` (Number) Node using USB
40+
- `mode` (String) USB port mode ('host' or 'device')
41+
- `node` (Number) Node which USB port is mapped to
2242

2343
### Read-Only
2444

25-
- `id` (String) Identifier
45+
- `id` (String) Unique identifier for this resource

internal/api/usb.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,25 @@ func (c *Client) SetUsb(mode, node int64) (Result, error) {
4242

4343
return response.Response[0], nil
4444
}
45+
46+
func ApiToMode(mode int64) (string, error) {
47+
switch mode {
48+
case 0:
49+
return "host", nil
50+
case 1:
51+
return "device", nil
52+
default:
53+
return "", fmt.Errorf("'%d' not a valid mode", mode)
54+
}
55+
}
56+
57+
func ModeToApi(mode string) (int64, error) {
58+
switch mode {
59+
case "host":
60+
return 0, nil
61+
case "node":
62+
return 1, nil
63+
default:
64+
return -1, fmt.Errorf("'%s' not a valid mode", mode)
65+
}
66+
}

internal/provider/usb_data_source.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type usbDataSource struct {
3131
// usbDataSourceModel describes the data source data model.
3232
type usbDataSourceModel struct {
3333
ID types.String `tfsdk:"id"`
34-
Mode types.Int64 `tfsdk:"mode"`
34+
Mode types.String `tfsdk:"mode"`
3535
Node types.Int64 `tfsdk:"node"`
3636
}
3737

@@ -49,7 +49,7 @@ func (d *usbDataSource) Schema(ctx context.Context, req datasource.SchemaRequest
4949
Description: "ID",
5050
Computed: true,
5151
},
52-
"mode": schema.Int64Attribute{
52+
"mode": schema.StringAttribute{
5353
MarkdownDescription: "USB mode",
5454
Computed: true,
5555
},
@@ -100,8 +100,15 @@ func (d *usbDataSource) Read(ctx context.Context, req datasource.ReadRequest, re
100100

101101
// For the purposes of this example code, hardcoding a response value to
102102
// save into the Terraform state.
103+
mode, err := turingpi.ApiToMode(usb.Mode)
104+
if err != nil {
105+
resp.Diagnostics.AddError("Api Error", fmt.Sprintf("Unable to convert API response, got error: %s", err))
106+
107+
return
108+
}
109+
103110
data.ID = types.StringValue("usb")
104-
data.Mode = types.Int64Value(usb.Mode)
111+
data.Mode = types.StringValue(mode)
105112
data.Node = types.Int64Value(usb.Node)
106113

107114
// Write logs using the tflog package

internal/provider/usb_resource.go

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
8+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
89
"github.com/hashicorp/terraform-plugin-framework/path"
910
"github.com/hashicorp/terraform-plugin-framework/resource"
1011
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -47,25 +48,23 @@ func (r *usbResource) Metadata(ctx context.Context, req resource.MetadataRequest
4748
func (r *usbResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
4849
resp.Schema = schema.Schema{
4950
// This description is used by the documentation generator and the language server.
50-
MarkdownDescription: "Turing PI Usb Resource",
51+
MarkdownDescription: "Interface to the USB CM4 port in the Turing PI 2 board. It allows switching between the host " +
52+
"and device mode as well as mapping to one of the 4 nodes.",
5153

5254
Attributes: map[string]schema.Attribute{
5355
"id": schema.StringAttribute{
5456
Computed: true,
55-
MarkdownDescription: "Identifier",
56-
// PlanModifiers: []planmodifier.String{
57-
// stringplanmodifier.UseStateForUnknown(),
58-
// },
57+
MarkdownDescription: "Unique identifier for this resource",
5958
},
60-
"mode": schema.Int64Attribute{
61-
MarkdownDescription: "USB mode",
59+
"mode": schema.StringAttribute{
60+
MarkdownDescription: "USB port mode ('host' or 'device')",
6261
Required: true,
63-
Validators: []validator.Int64{
64-
int64validator.OneOf(0, 1),
62+
Validators: []validator.String{
63+
stringvalidator.OneOf("host", "device"),
6564
},
6665
},
6766
"node": schema.Int64Attribute{
68-
MarkdownDescription: "Node using USB",
67+
MarkdownDescription: "Node which USB port is mapped to",
6968
Required: true,
7069
Validators: []validator.Int64{
7170
int64validator.Between(1, NodeCount),
@@ -99,15 +98,22 @@ func (r *usbResource) Create(ctx context.Context, req resource.CreateRequest, re
9998
return
10099
}
101100

102-
_, err := r.client.SetUsb(plan.Mode.ValueInt64(), plan.Node.ValueInt64())
101+
mode, err := turingpi.ModeToApi(plan.Mode.ValueString())
102+
if err != nil {
103+
resp.Diagnostics.AddError("Api Error", fmt.Sprintf("Unable to convert API response, got error: %s", err))
104+
105+
return
106+
}
107+
108+
_, err = r.client.SetUsb(mode, plan.Node.ValueInt64())
103109
if err != nil {
104110
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read usb data, got error: %s", err))
105111

106112
return
107113
}
108114

109115
plan.ID = types.StringValue("usb")
110-
plan.Mode = types.Int64Value(plan.Mode.ValueInt64())
116+
plan.Mode = types.StringValue(plan.Mode.ValueString())
111117
plan.Node = types.Int64Value(plan.Node.ValueInt64())
112118

113119
// Write logs using the tflog package
@@ -136,8 +142,15 @@ func (r *usbResource) Read(ctx context.Context, req resource.ReadRequest, resp *
136142
return
137143
}
138144

145+
mode, err := turingpi.ApiToMode(usb.Mode)
146+
if err != nil {
147+
resp.Diagnostics.AddError("Api Error", fmt.Sprintf("Unable to convert API response, got error: %s", err))
148+
149+
return
150+
}
151+
139152
plan.ID = types.StringValue("usb")
140-
plan.Mode = types.Int64Value(usb.Mode)
153+
plan.Mode = types.StringValue(mode)
141154
plan.Node = types.Int64Value(usb.Node)
142155

143156
// Write logs using the tflog package
@@ -158,7 +171,14 @@ func (r *usbResource) Update(ctx context.Context, req resource.UpdateRequest, re
158171
return
159172
}
160173

161-
_, err := r.client.SetUsb(plan.Mode.ValueInt64(), plan.Node.ValueInt64())
174+
mode, err := turingpi.ModeToApi(plan.Mode.ValueString())
175+
if err != nil {
176+
resp.Diagnostics.AddError("Api Error", fmt.Sprintf("Unable to convert API response, got error: %s", err))
177+
178+
return
179+
}
180+
181+
_, err = r.client.SetUsb(mode, plan.Node.ValueInt64())
162182
if err != nil {
163183
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to set usb data, got error: %s", err))
164184

@@ -172,8 +192,15 @@ func (r *usbResource) Update(ctx context.Context, req resource.UpdateRequest, re
172192
return
173193
}
174194

195+
modeAPi, err := turingpi.ApiToMode(usb.Mode)
196+
if err != nil {
197+
resp.Diagnostics.AddError("Api Error", fmt.Sprintf("Unable to convert API response, got error: %s", err))
198+
199+
return
200+
}
201+
175202
plan.ID = types.StringValue("usb")
176-
plan.Mode = types.Int64Value(usb.Mode)
203+
plan.Mode = types.StringValue(modeAPi)
177204
plan.Node = types.Int64Value(usb.Node)
178205

179206
// Save updated data into Terraform state

0 commit comments

Comments
 (0)