Skip to content

Commit

Permalink
improve documentation of the DBClient
Browse files Browse the repository at this point in the history
* Add docstrings for GetClusterDoc, DeleteClusterDoc, and
  GetSubscriptionDoc
* Rename partitionKey parameter to subscriptionID for clarity

Signed-off-by: Michael Shen <[email protected]>
  • Loading branch information
mjlshen committed Jun 5, 2024
1 parent 31ada3f commit 3a5e617
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
14 changes: 9 additions & 5 deletions frontend/pkg/database/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import "context"

var _ DBClient = &Cache{}

// Cache is a simple DBClient that allows us to perform simple tests without needing a real CosmosDB. For production,
// use CosmosDBClient instead. Call NewCache() to initialize a Cache correctly.
type Cache struct {
cluster map[string]*HCPOpenShiftClusterDocument
subscription map[string]*SubscriptionDocument
}

// NewCache initializes a new Cache to allow for simple tests without needing a real CosmosDB. For production, use
// NewCosmosDBConfig instead.
func NewCache() DBClient {
return &Cache{
cluster: make(map[string]*HCPOpenShiftClusterDocument),
Expand All @@ -20,7 +24,7 @@ func (c *Cache) DBConnectionTest(ctx context.Context) error {
return nil
}

func (c *Cache) GetClusterDoc(ctx context.Context, resourceID string, partitionKey string) (*HCPOpenShiftClusterDocument, error) {
func (c *Cache) GetClusterDoc(ctx context.Context, resourceID string, subscriptionID string) (*HCPOpenShiftClusterDocument, error) {
if _, ok := c.cluster[resourceID]; ok {
return c.cluster[resourceID], nil
}
Expand All @@ -33,14 +37,14 @@ func (c *Cache) SetClusterDoc(ctx context.Context, doc *HCPOpenShiftClusterDocum
return nil
}

func (c *Cache) DeleteClusterDoc(ctx context.Context, resourceID string, partitionKey string) error {
func (c *Cache) DeleteClusterDoc(ctx context.Context, resourceID string, subscriptionID string) error {
delete(c.cluster, resourceID)
return nil
}

func (c *Cache) GetSubscriptionDoc(ctx context.Context, partitionKey string) (*SubscriptionDocument, error) {
if _, ok := c.subscription[partitionKey]; ok {
return c.subscription[partitionKey], nil
func (c *Cache) GetSubscriptionDoc(ctx context.Context, subscriptionID string) (*SubscriptionDocument, error) {
if _, ok := c.subscription[subscriptionID]; ok {
return c.subscription[subscriptionID], nil
}

return nil, ErrNotFound
Expand Down
37 changes: 23 additions & 14 deletions frontend/pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ type DBClient interface {
// to be used, an error should be returned.
DBConnectionTest(ctx context.Context) error

GetClusterDoc(ctx context.Context, resourceID string, partitionKey string) (*HCPOpenShiftClusterDocument, error)
// GetClusterDoc retrieves an HCPOpenShiftClusterDocument from the database given its resourceID and containing
// subscriptionID. ErrNotFound is returned if an associated HCPOpenShiftClusterDocument cannot be found.
GetClusterDoc(ctx context.Context, resourceID string, subscriptionID string) (*HCPOpenShiftClusterDocument, error)
SetClusterDoc(ctx context.Context, doc *HCPOpenShiftClusterDocument) error
DeleteClusterDoc(ctx context.Context, resourceID string, partitionKey string) error
// DeleteClusterDoc deletes an HCPOpenShiftClusterDocument from the database given the resourceID and containing
// subscriptionID of a Microsoft.RedHatOpenshift/HcpOpenShiftClusters resource.
DeleteClusterDoc(ctx context.Context, resourceID string, subscriptionID string) error

GetSubscriptionDoc(ctx context.Context, partitionKey string) (*SubscriptionDocument, error)
// GetSubscriptionDoc retrieves a SubscriptionDocument from the database given the subscriptionID.
// ErrNotFound is returned if an associated SubscriptionDocument cannot be found.
GetSubscriptionDoc(ctx context.Context, subscriptionID string) (*SubscriptionDocument, error)
SetSubscriptionDoc(ctx context.Context, doc *SubscriptionDocument) error
}

Expand Down Expand Up @@ -94,7 +100,7 @@ func (d *CosmosDBClient) DBConnectionTest(ctx context.Context) error {
}

// GetClusterDoc retrieves a cluster document from async DB using resource ID
func (d *CosmosDBClient) GetClusterDoc(ctx context.Context, resourceID string, partitionKey string) (*HCPOpenShiftClusterDocument, error) {
func (d *CosmosDBClient) GetClusterDoc(ctx context.Context, resourceID string, subscriptionID string) (*HCPOpenShiftClusterDocument, error) {
container, err := d.client.NewContainer(d.config.DBName, clustersContainer)
if err != nil {
return nil, err
Expand All @@ -106,7 +112,7 @@ func (d *CosmosDBClient) GetClusterDoc(ctx context.Context, resourceID string, p
QueryParameters: []azcosmos.QueryParameter{{Name: "@key", Value: resourceID}},
}

pk := azcosmos.NewPartitionKeyString(partitionKey)
pk := azcosmos.NewPartitionKeyString(subscriptionID)
queryPager := container.NewQueryItemsPager(query, pk, &opt)

var doc *HCPOpenShiftClusterDocument
Expand Down Expand Up @@ -149,27 +155,30 @@ func (d *CosmosDBClient) SetClusterDoc(ctx context.Context, doc *HCPOpenShiftClu
return nil
}

// DeleteClusterDoc removes a cluter document from the async DB using resource ID
func (d *CosmosDBClient) DeleteClusterDoc(ctx context.Context, resourceID string, partitionKey string) error {
doc, err := d.GetClusterDoc(ctx, resourceID, partitionKey)
// DeleteClusterDoc removes a cluster document from the async DB using resource ID
func (d *CosmosDBClient) DeleteClusterDoc(ctx context.Context, resourceID string, subscriptionID string) error {
doc, err := d.GetClusterDoc(ctx, resourceID, subscriptionID)
if err != nil {
return err
if errors.Is(err, ErrNotFound) {
return nil
}
return fmt.Errorf("while attempting to delete the cluster, failed to get cluster document: %w", err)
}

container, err := d.client.NewContainer(d.config.DBName, clustersContainer)
if err != nil {
return err
}

_, err = container.DeleteItem(ctx, azcosmos.NewPartitionKeyString(partitionKey), doc.ID, nil)
_, err = container.DeleteItem(ctx, azcosmos.NewPartitionKeyString(subscriptionID), doc.ID, nil)
if err != nil {
return err
}
return nil
}

// GetSubscriptionDoc retreives a subscription document from async DB using the subscription ID
func (d *CosmosDBClient) GetSubscriptionDoc(ctx context.Context, partitionKey string) (*SubscriptionDocument, error) {
func (d *CosmosDBClient) GetSubscriptionDoc(ctx context.Context, subscriptionID string) (*SubscriptionDocument, error) {
container, err := d.client.NewContainer(d.config.DBName, subsContainer)
if err != nil {
return nil, err
Expand All @@ -178,10 +187,10 @@ func (d *CosmosDBClient) GetSubscriptionDoc(ctx context.Context, partitionKey st
query := "SELECT * FROM c WHERE c.partitionKey = @partitionKey"
opt := azcosmos.QueryOptions{
PageSizeHint: 1,
QueryParameters: []azcosmos.QueryParameter{{Name: "@partitionKey", Value: partitionKey}},
QueryParameters: []azcosmos.QueryParameter{{Name: "@partitionKey", Value: subscriptionID}},
}

pk := azcosmos.NewPartitionKeyString(partitionKey)
pk := azcosmos.NewPartitionKeyString(subscriptionID)
queryPager := container.NewQueryItemsPager(query, pk, &opt)

var doc *SubscriptionDocument
Expand All @@ -204,7 +213,7 @@ func (d *CosmosDBClient) GetSubscriptionDoc(ctx context.Context, partitionKey st
return nil, ErrNotFound
}

// SetClusterDoc creates/updates a subscription document in the async DB during cluster creation/patching
// SetSubscriptionDoc creates/updates a subscription document in the async DB during cluster creation/patching
func (d *CosmosDBClient) SetSubscriptionDoc(ctx context.Context, doc *SubscriptionDocument) error {
data, err := json.Marshal(doc)
if err != nil {
Expand Down

0 comments on commit 3a5e617

Please sign in to comment.