Ejemplo n.º 1
0
def main():
    module = AnsibleModule(argument_spec=dict(
        url_map_name=dict(required=True),
        state=dict(choices=['absent', 'present'], default='present'),
        default_service=dict(required=True),
        service_account_email=dict(),
        service_account_permissions=dict(type='list'),
        pem_file=dict(),
        credentials_file=dict(),
        project_id=dict(),
    ), )

    if not HAS_PYTHON26:
        module.fail_json(
            msg="GCE module requires python's 'ast' module, python v2.6+")

    client, conn_params = get_google_api_client(
        module,
        'compute',
        user_agent_product=USER_AGENT_PRODUCT,
        user_agent_version=USER_AGENT_VERSION)

    params = {}
    params['state'] = module.params.get('state')
    params['url_map_name'] = module.params.get('url_map_name')
    params['default_service'] = module.params.get('default_service')

    changed = False
    json_output = {'state': params['state']}
    url_map = get_url_map(client,
                          name=params['url_map_name'],
                          project_id=conn_params['project_id'])
    #    json_output['url_map'] = url_map

    if not url_map:
        json_output['not_urlmap'] = True
        if params['state'] == 'absent':
            # Doesn't exist in GCE, and state==absent.
            changed = False
            module.fail_json(msg="Cannot delete unknown url_map: %s" %
                             (params['url_map_name']))
        else:
            # Create
            json_output['try_create'] = True
            (changed, json_output['url_map']) = create_url_map(
                client, params=params, project_id=conn_params['project_id'])
    elif params['state'] == 'absent':
        # Delete
        (changed, json_output['url_map']) = delete_url_map(
            client,
            name=params['url_map_name'],
            project_id=conn_params['project_id'])
    else:
        json_output['try_update'] = True
        # Update
        pass

    json_output['changed'] = changed
    json_output.update(params)
    module.exit_json(**json_output)
Ejemplo n.º 2
0
def main():
    module = AnsibleModule(argument_spec=dict(
        target_proxy_name=dict(required=True),
        target_proxy_type=dict(required=True, choices=['HTTP']),
        url_map_name=dict(required=False),
        state=dict(required=True, choices=['absent', 'present']),
        service_account_email=dict(),
        service_account_permissions=dict(type='list'),
        pem_file=dict(),
        credentials_file=dict(),
        project_id=dict(),
    ), )

    client, conn_params = get_google_api_client(
        module,
        'compute',
        user_agent_product=USER_AGENT_PRODUCT,
        user_agent_version=USER_AGENT_VERSION)

    params = {}
    params['state'] = module.params.get('state')
    params['target_proxy_name'] = module.params.get('target_proxy_name')
    params['target_proxy_type'] = module.params.get('target_proxy_type')
    params['url_map'] = module.params.get('url_map_name', None)

    changed = False
    json_output = {'state': params['state']}
    target_proxy = get_target_http_proxy(client,
                                         name=params['target_proxy_name'],
                                         project_id=conn_params['project_id'])

    if not target_proxy:
        if params['state'] == 'absent':
            # Doesn't exist in GCE, and state==absent.
            changed = False
            module.fail_json(msg="Cannot delete unknown target_proxy: %s" %
                             (params['target_proxy_name']))
        else:
            # Create
            changed, json_output['target_proxy'] = create_target_http_proxy(
                client, params=params, project_id=conn_params['project_id'])
    elif params['state'] == 'absent':
        # Delete
        changed, json_output['target_proxy'] = delete_target_http_proxy(
            client,
            name=params['target_proxy_name'],
            project_id=conn_params['project_id'])
    else:
        changed, json_output['target_proxy'] = update_target_http_proxy(
            client,
            target_proxy=target_proxy,
            params=params,
            name=params['target_proxy_name'],
            project_id=conn_params['project_id'])
        json_output['updated_target_proxy'] = changed

    json_output['changed'] = changed
    json_output.update(params)
    module.exit_json(**json_output)
Ejemplo n.º 3
0
def main():
    module = AnsibleModule(argument_spec=dict(
        target_proxy_name=dict(required=True),
        target_proxy_type=dict(required=True, choices=['HTTP']),
        url_map_name=dict(required=False),
        state=dict(required=True, choices=['absent', 'present']),
        service_account_email=dict(),
        service_account_permissions=dict(type='list'),
        pem_file=dict(),
        credentials_file=dict(),
        project_id=dict(), ), )

    client, conn_params = get_google_api_client(module, 'compute', user_agent_product=USER_AGENT_PRODUCT,
                                                user_agent_version=USER_AGENT_VERSION)

    params = {}
    params['state'] = module.params.get('state')
    params['target_proxy_name'] = module.params.get('target_proxy_name')
    params['target_proxy_type'] = module.params.get('target_proxy_type')
    params['url_map'] = module.params.get('url_map_name', None)

    changed = False
    json_output = {'state': params['state']}
    target_proxy = get_target_http_proxy(client,
                                         name=params['target_proxy_name'],
                                         project_id=conn_params['project_id'])

    if not target_proxy:
        if params['state'] == 'absent':
            # Doesn't exist in GCE, and state==absent.
            changed = False
            module.fail_json(
                msg="Cannot delete unknown target_proxy: %s" %
                (params['target_proxy_name']))
        else:
            # Create
            changed, json_output['target_proxy'] = create_target_http_proxy(client,
                                                                            params=params,
                                                                            project_id=conn_params['project_id'])
    elif params['state'] == 'absent':
        # Delete
        changed, json_output['target_proxy'] = delete_target_http_proxy(client,
                                                                        name=params['target_proxy_name'],
                                                                        project_id=conn_params['project_id'])
    else:
        changed, json_output['target_proxy'] = update_target_http_proxy(client,
                                                                        target_proxy=target_proxy,
                                                                        params=params,
                                                                        name=params['target_proxy_name'],
                                                                        project_id=conn_params['project_id'])
        json_output['updated_target_proxy'] = changed

    json_output['changed'] = changed
    json_output.update(params)
    module.exit_json(**json_output)
Ejemplo n.º 4
0
def main():
    module = AnsibleModule(argument_spec=dict(
        url_map_name=dict(required=True),
        state=dict(choices=['absent', 'present'], default='present'),
        default_service=dict(required=True),
        path_matchers=dict(type='list', required=False),
        host_rules=dict(type='list', required=False),
        service_account_email=dict(),
        service_account_permissions=dict(type='list'),
        pem_file=dict(),
        credentials_file=dict(),
        project_id=dict(),
    ),
                           required_together=[
                               ['path_matchers', 'host_rules'],
                           ])

    client, conn_params = get_google_api_client(
        module,
        'compute',
        user_agent_product=USER_AGENT_PRODUCT,
        user_agent_version=USER_AGENT_VERSION)

    params = {}
    params['state'] = module.params.get('state')
    params['url_map_name'] = module.params.get('url_map_name')
    params['default_service'] = module.params.get('default_service')
    if module.params.get('path_matchers'):
        params['path_matchers'] = module.params.get('path_matchers')
    if module.params.get('host_rules'):
        params['host_rules'] = module.params.get('host_rules')

    try:
        _validate_params(params)
    except Exception as e:
        module.fail_json(msg=e.message, changed=False)

    changed = False
    json_output = {'state': params['state']}
    url_map = get_url_map(client,
                          name=params['url_map_name'],
                          project_id=conn_params['project_id'])

    if not url_map:
        if params['state'] == 'absent':
            # Doesn't exist in GCE, and state==absent.
            changed = False
            module.fail_json(msg="Cannot delete unknown url_map: %s" %
                             (params['url_map_name']))
        else:
            # Create
            changed, json_output['url_map'] = create_url_map(
                client, params=params, project_id=conn_params['project_id'])
    elif params['state'] == 'absent':
        # Delete
        changed, json_output['url_map'] = delete_url_map(
            client,
            name=params['url_map_name'],
            project_id=conn_params['project_id'])
    else:
        changed, json_output['url_map'] = update_url_map(
            client,
            url_map=url_map,
            params=params,
            name=params['url_map_name'],
            project_id=conn_params['project_id'])
        json_output['updated_url_map'] = changed

    json_output['changed'] = changed
    json_output.update(params)
    module.exit_json(**json_output)
Ejemplo n.º 5
0
def main():
    module = AnsibleModule(argument_spec=dict(
        healthcheck_name=dict(required=True),
        healthcheck_type=dict(required=True, choices=['HTTP', 'HTTPS']),
        request_path=dict(required=False, default='/'),
        check_interval=dict(required=False, type='int', default=5),
        healthy_threshold=dict(required=False, type='int', default=2),
        unhealthy_threshold=dict(required=False, type='int', default=2),
        host_header=dict(required=False, type='str', default=''),
        timeout=dict(required=False, type='int', default=5),
        port=dict(required=False, type='int'),
        state=dict(choices=['absent', 'present'], default='present'),
        service_account_email=dict(),
        service_account_permissions=dict(type='list'),
        credentials_file=dict(),
        project_id=dict(),
    ), )

    client, conn_params = get_google_api_client(
        module,
        'compute',
        user_agent_product=USER_AGENT_PRODUCT,
        user_agent_version=USER_AGENT_VERSION)

    params = {}

    params['healthcheck_name'] = module.params.get('healthcheck_name')
    params['healthcheck_type'] = module.params.get('healthcheck_type')
    params['request_path'] = module.params.get('request_path')
    params['check_interval'] = module.params.get('check_interval')
    params['healthy_threshold'] = module.params.get('healthy_threshold')
    params['unhealthy_threshold'] = module.params.get('unhealthy_threshold')
    params['host_header'] = module.params.get('host_header')
    params['timeout'] = module.params.get('timeout')
    params['port'] = module.params.get('port', None)
    params['state'] = module.params.get('state')

    if not params['port']:
        params['port'] = 80
        if params['healthcheck_type'] == 'HTTPS':
            params['port'] = 443
    try:
        _validate_healthcheck_params(params)
    except Exception as e:
        module.fail_json(msg=e.message, changed=False)

    changed = False
    json_output = {'state': params['state']}
    healthcheck = get_healthcheck(client,
                                  name=params['healthcheck_name'],
                                  project_id=conn_params['project_id'],
                                  resource_type=params['healthcheck_type'])

    if not healthcheck:
        if params['state'] == 'absent':
            # Doesn't exist in GCE, and state==absent.
            changed = False
            module.fail_json(msg="Cannot delete unknown healthcheck: %s" %
                             (params['healthcheck_name']))
        else:
            # Create
            changed, json_output['healthcheck'] = create_healthcheck(
                client,
                params=params,
                project_id=conn_params['project_id'],
                resource_type=params['healthcheck_type'])
    elif params['state'] == 'absent':
        # Delete
        changed, json_output['healthcheck'] = delete_healthcheck(
            client,
            name=params['healthcheck_name'],
            project_id=conn_params['project_id'],
            resource_type=params['healthcheck_type'])
    else:
        changed, json_output['healthcheck'] = update_healthcheck(
            client,
            healthcheck=healthcheck,
            params=params,
            name=params['healthcheck_name'],
            project_id=conn_params['project_id'],
            resource_type=params['healthcheck_type'])
    json_output['changed'] = changed
    json_output.update(params)
    module.exit_json(**json_output)
Ejemplo n.º 6
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            state=dict(choices=['absent', 'present'], default='present'),
            service_account_email=dict(),
            service_account_permissions=dict(type='list'),
            pem_file=dict(),
            credentials_file=dict(),
            labels=dict(required=False, type='dict', default={}),
            resource_url=dict(required=False, type='str'),
            resource_name=dict(required=False, type='str'),
            resource_location=dict(required=False, type='str'),
            resource_type=dict(required=False, type='str'),
            project_id=dict()
        ),
        required_together=[
            ['resource_name', 'resource_location', 'resource_type']
        ],
        mutually_exclusive=[
            ['resource_url', 'resource_name'],
            ['resource_url', 'resource_location'],
            ['resource_url', 'resource_type']
        ]
    )

    if not HAS_PYTHON26:
        module.fail_json(
            msg="GCE module requires python's 'ast' module, python v2.6+")

    client, cparams = get_google_api_client(module, 'compute',
                                            user_agent_product=UA_PRODUCT,
                                            user_agent_version=UA_VERSION,
                                            api_version=GCE_API_VERSION)

    # Get current resource info including labelFingerprint
    fingerprint, resource_info = _fetch_resource(client, module)
    new_labels = resource_info['labels'].copy()

    update_needed = False
    if module.params['state'] == 'absent':
        for k, v in module.params['labels'].items():
            if k in new_labels:
                if new_labels[k] == v:
                    update_needed = True
                    new_labels.pop(k, None)
                else:
                    module.fail_json(msg="Could not remove unmatched label pair '%s':'%s'" % (k, v))
    else:
        for k, v in module.params['labels'].items():
            if k not in new_labels:
                update_needed = True
                new_labels[k] = v

    changed = False
    json_output = {'state': module.params['state']}
    if update_needed:
        changed, err = _set_labels(client, new_labels, module, resource_info,
                                   fingerprint)
    json_output['changed'] = changed

    # TODO(erjohnso): probably want to re-fetch the resource to return the
    # new labelFingerprint, check that desired labels match updated labels.
    # BUT! Will need to wait for setLabels() to hit v1 API so we can use the
    # GCPUtils feature to poll for the operation to be complete. For now,
    # we'll just update the output with what we have from the original
    # state of the resource.
    json_output.update(resource_info)
    json_output.update(module.params)

    module.exit_json(**json_output)
Ejemplo n.º 7
0
def main():
    module = AnsibleModule(argument_spec=dict(
        forwarding_rule_name=dict(required=True),
        region=dict(required=True),
        target=dict(required=False),
        address=dict(type='str', required=False),
        protocol=dict(required=False, default='TCP', choices=['TCP']),
        port_range=dict(required=False),
        load_balancing_scheme=dict(required=False,
                                   default='EXTERNAL',
                                   choices=['EXTERNAL']),
        state=dict(required=True, choices=['absent', 'present']),
        service_account_email=dict(),
        service_account_permissions=dict(type='list'),
        pem_file=dict(),
        credentials_file=dict(),
        project_id=dict(),
    ), )

    client, conn_params = get_google_api_client(
        module,
        'compute',
        user_agent_product=USER_AGENT_PRODUCT,
        user_agent_version=USER_AGENT_VERSION)

    params = {}
    params['state'] = module.params.get('state')
    params['forwarding_rule_name'] = module.params.get('forwarding_rule_name')
    params['region'] = module.params.get('region')
    params['target'] = module.params.get('target', None)
    params['protocol'] = module.params.get('protocol', None)
    params['port_range'] = module.params.get('port_range')
    if module.params.get('address', None):
        params['address'] = module.params.get('address', None)

    if params['region'] != 'global':
        # This module currently doesn't support regional rules.
        module.fail_json(
            msg=("%s - Only global forwarding rules currently supported. "
                 "Be sure to specify 'global' for the region option.") %
            (params['forwarding_rule_name']))

    changed = False
    json_output = {'state': params['state']}
    forwarding_rule = None
    if params['region'] == 'global':
        forwarding_rule = get_global_forwarding_rule(
            client,
            name=params['forwarding_rule_name'],
            project_id=conn_params['project_id'])
    if not forwarding_rule:
        if params['state'] == 'absent':
            # Doesn't exist in GCE, and state==absent.
            changed = False
            module.fail_json(msg="Cannot delete unknown forwarding_rule: %s" %
                             (params['forwarding_rule_name']))
        else:
            # Create
            changed, json_output[
                'forwarding_rule'] = create_global_forwarding_rule(
                    client,
                    params=params,
                    project_id=conn_params['project_id'])
    elif params['state'] == 'absent':
        # Delete
        changed, json_output[
            'forwarding_rule'] = delete_global_forwarding_rule(
                client,
                name=params['forwarding_rule_name'],
                project_id=conn_params['project_id'])
    else:
        changed, json_output[
            'forwarding_rule'] = update_global_forwarding_rule(
                client,
                forwarding_rule=forwarding_rule,
                params=params,
                name=params['forwarding_rule_name'],
                project_id=conn_params['project_id'])

    json_output['changed'] = changed
    json_output.update(params)
    module.exit_json(**json_output)
Ejemplo n.º 8
0
def main():
    module = AnsibleModule(argument_spec=dict(
        url_map_name=dict(required=True),
        state=dict(choices=['absent', 'present'], default='present'),
        default_service=dict(required=True),
        path_matchers=dict(type='list', required=False),
        host_rules=dict(type='list', required=False),
        service_account_email=dict(),
        service_account_permissions=dict(type='list'),
        pem_file=dict(),
        credentials_file=dict(),
        project_id=dict(), ), required_together=[
            ['path_matchers', 'host_rules'], ])

    client, conn_params = get_google_api_client(module, 'compute', user_agent_product=USER_AGENT_PRODUCT,
                                                user_agent_version=USER_AGENT_VERSION)

    params = {}
    params['state'] = module.params.get('state')
    params['url_map_name'] = module.params.get('url_map_name')
    params['default_service'] = module.params.get('default_service')
    if module.params.get('path_matchers'):
        params['path_matchers'] = module.params.get('path_matchers')
    if module.params.get('host_rules'):
        params['host_rules'] = module.params.get('host_rules')

    try:
        _validate_params(params)
    except Exception as e:
        module.fail_json(msg=e.message, changed=False)

    changed = False
    json_output = {'state': params['state']}
    url_map = get_url_map(client,
                          name=params['url_map_name'],
                          project_id=conn_params['project_id'])

    if not url_map:
        if params['state'] == 'absent':
            # Doesn't exist in GCE, and state==absent.
            changed = False
            module.fail_json(
                msg="Cannot delete unknown url_map: %s" %
                (params['url_map_name']))
        else:
            # Create
            changed, json_output['url_map'] = create_url_map(client,
                                                             params=params,
                                                             project_id=conn_params['project_id'])
    elif params['state'] == 'absent':
        # Delete
        changed, json_output['url_map'] = delete_url_map(client,
                                                         name=params['url_map_name'],
                                                         project_id=conn_params['project_id'])
    else:
        changed, json_output['url_map'] = update_url_map(client,
                                                         url_map=url_map,
                                                         params=params,
                                                         name=params['url_map_name'],
                                                         project_id=conn_params['project_id'])
        json_output['updated_url_map'] = changed

    json_output['changed'] = changed
    json_output.update(params)
    module.exit_json(**json_output)
Ejemplo n.º 9
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            state=dict(choices=['absent', 'present'], default='present'),
            service_account_email=dict(),
            service_account_permissions=dict(type='list'),
            pem_file=dict(),
            credentials_file=dict(),
            labels=dict(required=False, type='dict', default={}),
            resource_url=dict(required=False, type='str'),
            resource_name=dict(required=False, type='str'),
            resource_location=dict(required=False, type='str'),
            resource_type=dict(required=False, type='str'),
            project_id=dict()
        ),
        required_together=[
            ['resource_name', 'resource_location', 'resource_type']
        ],
        mutually_exclusive=[
            ['resource_url', 'resource_name'],
            ['resource_url', 'resource_location'],
            ['resource_url', 'resource_type']
        ]
    )

    if not HAS_PYTHON26:
        module.fail_json(
            msg="GCE module requires python's 'ast' module, python v2.6+")

    client, cparams = get_google_api_client(module, 'compute',
                                            user_agent_product=UA_PRODUCT,
                                            user_agent_version=UA_VERSION,
                                            api_version=GCE_API_VERSION)

    # Get current resource info including labelFingerprint
    fingerprint, resource_info = _fetch_resource(client, module)
    new_labels = resource_info['labels'].copy()

    update_needed = False
    if module.params['state'] == 'absent':
        for k, v in module.params['labels'].items():
            if k in new_labels:
                if new_labels[k] == v:
                    update_needed = True
                    new_labels.pop(k, None)
                else:
                    module.fail_json(msg="Could not remove unmatched label pair '%s':'%s'" % (k, v))
    else:
        for k, v in module.params['labels'].items():
            if k not in new_labels:
                update_needed = True
                new_labels[k] = v

    changed = False
    json_output = {'state': module.params['state']}
    if update_needed:
        changed, err = _set_labels(client, new_labels, module, resource_info,
                                   fingerprint)
    json_output['changed'] = changed

    # TODO(erjohnso): probably want to re-fetch the resource to return the
    # new labelFingerprint, check that desired labels match updated labels.
    # BUT! Will need to wait for setLabels() to hit v1 API so we can use the
    # GCPUtils feature to poll for the operation to be complete. For now,
    # we'll just update the output with what we have from the original
    # state of the resource.
    json_output.update(resource_info)
    json_output.update(module.params)

    module.exit_json(**json_output)
Ejemplo n.º 10
0
def main():
    module = AnsibleModule(argument_spec=dict(
        healthcheck_name=dict(required=True),
        healthcheck_type=dict(required=True,
                              choices=['HTTP', 'HTTPS']),
        request_path=dict(required=False, default='/'),
        check_interval=dict(required=False, type='int', default=5),
        healthy_threshold=dict(required=False, type='int', default=2),
        unhealthy_threshold=dict(required=False, type='int', default=2),
        host_header=dict(required=False, type='str', default=''),
        timeout=dict(required=False, type='int', default=5),
        port=dict(required=False, type='int'),
        state=dict(choices=['absent', 'present'], default='present'),
        service_account_email=dict(),
        service_account_permissions=dict(type='list'),
        credentials_file=dict(),
        project_id=dict(), ), )

    client, conn_params = get_google_api_client(module, 'compute', user_agent_product=USER_AGENT_PRODUCT,
                                                user_agent_version=USER_AGENT_VERSION)

    params = {}

    params['healthcheck_name'] = module.params.get('healthcheck_name')
    params['healthcheck_type'] = module.params.get('healthcheck_type')
    params['request_path'] = module.params.get('request_path')
    params['check_interval'] = module.params.get('check_interval')
    params['healthy_threshold'] = module.params.get('healthy_threshold')
    params['unhealthy_threshold'] = module.params.get('unhealthy_threshold')
    params['host_header'] = module.params.get('host_header')
    params['timeout'] = module.params.get('timeout')
    params['port'] = module.params.get('port', None)
    params['state'] = module.params.get('state')

    if not params['port']:
        params['port'] = 80
        if params['healthcheck_type'] == 'HTTPS':
            params['port'] = 443
    try:
        _validate_healthcheck_params(params)
    except Exception as e:
        module.fail_json(msg=e.message, changed=False)

    changed = False
    json_output = {'state': params['state']}
    healthcheck = get_healthcheck(client,
                                  name=params['healthcheck_name'],
                                  project_id=conn_params['project_id'],
                                  resource_type=params['healthcheck_type'])

    if not healthcheck:
        if params['state'] == 'absent':
            # Doesn't exist in GCE, and state==absent.
            changed = False
            module.fail_json(
                msg="Cannot delete unknown healthcheck: %s" %
                (params['healthcheck_name']))
        else:
            # Create
            changed, json_output['healthcheck'] = create_healthcheck(client,
                                                                     params=params,
                                                                     project_id=conn_params['project_id'],
                                                                     resource_type=params['healthcheck_type'])
    elif params['state'] == 'absent':
        # Delete
        changed, json_output['healthcheck'] = delete_healthcheck(client,
                                                                 name=params['healthcheck_name'],
                                                                 project_id=conn_params['project_id'],
                                                                 resource_type=params['healthcheck_type'])
    else:
        changed, json_output['healthcheck'] = update_healthcheck(client,
                                                                 healthcheck=healthcheck,
                                                                 params=params,
                                                                 name=params['healthcheck_name'],
                                                                 project_id=conn_params['project_id'],
                                                                 resource_type=params['healthcheck_type'])
    json_output['changed'] = changed
    json_output.update(params)
    module.exit_json(**json_output)