Ejemplo n.º 1
0
def response_arn_matches_partition(client, response_arn: str) -> bool:
    parsed_arn = aws_stack.parse_arn(response_arn)
    return (
        client.meta.partition
        == boto3.session.Session().get_partition_for_region(parsed_arn["region"])
        and client.meta.partition == parsed_arn["partition"]
    )
Ejemplo n.º 2
0
def update_state(target, self, reason, reason_data, state_value):
    old_state = self.state_value
    target(self, reason, reason_data, state_value)

    # check the state and trigger required actions
    if not self.actions_enabled or old_state == self.state_value:
        return
    if self.state_value == "OK":
        actions = self.ok_actions
    elif self.state_value == "ALARM":
        actions = self.alarm_actions
    else:
        actions = self.insufficient_data_actions
    for action in actions:
        data = aws_stack.parse_arn(action)
        # test for sns - can this be done in a more generic way?
        if data["service"] == "sns":
            service = aws_stack.connect_to_service(data["service"])
            subject = f"""{self.state_value}: "{self.name}" in {self.region_name}"""
            message = create_message_response_update_state(self, old_state)
            service.publish(TopicArn=action, Subject=subject, Message=message)
        else:
            # TODO: support other actions
            LOG.warning(
                "Action for service %s not implemented, action '%s' will not be triggered.",
                data["service"],
                action,
            )
Ejemplo n.º 3
0
def connect_api_gateway_to_sqs(gateway_name, stage_name, queue_arn, path, region_name=None):
    resources = {}
    template = APIGATEWAY_SQS_DATA_INBOUND_TEMPLATE
    resource_path = path.replace("/", "")
    region_name = region_name or aws_stack.get_region()

    try:
        arn = parse_arn(queue_arn)
        queue_name = arn["resource"]
        sqs_region = arn["region"]
    except InvalidArnException:
        queue_name = queue_arn
        sqs_region = region_name

    resources[resource_path] = [
        {
            "httpMethod": "POST",
            "authorizationType": "NONE",
            "integrations": [
                {
                    "type": "AWS",
                    "uri": "arn:aws:apigateway:%s:sqs:path/%s/%s"
                    % (sqs_region, TEST_AWS_ACCOUNT_ID, queue_name),
                    "requestTemplates": {"application/json": template},
                }
            ],
        }
    ]
    return aws_stack.create_api_gateway(
        name=gateway_name,
        resources=resources,
        stage_name=stage_name,
        region_name=region_name,
    )
Ejemplo n.º 4
0
 def test_parse_arn(self):
     arn = parse_arn(
         "arn:aws:lambda:aws-region:acct-id:function:helloworld:42")
     assert arn["partition"] == "aws"
     assert arn["service"] == "lambda"
     assert arn["region"] == "aws-region"
     assert arn["account"] == "acct-id"
     assert arn["resource"] == "function:helloworld:42"
Ejemplo n.º 5
0
    def test_parse_arn_invalid(self):
        with pytest.raises(InvalidArnException):
            parse_arn("arn:aws:lambda:aws-region:acct-id")

        with pytest.raises(InvalidArnException):
            parse_arn("")
Ejemplo n.º 6
0
 def _require_queue_by_arn(self, queue_arn: str) -> SqsQueue:
     arn = parse_arn(queue_arn)
     key = QueueKey(region=arn["region"],
                    account_id=arn["account"],
                    name=arn["resource"])
     return self._require_queue(key)