'Creates OperationContract for upsertLoadBalancer.
Calls Spinnaker\'s upsertLoadBalancer with a configuration, then verifies
that the expected resources and configurations are visible on GCE. See
the contract builder for more info on what the expectations are.'

def upsert_load_balancer(self):

    bindings = self.bindings
    target_pool_name = '{0}/targetPools/{1}-tp'.format(bindings['TEST_GCE_REGION'], self.__lb_name)
    spec = {'checkIntervalSec': 9, 'healthyThreshold': 3, 'unhealthyThreshold': 5, 'timeoutSec': 2, 'port': 80}
    payload = self.agent.make_json_payload_from_kwargs(job=[{'cloudProvider': 'gce', 'provider': 'gce', 'stack': bindings['TEST_STACK'], 'detail': self.__lb_detail, 'credentials': bindings['SPINNAKER_GOOGLE_ACCOUNT'], 'region': bindings['TEST_GCE_REGION'], 'ipProtocol': 'TCP', 'portRange': spec['port'], 'loadBalancerName': self.__lb_name, 'name': self.__lb_name, 'healthCheck': {'port': spec['port'], 'timeoutSec': spec['timeoutSec'], 'checkIntervalSec': spec['checkIntervalSec'], 'healthyThreshold': spec['healthyThreshold'], 'unhealthyThreshold': spec['unhealthyThreshold']}, 'type': 'upsertLoadBalancer', 'availabilityZones': {bindings['TEST_GCE_REGION']: []}, 'user': '[anonymous]'}], description=('Create    Load    Balancer:    ' + self.__lb_name), application=self.TEST_APP)
    hc_match_spec = {key: jp.NUM_EQ(value) for (key, value) in spec.items()}
    hc_match_spec['name'] = jp.STR_SUBSTR(('%s-hc' % self.__lb_name))
    builder = gcp.GcpContractBuilder(self.gcp_observer)
    builder.new_clause_builder('Health    Check    Added', retryable_for_secs=30).list_resource('httpHealthChecks').EXPECT(ov_factory.value_list_contains(jp.DICT_MATCHES(hc_match_spec)))
    builder.new_clause_builder('Target    Pool    Added', retryable_for_secs=30).list_resource('targetPools').EXPECT(ov_factory.value_list_path_contains('name', jp.STR_SUBSTR(('%s-tp' % self.__lb_name))))
    builder.new_clause_builder('Forwarding    Rules    Added', retryable_for_secs=30).list_resource('forwardingRules').EXPECT(ov_factory.value_list_contains(jp.DICT_MATCHES({'name': jp.STR_SUBSTR(self.__lb_name), 'target': jp.STR_SUBSTR(target_pool_name)})))
    return st.OperationContract(self.new_post_operation(title='upsert_load_balancer', data=payload, path='tasks'), contract=builder.build())
