def main():
    required_if = [
        ["kind", "str", ["variations"]],
        ["kind", "json", ["variations"]],
        ["kind", "number", ["variations"]],
    ]

    argument_spec = ld_common_argument_spec()
    argument_spec.update(
        dict(
            state=dict(type="str",
                       default="present",
                       choices=["absent", "present"]),
            name=dict(type="str", required_if=["state", "present"]),
            kind=dict(
                choices=["str", "bool", "json", "number"],
                required_if=["state", "present"],
            ),
            project_key=dict(default="default", type="str"),
            key=dict(required=True, type="str"),
            temporary=dict(type="bool", default=True),
            tags=dict(type="list"),
            description=dict(type="str"),
            variations=dict(
                type="list",
                elements="dict",
                options=dict(
                    name=dict(type="str"),
                    value=dict(type="raw"),
                    description=dict(type="str"),
                ),
            ),
            include_in_snippet=dict(type="bool", default=False),
            comment=dict(type="str"),
            maintainer_id=dict(type="str"),
        ))

    module = AnsibleModule(argument_spec=argument_spec,
                           required_if=required_if)

    if not HAS_LD:
        module.fail_json(msg=missing_required_lib("launchdarkly_api"),
                         exception=LD_IMP_ERR)

    # Set up API
    configuration = configure_instance(module.params["api_key"])
    api_instance = launchdarkly_api.FeatureFlagsApi(
        launchdarkly_api.ApiClient(configuration))

    if module.params["state"] == "present":
        feature_flag = _fetch_flag(module, api_instance)
        if feature_flag:
            _configure_flag(module, api_instance, feature_flag)
        else:
            _create_flag(module, api_instance)
    elif module.params["state"] == "absent":
        _delete_flag(module, api_instance)
Пример #2
0
def main():
    module = AnsibleModule(argument_spec=dict(
        state=dict(
            type="str", default="present", choices=["absent", "present"]),
        api_key=dict(
            required=True,
            type="str",
            no_log=True,
            fallback=(env_fallback, ["LAUNCHDARKLY_ACCESS_TOKEN"]),
        ),
        environment_key=dict(type="str", required=True),
        project_key=dict(type="str", required=True),
        flag_key=dict(type="str", required=True),
        environment_targets=dict(type="list", required=True),
        included_actions=dict(
            type="list",
            choices=[
                "updateOn",
                "updatePrerequisites",
                "updateTargets",
                "updateRules",
                "updateFallthrough",
                "updateOffVariation",
            ],
        ),
        excluded_actions=dict(
            type="list",
            choices=[
                "updateOn",
                "updatePrerequisites",
                "updateTargets",
                "updateRules",
                "updateFallthrough",
                "updateOffVariation",
            ],
        ),
    ))

    if not HAS_LD:
        module.fail_json(msg=missing_required_lib("launchdarkly_api"),
                         exception=LD_IMP_ERR)

    configuration = configure_instance(module.params["api_key"])
    api_instance = launchdarkly_api.FeatureFlagsApi(
        launchdarkly_api.ApiClient(configuration))

    _configure_flag_sync(module, api_instance)
def main():
    module = AnsibleModule(
        argument_spec=dict(
            api_key=dict(
                required=True,
                type="str",
                no_log=True,
                fallback=(env_fallback, ["LAUNCHDARKLY_ACCESS_TOKEN"]),
            ),
            env=dict(type="str"),
            project_key=dict(type="str", required=True),
            key=dict(type="str"),
            summary=dict(type="bool"),
            archived=dict(type="bool"),
            tag=dict(type="str"),
        )
    )

    if not HAS_LD:
        module.fail_json(
            msg=missing_required_lib("launchdarkly_api"), exception=LD_IMP_ERR
        )

    # Set up API
    configuration = configure_instance(module.params["api_key"])
    api_instance = launchdarkly_api.FeatureFlagsApi(
        launchdarkly_api.ApiClient(configuration)
    )

    try:
        feature_flags = fetch_flags(module.params, api_instance)
    except launchdarkly_api.rest.ApiException as e:
        fail_exit(module, e)

    if feature_flags.get("items"):
        flags = feature_flags["items"]
    else:
        flags = feature_flags
    module.exit_json(changed=True, feature_flags=flags)
def main():
    argument_spec = ld_common_argument_spec()
    argument_spec.update(
        dict(
            env=dict(type="str"),
            project_key=dict(type="str", required=True),
            tag=dict(type="str"),
        )
    )

    module = AnsibleModule(argument_spec=argument_spec)

    if not HAS_LD:
        module.fail_json(
            msg=missing_required_lib("launchdarkly_api"), exception=LD_IMP_ERR
        )

    # Set up API
    configuration = configure_instance(module.params["api_key"])
    api_instance = launchdarkly_api.FeatureFlagsApi(
        launchdarkly_api.ApiClient(configuration)
    )

    feature_flags = _fetch_flags(module, api_instance)

    flags = feature_flags["items"]
    results = []
    for flag in flags:
        result = rego_test(module, flag)
        if result.results[0].failures:
            validation_fail = {"key": flag["key"], "failures": []}
            for failure in result.results[0].failures:
                validation_fail["failures"].append(failure["msg"])
            results.append(validation_fail)

    if results:
        module.exit_json(failed=True, validated=False, validation=results)
    else:
        module.exit_json(changed=True, validated=True)
Пример #5
0
    def __init__(self,
                 api_key: str,
                 project_key: str = None,
                 environment_key: str = None):
        """
        Instantiate a new LaunchDarklyApi instance.

        :param api_key: API Access Key for LaunchDarkly.
        :param project_key: Key for project.
        :param environment_key: Environment in which to \
            pull state from.
        """
        self.api_key = api_key
        self.project_key = project_key
        self.environment_key = environment_key

        # get new LD client
        configuration = launchdarkly_api.Configuration()
        configuration.api_key['Authorization'] = api_key
        self.client = launchdarkly_api.ProjectsApi(
            launchdarkly_api.ApiClient(configuration))
        self.feature = launchdarkly_api.FeatureFlagsApi(
            launchdarkly_api.ApiClient(configuration))
Пример #6
0
def main():
    argument_spec = ld_common_argument_spec()
    argument_spec.update(
        dict(
            state=dict(
                type="str",
                default="present",
                choices=["absent", "present", "enabled", "disabled"],
            ),
            flag_key=dict(type="str", required=True),
            environment_key=dict(type="str", required=True),
            project_key=dict(type="str", required=True),
            off_variation=dict(type="int"),
            track_events=dict(type="bool"),
            comment=dict(type="str"),
            salt=dict(type="str"),
            targets=dict(
                type="list",
                elements="dict",
                options=dict(
                    values=dict(type="list"),
                    variation=dict(type="int"),
                    state=dict(
                        type="str",
                        default="replace",
                        choices=["add", "remove", "replace", "absent"],
                    ),
                ),
            ),
            fallthrough=dict(
                type="dict",
                options=dict(
                    variation=dict(type="int"),
                    rollout=dict(
                        type="dict",
                        # elements="dict",
                        bucket_by=dict(type="str"),
                        weighted_variations=dict(
                            type="list",
                            elements="dict",
                            options=dict(
                                variation=dict(type="int"), weight=dict(type="int")
                            ),
                        ),
                    ),
                ),
            ),
            rules=rule_argument_spec(),
            prerequisites=dict(
                type="list",
                options=dict(key=dict(type="str"), variation=dict(type="int")),
            ),
        )
    )

    module = AnsibleModule(argument_spec=argument_spec)

    if not HAS_LD:
        module.fail_json(
            msg=missing_required_lib("launchdarkly_api"), exception=LD_IMP_ERR
        )

    configuration = configure_instance(module.params["api_key"])
    api_instance = launchdarkly_api.FeatureFlagsApi(
        launchdarkly_api.ApiClient(configuration)
    )

    if module.params["state"] == "absent":
        _delete_feature_flag_env(module, api_instance)
    else:
        feature_flag = _fetch_feature_flag(module, api_instance)
        _configure_feature_flag_env(module, api_instance, feature_flag)
Пример #7
0
from __future__ import print_function
import os
from pprint import pprint

import launchdarkly_api
import launchdarkly_api.models
from launchdarkly_api.rest import ApiException

configuration = launchdarkly_api.Configuration()
configuration.api_key['Authorization'] = os.getenv("LD_API_KEY")

client = launchdarkly_api.ApiClient(configuration)
api_instance = launchdarkly_api.FeatureFlagsApi(client)

project_key = "openapi"
flag_key = "test-python"

# Create a flag with json variations
feature_flag_body = launchdarkly_api.FeatureFlagBody(
    name=flag_key,
    key=flag_key,
    variations=[
        launchdarkly_api.models.Variation(value=[1, 2]),
        launchdarkly_api.models.Variation(value=[3, 4]),
        launchdarkly_api.models.Variation(value=[5])
    ])

try:
    api_response = api_instance.post_feature_flag(project_key,
                                                  feature_flag_body)
    pprint(api_response)
def main():
    module = AnsibleModule(argument_spec=dict(
        api_key=dict(
            required=True,
            type="str",
            no_log=True,
            fallback=(env_fallback, ["LAUNCHDARKLY_ACCESS_TOKEN"]),
        ),
        api_key_dest=dict(
            required=True,
            type="str",
            no_log=True,
            fallback=(env_fallback, ["LAUNCHDARKLY_DEST_ACCESS_TOKEN"]),
        ),
        project_key=dict(type="str", required=True),
        project_key_dest=dict(type="str", required=True),
        flag_tag=dict(type="list", elements="str"),
        environments_copy=dict(type="bool", default=True),
        name=dict(type="str"),
    ))

    if not HAS_LD:
        module.fail_json(msg=missing_required_lib("launchdarkly_api"),
                         exception=LD_IMP_ERR)

    # Setup the necessary API clients
    api_instance_src_user = launchdarkly_api.UserSegmentsApi(
        launchdarkly_api.ApiClient(configure_instance(
            module.params["api_key"])))

    api_instance_src_proj = launchdarkly_api.ProjectsApi(
        launchdarkly_api.ApiClient(configure_instance(
            module.params["api_key"])))

    api_instance_src_fflag = launchdarkly_api.FeatureFlagsApi(
        launchdarkly_api.ApiClient(configure_instance(
            module.params["api_key"])))

    api_instance_dest_user = launchdarkly_api.UserSegmentsApi(
        launchdarkly_api.ApiClient(
            configure_instance(module.params["api_key_dest"])))

    api_instance_dest_proj = launchdarkly_api.ProjectsApi(
        launchdarkly_api.ApiClient(
            configure_instance(module.params["api_key_dest"])))

    api_instance_dest_fflag = launchdarkly_api.FeatureFlagsApi(
        launchdarkly_api.ApiClient(
            configure_instance(module.params["api_key_dest"])))

    api_instance_dest_env = launchdarkly_api.EnvironmentsApi(
        launchdarkly_api.ApiClient(
            configure_instance(module.params["api_key_dest"])))

    _project_sync(
        module,
        api_instance_src_proj,
        api_instance_dest_proj,
        api_instance_dest_env,
        api_instance_src_user,
        api_instance_dest_user,
        api_instance_src_fflag,
        api_instance_dest_fflag,
    )
Пример #9
0
import launchdarkly_api
import jinja2

configuration = launchdarkly_api.Configuration()
configuration.api_key['Authorization'] = 'CHANGE_ME'
api_instance = launchdarkly_api.FeatureFlagsApi(
    launchdarkly_api.ApiClient(configuration))

flags = api_instance.get_feature_flags('dano-test-project', env='production')
prefix = "ld_"
type_hints = True

templateLoader = jinja2.FileSystemLoader(searchpath="./")
templateEnv = jinja2.Environment(loader=templateLoader)
TEMPLATE_FILE = "template.j2"
template = templateEnv.get_template(TEMPLATE_FILE)
flag_template = flags.to_dict()
output = template.render(
    th=type_hints,
    flags=flag_template['items'],
    project='dano-test-project',
    environment='production',
    prefix=prefix)  # this is where to put args to the template renderer

with open('output.py', 'w') as f:
    f.write(output)