Exemplo n.º 1
0
func (s *S) TestQuery(t *C) {
	ctx := s.ctx

	ds := datastore.New()

	k := datastore.NewKey("tweet", "123")
	tweet := &tweettest{"kimchy", "NY", "2010-11-15T14:12:12", "trying out Elasticsearch", version}

	err := ds.Put(ctx, k, tweet)
	if err != nil {
		t.Errorf("%v", err)
	}

	k = datastore.NewKey("tweet", "234")
	tweet = &tweettest{"kimchy2", "NY", "2010-11-15T14:12:12", "trying out Elasticsearch again", version}
	err = ds.Put(ctx, k, tweet)
	if err != nil {
		t.Errorf("%v", err)
	}

	query := search.Query().Search("_exists_:State")
	testSearch := search.Search("twitter").Type("tweet").Size("10000").Query(query)

	q := datastore.NewQuery(ctx)
	msgs, err := q.Execute(testSearch)

	if err != nil {
		t.Errorf("Unepected error %v", err)
	}
	if msgs.Len() != 2 {
		t.Errorf("Expected 2 msgs, got  %v", msgs.Len())
	}

	//query for non-existant entity
	query = search.Query().Search("_exists_:blam")
	testSearch = search.Search("twitter").Type("tweet").Size("10000").Query(query)

	q = datastore.NewQuery(ctx)
	msgs, err = q.Execute(testSearch)

	if err != nil {
		t.Errorf("Unepected error %v", err)
	}
	if msgs.Len() != 0 {
		t.Errorf("Expected 0 msgs, got  %v", msgs.Len())
	}

}
Exemplo n.º 2
0
func (s *Store) FindChildService(ctx datastore.Context, deploymentID, parentID, serviceName string) (*Service, error) {
	parentID = strings.TrimSpace(parentID)

	if deploymentID = strings.TrimSpace(deploymentID); deploymentID == "" {
		return nil, errors.New("empty deployment ID not allowed")
	} else if serviceName = strings.TrimSpace(serviceName); serviceName == "" {
		return nil, errors.New("empty service name not allowed")
	}

	search := search.Search("controlplane").Type(kind).Filter(
		"and",
		search.Filter().Terms("DeploymentID", deploymentID),
		search.Filter().Terms("ParentServiceID", parentID),
		search.Filter().Terms("Name", serviceName),
	)

	q := datastore.NewQuery(ctx)
	results, err := q.Execute(search)
	if err != nil {
		return nil, err
	}

	if results.Len() == 0 {
		return nil, nil
	} else if svcs, err := convert(results); err != nil {
		return nil, err
	} else {
		return &svcs[0], nil
	}
}
Exemplo n.º 3
0
// GetN returns all hosts up to limit.
func (hs *HostStore) GetN(ctx datastore.Context, limit uint64) ([]Host, error) {
	q := datastore.NewQuery(ctx)
	query := search.Query().Search("_exists_:ID")
	search := search.Search("controlplane").Type(kind).Size(strconv.FormatUint(limit, 10)).Query(query)
	results, err := q.Execute(search)
	if err != nil {
		return nil, err
	}
	return convert(results)
}
Exemplo n.º 4
0
func (s *Store) GetServiceAddressAssignments(ctx datastore.Context, serviceID string) ([]AddressAssignment, error) {
	q := datastore.NewQuery(ctx)
	query := search.Query().Term("ServiceID", serviceID)
	search := search.Search("controlplane").Type(kind).Size("50000").Query(query)
	results, err := q.Execute(search)
	if err != nil {
		return nil, err
	}
	return convert(results)
}
Exemplo n.º 5
0
func query(ctx datastore.Context, query string) ([]Service, error) {
	q := datastore.NewQuery(ctx)
	elasticQuery := search.Query().Search(query)
	search := search.Search("controlplane").Type(kind).Size("50000").Query(elasticQuery)
	results, err := q.Execute(search)
	if err != nil {
		return nil, err
	}
	return convert(results)
}
Exemplo n.º 6
0
//GetUpdatedServices returns all services updated since "since" time.Duration ago
func (s *Store) GetUpdatedServices(ctx datastore.Context, since time.Duration) ([]Service, error) {
	q := datastore.NewQuery(ctx)
	t0 := time.Now().Add(-since).Format(time.RFC3339)
	elasticQuery := search.Query().Range(search.Range().Field("UpdatedAt").From(t0)).Search("_exists_:ID")
	search := search.Search("controlplane").Type(kind).Size("50000").Query(elasticQuery)
	results, err := q.Execute(search)
	if err != nil {
		return nil, err
	}
	return convert(results)
}
Exemplo n.º 7
0
// GetServiceTemplates returns all ServiceTemplates
func (s *Store) GetServiceTemplates(ctx datastore.Context) ([]*ServiceTemplate, error) {
	glog.V(3).Infof("Store.GetServiceTemplates")
	q := datastore.NewQuery(ctx)
	query := search.Query().Search("_exists_:ID")
	search := search.Search("controlplane").Type(kind).Query(query)
	results, err := q.Execute(search)
	if err != nil {
		return nil, err
	}
	return convert(results)
}
Exemplo n.º 8
0
// FindHostsWithPoolID returns all hosts with the given poolid.
func (hs *HostStore) FindHostsWithPoolID(ctx datastore.Context, poolID string) ([]Host, error) {
	id := strings.TrimSpace(poolID)
	if id == "" {
		return nil, errors.New("empty poolId not allowed")
	}
	q := datastore.NewQuery(ctx)
	query := search.Query().Term("PoolID", id)
	search := search.Search("controlplane").Type(kind).Size("50000").Query(query)
	results, err := q.Execute(search)
	if err != nil {
		return nil, err
	}
	return convert(results)
}
Exemplo n.º 9
0
func (s *Store) GetServiceAddressAssignmentsByPort(ctx datastore.Context, port uint16) ([]AddressAssignment, error) {
	if port == 0 {
		return nil, fmt.Errorf("port must be greater than 0")
	}

	query := search.Query().Term("Port", strconv.FormatUint(uint64(port), 10))
	search := search.Search("controlplane").Type(kind).Size("50000").Query(query)

	if results, err := datastore.NewQuery(ctx).Execute(search); err != nil {
		return nil, err
	} else {
		return convert(results)
	}
}
Exemplo n.º 10
0
//GetChildServices returns services that are children of the given parent service id
func (s *Store) GetChildServices(ctx datastore.Context, parentID string) ([]Service, error) {
	id := strings.TrimSpace(parentID)
	if id == "" {
		return nil, errors.New("empty parent service id not allowed")
	}
	q := datastore.NewQuery(ctx)
	query := search.Query().Term("ParentServiceID", id)
	search := search.Search("controlplane").Type(kind).Size("50000").Query(query)
	results, err := q.Execute(search)
	if err != nil {
		return nil, err
	}
	return convert(results)
}
Exemplo n.º 11
0
// GetHostByIP looks up a host by the given ip address
func (hs *HostStore) GetHostByIP(ctx datastore.Context, hostIP string) (*Host, error) {
	if hostIP = strings.TrimSpace(hostIP); hostIP == "" {
		return nil, errors.New("empty hostIP not allowed")
	}

	query := search.Query().Term("IPs.IPAddress", hostIP)
	search := search.Search("controlplane").Type(kind).Query(query)
	results, err := datastore.NewQuery(ctx).Execute(search)
	if err != nil {
		return nil, err
	}

	if results.Len() == 0 {
		return nil, nil
	} else if hosts, err := convert(results); err != nil {
		return nil, err
	} else {
		return &hosts[0], nil
	}
}
Exemplo n.º 12
0
func (s *Store) FindAssignmentByHostPort(ctx datastore.Context, ipAddr string, port uint16) (*AddressAssignment, error) {
	if ipAddr = strings.TrimSpace(ipAddr); ipAddr == "" {
		return nil, fmt.Errorf("hostIP cannot be empty")
	} else if port == 0 {
		return nil, fmt.Errorf("port must be greater than 0")
	}

	search := search.Search("controlplane").Type(kind).Filter(
		"and",
		search.Filter().Terms("IPAddr", ipAddr),
		search.Filter().Terms("Port", strconv.FormatUint(uint64(port), 10)),
	)

	if results, err := datastore.NewQuery(ctx).Execute(search); err != nil {
		return nil, err
	} else if output, err := convert(results); err != nil {
		return nil, err
	} else if len(output) == 0 {
		return nil, nil
	} else {
		return &output[0], nil
	}
}
Exemplo n.º 13
0
func (s *Store) FindAssignmentByServiceEndpoint(ctx datastore.Context, serviceID, endpointName string) (*AddressAssignment, error) {
	if serviceID = strings.TrimSpace(serviceID); serviceID == "" {
		return nil, fmt.Errorf("service ID cannot be empty")
	} else if endpointName = strings.TrimSpace(endpointName); endpointName == "" {
		return nil, fmt.Errorf("endpoint name cannot be empty")
	}

	search := search.Search("controlplane").Type(kind).Filter(
		"and",
		search.Filter().Terms("ServiceID", serviceID),
		search.Filter().Terms("EndpointName", endpointName),
	)

	if results, err := datastore.NewQuery(ctx).Execute(search); err != nil {
		return nil, err
	} else if output, err := convert(results); err != nil {
		return nil, err
	} else if len(output) == 0 {
		return nil, nil
	} else {
		return &output[0], nil
	}
}