def map_context(self, context: TemplateContext) -> TemplateContext: if self._is_ambassador(context): context.append_templates([{ 'apiVersion': 'getambassador.io/v1', 'kind': 'Mapping', 'metadata': { 'name': self.name + '-mapping' }, 'spec': nmap({ 'prefix': '/', 'service': self.service + str( M.map(self.port, (lambda x: ':' + str(x))) | M.value_or_default | '') }).append_if_value( 'host', M.nothing() if self.host == '*' else M.just(self.host)).to_map() }]) return context if self._is_istio(context): context.append_templates([{ 'apiVersion': 'networking.istio.io/v1alpha3', 'kind': 'VirtualService', 'metadata': { 'name': self.name + '-vs' }, 'spec': { 'hosts': [self.host], 'gateways': [self.gateway_name], 'http': nlist([ nmap({ 'route': [{ 'destination': nmap({ 'host': self.service }).append_if_value( 'port', M.map(self.port, (lambda x: { 'number': x }))).to_map() }] }).to_map(), ]).to_list() } }]) return context raise AssertionError('Unsupported gateway')
def to_maybe(self): if len(self.keys()) == 0: return M.nothing() else: return M.just(self.to_map())
def with_init_containers(self, init_containers: List[K8sContainer]): self.init_containers = M.just(init_containers) return self
def with_command(self, command_args: List[str]): self.command = M.just(command_args) return self
def test_from_maybe_can_return_on_value(): assert M.just(1) \ | M.from_maybe | dict( if_just=lambda x: x + 1, if_nothing=lambda: "nothing") == 2
def test_can_ignore_catch_nothing_if_value(): assert M.just(2) | M.catch_nothing | (lambda: 1) == M.just(2)
def test_can_catch_nothing_and_return_value(): assert M.nothing() | M.catch_nothing | (lambda: 1) == M.just(1)
def test_can_make_just_maybe(): assert M.just(1) == ("just", 1)
def test_can_chain_maybe(): assert M.just(1) | M.then | (lambda _: M.nothing()) == M.nothing()
def test_can_chain_just_maybe_and_wrap_non_maybe_result(): assert M.just(1) | M.then | (lambda x: x + 1) == M.just(2)
def test_can_chain_just_maybe(): assert M.just(1) | M.then | (lambda x: M.just(x + 1)) == M.just(2)
def test_can_map_just_maybe(): assert M.just(1) | M.map | (lambda x: x + 1) == M.just(2)
def with_command(self, command: str): self.command = M.just(command) return self
def test_will_append_to_list_if_value(): assert nlist([1]).append_if_value(M.just(2)) == [1, 2]
def test_will_append_to_map_if_value(): assert nmap({'a': 1}).append_if_value('b', M.just(2)) == {'a': 1, 'b': 2}
def test_will_concat_list_if_value(): assert nlist([1, 2]).append_if_list(M.just([3, 4])) == [1, 2, 3, 4]