-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move qemu serial to new package
- Loading branch information
1 parent
a8acb26
commit 2aff1cc
Showing
4 changed files
with
104 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package serial | ||
|
||
import ( | ||
pveAPI "github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/hashicorp/go-cty/cty" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
const ( | ||
Root string = "serial" | ||
|
||
schemaID string = "id" | ||
schemaType string = "type" | ||
|
||
valueSocket string = "socket" | ||
) | ||
|
||
func Schema() *schema.Schema { | ||
return &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
schemaID: { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ValidateDiagFunc: func(i interface{}, k cty.Path) diag.Diagnostics { | ||
v := i.(int) | ||
if err := pveAPI.SerialID(v).Validate(); err != nil { | ||
return diag.Errorf(Root+" "+schemaID+" must be between 0 and 3, got: %d", v) | ||
} | ||
return nil | ||
}}, | ||
schemaType: { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: valueSocket, | ||
ValidateDiagFunc: func(i interface{}, k cty.Path) diag.Diagnostics { | ||
v := i.(string) | ||
if v == valueSocket { | ||
return nil | ||
} | ||
if err := pveAPI.SerialPath(v).Validate(); err != nil { | ||
return diag.Errorf(Root+" "+schemaType+" must be '"+valueSocket+"' or match the following regex `/dev/.+`, got: %s", v) | ||
} | ||
return nil | ||
}}}}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package serial | ||
|
||
import ( | ||
pveAPI "github.com/Telmate/proxmox-api-go/proxmox" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func SDK(d *schema.ResourceData) pveAPI.SerialInterfaces { | ||
serials := pveAPI.SerialInterfaces{ | ||
pveAPI.SerialID0: pveAPI.SerialInterface{Delete: true}, | ||
pveAPI.SerialID1: pveAPI.SerialInterface{Delete: true}, | ||
pveAPI.SerialID2: pveAPI.SerialInterface{Delete: true}, | ||
pveAPI.SerialID3: pveAPI.SerialInterface{Delete: true}} | ||
serialsMap := d.Get(Root).(*schema.Set) | ||
for _, serial := range serialsMap.List() { | ||
serialMap := serial.(map[string]interface{}) | ||
newSerial := pveAPI.SerialInterface{Delete: false} | ||
serialType := serialMap[schemaType].(string) | ||
if serialType == valueSocket { | ||
newSerial.Socket = true | ||
} else { | ||
newSerial.Path = pveAPI.SerialPath(serialType) | ||
} | ||
serials[pveAPI.SerialID(serialMap[schemaID].(int))] = newSerial | ||
} | ||
return serials | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package serial | ||
|
||
import ( | ||
pveAPI "github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func Terraform(config pveAPI.SerialInterfaces, d *schema.ResourceData) { | ||
var index int | ||
serials := make([]interface{}, len(config)) | ||
for i, e := range config { | ||
localMap := map[string]interface{}{schemaID: int(i)} | ||
if e.Socket { | ||
localMap[schemaType] = valueSocket | ||
} else { | ||
localMap[schemaType] = string(e.Path) | ||
} | ||
serials[index] = localMap | ||
index++ | ||
} | ||
d.Set(Root, serials) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters