Skip to content

Commit

Permalink
chore: fix some bugs and implement license ID in PUT endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Steiner <[email protected]>
  • Loading branch information
kosmoz committed Feb 13, 2025
1 parent 30eea55 commit 76d4200
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 91 deletions.
13 changes: 7 additions & 6 deletions api/deployment_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ type DeploymentTargetAccessTokenResponse struct {
}

type DeploymentRequest struct {
ID uuid.UUID `json:"deploymentId"`
DeploymentTargetID uuid.UUID `json:"deploymentTargetId"`
ApplicationVersionID uuid.UUID `json:"applicationVersionId"`
ReleaseName *string `json:"releaseName"`
ValuesYaml []byte `json:"valuesYaml"`
EnvFileData []byte `json:"envFileData"`
DeploymentID *uuid.UUID `json:"deploymentId"`
DeploymentTargetID uuid.UUID `json:"deploymentTargetId"`
ApplicationVersionID uuid.UUID `json:"applicationVersionId"`
ApplicationLicenseID *uuid.UUID `json:"applicationLicenseId"`
ReleaseName *string `json:"releaseName"`
ValuesYaml []byte `json:"valuesYaml"`
EnvFileData []byte `json:"envFileData"`
}

func (d DeploymentRequest) ParsedValuesFile() (result map[string]any, err error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<p class="mt-1 text-sm text-red-600 dark:text-red-500">Field is required.</p>
}
</div>
@if (deployForm.controls.applicationLicenseId.enabled) {
@if (shouldShowLicense$ | async) {
<div class="col-span-2 sm:col-span-1">
<label for="applicationLicense" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
Select License
Expand Down
48 changes: 32 additions & 16 deletions frontend/ui/src/app/deployment-form/deployment-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import {
combineLatest,
combineLatestWith,
distinctUntilChanged,
from,
map,
NEVER,
Observable,
of,
shareReplay,
startWith,
Subject,
switchMap,
takeUntil,
tap,
} from 'rxjs';
import {YamlEditorComponent} from '../components/yaml-editor.component';
import {AutotrimDirective} from '../directives/autotrim.directive';
Expand Down Expand Up @@ -65,22 +69,22 @@ export class DeploymentFormComponent implements OnInit, OnDestroy, ControlValueA
});

private readonly deploymentTargetId$ = this.deployForm.controls.deploymentTargetId.valueChanges.pipe(
shareReplay(1),
distinctUntilChanged()
distinctUntilChanged(),
shareReplay(1)
);
private readonly applicationId$ = this.deployForm.controls.applicationId.valueChanges.pipe(
shareReplay(1),
distinctUntilChanged()
distinctUntilChanged(),
shareReplay(1)
);
private readonly applicationVersionId$ = this.deployForm.controls.applicationVersionId.valueChanges.pipe(
shareReplay(1),
distinctUntilChanged()
distinctUntilChanged(),
shareReplay(1)
);
private readonly applicationLicenseId$ = this.deployForm.controls.applicationLicenseId.valueChanges.pipe(
shareReplay(1),
distinctUntilChanged()
distinctUntilChanged(),
shareReplay(1)
);
private readonly shouldShowLicense$ = this.featureFlags.isLicensingEnabled$.pipe(
protected readonly shouldShowLicense$ = this.featureFlags.isLicensingEnabled$.pipe(
map((isLicensingEnabled) => isLicensingEnabled && this.auth.hasRole('customer'))
);
private readonly deploymentTarget$ = this.deploymentTargetId$.pipe(
Expand Down Expand Up @@ -129,13 +133,21 @@ export class DeploymentFormComponent implements OnInit, OnDestroy, ControlValueA
this.onTouched?.(callbackArg);
});

this.shouldShowLicense$.pipe(takeUntil(this.destroyed$)).subscribe((isLicensingEnabled) => {
if (isLicensingEnabled) {
this.deployForm.controls.applicationLicenseId.enable();
} else {
this.deployForm.controls.applicationLicenseId.disable();
}
});
combineLatest([
this.shouldShowLicense$,
this.deploymentTarget$.pipe(
map((dt) => !!dt?.deployment),
distinctUntilChanged()
),
])
.pipe(takeUntil(this.destroyed$))
.subscribe(([isLicensingEnabled, existingDeployment]) => {
if (isLicensingEnabled && !existingDeployment) {
this.deployForm.controls.applicationLicenseId.enable();
} else {
this.deployForm.controls.applicationLicenseId.disable();
}
});

this.deploymentTarget$
.pipe(
Expand Down Expand Up @@ -221,6 +233,10 @@ export class DeploymentFormComponent implements OnInit, OnDestroy, ControlValueA
// this.deployForm.controls.applicationVersionId.reset();
}
});

// This is needed because the first value could be missed otherwise
// TODO: Find a better solution for this
this.applicationLicenseId$.pipe(takeUntil(this.destroyed$)).subscribe();
}

ngOnDestroy(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export class DeploymentTargetsComponent implements AfterViewInit, OnDestroy {
deploymentTargetId: deploymentTarget.id,
applicationId: deploymentTarget.deployment?.applicationId,
applicationVersionId: deploymentTarget.deployment?.applicationVersionId,
applicationLicenseId: deploymentTarget.deployment?.applicationLicenseId,
releaseName: deploymentTarget.deployment?.releaseName,
valuesYaml: deploymentTarget.deployment?.valuesYaml ? atob(deploymentTarget.deployment.valuesYaml) : undefined,
envFileData: deploymentTarget.deployment?.envFileData ? atob(deploymentTarget.deployment.envFileData) : undefined,
Expand Down
11 changes: 4 additions & 7 deletions frontend/ui/src/app/services/license.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ export class LicenseService {
return from([
[
{
id: 'a',
name: 'L1',
versions: [
{id: 'a', name: 'V1'},
{id: 'c', name: 'V3'},
],
id: 'ced3992d-6717-498e-8726-6845ec3069b4',
name: 'test',
applicationId,
versions: [{id: 'bf58c94e-0c5b-476a-a647-09464d85bcae', name: 'v4.2.0'}],
},
{id: 'b', name: 'L2', versions: [{id: 'b', name: 'V2'}]},
],
]);
}
Expand Down
1 change: 1 addition & 0 deletions frontend/ui/src/app/types/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import {ApplicationVersion, BaseModel} from '@glasskube/distr-sdk';
export interface License extends BaseModel {
expiresAt?: string;
name: string;
applicationId: string;
versions: ApplicationVersion[];
}
2 changes: 1 addition & 1 deletion internal/db/application_license.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func GetApplicationLicensesWithOwnerID(
}
}

func GetApplicationLicenseWithID(ctx context.Context, id string) (*types.ApplicationLicenseWithVersions, error) {
func GetApplicationLicenseWithID(ctx context.Context, id uuid.UUID) (*types.ApplicationLicenseWithVersions, error) {
db := internalctx.GetDb(ctx)
rows, err := db.Query(
ctx,
Expand Down
23 changes: 12 additions & 11 deletions internal/db/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,18 @@ func GetLatestDeploymentForDeploymentTarget(ctx context.Context, deploymentTarge
}
}

func CreateDeployment(ctx context.Context, d *api.DeploymentRequest) error {
func CreateDeployment(ctx context.Context, request *api.DeploymentRequest) error {
db := internalctx.GetDb(ctx)
rows, err := db.Query(
ctx,
`INSERT INTO Deployment AS d
(deployment_target_id, release_name)
VALUES (@deploymentTargetId, @releaseName)
(deployment_target_id, release_name, application_license_id)
VALUES (@deploymentTargetId, @releaseName, @applicationLicenseId)
RETURNING`+deploymentOutputExpr,
pgx.NamedArgs{
"deploymentTargetId": d.DeploymentTargetID,
"releaseName": d.ReleaseName,
"deploymentTargetId": request.DeploymentTargetID,
"releaseName": request.ReleaseName,
"applicationLicenseId": request.ApplicationLicenseID,
},
)
if err != nil {
Expand All @@ -126,12 +127,12 @@ func CreateDeployment(ctx context.Context, d *api.DeploymentRequest) error {
if err != nil {
return fmt.Errorf("could not save Deployment: %w", err)
} else {
d.ID = result.ID
request.DeploymentID = &result.ID
return nil
}
}

func CreateDeploymentRevision(ctx context.Context, d *api.DeploymentRequest) (*types.DeploymentRevision, error) {
func CreateDeploymentRevision(ctx context.Context, request *api.DeploymentRequest) (*types.DeploymentRevision, error) {
db := internalctx.GetDb(ctx)
rows, err := db.Query(
ctx,
Expand All @@ -140,10 +141,10 @@ func CreateDeploymentRevision(ctx context.Context, d *api.DeploymentRequest) (*t
VALUES (@deploymentId, @applicationVersionId, @valuesYaml, @envFileData)
RETURNING d.id, d.created_at, d.deployment_id, d.application_version_id`,
pgx.NamedArgs{
"deploymentId": d.ID,
"applicationVersionId": d.ApplicationVersionID,
"valuesYaml": d.ValuesYaml,
"envFileData": d.EnvFileData,
"deploymentId": request.DeploymentID,
"applicationVersionId": request.ApplicationVersionID,
"valuesYaml": request.ValuesYaml,
"envFileData": request.EnvFileData,
},
)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/handlers/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func updateApplication(w http.ResponseWriter, r *http.Request) {
}

existing := internalctx.GetApplication(ctx)
if IsEmptyUUID(application.ID) {
if application.ID == uuid.Nil {
application.ID = existing.ID
} else if application.ID != existing.ID || application.Type != existing.Type {
w.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -259,7 +259,7 @@ func updateApplicationVersion(w http.ResponseWriter, r *http.Request) {
if existingVersion == nil {
w.WriteHeader(http.StatusBadRequest)
return
} else if IsEmptyUUID(applicationVersion.ID) {
} else if applicationVersion.ID == uuid.Nil {
applicationVersion.ID = existingVersion.ID
}

Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/deployment_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func updateDeploymentTarget(w http.ResponseWriter, r *http.Request) {
dt.AgentVersionID = &dt.AgentVersion.ID

existing := internalctx.GetDeploymentTarget(ctx)
if IsEmptyUUID(dt.ID) {
if dt.ID == uuid.Nil {
dt.ID = existing.ID
} else if dt.ID != existing.ID {
w.WriteHeader(http.StatusBadRequest)
Expand Down
Loading

0 comments on commit 76d4200

Please sign in to comment.