Skip to content

Commit ffff9f0

Browse files
committed
fix: imported xenorchestra_bonded_network is missing some fields #346
1 parent 52b6b67 commit ffff9f0

File tree

5 files changed

+77
-4
lines changed

5 files changed

+77
-4
lines changed

docs/data-sources/pif.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ resource "xenorchestra_vm" "demo-vm" {
4747
### Read-Only
4848

4949
- `attached` (Boolean) If the PIF is attached to the network.
50+
- `bond_master` (String) In case of a bond slave, the uuid of the bond master.
51+
- `bond_slaves` (List of String) In case of a bond master, the pifs (uuid) that are used for this bond.
5052
- `host` (String) The host the PIF is associated with.
5153
- `id` (String) The ID of this resource.
54+
- `is_bond_master` (Boolean) True if this PIF is a bond master.
55+
- `is_bond_slave` (Boolean) True if this PIF is a bond slave.
5256
- `network` (String) The network the PIF is associated with.
5357
- `pool_id` (String) The pool the PIF is associated with.
5458
- `uuid` (String) The uuid of the PIF.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ toolchain go1.24.3
77
require (
88
github.com/hashicorp/terraform-plugin-docs v0.18.0
99
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
10-
github.com/vatesfr/xenorchestra-go-sdk v1.1.0
10+
github.com/vatesfr/xenorchestra-go-sdk v1.1.1-0.20250519134506-c1a6ccdd55cb
1111
)
1212

1313
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
184184
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
185185
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
186186
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
187-
github.com/vatesfr/xenorchestra-go-sdk v1.1.0 h1:O7VbvBQ1rogzI5Xv8Me88CrRUGwrK/vMXT2CUmCToK0=
188-
github.com/vatesfr/xenorchestra-go-sdk v1.1.0/go.mod h1:3679gtr7PLytF3C9/QeF/llA0L5ZRvXfaP1oWtk7Hf4=
187+
github.com/vatesfr/xenorchestra-go-sdk v1.1.1-0.20250519134506-c1a6ccdd55cb h1:qHW07vFNY5N6YzfzlPMl3MOlWWykUFnt8ZFZaA8Yd80=
188+
github.com/vatesfr/xenorchestra-go-sdk v1.1.1-0.20250519134506-c1a6ccdd55cb/go.mod h1:3679gtr7PLytF3C9/QeF/llA0L5ZRvXfaP1oWtk7Hf4=
189189
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
190190
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
191191
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=

xoa/data_source_xenorchestra_pif.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ Ensure that your device, vlan, host_id and other arguments identify a unique PIF
5757
Required: true,
5858
Description: "The VLAN the PIF belongs to.",
5959
},
60+
"is_bond_master": &schema.Schema{
61+
Type: schema.TypeBool,
62+
Computed: true,
63+
Description: "True if this PIF is a bond master.",
64+
},
65+
"bond_slaves": &schema.Schema{
66+
Type: schema.TypeList,
67+
Elem: &schema.Schema{
68+
Type: schema.TypeString,
69+
},
70+
Computed: true,
71+
Description: "In case of a bond master, the pifs (uuid) that are used for this bond.",
72+
},
73+
"is_bond_slave": &schema.Schema{
74+
Type: schema.TypeBool,
75+
Computed: true,
76+
Description: "True if this PIF is a bond slave.",
77+
},
78+
"bond_master": &schema.Schema{
79+
Type: schema.TypeString,
80+
Computed: true,
81+
Description: "In case of a bond slave, the uuid of the bond master.",
82+
},
6083
},
6184
}
6285
}
@@ -95,5 +118,9 @@ func dataSourcePIFRead(d *schema.ResourceData, m interface{}) error {
95118
d.Set("pool_id", pif.PoolId)
96119
d.Set("network", pif.Network)
97120
d.Set("vlan", pif.Vlan)
121+
d.Set("bond_slaves", pif.BondSlaves)
122+
d.Set("is_bond_master", pif.IsBondMaster)
123+
d.Set("is_bond_slave", pif.IsBondSlave)
124+
d.Set("bond_master", pif.BondMaster)
98125
return nil
99126
}

xoa/resource_xenorchestra_bonded_network.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func resourceXoaBondedNetwork() *schema.Resource {
1818
Read: resourceBondedNetworkRead,
1919
Update: resourceBondedNetworkUpdate,
2020
Importer: &schema.ResourceImporter{
21-
State: schema.ImportStatePassthrough,
21+
State: resourceBondedNetworkImport,
2222
},
2323
Schema: map[string]*schema.Schema{
2424
"automatic": &schema.Schema{
@@ -49,12 +49,14 @@ func resourceXoaBondedNetwork() *schema.Resource {
4949
},
5050
Optional: true,
5151
ForceNew: true,
52+
Computed: true,
5253
Description: "The pifs (uuid) that should be used for this network.",
5354
},
5455
"bond_mode": &schema.Schema{
5556
Type: schema.TypeString,
5657
Optional: true,
5758
ForceNew: true,
59+
Computed: true,
5860
Description: "The bond mode that should be used for this network.",
5961
ValidateFunc: validation.StringInSlice(validBondModes, false),
6062
},
@@ -159,6 +161,46 @@ func resourceBondedNetworkDelete(d *schema.ResourceData, m interface{}) error {
159161
return nil
160162
}
161163

164+
// Custom importer to populate pif_ids from BondSlaves of the network's main PIF
165+
func resourceBondedNetworkImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
166+
c := m.(client.XOClient)
167+
network, err := c.GetNetwork(client.Network{Id: d.Id()})
168+
if err != nil {
169+
return nil, err
170+
}
171+
172+
if len(network.PIFs) < 1 {
173+
return nil, errors.New("network should contain one PIF")
174+
}
175+
176+
// Get the bonded pifs and bond mode from the master pif
177+
for _, pifID := range network.PIFs {
178+
bondPifs, err := c.GetPIF(client.PIF{Id: pifID})
179+
if err != nil {
180+
return nil, err
181+
}
182+
if len(bondPifs) < 1 {
183+
return nil, errors.New("no PIF returned for ID: %s" + pifID)
184+
}
185+
if bondPifs[0].IsBondMaster {
186+
if err := d.Set("pif_ids", bondPifs[0].BondSlaves); err != nil {
187+
return nil, err
188+
}
189+
bond, err := c.GetBond(client.Bond{Master: bondPifs[0].Id})
190+
if err != nil {
191+
return nil, err
192+
}
193+
d.Set("bond_mode", bond.Mode)
194+
break
195+
}
196+
}
197+
198+
if err := bondedNetworkToData(network, d); err != nil {
199+
return nil, err
200+
}
201+
return []*schema.ResourceData{d}, nil
202+
}
203+
162204
func bondedNetworkToData(network *client.Network, d *schema.ResourceData) error {
163205
d.SetId(network.Id)
164206
if err := d.Set("name_label", network.NameLabel); err != nil {

0 commit comments

Comments
 (0)