Skip to content

Commit 61de61e

Browse files
Merge pull request #4025 from Azure/cs-123-collapse-sub
Remove database conversion and logic that isn't needed anymore
2 parents 3aa85a1 + a374924 commit 61de61e

File tree

42 files changed

+186
-453
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+186
-453
lines changed

internal/api/arm/subscription.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func ToSubscriptionResourceIDString(subscriptionName string) string {
4040
type Subscription struct {
4141
CosmosMetadata `json:"cosmosMetadata"`
4242

43+
// kept so untyped client can function
4344
ResourceID *azcorearm.ResourceID `json:"resourceId,omitempty"`
4445

4546
// The resource provider contract gives an example RegistrationDate

internal/database/convert_any.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
azcorearm "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
2222

2323
"github.com/Azure/ARO-HCP/internal/api"
24-
"github.com/Azure/ARO-HCP/internal/api/arm"
2524
"github.com/Azure/ARO-HCP/internal/utils"
2625
)
2726

@@ -38,15 +37,6 @@ func CosmosToInternal[InternalAPIType, CosmosAPIType any](obj *CosmosAPIType) (*
3837
case *NodePool:
3938
internalObj, err = CosmosToInternalNodePool(cosmosObj)
4039

41-
case *Controller:
42-
internalObj, err = CosmosToInternalController(cosmosObj)
43-
44-
case *Operation:
45-
internalObj, err = CosmosToInternalOperation(cosmosObj)
46-
47-
case *Subscription:
48-
internalObj, err = CosmosToInternalSubscription(cosmosObj)
49-
5040
case *TypedDocument:
5141
var expectedObj InternalAPIType
5242
switch castObj := any(expectedObj).(type) {
@@ -103,15 +93,6 @@ func InternalToCosmos[InternalAPIType, CosmosAPIType any](obj *InternalAPIType)
10393
case *api.HCPOpenShiftClusterNodePool:
10494
cosmosObj, err = InternalToCosmosNodePool(internalObj)
10595

106-
case *api.Controller:
107-
cosmosObj, err = InternalToCosmosController(internalObj)
108-
109-
case *api.Operation:
110-
cosmosObj, err = InternalToCosmosOperation(internalObj)
111-
112-
case *arm.Subscription:
113-
cosmosObj, err = InternalToCosmosSubscription(internalObj)
114-
11596
case *TypedDocument:
11697
var expectedObj CosmosAPIType
11798
switch castObj := any(expectedObj).(type) {

internal/database/convert_controller.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

internal/database/convert_generic.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ import (
1818
"fmt"
1919
"strings"
2020

21+
"github.com/Azure/ARO-HCP/internal/api"
2122
"github.com/Azure/ARO-HCP/internal/api/arm"
2223
)
2324

25+
const operationTimeToLive = 604800 // 7 days
26+
2427
func InternalToCosmosGeneric[InternalAPIType any](internalObj *InternalAPIType) (*GenericDocument[InternalAPIType], error) {
2528
if internalObj == nil {
2629
return nil, nil
@@ -43,6 +46,13 @@ func InternalToCosmosGeneric[InternalAPIType any](internalObj *InternalAPIType)
4346
Content: *internalObj,
4447
}
4548

49+
// this isn't pretty, but on balance it's a better choice so that we can share all the rest.
50+
switch any(internalObj).(type) {
51+
case *api.Operation:
52+
// TODO Add TTL to cosmosMetadata
53+
cosmosObj.TimeToLive = operationTimeToLive
54+
}
55+
4656
return cosmosObj, nil
4757
}
4858

@@ -58,5 +68,13 @@ func CosmosGenericToInternal[InternalAPIType any](cosmosObj *GenericDocument[Int
5868
ret.(arm.CosmosPersistable).GetCosmosData().ExistingCosmosUID = cosmosObj.ID
5969
ret.SetEtag(cosmosObj.CosmosETag)
6070

71+
// this isn't pretty, but on balance it's a better choice so that we can share all the rest.
72+
switch castObj := any(ret).(type) {
73+
case *arm.Subscription:
74+
castObj.LastUpdated = cosmosObj.CosmosTimestamp
75+
case arm.Subscription:
76+
castObj.LastUpdated = cosmosObj.CosmosTimestamp
77+
}
78+
6179
return &cosmosObj.Content, nil
6280
}

internal/database/convert_operation.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

internal/database/convert_subscription.go

Lines changed: 0 additions & 68 deletions
This file was deleted.

internal/database/crud_hcpcluster.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type OperationCRUD interface {
4848
}
4949

5050
type operationCRUD struct {
51-
*nestedCosmosResourceCRUD[api.Operation, Operation]
51+
*nestedCosmosResourceCRUD[api.Operation, GenericDocument[api.Operation]]
5252
}
5353

5454
func NewOperationCRUD(containerClient *azcosmos.ContainerClient, subscriptionID string) OperationCRUD {
@@ -59,7 +59,7 @@ func NewOperationCRUD(containerClient *azcosmos.ContainerClient, subscriptionID
5959
parentResourceID := api.Must(azcorearm.ParseResourceID(path.Join(parts...)))
6060

6161
return &operationCRUD{
62-
nestedCosmosResourceCRUD: NewCosmosResourceCRUD[api.Operation, Operation](containerClient, parentResourceID, api.OperationStatusResourceType),
62+
nestedCosmosResourceCRUD: NewCosmosResourceCRUD[api.Operation, GenericDocument[api.Operation]](containerClient, parentResourceID, api.OperationStatusResourceType),
6363
}
6464
}
6565

@@ -104,7 +104,7 @@ func (d *operationCRUD) ListActiveOperations(options *DBClientListActiveOperatio
104104
}
105105

106106
pager := d.containerClient.NewQueryItemsPager(query, NewPartitionKey(d.parentResourceID.SubscriptionID), &queryOptions)
107-
return newQueryResourcesIterator[api.Operation, Operation](pager)
107+
return newQueryResourcesIterator[api.Operation, GenericDocument[api.Operation]](pager)
108108
}
109109

110110
type HCPClusterCRUD interface {
@@ -228,5 +228,5 @@ func (h *nodePoolsCRUD) Controllers(nodePoolName string) ResourceCRUD[api.Contro
228228
func NewControllerCRUD(
229229
containerClient *azcosmos.ContainerClient, parentResourceID *azcorearm.ResourceID, resourceType azcorearm.ResourceType) ResourceCRUD[api.Controller] {
230230

231-
return NewCosmosResourceCRUD[api.Controller, Controller](containerClient, parentResourceID, resourceType)
231+
return NewCosmosResourceCRUD[api.Controller, GenericDocument[api.Controller]](containerClient, parentResourceID, resourceType)
232232
}

internal/database/crud_helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func list[InternalAPIType, CosmosAPIType any](ctx context.Context, containerClie
113113
if prefix == nil {
114114
query = "SELECT * FROM c"
115115
} else {
116-
query = "SELECT * FROM c WHERE STARTSWITH(c.properties.resourceId, @prefix, true)"
116+
query = "SELECT * FROM c WHERE STARTSWITH(c.resourceID, @prefix, true)"
117117
queryOptions = azcosmos.QueryOptions{
118118
PageSizeHint: -1,
119119
QueryParameters: []azcosmos.QueryParameter{
@@ -148,7 +148,7 @@ func list[InternalAPIType, CosmosAPIType any](ctx context.Context, containerClie
148148
}
149149

150150
// no sql injection risk because it's an int we control
151-
query += fmt.Sprintf(" AND (LENGTH(c.properties.resourceId) - LENGTH(REPLACE(c.properties.resourceId, '/', ''))) = %d", requiredNumSlashes)
151+
query += fmt.Sprintf(" AND (LENGTH(c.resourceID) - LENGTH(REPLACE(c.resourceID, '/', ''))) = %d", requiredNumSlashes)
152152
}
153153

154154
if options != nil {

0 commit comments

Comments
 (0)