Skip to content

Commit

Permalink
attempts to resuse code
Browse files Browse the repository at this point in the history
  • Loading branch information
tonytheleg committed May 21, 2024
1 parent abbfc31 commit a5f92c5
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 32 deletions.
2 changes: 1 addition & 1 deletion dev-infrastructure/configurations/svc-cluster.bicepparam
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ param podSubnetPrefix = '10.128.64.0/18'
param enablePrivateCluster = false
param persist = false
param disableLocalAuth = false
param deployFrontendCosmos = false
param deployFrontendCosmos = true
param deployMaestroInfra = false
param maestroNamespace = 'maestro'
param workloadIdentities = items({
Expand Down
31 changes: 31 additions & 0 deletions frontend/cluster.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"properties": {
"spec": {
"version": {
"id": "1.19.0",
"channelGroup": "stable"
},
"dns": {
"baseDomainPrefix": "xyz"
},
"network": {
"podCidr": "10.10.0.0/24",
"serviceCidr": "10.10.0.0/24",
"machineCidr": "10.10.0.0/24"
},
"console": {},
"api": {
"visibility": "public"
},
"proxy": {},
"platform": {
"managedResourceGroup": "xyz",
"subnetId": "/subscriptions/xyz/resourceGroups/xyz/providers/Microsoft.Network/virtualNetworks/xyz/subnets/xyz",
"outboundType": "loadBalancer",
"networkSecurityGroupId": "/subscriptions/xyz/resourceGroups/xyz/providers/Microsoft.Network/networkSecurityGroups/xyz",
"etcdEncryptionSetId": "/subscriptions/xyz/resourceGroups/xyz/providers/Microsoft.Compute/encryptionSets/xyz"
},
"externalAuth": {}
}
}
}
78 changes: 63 additions & 15 deletions frontend/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

const (
clustersContainer = "Clusters"
clustersContainer = "Clusters"
subscriptionsContainer = "Subscriptions"
)

// DBClient defines the needed values to perform CRUD operations against the async DB
Expand Down Expand Up @@ -75,9 +76,8 @@ func (d *DBClient) DBConnectionTest(ctx context.Context) (string, error) {
return result.DatabaseProperties.ID, nil
}

// GetCluster retreives a cluster document from async DB using resource ID
func (d *DBClient) GetClusterDoc(ctx context.Context, resourceID string, partitionKey string) (*HCPOpenShiftClusterDocument, bool, error) {
container, err := d.client.NewContainer(d.config.DBName, clustersContainer)
func (d *DBClient) GetDocument(ctx context.Context, resourceID string, partitionKey string, containerName string) ([]byte, bool, error) {
container, err := d.client.NewContainer(d.config.DBName, containerName)
if err != nil {
return nil, false, err
}
Expand All @@ -91,25 +91,53 @@ func (d *DBClient) GetClusterDoc(ctx context.Context, resourceID string, partiti
pk := azcosmos.NewPartitionKeyString(partitionKey)
queryPager := container.NewQueryItemsPager(query, pk, &opt)

var doc *HCPOpenShiftClusterDocument
for queryPager.More() {
if queryPager.More() {
queryResponse, err := queryPager.NextPage(ctx)
if err != nil {
return nil, false, err
}

for _, item := range queryResponse.Items {
err = json.Unmarshal(item, &doc)
if err != nil {
return nil, false, err
}
if len(queryResponse.Items) > 0 {
return queryResponse.Items[0], true, nil
}
}
if doc != nil {
return doc, true, nil
}
return nil, false, nil
}

// GetCluster retreives a cluster document from async DB using resource ID
func (d *DBClient) GetClusterDoc(ctx context.Context, resourceID string, partitionKey string) (*HCPOpenShiftClusterDocument, bool, error) {
doc, found, err := d.GetDocument(ctx, resourceID, partitionKey, clustersContainer)
if err != nil {
return nil, false, err
}
if !found {
return nil, false, nil
}

var clusterDoc *HCPOpenShiftClusterDocument
err = json.Unmarshal(doc, &clusterDoc)
if err != nil {
return nil, false, err
}
return clusterDoc, true, nil

}

// GetCluster retreives a cluster document from async DB using resource ID
func (d *DBClient) GetSubscriptionDoc(ctx context.Context, resourceID string, partitionKey string) (*SubscriptionDocument, bool, error) {
doc, found, err := d.GetDocument(ctx, resourceID, partitionKey, subscriptionsContainer)
if err != nil {
return nil, false, err
}
if !found {
return nil, false, nil
}

var subDoc *SubscriptionDocument
err = json.Unmarshal(doc, &subDoc)
if err != nil {
return nil, false, err
}
return subDoc, true, nil
}

// SetCluster creates/updates a cluster document in the async DB during cluster creation/patching
Expand All @@ -132,6 +160,26 @@ func (d *DBClient) SetClusterDoc(ctx context.Context, doc *HCPOpenShiftClusterDo
return nil
}

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

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

_, err = container.UpsertItem(ctx, azcosmos.NewPartitionKeyString(doc.PartitionKey), data, nil)
if err != nil {
return err
}

return nil
}

// DeleteCluster removes a cluter document from the async DB using resource ID
func (d *DBClient) DeleteClusterDoc(ctx context.Context, resourceID string, partitionKey string) error {
doc, found, err := d.GetClusterDoc(ctx, resourceID, partitionKey)
Expand Down
32 changes: 32 additions & 0 deletions frontend/document.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import "github.com/Azure/ARO-HCP/internal/api/arm"

// HCPOpenShiftClusterDocument represents an HCP OpenShift cluster document.
type HCPOpenShiftClusterDocument struct {
ID string `json:"id,omitempty"`
Key string `json:"key,omitempty"`
PartitionKey string `json:"partitionKey,omitempty"`
ClusterID string `json:"clusterid,omitempty"`

// Values provided by Cosmos after doc creation
ResourceID string `json:"_rid,omitempty"`
Self string `json:"_self,omitempty"`
ETag string `json:"_etag,omitempty"`
Attachments string `json:"_attachments,omitempty"`
Timestamp int `json:"_ts,omitempty"`
}

// SubscriptionDocument represents an Azure Susbcription document.
type SubscriptionDocument struct {
ID string `json:"id,omitempty"`
PartitionKey string `json:"partitionKey,omitempty"`
Subscription *arm.Subscription `json:"subscription,omitempty"`

// Values provided by Cosmos after doc creation
ResourceID string `json:"_rid,omitempty"`
Self string `json:"_self,omitempty"`
ETag string `json:"_etag,omitempty"`
Attachments string `json:"_attachments,omitempty"`
Timestamp int `json:"_ts,omitempty"`
}
3 changes: 3 additions & 0 deletions frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,9 @@ func (f *Frontend) ArmSubscriptionAction(writer http.ResponseWriter, request *ht
return
}

/*
HERE
*/
subId := request.PathValue(PathSegmentSubscriptionID)
f.cache.SetSubscription(subId, &subscription)

Expand Down
16 changes: 0 additions & 16 deletions frontend/hcpclusterdocument.go

This file was deleted.

0 comments on commit a5f92c5

Please sign in to comment.