コード例 #1
0
def run_module():
    from ansible.module_utils.basic import AnsibleModule

    module = AnsibleModule(argument_spec=module_args,
                           supports_check_mode=False)

    # New resource required arguments checks
    missing_args = []
    if module.params['id'] is None:
        for arg, _ in TL_REQUIRED_PARAMETERS:
            if module.params[arg] is None:
                missing_args.append(arg)
        if missing_args:
            module.fail_json(msg=("missing required arguments: " +
                                  ", ".join(missing_args)))

    conflicts = {}
    if len(TL_CONFLICTS_MAP) != 0:
        for arg in TL_CONFLICTS_MAP:
            if module.params[arg]:
                for conflict in TL_CONFLICTS_MAP[arg]:
                    try:
                        if module.params[conflict]:
                            conflicts[arg] = conflict
                    except KeyError:
                        pass
    if len(conflicts):
        module.fail_json(msg=("conflicts exist: {}".format(conflicts)))

    # VPC required arguments checks
    if module.params['generation'] == 1:
        missing_args = []
        if module.params['iaas_classic_username'] is None:
            missing_args.append('iaas_classic_username')
        if module.params['iaas_classic_api_key'] is None:
            missing_args.append('iaas_classic_api_key')
        if missing_args:
            module.fail_json(
                msg=("VPC generation=1 missing required arguments: " +
                     ", ".join(missing_args)))
    elif module.params['generation'] == 2:
        if module.params['ibmcloud_api_key'] is None:
            module.fail_json(
                msg=("VPC generation=2 missing required argument: "
                     "ibmcloud_api_key"))

    result_ds = ibmcloud_terraform(
        resource_type='ibm_is_lb',
        tf_type='data',
        parameters=module.params,
        ibm_provider_version='1.13.1',
        tl_required_params=TL_REQUIRED_PARAMETERS_DS,
        tl_all_params=TL_ALL_PARAMETERS_DS)

    if result_ds['rc'] != 0 or (result_ds['rc'] == 0 and
                                (module.params['id'] is not None
                                 or module.params['state'] == 'absent')):
        result = ibmcloud_terraform(resource_type='ibm_is_lb',
                                    tf_type='resource',
                                    parameters=module.params,
                                    ibm_provider_version='1.13.1',
                                    tl_required_params=TL_REQUIRED_PARAMETERS,
                                    tl_all_params=TL_ALL_PARAMETERS)
        if result['rc'] > 0:
            module.fail_json(msg=Terraform.parse_stderr(result['stderr']),
                             **result)

        module.exit_json(**result)
    else:
        module.exit_json(**result_ds)