def update_global_forwarding_rule(client, forwarding_rule, params, name,
                                  project_id):
    """
    Update a Global Forwarding_Rule. Currently, only a target can be updated.

    If the forwarding_rule has not changed, the update will not occur.

    :param client: An initialized GCE Compute Discovery resource.
    :type client:  :class: `googleapiclient.discovery.Resource`

    :param forwarding_rule: Name of the Target Proxy.
    :type forwarding_rule:  ``dict``

    :param params: Dictionary of arguments from AnsibleModule.
    :type params:  ``dict``

    :param name: Name of the Global Forwarding Rule.
    :type name:  ``str``

    :param project_id: The GCP project ID.
    :type project_id:  ``str``

    :return: Tuple with changed status and response dict
    :rtype: ``tuple`` in the format of (bool, dict)
    """
    gcp_dict = _build_global_forwarding_rule_dict(params, project_id)

    GCPUtils.are_params_equal(forwarding_rule, gcp_dict)
    if forwarding_rule['target'] == gcp_dict['target']:
        return (False, 'no update necessary')

    try:
        req = client.globalForwardingRules().setTarget(
            project=project_id,
            forwardingRule=name,
            body={'target': gcp_dict['target']})
        return_data = GCPUtils.execute_api_client_req(req,
                                                      client=client,
                                                      raw=False)
        return (True, return_data)
    except Exception:
        raise
def update_healthcheck(client, healthcheck, params, name, project_id,
                       resource_type='HTTP'):
    """
    Update a Healthcheck.

    If the healthcheck has not changed, the update will not occur.

    :param client: An initialized GCE Compute Discovery resource.
    :type client:  :class: `googleapiclient.discovery.Resource`

    :param healthcheck: Name of the Url Map.
    :type healthcheck:  ``dict``

    :param params: Dictionary of arguments from AnsibleModule.
    :type params:  ``dict``

    :param name: Name of the Url Map.
    :type name:  ``str``

    :param project_id: The GCP project ID.
    :type project_id:  ``str``

    :return: Tuple with changed status and response dict
    :rtype: ``tuple`` in the format of (bool, dict)
    """
    gcp_dict = _build_healthcheck_dict(params)
    ans = GCPUtils.are_params_equal(healthcheck, gcp_dict)
    if ans:
        return (False, 'no update necessary')

    try:
        resource, entity_name = _get_req_resource(client, resource_type)
        args = {'project': project_id, entity_name: name, 'body': gcp_dict}
        req = resource.update(**args)
        return_data = GCPUtils.execute_api_client_req(
            req, client=client, raw=False)
        return (True, return_data)
    except Exception:
        raise
def update_url_map(client, url_map, params, name, project_id):
    """
    Update a Url_Map.

    If the url_map has not changed, the update will not occur.

    :param client: An initialized GCE Compute Discovery resource.
    :type client:  :class: `googleapiclient.discovery.Resource`

    :param url_map: Name of the Url Map.
    :type url_map:  ``dict``

    :param params: Dictionary of arguments from AnsibleModule.
    :type params:  ``dict``

    :param name: Name of the Url Map.
    :type name:  ``str``

    :param project_id: The GCP project ID.
    :type project_id:  ``str``

    :return: Tuple with changed status and response dict
    :rtype: ``tuple`` in the format of (bool, dict)
    """
    gcp_dict = _build_url_map_dict(params, project_id)

    ans = GCPUtils.are_params_equal(url_map, gcp_dict)
    if ans:
        return (False, 'no update necessary')

    gcp_dict['fingerprint'] = url_map['fingerprint']
    try:
        req = client.urlMaps().update(project=project_id,
                                      urlMap=name, body=gcp_dict)
        return_data = GCPUtils.execute_api_client_req(req, client=client, raw=False)
        return (True, return_data)
    except Exception:
        raise
Exemple #4
0
    def test_are_params_equal(self):
        params1 = {'one': 1}
        params2 = {'one': 1}
        actual = GCPUtils.are_params_equal(params1, params2)
        self.assertTrue(actual)

        params1 = {'one': 1}
        params2 = {'two': 2}
        actual = GCPUtils.are_params_equal(params1, params2)
        self.assertFalse(actual)

        params1 = {'three': 3, 'two': 2, 'one': 1}
        params2 = {'one': 1, 'two': 2, 'three': 3}
        actual = GCPUtils.are_params_equal(params1, params2)
        self.assertTrue(actual)

        params1 = {
            "creationTimestamp":
            "2017-04-21T11:19:20.718-07:00",
            "defaultService":
            "https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/default-backend-service",
            "description":
            "",
            "fingerprint":
            "ickr_pwlZPU=",
            "hostRules": [{
                "description": "",
                "hosts": ["*."],
                "pathMatcher": "path-matcher-one"
            }],
            "id":
            "8566395781175047111",
            "kind":
            "compute#urlMap",
            "name":
            "newtesturlmap-foo",
            "pathMatchers": [{
                "defaultService":
                "https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/bes-pathmatcher-one-default",
                "description":
                "path matcher one",
                "name":
                "path-matcher-one",
                "pathRules": [{
                    "paths": ["/data", "/aboutus"],
                    "service":
                    "https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/my-one-bes"
                }]
            }],
            "selfLink":
            "https://www.googleapis.com/compute/v1/projects/myproject/global/urlMaps/newtesturlmap-foo"
        }
        params2 = {
            "defaultService":
            "https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/default-backend-service",
            "hostRules": [{
                "description": "",
                "hosts": ["*."],
                "pathMatcher": "path-matcher-one"
            }],
            "name":
            "newtesturlmap-foo",
            "pathMatchers": [{
                "defaultService":
                "https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/bes-pathmatcher-one-default",
                "description":
                "path matcher one",
                "name":
                "path-matcher-one",
                "pathRules": [{
                    "paths": ["/data", "/aboutus"],
                    "service":
                    "https://www.googleapis.com/compute/v1/projects/myproject/global/backendServices/my-one-bes"
                }]
            }],
        }

        # params1 has exclude fields, params2 doesn't. Should be equal
        actual = GCPUtils.are_params_equal(params1, params2)
        self.assertTrue(actual)