Skip to content

Commit de46bb8

Browse files
authored
Merge pull request #65 from ddelnano/ddelnano/sohonetlabs/iso-sr
Allow ISO Storage repository to fallback to default SR if unspecified
2 parents bb64b86 + 3c5fc29 commit de46bb8

2 files changed

Lines changed: 37 additions & 24 deletions

File tree

builder/xenserver/common/common_config.go

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type CommonConfig struct {
2020
VMName string `mapstructure:"vm_name"`
2121
VMDescription string `mapstructure:"vm_description"`
2222
SrName string `mapstructure:"sr_name"`
23-
SrISOName string `mapstructure:"sr_iso_name"`
23+
SrISOName string `mapstructure:"sr_iso_name" required:"false"`
2424
FloppyFiles []string `mapstructure:"floppy_files"`
2525
NetworkNames []string `mapstructure:"network_names"`
2626
ExportNetworkNames []string `mapstructure:"export_network_names"`
@@ -223,29 +223,11 @@ func (c CommonConfig) ShouldKeepVM(state multistep.StateBag) bool {
223223
}
224224

225225
func (config CommonConfig) GetSR(c *Connection) (xenapi.SRRef, error) {
226-
var srRef xenapi.SRRef
227226
if config.SrName == "" {
228-
hostRef, err := c.GetClient().Session.GetThisHost(c.session, c.session)
229-
230-
if err != nil {
231-
return srRef, err
232-
}
233-
234-
pools, err := c.GetClient().Pool.GetAllRecords(c.session)
235-
236-
if err != nil {
237-
return srRef, err
238-
}
239-
240-
for _, pool := range pools {
241-
if pool.Master == hostRef {
242-
return pool.DefaultSR, nil
243-
}
244-
}
245-
246-
return srRef, errors.New(fmt.Sprintf("failed to find default SR on host '%s'", hostRef))
247-
227+
return getDefaultSR(c)
248228
} else {
229+
var srRef xenapi.SRRef
230+
249231
// Use the provided name label to find the SR to use
250232
srs, err := c.GetClient().SR.GetByNameLabel(c.session, config.SrName)
251233

@@ -267,7 +249,7 @@ func (config CommonConfig) GetSR(c *Connection) (xenapi.SRRef, error) {
267249
func (config CommonConfig) GetISOSR(c *Connection) (xenapi.SRRef, error) {
268250
var srRef xenapi.SRRef
269251
if config.SrISOName == "" {
270-
return srRef, errors.New("sr_iso_name must be specified in the packer configuration")
252+
return getDefaultSR(c)
271253

272254
} else {
273255
// Use the provided name label to find the SR to use
@@ -287,3 +269,34 @@ func (config CommonConfig) GetISOSR(c *Connection) (xenapi.SRRef, error) {
287269
return srs[0], nil
288270
}
289271
}
272+
273+
func getDefaultSR(c *Connection) (xenapi.SRRef, error) {
274+
var srRef xenapi.SRRef
275+
client := c.GetClient()
276+
hostRef, err := client.Session.GetThisHost(c.session, c.session)
277+
278+
if err != nil {
279+
return srRef, err
280+
}
281+
282+
// The current version of the go-xen-api-client does not fully support XenAPI version 8.2
283+
// In particular, some values for the pool `allowed_operations` are not recognised, resulting
284+
// in a parse error when retrieving pool records. As a workaround, we only fetch pool refs.
285+
pool_refs, err := client.Pool.GetAll(c.session)
286+
287+
if err != nil {
288+
return srRef, err
289+
}
290+
291+
for _, pool_ref := range pool_refs {
292+
pool_master, err := client.Pool.GetMaster(c.session, pool_ref)
293+
if err != nil {
294+
return srRef, err
295+
}
296+
if pool_master == hostRef {
297+
return client.Pool.GetDefaultSR(c.session, pool_ref)
298+
}
299+
}
300+
301+
return srRef, errors.New(fmt.Sprintf("failed to find default SR on host '%s'", hostRef))
302+
}

builder/xenserver/common/config.hcl2spec.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)