def _set_env(self, values): """Update attributes for the named environment""" if values.env_name == BRKT_HOSTED_ENV_NAME: raise ValidationError( 'Error: cannot modify environment ' + values.env_name) try: env = self.parsed_config.get_env(values.env_name) except UnknownEnvironmentError: env = brkt_cli.BracketEnvironment() opt_attr = { 'api': 'api', 'key': 'hsmproxy', 'public_api': 'public_api', 'network': 'network', } for k in opt_attr.iterkeys(): endpoint = k + '_server' endpoint = getattr(values, endpoint) if endpoint is None: continue try: parts = parse_endpoint(endpoint) except ValueError: raise ValidationError('Error: Invalid value for option --' + k + '-server') setattr(env, opt_attr[k] + '_host', parts['host']) setattr(env, opt_attr[k] + '_port', parts.get('port', 443)) if values.service_domain is not None: env = brkt_cli.brkt_env_from_domain(values.service_domain) self.parsed_config.set_env(values.env_name, env) self._write_config() return 0
def test_brkt_env_from_values(self): """ Test parsing BracketEnvironment from the --service-domain and --brkt-env command line options. """ # No values are set. self.assertIsNone(brkt_cli.brkt_env_from_values(DummyValues())) # Test --service-domain values = DummyValues() values.service_domain = 'example.com' brkt_env = brkt_cli.brkt_env_from_values(values) self.assertEqual( str(brkt_cli.brkt_env_from_domain('example.com')), str(brkt_env) ) # Test --brkt-env values = DummyValues() values.brkt_env = 'yetiapi.example.com:443,' + \ 'hsmproxy.example.com:443,network.example.com:443' brkt_env = brkt_cli.brkt_env_from_values(values) self.assertEqual( str(brkt_cli.parse_brkt_env(values.brkt_env)), str(brkt_env) )
def test_brkt_env_from_domain(self): be = brkt_cli.brkt_env_from_domain('example.com') self.assertEqual('yetiapi.example.com', be.api_host) self.assertEqual(443, be.api_port) self.assertEqual('hsmproxy.example.com', be.hsmproxy_host) self.assertEqual(443, be.hsmproxy_port) self.assertEqual('api.example.com', be.public_api_host) self.assertEqual(443, be.public_api_port)
def test_env_from_config(self): """ Test that we can use a custom environment that is stored in the config """ config = CLIConfig() env = brkt_cli.brkt_env_from_domain('foo.com') config.set_env('test', env) config.set_current_env('test') for mode in (INSTANCE_CREATOR_MODE, INSTANCE_UPDATER_MODE): brkt_config = _get_brkt_config_for_cli_args( mode=mode, cli_config=config) for attr in ('api', 'hsmproxy', 'network'): endpoint = '%s:%d' % ( getattr(env, attr + '_host'), getattr(env, attr + '_port')) self.assertEqual(endpoint, brkt_config.get(attr + '_host'))
def test_brkt_env_from_values(self): """ Test parsing BracketEnvironment from the --service-domain and --brkt-env command line options. """ # No values are set. self.assertIsNone(brkt_cli.brkt_env_from_values(DummyValues())) # Test --service-domain values = DummyValues() values.service_domain = 'example.com' brkt_env = brkt_cli.brkt_env_from_values(values) self.assertEqual(str(brkt_cli.brkt_env_from_domain('example.com')), str(brkt_env)) # Test --brkt-env values = DummyValues() values.brkt_env = 'yetiapi.example.com:443,hsmproxy.example.com:443' brkt_env = brkt_cli.brkt_env_from_values(values) self.assertEqual(str(brkt_cli.parse_brkt_env(values.brkt_env)), str(brkt_env))