|
| 1 | +/* |
| 2 | + * Copyright 2013-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.cloudfoundry.client.v3; |
| 18 | + |
| 19 | +import org.cloudfoundry.AbstractIntegrationTest; |
| 20 | +import org.cloudfoundry.CloudFoundryVersion; |
| 21 | +import org.cloudfoundry.IfCloudFoundryVersion; |
| 22 | +import org.cloudfoundry.client.CloudFoundryClient; |
| 23 | +import org.cloudfoundry.client.v3.organizations.CreateOrganizationRequest; |
| 24 | +import org.cloudfoundry.client.v3.organizations.Organization; |
| 25 | +import org.cloudfoundry.client.v3.spacequotadefinitions.Apps; |
| 26 | +import org.cloudfoundry.client.v3.spacequotadefinitions.CreateSpaceQuotaDefinitionRequest; |
| 27 | +import org.cloudfoundry.client.v3.spacequotadefinitions.CreateSpaceQuotaDefinitionResponse; |
| 28 | +import org.cloudfoundry.client.v3.spacequotadefinitions.DeleteSpaceQuotaDefinitionRequest; |
| 29 | +import org.cloudfoundry.client.v3.spacequotadefinitions.GetSpaceQuotaDefinitionRequest; |
| 30 | +import org.cloudfoundry.client.v3.spacequotadefinitions.GetSpaceQuotaDefinitionResponse; |
| 31 | +import org.cloudfoundry.client.v3.spacequotadefinitions.ListSpaceQuotaDefinitionsRequest; |
| 32 | +import org.cloudfoundry.client.v3.spacequotadefinitions.Routes; |
| 33 | +import org.cloudfoundry.client.v3.spacequotadefinitions.Services; |
| 34 | +import org.cloudfoundry.client.v3.spacequotadefinitions.SpaceQuotaDefinitionRelationships; |
| 35 | +import org.cloudfoundry.client.v3.spacequotadefinitions.SpaceQuotaDefinitionResource; |
| 36 | +import org.cloudfoundry.client.v3.spacequotadefinitions.UpdateSpaceQuotaDefinitionRequest; |
| 37 | +import org.cloudfoundry.util.JobUtils; |
| 38 | +import org.cloudfoundry.util.PaginationUtils; |
| 39 | +import org.jetbrains.annotations.NotNull; |
| 40 | +import org.junit.jupiter.api.BeforeEach; |
| 41 | +import org.junit.jupiter.api.Test; |
| 42 | +import org.springframework.beans.factory.annotation.Autowired; |
| 43 | +import reactor.core.publisher.Flux; |
| 44 | +import reactor.core.publisher.Mono; |
| 45 | +import reactor.test.StepVerifier; |
| 46 | + |
| 47 | +import java.time.Duration; |
| 48 | + |
| 49 | +import static org.assertj.core.api.Assertions.assertThat; |
| 50 | + |
| 51 | +@IfCloudFoundryVersion(greaterThanOrEqualTo = CloudFoundryVersion.PCF_2_8) |
| 52 | +public final class SpaceQuotaDefinitionsTest extends AbstractIntegrationTest { |
| 53 | + |
| 54 | + @Autowired |
| 55 | + private CloudFoundryClient cloudFoundryClient; |
| 56 | + |
| 57 | + private String organizationId; |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + public void createOrganization() { |
| 61 | + String orgName = this.nameFactory.getOrganizationName(); |
| 62 | + organizationId = createOrganization(this.cloudFoundryClient, orgName).getId(); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void create() { |
| 67 | + String spaceQuotaName = this.nameFactory.getQuotaDefinitionName(); |
| 68 | + SpaceQuotaDefinitionRelationships spaceQuotaDefinitionRelationships = createSpaceQuotaDefinitionRelationships(organizationId); |
| 69 | + |
| 70 | + this.cloudFoundryClient |
| 71 | + .spaceQuotaDefinitionsV3() |
| 72 | + .create(CreateSpaceQuotaDefinitionRequest.builder().name(spaceQuotaName).relationships(spaceQuotaDefinitionRelationships).build()) |
| 73 | + .thenMany(requestListSpaceQuotas(this.cloudFoundryClient, spaceQuotaName)) |
| 74 | + .single() |
| 75 | + .as(StepVerifier::create) |
| 76 | + .expectNextCount(1) |
| 77 | + .expectComplete() |
| 78 | + .verify(Duration.ofMinutes(5)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void get() { |
| 83 | + String spaceQuotaName = this.nameFactory.getQuotaDefinitionName(); |
| 84 | + |
| 85 | + createSpaceQuotaId(this.cloudFoundryClient, spaceQuotaName, organizationId) |
| 86 | + .flatMap( |
| 87 | + spaceQuotaId -> |
| 88 | + this.cloudFoundryClient |
| 89 | + .spaceQuotaDefinitionsV3() |
| 90 | + .get( |
| 91 | + GetSpaceQuotaDefinitionRequest.builder() |
| 92 | + .spaceQuotaDefinitionId(spaceQuotaId) |
| 93 | + .build())) |
| 94 | + .map(GetSpaceQuotaDefinitionResponse::getName) |
| 95 | + .as(StepVerifier::create) |
| 96 | + .expectNext(spaceQuotaName) |
| 97 | + .expectComplete() |
| 98 | + .verify(Duration.ofMinutes(5)); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void list() { |
| 103 | + String spaceQuotaName = this.nameFactory.getQuotaDefinitionName(); |
| 104 | + |
| 105 | + createSpaceQuota(this.cloudFoundryClient, spaceQuotaName, organizationId) |
| 106 | + .thenMany( |
| 107 | + PaginationUtils.requestClientV3Resources( |
| 108 | + page -> |
| 109 | + this.cloudFoundryClient |
| 110 | + .spaceQuotaDefinitionsV3() |
| 111 | + .list( |
| 112 | + ListSpaceQuotaDefinitionsRequest.builder() |
| 113 | + .page(page) |
| 114 | + .build()))) |
| 115 | + .filter(resource -> spaceQuotaName.equals(resource.getName())) |
| 116 | + .as(StepVerifier::create) |
| 117 | + .expectNextCount(1) |
| 118 | + .expectComplete() |
| 119 | + .verify(Duration.ofMinutes(5)); |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void update() { |
| 125 | + String spaceQuotaName = this.nameFactory.getQuotaDefinitionName(); |
| 126 | + int totalMemoryLimit = 64 * 1024; // 64 GB |
| 127 | + |
| 128 | + createSpaceQuotaId(this.cloudFoundryClient, spaceQuotaName, organizationId) |
| 129 | + .flatMap( |
| 130 | + spaceQuotaId -> |
| 131 | + this.cloudFoundryClient |
| 132 | + .spaceQuotaDefinitionsV3() |
| 133 | + .update( |
| 134 | + UpdateSpaceQuotaDefinitionRequest.builder() |
| 135 | + .spaceQuotaDefinitionId(spaceQuotaId) |
| 136 | + .apps(Apps.builder().totalMemoryInMb(totalMemoryLimit).build()) |
| 137 | + .routes(Routes.builder().totalRoutes(100).build()) |
| 138 | + .services(Services.builder().isPaidServicesAllowed(true).totalServiceInstances(100).build()) |
| 139 | + .build())) |
| 140 | + .thenMany(requestListSpaceQuotas(this.cloudFoundryClient, spaceQuotaName)) |
| 141 | + .as(StepVerifier::create) |
| 142 | + .consumeNextWith( |
| 143 | + organizationQuotaDefinitionResource -> { |
| 144 | + assertThat(organizationQuotaDefinitionResource.getApps().getTotalMemoryInMb()).isEqualTo(totalMemoryLimit); |
| 145 | + assertThat(organizationQuotaDefinitionResource.getRoutes().getTotalRoutes()).isEqualTo(100); |
| 146 | + assertThat(organizationQuotaDefinitionResource.getServices().getTotalServiceInstances()).isEqualTo(100); |
| 147 | + }) |
| 148 | + .expectComplete() |
| 149 | + .verify(Duration.ofMinutes(5)); |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + @Test |
| 155 | + public void delete() { |
| 156 | + String spaceQuotaName = this.nameFactory.getQuotaDefinitionName(); |
| 157 | + |
| 158 | + createSpaceQuotaId(this.cloudFoundryClient, spaceQuotaName, organizationId) |
| 159 | + .flatMap( |
| 160 | + spaceQuotaId -> |
| 161 | + this.cloudFoundryClient |
| 162 | + .spaceQuotaDefinitionsV3() |
| 163 | + .delete( |
| 164 | + DeleteSpaceQuotaDefinitionRequest.builder() |
| 165 | + .spaceQuotaDefinitionId(spaceQuotaId) |
| 166 | + .build()) |
| 167 | + .flatMap( |
| 168 | + job -> |
| 169 | + JobUtils.waitForCompletion( |
| 170 | + this.cloudFoundryClient, |
| 171 | + Duration.ofMinutes(5), |
| 172 | + job))) |
| 173 | + .thenMany(requestListSpaceQuotas(this.cloudFoundryClient, spaceQuotaName)) |
| 174 | + .as(StepVerifier::create) |
| 175 | + .expectComplete() |
| 176 | + .verify(Duration.ofMinutes(5)); |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | + private static Organization createOrganization(CloudFoundryClient cloudFoundryClient, String orgName) { |
| 181 | + return cloudFoundryClient.organizationsV3() |
| 182 | + .create(CreateOrganizationRequest.builder().name(orgName).build()) |
| 183 | + .block(Duration.ofMinutes(5)); |
| 184 | + |
| 185 | + } |
| 186 | + |
| 187 | + @NotNull |
| 188 | + private static SpaceQuotaDefinitionRelationships createSpaceQuotaDefinitionRelationships(String orgGuid) { |
| 189 | + ToOneRelationship organizationRelationship = ToOneRelationship |
| 190 | + .builder() |
| 191 | + .data(Relationship.builder().id(orgGuid).build()) |
| 192 | + .build(); |
| 193 | + return SpaceQuotaDefinitionRelationships |
| 194 | + .builder() |
| 195 | + .organization(organizationRelationship) |
| 196 | + .build(); |
| 197 | + } |
| 198 | + |
| 199 | + private static Mono<String> createSpaceQuotaId( |
| 200 | + CloudFoundryClient cloudFoundryClient, String spaceQuotaName, String orgGuid) { |
| 201 | + return createSpaceQuota(cloudFoundryClient, spaceQuotaName, orgGuid) |
| 202 | + .map(CreateSpaceQuotaDefinitionResponse::getId); |
| 203 | + } |
| 204 | + |
| 205 | + private static Mono<CreateSpaceQuotaDefinitionResponse> createSpaceQuota( |
| 206 | + CloudFoundryClient cloudFoundryClient, String spaceQuotaName, String orgGuid) { |
| 207 | + SpaceQuotaDefinitionRelationships spaceQuotaDefinitionRelationships = createSpaceQuotaDefinitionRelationships(orgGuid); |
| 208 | + return cloudFoundryClient |
| 209 | + .spaceQuotaDefinitionsV3() |
| 210 | + .create(CreateSpaceQuotaDefinitionRequest |
| 211 | + .builder() |
| 212 | + .name(spaceQuotaName) |
| 213 | + .relationships(spaceQuotaDefinitionRelationships) |
| 214 | + .build() |
| 215 | + ); |
| 216 | + } |
| 217 | + |
| 218 | + private static Flux<SpaceQuotaDefinitionResource> requestListSpaceQuotas( |
| 219 | + CloudFoundryClient cloudFoundryClient, String spaceName) { |
| 220 | + return PaginationUtils.requestClientV3Resources( |
| 221 | + page -> |
| 222 | + cloudFoundryClient |
| 223 | + .spaceQuotaDefinitionsV3() |
| 224 | + .list( |
| 225 | + ListSpaceQuotaDefinitionsRequest.builder() |
| 226 | + .name(spaceName) |
| 227 | + .page(page) |
| 228 | + .build())); |
| 229 | + } |
| 230 | +} |
0 commit comments