2014-04-28 00:06:09 +02:00
|
|
|
package registry
|
|
|
|
|
|
|
|
type Service struct {
|
2014-10-07 03:54:52 +02:00
|
|
|
Config *ServiceConfig
|
2014-04-28 00:06:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService returns a new instance of Service ready to be
|
|
|
|
// installed no an engine.
|
2014-10-07 03:54:52 +02:00
|
|
|
func NewService(options *Options) *Service {
|
2014-08-20 17:31:24 +02:00
|
|
|
return &Service{
|
2014-10-07 03:54:52 +02:00
|
|
|
Config: NewServiceConfig(options),
|
2014-08-20 17:31:24 +02:00
|
|
|
}
|
2014-04-28 00:06:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Auth contacts the public registry with the provided credentials,
|
|
|
|
// and returns OK if authentication was sucessful.
|
|
|
|
// It can be used to verify the validity of a client's credentials.
|
2015-04-01 01:21:37 +02:00
|
|
|
func (s *Service) Auth(authConfig *AuthConfig) (string, error) {
|
2014-12-12 02:55:15 +01:00
|
|
|
addr := authConfig.ServerAddress
|
|
|
|
if addr == "" {
|
|
|
|
// Use the official registry address if not specified.
|
|
|
|
addr = IndexServerAddress()
|
2014-04-28 00:06:09 +02:00
|
|
|
}
|
2015-04-01 01:21:37 +02:00
|
|
|
index, err := s.ResolveIndex(addr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2014-04-28 00:06:09 +02:00
|
|
|
}
|
2015-04-01 01:21:37 +02:00
|
|
|
endpoint, err := NewEndpoint(index)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2014-12-12 02:55:15 +01:00
|
|
|
}
|
|
|
|
authConfig.ServerAddress = endpoint.String()
|
2015-04-01 01:21:37 +02:00
|
|
|
return Login(authConfig, endpoint, HTTPRequestFactory(nil))
|
2014-04-28 00:06:09 +02:00
|
|
|
}
|
2014-04-28 00:21:42 +02:00
|
|
|
|
|
|
|
// Search queries the public registry for images matching the specified
|
|
|
|
// search terms, and returns the results.
|
2015-04-01 01:21:37 +02:00
|
|
|
func (s *Service) Search(term string, authConfig *AuthConfig, headers map[string][]string) (*SearchResults, error) {
|
|
|
|
repoInfo, err := s.ResolveRepository(term)
|
2014-08-09 09:16:54 +02:00
|
|
|
if err != nil {
|
2015-04-01 01:21:37 +02:00
|
|
|
return nil, err
|
2014-08-09 09:16:54 +02:00
|
|
|
}
|
2014-10-07 03:54:52 +02:00
|
|
|
// *TODO: Search multiple indexes.
|
|
|
|
endpoint, err := repoInfo.GetEndpoint()
|
2014-08-09 09:16:54 +02:00
|
|
|
if err != nil {
|
2015-04-01 01:21:37 +02:00
|
|
|
return nil, err
|
2014-04-28 00:21:42 +02:00
|
|
|
}
|
2015-04-01 01:21:37 +02:00
|
|
|
r, err := NewSession(authConfig, HTTPRequestFactory(headers), endpoint, true)
|
2014-04-28 00:21:42 +02:00
|
|
|
if err != nil {
|
2015-04-01 01:21:37 +02:00
|
|
|
return nil, err
|
2014-04-28 00:21:42 +02:00
|
|
|
}
|
2015-04-01 01:21:37 +02:00
|
|
|
return r.SearchRepositories(repoInfo.GetSearchTerm())
|
2014-04-28 00:21:42 +02:00
|
|
|
}
|
2014-10-07 03:54:52 +02:00
|
|
|
|
|
|
|
// ResolveRepository splits a repository name into its components
|
|
|
|
// and configuration of the associated registry.
|
2015-04-01 01:21:37 +02:00
|
|
|
func (s *Service) ResolveRepository(name string) (*RepositoryInfo, error) {
|
|
|
|
return s.Config.NewRepositoryInfo(name)
|
2014-10-07 03:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveIndex takes indexName and returns index info
|
2015-04-01 01:21:37 +02:00
|
|
|
func (s *Service) ResolveIndex(name string) (*IndexInfo, error) {
|
|
|
|
return s.Config.NewIndexInfo(name)
|
2014-10-07 03:54:52 +02:00
|
|
|
}
|