Skip to content

Commit cda48bd

Browse files
authored
Merge pull request #63 from ddelnano/ddelnano/sohonetlabs/iso-sr
Allow ISO Storage repository to fallback to default SR if unspecified
2 parents b55b096 + ffc0ffb commit cda48bd

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

builder/xenserver/common/common_config.go

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)