380
380
class PromptingExpectChild(FakeExpectChild):
381
"""A fake ExpectChild based on prompt/response.
383
It accepts an iterator of prompts. If that iterator supports send(),
384
the last input to sendline will be sent.
386
This allows fairly natural generators, e.g.:
388
foo = yield "Please give me foo".
390
You can also just iterate through prompts and retrieve the corresponding
391
values from self.values at the end.
382
394
def __init__(self, backend, juju_home, extra_env, prompts):
383
395
super(PromptingExpectChild, self).__init__(backend, juju_home,
385
self.prompts = iter(prompts)
397
self._prompts = iter(prompts)
400
# If not a generator, invoke next() instead of send.
401
self._send = getattr(self._prompts, 'send',
402
lambda x: next(self._prompts))
403
self._send_line = None
389
409
def expect(self, pattern):
390
410
if type(pattern) is not list:
391
411
pattern = [pattern]
393
prompt = next(self.prompts)
413
prompt = self._send(self._send_line)
414
self._send_line = None
394
415
except StopIteration:
395
416
if pexpect.EOF not in pattern:
413
434
super(PromptingExpectChild, self).expect(regex)
415
436
def sendline(self, line=''):
437
if self._send_line is not None:
438
raise ValueError('Sendline called twice with no expect.')
416
439
full_match = self.match.group(0)
417
440
self.values[full_match] = line.rstrip()
418
441
self.lines.append((full_match, line))
442
self._send_line = line
421
445
class RegisterHost(PromptingExpectChild):
490
514
def iter_prompts(self):
493
if self.values[self.TYPE] != 'bogus':
516
provider_type = yield self.TYPE
517
if provider_type != 'bogus':
496
yield self.name_prompt
497
if '/' not in self.values[self.name_prompt]:
520
name = yield self.name_prompt
499
if self.values[self.TYPE] == 'maas':
500
yield self.API_ENDPOINT
501
endpoint = self.values[self.API_ENDPOINT]
502
while len(endpoint) > 1000:
503
yield self.cant_validate(endpoint)
504
elif self.values[self.TYPE] == 'manual':
506
endpoint = self.values[self.HOST]
507
while len(endpoint) > 1000:
508
yield self.cant_validate(endpoint)
509
elif self.values[self.TYPE] == 'openstack':
510
yield self.CLOUD_ENDPOINT
511
endpoint = self.values[self.CLOUD_ENDPOINT]
523
if provider_type == 'maas':
524
endpoint = yield self.API_ENDPOINT
525
while len(endpoint) > 1000:
526
yield self.cant_validate(endpoint)
527
elif provider_type == 'manual':
528
endpoint = yield self.HOST
529
while len(endpoint) > 1000:
530
yield self.cant_validate(endpoint)
531
elif provider_type == 'openstack':
532
endpoint = yield self.CLOUD_ENDPOINT
512
533
while len(endpoint) > 1000:
513
534
yield self.cant_validate(endpoint)
516
if 'invalid' not in self.values[self.AUTH]:
536
auth = yield self.AUTH
537
if 'invalid' not in auth:
518
while self.values.get(self.ANOTHER_REGION) != 'n':
519
540
yield self.REGION_NAME
520
yield self.REGION_ENDPOINT
521
endpoint = self.values[self.REGION_ENDPOINT]
541
endpoint = yield self.REGION_ENDPOINT
522
542
if len(endpoint) > 1000:
523
543
yield self.cant_validate(endpoint)
524
yield self.ANOTHER_REGION
525
if self.values['Select cloud type:'] == 'vsphere':
526
yield self.CLOUD_ENDPOINT
527
endpoint = self.values[self.CLOUD_ENDPOINT]
544
if (yield self.ANOTHER_REGION) == 'n':
546
elif provider_type == 'vsphere':
547
endpoint = yield self.CLOUD_ENDPOINT
528
548
if len(endpoint) > 1000:
529
549
yield self.cant_validate(endpoint)
530
while self.values.get(self.ANOTHER_REGION) != 'n':
531
551
yield self.REGION_NAME
532
yield self.ANOTHER_REGION
552
if (yield self.ANOTHER_REGION) == 'n':