'Creates OperationContract for upsertLoadBalancer.
Calls Spinnaker\'s upsertLoadBalancer with a configuration, then verifies
that the expected resources and configurations are visible on AWS. See
the contract builder for more info on what the expectations are.
Args:
use_vpc: [bool] if True configure a VPC otherwise dont.'

def upsert_load_balancer(self, use_vpc):

    bindings = self.bindings
    context = citest.base.ExecutionContext()
    load_balancer_name = self.lb_name
    if use_vpc:
        subnet_type = 'internal    (defaultvpc)'
        vpc_id = bindings['TEST_AWS_VPC_ID']
        security_groups = [bindings['TEST_AWS_SECURITY_GROUP_ID']]
        security_groups = ['default']
        subnets_response = self.aws_observer.call_method(context, self.ec2_client.describe_subnets, Filters=[{'Name': 'vpc-id', 'Values': [vpc_id]}])
        subnet_details = subnets_response['Subnets']
        try:
            avail_zones = [detail['AvailabilityZone'] for detail in subnet_details]
            region = avail_zones[0][:(-1)]
            if (len(avail_zones) > 2):
                avail_zones = [avail_zones[0], avail_zones[(-1)]]
        except KeyError:
            raise ValueError('vpc_id={0}    appears    to    be    unknown'.format(vpc_id))
    else:
        region = bindings['TEST_AWS_REGION']
        avail_zones = [(region + 'a'), (region + 'b')]
        subnet_type = ''
        vpc_id = None
        security_groups = None
        load_balancer_name += '-pub'
    listener = {'Listener': {'InstancePort': 80, 'LoadBalancerPort': 80}}
    health_check = {'HealthyThreshold': 8, 'UnhealthyThreshold': 3, 'Interval': 12, 'Timeout': 6, 'Target': ('HTTP:%d/' % listener['Listener']['InstancePort'])}
    payload = self.agent.make_json_payload_from_kwargs(job=[{'type': 'upsertLoadBalancer', 'cloudProvider': 'aws', 'credentials': bindings['SPINNAKER_AWS_ACCOUNT'], 'name': load_balancer_name, 'stack': bindings['TEST_STACK'], 'detail': self.lb_detail, 'region': bindings['TEST_AWS_REGION'], 'availabilityZones': {region: avail_zones}, 'regionZones': avail_zones, 'listeners': [{'internalProtocol': 'HTTP', 'internalPort': listener['Listener']['InstancePort'], 'externalProtocol': 'HTTP', 'externalPort': listener['Listener']['LoadBalancerPort']}], 'healthCheck': health_check['Target'], 'healthCheckProtocol': 'HTTP', 'healthCheckPort': listener['Listener']['LoadBalancerPort'], 'healthCheckPath': '/', 'healthTimeout': health_check['Timeout'], 'healthInterval': health_check['Interval'], 'healthyThreshold': health_check['HealthyThreshold'], 'unhealthyThreshold': health_check['UnhealthyThreshold'], 'user': '[anonymous]', 'usePreferredZones': True, 'vpcId': vpc_id, 'subnetType': subnet_type, 'securityGroups': security_groups}], description=('Create    Load    Balancer:    ' + load_balancer_name), application=self.TEST_APP)
    builder = aws.AwsPythonContractBuilder(self.aws_observer)
    builder.new_clause_builder('Load    Balancer    Added', retryable_for_secs=10).call_method(self.elb_client.describe_load_balancers, LoadBalancerNames=[load_balancer_name]).EXPECT(ov_factory.value_list_path_contains('LoadBalancerDescriptions', jp.LIST_MATCHES([jp.DICT_MATCHES({'HealthCheck': jp.DICT_MATCHES({key: jp.EQUIVALENT(value) for (key, value) in health_check.items()}), 'AvailabilityZones': jp.LIST_SIMILAR(avail_zones), 'ListenerDescriptions/Listener': jp.DICT_MATCHES({key: jp.NUM_EQ(value) for (key, value) in listener['Listener'].items()})})])))
    title_decorator = ('_with_vpc' if use_vpc else '_without_vpc')
    return st.OperationContract(self.new_post_operation(title=('upsert_load_balancer' + title_decorator), data=payload, path='tasks'), contract=builder.build())
