Beispiel #1
0
def config():
    return types.MachConfig(
        mach_composer=types.MachComposerConfig(version="1.1"),
        general_config=types.GlobalConfig(
            environment="test",
            terraform_config=types.TerraformConfig(
                aws_remote_state=types.AWSTFState(
                    bucket="unittest",
                    key_prefix="test",
                    region="eu-central-1",
                )),
            cloud=types.CloudOption.AWS,
            sentry=types.SentryConfig(dsn="sentry-dsn"),
        ),
        sites=[
            types.Site(
                identifier="unittest-nl",
                components=[types.Component(name="api-extensions", )],
                aws=types.SiteAWSSettings(
                    account_id=1234567890,
                    region="eu-central-1",
                ),
            ),
        ],
        components=[
            types.ComponentConfig(
                name="api-extensions",
                source="some-source//terraform",
                version="1.0",
            )
        ],
        output_path=tempfile.gettempdir(),
    )
Beispiel #2
0
def test_generate_w_sentry(parsed_config: types.MachConfig, tf_mock):
    data = tf.generate(parsed_config)
    data_str = json.dumps(data)
    assert "sentry" not in data_str

    parsed_config.components[0].integrations = ["aws", "sentry"]
    data = tf.generate(parsed_config)

    assert "sentry_dsn" in data.module["api-extensions"]
    assert "sentry_key" not in data.get("resource", {})

    parsed_config.general_config.sentry = types.SentryConfig(
        auth_token="12345",
        organization="labd",
        project="unittest",
    )
    data = tf.generate(parsed_config)
    assert "sentry_dsn" in data.module["api-extensions"]
    assert "sentry_key" in data.resource
    assert "api-extensions" in data.resource.sentry_key
    sentry_data = data.resource.sentry_key["api-extensions"]
    assert "rate_limit_window" not in sentry_data
    assert "rate_limit_count" not in sentry_data

    comp_sentry = parsed_config.sites[0].components[0].sentry
    comp_sentry.rate_limit_window = 21600
    comp_sentry.rate_limit_count = 100

    data = tf.generate(parsed_config)
    sentry_data = data.resource.sentry_key["api-extensions"]
    assert sentry_data["rate_limit_window"] == 21600
    assert sentry_data["rate_limit_count"] == 100
Beispiel #3
0
def test_validate_sentry():
    with pytest.raises(ValidationError):
        validate.validate_sentry_config(types.SentryConfig())

    validate.validate_sentry_config(types.SentryConfig(dsn="12345"))

    with pytest.raises(ValidationError):
        validate.validate_sentry_config(types.SentryConfig(auth_token="12345"))

    with pytest.raises(ValidationError):
        validate.validate_sentry_config(
            types.SentryConfig(auth_token="12345", dsn="12345"))

    validate.validate_sentry_config(
        types.SentryConfig(auth_token="12345",
                           project="my-project",
                           organization="my-organization"))
Beispiel #4
0
def azure_config():
    return types.MachConfig(
        mach_composer=types.MachComposerConfig(version="1.1"),
        general_config=types.GlobalConfig(
            environment="test",
            terraform_config=types.TerraformConfig(
                azure_remote_state=types.AzureTFState(
                    resource_group="shared-rg",
                    storage_account="machsaterra",
                    container_name="tfstate",
                    state_folder="test",
                )),
            azure=types.AzureConfig(
                tenant_id="6f10659d-4227-43e6-95ab-80d12a18acf9",
                subscription_id="5f34d95d-4dd8-40b3-9d18-f9007e2ce6ac",
                region="westeurope",
                frontdoor=types.FrontdoorSettings(
                    dns_resource_group="shared-rg"),
            ),
            cloud=types.CloudOption.AZURE,
            sentry=types.SentryConfig(dsn="sentry-dsn"),
        ),
        sites=[
            types.Site(
                identifier="unittest-nl",
                components=[types.Component(name="api-extensions", )],
            ),
        ],
        components=[
            types.ComponentConfig(
                name="api-extensions",
                source="some-source//terraform",
                version="1.0",
                azure=types.ComponentAzureConfig(short_name="apiexts",
                                                 service_plan="default"),
            ),
            types.ComponentConfig(
                name="product-types",
                source="product-types//terraform",
                version="v0.1.0",
                integrations=[""],
            ),
            types.ComponentConfig(
                name="payment",
                source="payment//terraform",
                azure=types.ComponentAzureConfig(short_name="payment"),
                version="1.0",
            ),
        ],
        output_path=tempfile.gettempdir(),
    )
Beispiel #5
0
def _create_config() -> types.MachConfig:  # noqa: C901
    environment = click.prompt("Environment", "test")
    cloud = click.prompt("Cloud environment",
                         type=click.Choice(["aws", "azure"]),
                         default="aws")
    site_id = click.prompt("Site identifier")
    use_commercetools = click.confirm("Use commercetools?", default=True)
    ct_project = ""
    if use_commercetools:
        ct_project = click.prompt("commercetools project name",
                                  default=site_id)
    use_sentry = click.confirm("Use Sentry?", default=False)
    use_contentful = click.confirm("Use Contentful?", default=False)
    use_amplience = click.confirm("Use Amplience?", default=False)

    integrations = []
    if use_commercetools:
        integrations.append("commercetools")
    if use_sentry:
        integrations.append("sentry")
    if use_contentful:
        integrations.append("contentful")
    if use_amplience:
        integrations.append("amplience")
    # If we do have integrations, add the default (cloud) integration here as well
    if integrations:
        integrations = [cloud] + integrations

    if cloud == "aws":
        tf_config = types.TerraformConfig(aws_remote_state=types.AWSTFState(
            bucket="<your bucket>",
            key_prefix="mach",
            region="eu-central-1",
        ))
    else:
        tf_config = types.TerraformConfig(
            azure_remote_state=types.AzureTFState(
                resource_group="<your-resource-group>",
                storage_account="<your-storage-account>",
                container_name="<your-container-name>",
                state_folder=environment,
            ))

    general_config_kwargs = dict(
        environment=environment,
        terraform_config=tf_config,
        cloud=cloud,
    )

    if cloud == "azure":
        general_config_kwargs["azure"] = types.AzureConfig(
            tenant_id="<your-tenant-id>",
            subscription_id="<your-subscription-id>",
            region="westeurope",
        )

    if use_sentry:
        general_config_kwargs["sentry"] = types.SentryConfig(
            auth_token="<your-auth-token>",
            project="<your-project>",
            organization="<your-organization>",
        )

    if use_contentful:
        general_config_kwargs["contentful"] = types.ContentfulConfig(
            cma_token="<your-cma-token>",
            organization_id="<your-organization-id>",
        )

    if use_amplience:
        general_config_kwargs["amplience"] = types.AmplienceConfig(
            client_id="<your-client-id>",
            client_secret="<your-client-secret>",
        )

    site = types.Site(
        identifier=site_id,
        components=[
            types.Component(
                name="your-component",
                variables={"FOO_VAR": "my-value"},
                secrets={"MY_SECRET": "secretvalue"},
            )
        ],
    )

    if cloud == "aws":
        site.aws = types.SiteAWSSettings(
            account_id=123456789,
            region="eu-central-1",
        )

    if use_commercetools:
        site.commercetools = types.CommercetoolsSettings(
            project_key=ct_project,
            client_id="<client-id>",
            client_secret="<client-secret>",
            scopes=(f"manage_api_clients:{ct_project} "
                    f"manage_project:{ct_project} "
                    f"view_api_clients:{ct_project}"),
            project_settings=types.CommercetoolsProjectSettings(
                # TODO: Improve this by letting user select one or more countries
                # and generate/guess the correct languages and currencies that probably
                # need to be applied for that project
                languages=["nl-NL"],
                countries=["NL"],
                currencies=["EUR"],
            ),
        )

    component_config = types.ComponentConfig(
        name="your-component",
        source=
        "git::https://github.com/<username>/<your-component>.git//terraform",
        version="0.1.0",
        integrations=integrations,
    )
    if cloud == "azure":
        component_config.azure = types.ComponentAzureConfig(
            short_name="yourcomp", )

    return types.MachConfig(
        mach_composer=types.MachComposerConfig(version=__version__),
        general_config=types.GlobalConfig(**general_config_kwargs, ),
        sites=[site],
        components=[component_config],
    )