Esempio n. 1
0
    def test_from_dict(self):
        from troposphere_mate.apigateway import RestApi

        tpl = Template()
        rest_api = RestApi(
            "RestApi",
            template=tpl,
            Metadata={"description": "a rest api"},
            Name="MyApi",
        )
        output_rest_api_id = Output("RestApiId",
                                    Value=Ref(rest_api),
                                    DependsOn=rest_api)
        tpl.add_output(output_rest_api_id)

        dct = tpl.to_dict()
        # print(tpl.to_json())
        tpl = Template.from_dict(dct)
        tpl.remove_resource_by_label(label="na")
        assert tpl.to_dict() == dct

        assert isinstance(tpl.resources["RestApi"], RestApi)
        assert isinstance(tpl.outputs["RestApiId"], Output)
        assert getattr(
            tpl.outputs[output_rest_api_id.title],
            mtdt.TemplateLevelField.OUTPUTS_DEPENDS_ON,
        ) == [
            rest_api.title,
        ]

        assert tpl.to_dict() == dct
Esempio n. 2
0
    def test_update_tags(self):
        from troposphere_mate import ec2

        # overwrite=False works
        tpl = Template()

        my_vpc = ec2.VPC("MyVPC",
                         template=tpl,
                         CidrBlock="10.0.0.0/16",
                         Tags=Tags(Creator="Alice"))
        my_sg = ec2.SecurityGroup(
            "MySG",
            template=tpl,
            GroupDescription="My",
            GroupName="MySG",
            VpcId=Ref(my_vpc),
        )
        my_subnet = ec2.Subnet(
            "MySubnet",
            template=tpl,
            CidrBlock="10.0.1.0/24",
            VpcId=Ref(my_vpc),
        )

        def get_name(resource, project):
            if resource.resource_type == "AWS::EC2::SecurityGroup":
                return "{}/sg/{}".format(project, resource.GroupName)

        tpl.update_tags(dict(Project="my-project"), overwrite=False)
        tpl.update_tags(tags_dct=dict(
            Name=functools.partial(get_name, project="my-project"),
            Creator="Bob",
        ),
                        overwrite=False)

        assert tags_list_to_dct(
            tpl.to_dict()["Resources"]["MyVPC"]["Properties"]["Tags"]) == dict(
                Project="my-project",
                Creator="Alice",
            )
        assert tags_list_to_dct(
            tpl.to_dict()["Resources"]["MySG"]["Properties"]["Tags"]) == dict(
                Project="my-project",
                Name="my-project/sg/MySG",
                Creator="Bob",
            )
Esempio n. 3
0
def test_mutable_aws_object():
    """
    确定 ``mate.AWSObject`` 变化时, 对应的 ``mate.AWSObject.aws_object`` 也应该
    跟着变化.
    """
    from troposphere_mate import iam

    tpl = Template()
    my_policy = iam.ManagedPolicy(
        title="MyPolicy",
        template=tpl,
        PolicyDocument={},
    )
    my_role = iam.Role(
        title="MyRole",
        template=tpl,
        RoleName="my-role",
        AssumeRolePolicyDocument={},
    )
    assert tpl.to_dict(
    )["Resources"]["MyRole"]["Properties"]["RoleName"] == "my-role"
    assert "ManagedPolicyArns" not in tpl.to_dict(
    )["Resources"]["MyRole"]["Properties"]

    my_role.RoleName = "my-role-two"
    my_role.ManagedPolicyArns = [Ref(my_policy)]
    assert tpl.to_dict(
    )["Resources"]["MyRole"]["Properties"]["RoleName"] == "my-role-two"
    assert tpl.to_dict(
    )["Resources"]["MyRole"]["Properties"]["ManagedPolicyArns"] == [{
        "Ref":
        "MyPolicy"
    }]

    outputs = [
        Output(
            "MyRolePath",
            Value=GetAtt(my_role, "Path"),
            DependsOn=my_role,
        )
    ]
    for output in outputs:
        tpl.add_output(output)
    assert tpl.to_dict()["Outputs"]["MyRolePath"]["Value"] == {
        "Fn::GetAtt": ["MyRole", "Path"]
    }

    dct = tpl.to_dict()

    tpl2 = Template()
    tpl2.add_resource(my_policy)
    tpl2.add_resource(my_role)
    for output in outputs:
        tpl2.add_output(output)
    dct2 = tpl2.to_dict()

    assert dct == dct2
Esempio n. 4
0
    def test_create_resource_type_label(self):
        from troposphere_mate import s3, apigateway
        tpl = Template()
        s3_bucket = s3.Bucket(
            "Bucket",
            template=tpl,
            BucketName="my-bucket",
        )
        rest_api = apigateway.RestApi("RestApi",
                                      template=tpl,
                                      DependsOn=[
                                          s3_bucket,
                                      ])
        tpl.create_resource_type_label()
        tpl.create_resource_type_label()  # see if second call raise exception

        assert len(tpl.to_dict()["Resources"]["Bucket"]["Metadata"]
                   [DEFAULT_LABELS_FIELD]) == 1
        assert len(tpl.to_dict()["Resources"]["RestApi"]["Metadata"]
                   [DEFAULT_LABELS_FIELD]) == 1
Esempio n. 5
0
def test_tags():
    """

    .. note::

        ``my_bucket.Tags = [{"Key": "Creator", "Value": "Alice"},]`` method doesn't
        work anymore in troposphere.
    """
    from troposphere_mate import s3

    tpl = Template()

    my_bucket = s3.Bucket("MyBucket",
                          template=tpl,
                          BucketName="my-bucket",
                          Tags=Tags(Creator="Alice"))
    assert tpl.to_dict()["Resources"]["MyBucket"]["Properties"]["Tags"] == [{
        "Key":
        "Creator",
        "Value":
        "Alice"
    }]
template.add_parameter(param_env_name)

if __name__ == "__main__":
    import boto3

    AWS_PROFILE = "eq_sanhe"
    AWS_REGION = "us-east-1"
    ENV_NAME = "login-gov-metrics-dev"
    CFT_BUCKET = "eq-sanhe-for-everything"

    boto_ses = boto3.session.Session(profile_name=AWS_PROFILE,
                                     region_name=AWS_REGION)
    s3_client = boto_ses.client("s3")
    cf_client = boto_ses.client("cloudformation")

    template_data = template.to_dict()
    template_data["Resources"][
        kinesis_analytics_application_logic_id] = kinesis_analytics_application_data
    template_data["Resources"][
        kinesis_analytics_application_output_logic_id] = kinesis_analytics_application_output_data
    template_json = json.dumps(template_data, indent=4, sort_keys=True)
    template_url = upload_template(
        s3_client=s3_client,
        template_content=template_json,
        bucket_name=CFT_BUCKET,
    )
    deploy_stack(
        cf_client=cf_client,
        stack_name=ENV_NAME,
        template_url=template_url,
        stack_tags={