Skip to content

Commit

Permalink
Merge pull request #76 from philips-software/feature/docker-repositories
Browse files Browse the repository at this point in the history
Docker Registry: Add repositories management
  • Loading branch information
loafoe authored Oct 26, 2021
2 parents 1dd882e + 49f290d commit 7e38291
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

# v0.48.0

- Docker Registry: service keys, namespace and repository management

# v0.47.0

- Logging: Add traceId and spanId fields
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ The current implement covers only a subset of HSDP APIs. Basically, we implement
- [x] Docker Registry
- [x] Service Keys management
- [x] Namespace management
- [ ] Repository management
- [x] Repository management
- [x] IronIO tasks, codes and schedules management ([examples](iron/README.md))
- [x] Clinical Data Lake (CDL) management
- [x] Research Studies
Expand Down
6 changes: 4 additions & 2 deletions console/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ type Client struct {

debugFile *os.File

ServiceKeys *ServiceKeysService
Namespaces *NamespacesService
ServiceKeys *ServiceKeysService
Namespaces *NamespacesService
Repositories *RepositoriesService
}

// NewClient returns a new HSDP Docker Registry API client. A configured console client
Expand Down Expand Up @@ -72,6 +73,7 @@ func newClient(consoleClient *console.Client, config *Config) (*Client, error) {
c.gql = graphql.NewClient(config.DockerAPIURL, consoleClient.Client)
c.ServiceKeys = &ServiceKeysService{client: c}
c.Namespaces = &NamespacesService{client: c}
c.Repositories = &RepositoriesService{client: c}

return c, nil
}
Expand Down
17 changes: 17 additions & 0 deletions console/docker/namespaces_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,23 @@ func (s *NamespacesService) AddNamespaceUser(ctx context.Context, namespaceID, u
return &mutation.AddUserToNamespace, nil
}

func (s *NamespacesService) GetRepositories(ctx context.Context, namespaceId string) (*[]Repository, error) {
var query struct {
Resources struct {
Repositories []Repository
} `graphql:"namespace(id: $namespaceId)"`
}
err := s.client.gql.Query(ctx, &query, map[string]interface{}{
"namespaceId": graphql.String(namespaceId),
})
if err != nil {
return nil, err
}
repositories := make([]Repository, 0)
repositories = append(repositories, query.Resources.Repositories...)
return &repositories, nil
}

func (s *NamespacesService) DeleteNamespaceUser(ctx context.Context, namespaceID, username string) error {
var mutation struct {
DeleteUserFromNamespace bool `graphql:"removeUserFromNamespace(namespaceId: $namespaceId, userId: $userId)"`
Expand Down
127 changes: 127 additions & 0 deletions console/docker/repositories_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package docker

import (
"context"
"fmt"
"time"

"github.com/hasura/go-graphql-client"
)

type RepositoriesService struct {
client *Client
}

type Repository struct {
ID string `json:"id"`
NamespaceId string `json:"namespaceId"`
Name string `json:"name"`
NumPulls int `json:"numPulls"`
NumTags int `json:"numTags"`
}

type Tag struct {
ID int `json:"id"`
Name string `json:"name"`
CompressedSize int `json:"compressedSize"`
UpdatedAt time.Time `json:"updatedAt"`
NumPulls int `json:"numPulls"`
Digest string `json:"digest"`
ImageId string `json:"imageId"`
}

type RepositoryInput struct {
NamespaceID string `json:"namespaceId"`
Name string `json:"name"`
}

type RepositoryDetailsInput struct {
ShortDescription string `json:"shortDescription"`
FullDescription string `json:"fullDescription"`
}

type RepositoryResult struct {
ID string `json:"id"`
Name string `json:"name"`
NamespaceID string `json:"namespaceId"`
}

func (r *RepositoriesService) CreateRepository(ctx context.Context, repository RepositoryInput, details RepositoryDetailsInput) (*RepositoryResult, error) {
var mutation struct {
Repository RepositoryResult `graphql:"createRepository(repository: $repository, details: $details)"`
}
err := r.client.gql.Mutate(ctx, &mutation, map[string]interface{}{
"repository": repository,
"details": details,
})
if err != nil {
return nil, err
}
if mutation.Repository.ID == "" {
return nil, fmt.Errorf("error creating repository")
}
return &mutation.Repository, nil
}

func (r *RepositoriesService) UpdateRepository(ctx context.Context, repository Repository, details RepositoryDetailsInput) (*RepositoryDetailsInput, error) {
var mutation struct {
Resources struct {
Details RepositoryDetailsInput
} `graphql:"updateRepository(id: $id, details: $details)"`
}
err := r.client.gql.Mutate(ctx, &mutation, map[string]interface{}{
"id": graphql.String(repository.ID),
"details": details,
})
if err != nil {
return nil, err
}
return &mutation.Resources.Details, nil
}

func (r *RepositoriesService) DeleteRepository(ctx context.Context, repository Repository) error {
var mutation struct {
DeleteRepository bool `graphql:"deleteRepository(id: $id)"`
}
err := r.client.gql.Mutate(ctx, &mutation, map[string]interface{}{
"id": graphql.String(repository.ID),
})
if err != nil {
return fmt.Errorf("eror deleting repository: %w", err)
}
if !mutation.DeleteRepository {
return fmt.Errorf("failed to delete repository")
}
return nil
}

func (r *RepositoriesService) GetRepository(ctx context.Context, namespaceId, name string) (*Repository, error) {
var query struct {
Repository Repository `graphql:"repository(namespaceId: $namespaceId, name: $name)"`
}
err := r.client.gql.Query(ctx, &query, map[string]interface{}{
"namespaceId": graphql.String(namespaceId),
"name": graphql.String(name),
})
if err != nil {
return nil, err
}
return &query.Repository, nil
}

func (r *RepositoriesService) GetTags(ctx context.Context, repositoryId string) (*[]Tag, error) {
var query struct {
Tags []Tag `graphql:"tags(repositoryId: $repositoryId, page: $page, limit: $limit, orderBy: UPDATED_AT)"`
}
err := r.client.gql.Query(ctx, &query, map[string]interface{}{
"repositoryId": graphql.String(repositoryId),
"page": graphql.Int(1),
"limit": graphql.Int(1000),
})
if err != nil {
return nil, err
}
tags := make([]Tag, 0)
tags = append(tags, query.Tags...)
return &tags, nil
}
30 changes: 8 additions & 22 deletions console/docker/service_keys_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,16 @@ func (a *ServiceKeysService) GetServiceKeys(ctx context.Context) (*[]ServiceKeyN
}

func (a *ServiceKeysService) GetServiceKeyByID(ctx context.Context, id int) (*ServiceKeyNode, error) {
// TODO: https://github.com/philips-internal/hsdp-docker-api/pull/3
/*
var query struct {
ServiceKeyNode ServiceKeyNode `graphql:"serviceKey(id: $keyId)"`
}
err := a.client.gql.Query(ctx, &query, map[string]interface{}{
"keyId": graphql.String(strconv.Itoa(id)),
})
if err != nil {
return nil, fmt.Errorf("service key read: %w", err)
}
return &query.ServiceKeyNode, nil
*/
// Simulate for now
keys, err := a.GetServiceKeys(ctx)
if err != nil {
return nil, err
var query struct {
ServiceKeyNode ServiceKeyNode `graphql:"serviceKey(id: $keyId)"`
}
for _, k := range *keys {
if k.ID == id {
return &k, nil
}
err := a.client.gql.Query(ctx, &query, map[string]interface{}{
"keyId": graphql.Int(id),
})
if err != nil {
return nil, fmt.Errorf("service key read: %w", err)
}
return nil, fmt.Errorf("simulated serviceKey(id: $id) did not find a match for '%d'", id)
return &query.ServiceKeyNode, nil
}

func (a *ServiceKeysService) CreateServiceKey(ctx context.Context, description string) (*ServiceKey, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/version.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package internal

const (
LibraryVersion = "0.47.0"
LibraryVersion = "0.48.0"
)

0 comments on commit 7e38291

Please sign in to comment.