def test_fail_if_object_doesnt_exist(self): # Setup stack = Stack() data = ZeroAttributeObject() # Exercise & Verify with pytest.raises(ValueError) as excinfo: stack.get_logical_name(data) assert "not part of this stack" in str(excinfo.value)
def test_fail_if_object_is_pseudo_parameter_when_only_searching_resources( self): # Setup data = AWS_Region stack = Stack() # Exercise & Verify with pytest.raises(ValueError) as excinfo: stack.get_logical_name(data, resources_only=True) assert "not part of this stack" in str(excinfo.value)
def test_only_search_resources_when_requested(self, object_type): # Setup stack = Stack() data = ZeroAttributeObject() setattr(stack, object_type, {"Foo": data}) # Exercise & Verify with pytest.raises(ValueError) as excinfo: stack.get_logical_name(data, resources_only=True) assert "not part of this stack" in str(excinfo.value)
def test_fail_if_object_doesnt_exist_except_in_another_stack(self): # Setup name = "Foo" stack = Stack() stack2 = Stack() data = ZeroAttributeObject() stack2.Resources[name] = data # Exercise & Verify with pytest.raises(ValueError) as excinfo: stack.get_logical_name(data) assert "not part of this stack" in str(excinfo.value)
def test_fail_if_object_is_duplicated(self): # Setup name1 = "Foo" name2 = "Bar" stack = Stack() data = Parameter(Type="String") stack.Parameters[name1] = data stack.Parameters[name2] = data # Exercise & Verify with pytest.raises(ValueError) as excinfo: stack.get_logical_name(data) assert "multiple names" in str(excinfo.value)
def test_find_a_pseudo_parameter(self): # Setup data = AWS_Region stack = Stack() # Exercise & Verify assert stack.get_logical_name(data) == "AWS::Region"
def test_find_a_resource_when_only_searching_resources(self): # Setup name = "Foo" stack = Stack() data = ZeroAttributeObject() stack.Resources[name] = data # Exercise & Verify assert stack.get_logical_name(data, resources_only=True) == name
def test_find_a_resource_which_is_a_plain_dict(self): # Setup name = "Foo" stack = Stack() data = dict() stack.Resources[name] = data # Exercise & Verify assert stack.get_logical_name(data) == name
def test_find_a_resource(self): # Setup name = "Foo" stack = Stack() data = ZeroAttributeObject() stack.Resources[name] = data # Exercise & Verify assert stack.get_logical_name(data) == name
def test_find_a_parameter(self): # Setup name = "Foo" stack = Stack() data = Parameter(Type="String") stack.Parameters[name] = data # Exercise & Verify assert stack.get_logical_name(data) == name
def ensure_internet_gateway_exists(self, stack: Stack): """Ensure there is an internet gateway attached to this VPC as part of the stack.""" # If we have already attached a gateway to this VPC, then there is # nothing more to do if self._internet_gateway is not None: # Check that the internet gateway is in the desired stack if not [ res for res in stack.Resources.values() if res is self._internet_gateway ]: raise RuntimeError("Existing InternetGateway is not in this stack") if not [ res for res in stack.Resources.values() if res is self._internet_gateway_attachment ]: raise RuntimeError( "Existing VPCGatewayAttachment for InternetGateway is not in this stack" ) return # Look for an existing gateway attached to this VPC for res in stack.Resources.values(): if ( isinstance(res, VPCGatewayAttachment) and res.Properties.VpcId == Ref(self) and res.Properties.InternetGatewayId ): self._internet_gateway_attachment = res # Try to dodgily unwrap the internet gateway... gateway_ref = res.Properties.InternetGatewayId if not isinstance(gateway_ref, Ref): raise RuntimeError("Can't deal with direct ID references!") if not isinstance(gateway_ref._data, InternetGateway): raise RuntimeError( "There's something weird attached to this VPC instead of an Internet Gateway" ) self._internet_gateway = gateway_ref._data return # Create an Internet Gateway self._internet_gateway = InternetGateway() if self.name: # Copy the Name of the VPC if one has already been set self._internet_gateway.name = self.name igw_stack_name = stack.get_logical_name(self, resources_only=True) + "IGW" if igw_stack_name in stack.Resources: raise RuntimeError(f"There's already a resource named {igw_stack_name}") stack.Resources[igw_stack_name] = self._internet_gateway # Attach the gateway to this VPC self._internet_gateway_attachment = VPCGatewayAttachment( Properties=VPCGatewayAttachmentProperties( InternetGatewayId=Ref(self._internet_gateway), VpcId=Ref(self) ) ) attachment_stack_name = igw_stack_name + "Attachment" if attachment_stack_name in stack.Resources: raise RuntimeError( f"There's already a resource named {attachment_stack_name}" ) stack.Resources[attachment_stack_name] = self._internet_gateway_attachment