Skip to content

Commit 4e93488

Browse files
4243 fix cf rename space (#4250)
* Add support for patching space name * Adjust tests after review
1 parent 54991c7 commit 4e93488

File tree

8 files changed

+143
-56
lines changed

8 files changed

+143
-56
lines changed

api/handlers/fake/cfspace_repository.go

Lines changed: 39 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/handlers/space.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type CFSpaceRepository interface {
3131
ListSpaces(context.Context, authorization.Info, repositories.ListSpacesMessage) (repositories.ListResult[repositories.SpaceRecord], error)
3232
GetSpace(context.Context, authorization.Info, string) (repositories.SpaceRecord, error)
3333
DeleteSpace(context.Context, authorization.Info, repositories.DeleteSpaceMessage) error
34-
PatchSpaceMetadata(context.Context, authorization.Info, repositories.PatchSpaceMetadataMessage) (repositories.SpaceRecord, error)
34+
PatchSpace(context.Context, authorization.Info, repositories.PatchSpaceMessage) (repositories.SpaceRecord, error)
3535
GetDeletedAt(context.Context, authorization.Info, string) (*time.Time, error)
3636
}
3737

@@ -120,7 +120,7 @@ func (h *Space) update(r *http.Request) (*routing.Response, error) {
120120
return nil, apierrors.LogAndReturn(logger, err, "failed to decode payload")
121121
}
122122

123-
space, err = h.spaceRepo.PatchSpaceMetadata(r.Context(), authInfo, payload.ToMessage(spaceGUID, space.OrganizationGUID))
123+
space, err = h.spaceRepo.PatchSpace(r.Context(), authInfo, payload.ToMessage(spaceGUID, space.OrganizationGUID))
124124
if err != nil {
125125
return nil, apierrors.LogAndReturn(logger, err, "Failed to patch space metadata", "GUID", spaceGUID)
126126
}

api/handlers/space_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,15 @@ var _ = Describe("Space", func() {
328328
requestMethod = http.MethodPatch
329329
requestPath += "/the-space-guid"
330330

331-
spaceRepo.PatchSpaceMetadataReturns(repositories.SpaceRecord{
332-
Name: "the-space",
331+
newSpaceName := tools.PtrTo("new-space-name")
332+
spaceRepo.PatchSpaceReturns(repositories.SpaceRecord{
333+
Name: *newSpaceName,
333334
GUID: "the-space-guid",
334335
OrganizationGUID: "the-org-guid",
335336
}, nil)
336337

337338
requestValidator.DecodeAndValidateJSONPayloadStub = decodeAndValidatePayloadStub(&payloads.SpacePatch{
339+
Name: newSpaceName,
338340
Metadata: payloads.MetadataPatch{
339341
Annotations: map[string]*string{
340342
"hello": tools.PtrTo("there"),
@@ -353,9 +355,10 @@ var _ = Describe("Space", func() {
353355
actualReq, _ := requestValidator.DecodeAndValidateJSONPayloadArgsForCall(0)
354356
Expect(bodyString(actualReq)).To(Equal("the-json-body"))
355357

356-
Expect(spaceRepo.PatchSpaceMetadataCallCount()).To(Equal(1))
357-
_, _, msg := spaceRepo.PatchSpaceMetadataArgsForCall(0)
358+
Expect(spaceRepo.PatchSpaceCallCount()).To(Equal(1))
359+
_, _, msg := spaceRepo.PatchSpaceArgsForCall(0)
358360
Expect(msg.GUID).To(Equal("the-space-guid"))
361+
Expect(msg.Name).To(PointTo(Equal("new-space-name")))
359362
Expect(msg.OrgGUID).To(Equal("the-org-guid"))
360363
Expect(msg.Annotations).To(HaveKeyWithValue("hello", PointTo(Equal("there"))))
361364
Expect(msg.Annotations).To(HaveKeyWithValue("foo.example.com/lorem-ipsum", PointTo(Equal("Lorem ipsum."))))
@@ -366,6 +369,7 @@ var _ = Describe("Space", func() {
366369
Expect(rr).To(HaveHTTPHeaderWithValue("Content-Type", "application/json"))
367370

368371
Expect(rr).To(HaveHTTPBody(SatisfyAll(
372+
MatchJSONPath("$.name", "new-space-name"),
369373
MatchJSONPath("$.guid", "the-space-guid"),
370374
MatchJSONPath("$.links.self.href", "https://api.example.org/v3/spaces/the-space-guid"),
371375
)))
@@ -378,7 +382,7 @@ var _ = Describe("Space", func() {
378382

379383
It("returns a not found error and does not try updating the space", func() {
380384
expectNotFoundError(repositories.SpaceResourceType)
381-
Expect(spaceRepo.PatchSpaceMetadataCallCount()).To(Equal(0))
385+
Expect(spaceRepo.PatchSpaceCallCount()).To(Equal(0))
382386
})
383387
})
384388

@@ -389,13 +393,13 @@ var _ = Describe("Space", func() {
389393

390394
It("returns an error and does not try updating the space", func() {
391395
expectUnknownError()
392-
Expect(spaceRepo.PatchSpaceMetadataCallCount()).To(Equal(0))
396+
Expect(spaceRepo.PatchSpaceCallCount()).To(Equal(0))
393397
})
394398
})
395399

396400
When("patching the org errors", func() {
397401
BeforeEach(func() {
398-
spaceRepo.PatchSpaceMetadataReturns(repositories.SpaceRecord{}, errors.New("boom"))
402+
spaceRepo.PatchSpaceReturns(repositories.SpaceRecord{}, errors.New("boom"))
399403
})
400404

401405
It("returns an error", func() {

api/payloads/space.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,22 @@ func (r SpaceRelationships) Validate() error {
4242
}
4343

4444
type SpacePatch struct {
45+
Name *string `json:"name"`
4546
Metadata MetadataPatch `json:"metadata"`
4647
}
4748

4849
func (p SpacePatch) Validate() error {
4950
return jellidation.ValidateStruct(&p,
5051
jellidation.Field(&p.Metadata),
52+
jellidation.Field(&p.Name, jellidation.NilOrNotEmpty),
5153
)
5254
}
5355

54-
func (p SpacePatch) ToMessage(spaceGUID, orgGUID string) repositories.PatchSpaceMetadataMessage {
55-
return repositories.PatchSpaceMetadataMessage{
56+
func (p SpacePatch) ToMessage(spaceGUID, orgGUID string) repositories.PatchSpaceMessage {
57+
return repositories.PatchSpaceMessage{
5658
GUID: spaceGUID,
5759
OrgGUID: orgGUID,
60+
Name: p.Name,
5861
MetadataPatch: repositories.MetadataPatch{
5962
Labels: p.Metadata.Labels,
6063
Annotations: p.Metadata.Annotations,

api/payloads/space_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ var _ = Describe("Space", func() {
103103

104104
BeforeEach(func() {
105105
payload = payloads.SpacePatch{
106+
Name: tools.PtrTo("new-space-name"),
106107
Metadata: payloads.MetadataPatch{
107108
Annotations: map[string]*string{
108109
"foo": tools.PtrTo("bar"),
@@ -137,6 +138,18 @@ var _ = Describe("Space", func() {
137138
)
138139
})
139140
})
141+
When("the name is invalid", func() {
142+
BeforeEach(func() {
143+
payload.Name = tools.PtrTo("")
144+
})
145+
146+
It("returns an unprocessable entity error", func() {
147+
expectUnprocessableEntityError(
148+
validatorErr,
149+
"name cannot be blank",
150+
)
151+
})
152+
})
140153
})
141154

142155
Describe("SpaceList", func() {

api/repositories/space_repository.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,18 @@ type DeleteSpaceMessage struct {
5656
OrganizationGUID string
5757
}
5858

59-
type PatchSpaceMetadataMessage struct {
59+
type PatchSpaceMessage struct {
6060
MetadataPatch
6161
GUID string
6262
OrgGUID string
63+
Name *string
64+
}
65+
66+
func (p *PatchSpaceMessage) Apply(space *korifiv1alpha1.CFSpace) {
67+
if p.Name != nil {
68+
space.Spec.DisplayName = *p.Name
69+
}
70+
p.MetadataPatch.Apply(space)
6371
}
6472

6573
type SpaceRecord struct {
@@ -212,7 +220,7 @@ func (r *SpaceRepo) DeleteSpace(ctx context.Context, info authorization.Info, me
212220
return apierrors.FromK8sError(err, SpaceResourceType)
213221
}
214222

215-
func (r *SpaceRepo) PatchSpaceMetadata(ctx context.Context, authInfo authorization.Info, message PatchSpaceMetadataMessage) (SpaceRecord, error) {
223+
func (r *SpaceRepo) PatchSpace(ctx context.Context, authInfo authorization.Info, message PatchSpaceMessage) (SpaceRecord, error) {
216224
cfSpace := &korifiv1alpha1.CFSpace{
217225
ObjectMeta: metav1.ObjectMeta{
218226
Namespace: message.OrgGUID,

api/repositories/space_repository_test.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.cloudfoundry.org/korifi/tools"
1616
"code.cloudfoundry.org/korifi/tools/k8s"
1717

18+
"github.com/google/uuid"
1819
. "github.com/onsi/ginkgo/v2"
1920
. "github.com/onsi/gomega"
2021
. "github.com/onsi/gomega/gstruct"
@@ -446,9 +447,11 @@ var _ = Describe("SpaceRepository", func() {
446447
})
447448
})
448449

449-
Describe("PatchSpaceMetadata", func() {
450+
Describe("PatchSpace", func() {
450451
var (
451452
spaceGUID string
453+
displayName string
454+
spaceNewName *string
452455
orgGUID string
453456
cfSpace *korifiv1alpha1.CFSpace
454457
cfOrg *korifiv1alpha1.CFOrg
@@ -460,23 +463,26 @@ var _ = Describe("SpaceRepository", func() {
460463
BeforeEach(func() {
461464
cfOrg = createOrgWithCleanup(ctx, prefixedGUID("org"))
462465
orgGUID = cfOrg.Name
463-
cfSpace = createSpaceWithCleanup(ctx, cfOrg.Name, "the-space")
466+
displayName = uuid.NewString()
467+
cfSpace = createSpaceWithCleanup(ctx, cfOrg.Name, displayName)
464468
spaceGUID = cfSpace.Name
465469
labelsPatch = nil
466470
annotationsPatch = nil
471+
spaceNewName = tools.PtrTo(uuid.NewString())
467472
})
468473

469474
JustBeforeEach(func() {
470-
patchMsg := repositories.PatchSpaceMetadataMessage{
475+
patchMsg := repositories.PatchSpaceMessage{
471476
GUID: spaceGUID,
472477
OrgGUID: orgGUID,
478+
Name: spaceNewName,
473479
MetadataPatch: repositories.MetadataPatch{
474480
Annotations: annotationsPatch,
475481
Labels: labelsPatch,
476482
},
477483
}
478484

479-
spaceRecord, patchErr = spaceRepo.PatchSpaceMetadata(ctx, authInfo, patchMsg)
485+
spaceRecord, patchErr = spaceRepo.PatchSpace(ctx, authInfo, patchMsg)
480486
})
481487

482488
When("the user is authorized and the space exists", func() {
@@ -514,6 +520,7 @@ var _ = Describe("SpaceRepository", func() {
514520
"key-two": "value-two",
515521
},
516522
))
523+
Expect(spaceRecord.Name).To(Equal(*spaceNewName))
517524
})
518525

519526
It("sets the k8s CFSpace resource", func() {
@@ -530,6 +537,7 @@ var _ = Describe("SpaceRepository", func() {
530537
"key-two": "value-two",
531538
},
532539
))
540+
Expect(updatedCFSpace.Spec.DisplayName).To(Equal(*spaceNewName))
533541
})
534542
})
535543

@@ -576,6 +584,7 @@ var _ = Describe("SpaceRepository", func() {
576584
"key-two": "value-two",
577585
},
578586
))
587+
Expect(spaceRecord.Name).To(Equal(*spaceNewName))
579588
})
580589

581590
It("sets the k8s CFSpace resource", func() {
@@ -594,6 +603,7 @@ var _ = Describe("SpaceRepository", func() {
594603
"key-two": "value-two",
595604
},
596605
))
606+
Expect(spaceRecord.Name).To(Equal(*spaceNewName))
597607
})
598608
})
599609

@@ -632,6 +642,17 @@ var _ = Describe("SpaceRepository", func() {
632642
))
633643
})
634644
})
645+
646+
When("the name is nil", func() {
647+
BeforeEach(func() {
648+
spaceNewName = nil
649+
})
650+
651+
It("space display name remains unchanged", func() {
652+
Expect(patchErr).NotTo(HaveOccurred())
653+
Expect(spaceRecord.Name).To(Equal(displayName))
654+
})
655+
})
635656
})
636657

637658
When("the user is authorized but the Space does not exist", func() {

0 commit comments

Comments
 (0)