Skip to content

Commit 332c43c

Browse files
authored
adds delete_recursive_allowed field in space and org ressources : (#528)
- to allow recursive deletion of spaces within an organization - to allow recursive deletion of apps, routes and service instances within a space update orgs and spaces deletion with api v3 Co-authored-by: Gilles Miraillet <Gmllt>
1 parent 31b9c3d commit 332c43c

File tree

6 files changed

+75
-7
lines changed

6 files changed

+75
-7
lines changed

cloudfoundry/managers/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ type Config struct {
1717
DefaultQuotaName string
1818
StoreTokensPath string
1919
ForceNotFailBrokerCatalog bool
20+
DeleteRecursiveAllowed bool
2021
}

cloudfoundry/provider.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ func Provider() *schema.Provider {
9494
DefaultFunc: schema.EnvDefaultFunc("CF_FORCE_BROKER_NOT_FAIL_CATALOG", false),
9595
Description: "Set to true to not trigger fail on catalog on service broker",
9696
},
97+
"delete_recursive_allowed": &schema.Schema{
98+
Type: schema.TypeBool,
99+
Optional: true,
100+
DefaultFunc: schema.EnvDefaultFunc("CF_DELETE_RECURSIVE_ALLOWED", true),
101+
Description: "Set to false to disallow recurive deletion",
102+
},
97103
},
98104

99105
DataSourcesMap: map[string]*schema.Resource{
@@ -165,6 +171,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
165171
DefaultQuotaName: d.Get("default_quota_name").(string),
166172
StoreTokensPath: d.Get("store_tokens_path").(string),
167173
ForceNotFailBrokerCatalog: d.Get("force_broker_not_fail_when_catalog_not_accessible").(bool),
174+
DeleteRecursiveAllowed: d.Get("delete_recursive_allowed").(bool),
168175
}
169176
session, err := managers.NewSession(c)
170177
return session, diag.FromErr(err)

cloudfoundry/resource_cf_org.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package cloudfoundry
22

33
import (
4-
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
54
"context"
5+
6+
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
7+
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
68
"github.com/hashicorp/go-uuid"
79
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
810
"github.com/terraform-providers/terraform-provider-cloudfoundry/cloudfoundry/managers"
@@ -62,6 +64,12 @@ func resourceOrg() *schema.Resource {
6264
},
6365
labelsKey: labelsSchema(),
6466
annotationsKey: annotationsSchema(),
67+
"delete_recursive_allowed": {
68+
Type: schema.TypeBool,
69+
Optional: true,
70+
Default: true,
71+
Description: "Allow recursive deletion of spaces.",
72+
},
6573
},
6674
}
6775
}
@@ -170,14 +178,19 @@ func resourceOrgUpdate(ctx context.Context, d *schema.ResourceData, meta interfa
170178

171179
func resourceOrgDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
172180
session := meta.(*managers.Session)
173-
client := session.ClientV2
181+
client := session.ClientV3
174182

175183
id := d.Id()
176-
spaces, _, err := client.GetSpaces(ccv2.FilterByOrg(id))
177-
184+
spaces, _, _, err := session.ClientV3.GetSpaces(ccv3.Query{
185+
Key: ccv3.OrganizationGUIDFilter,
186+
Values: []string{d.Id()},
187+
})
178188
if err != nil {
179189
return diag.FromErr(err)
180190
}
191+
if !session.Config.DeleteRecursiveAllowed && len(spaces) > 0 {
192+
return diag.Errorf("Organization %s has %d spaces. Please delete them first or set delete_recursive_allowed to true", id, len(spaces))
193+
}
181194
for _, s := range spaces {
182195
j, _, err := client.DeleteSpace(s.GUID)
183196
if err != nil {

cloudfoundry/resource_cf_space.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package cloudfoundry
22

33
import (
4-
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
54
"context"
5+
6+
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2"
7+
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
68
"github.com/hashicorp/go-uuid"
79
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
810
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -86,6 +88,12 @@ func resourceSpace() *schema.Resource {
8688
},
8789
labelsKey: labelsSchema(),
8890
annotationsKey: annotationsSchema(),
91+
"delete_recursive_allowed": {
92+
Type: schema.TypeBool,
93+
Optional: true,
94+
Default: true,
95+
Description: "Allow recursive deletion of apps, routes, service instances.",
96+
},
8997
},
9098
}
9199
}
@@ -311,11 +319,48 @@ func resourceSpaceUpdate(ctx context.Context, d *schema.ResourceData, meta inter
311319

312320
func resourceSpaceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
313321
session := meta.(*managers.Session)
314-
j, _, err := session.ClientV2.DeleteSpace(d.Id())
322+
323+
if !session.Config.DeleteRecursiveAllowed {
324+
// Check for apps
325+
apps, _, err := session.ClientV3.GetApplications(ccv3.Query{
326+
Key: ccv3.SpaceGUIDFilter,
327+
Values: []string{d.Id()},
328+
})
329+
if err != nil {
330+
return diag.FromErr(err)
331+
}
332+
if len(apps) > 0 {
333+
return diag.Errorf("Space %s has %d apps. Please delete them first or set delete_recursive_allowed to true", d.Id(), len(apps))
334+
}
335+
// Check routes
336+
routes, _, err := session.ClientV3.GetRoutes(ccv3.Query{
337+
Key: ccv3.SpaceGUIDFilter,
338+
Values: []string{d.Id()},
339+
})
340+
if err != nil {
341+
return diag.FromErr(err)
342+
}
343+
if len(routes) > 0 {
344+
return diag.Errorf("Space %s has %d routes. Please delete them first or set delete_recursive_allowed to true", d.Id(), len(routes))
345+
}
346+
// Check service instances
347+
serviceInstances, _, _, err := session.ClientV3.GetServiceInstances(ccv3.Query{
348+
Key: ccv3.SpaceGUIDFilter,
349+
Values: []string{d.Id()},
350+
})
351+
if err != nil {
352+
return diag.FromErr(err)
353+
}
354+
if len(serviceInstances) > 0 {
355+
return diag.Errorf("Space %s has %d service instances. Please delete them first or set delete_recursive_allowed to true", d.Id(), len(serviceInstances))
356+
}
357+
}
358+
359+
j, _, err := session.ClientV3.DeleteSpace(d.Id())
315360
if err != nil {
316361
return diag.FromErr(err)
317362
}
318-
_, err = session.ClientV2.PollJob(j)
363+
_, err = session.ClientV3.PollJob(j)
319364
if err != nil {
320365
return diag.FromErr(err)
321366
}

docs/resources/org.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ The following arguments are supported:
3434
Works only on cloud foundry with api >= v3.63.
3535
* `annotations` - (Optional, map string of string) Add annotations as described [here](https://docs.cloudfoundry.org/adminguide/metadata.html#-view-metadata-for-an-object).
3636
Works only on cloud foundry with api >= v3.63.
37+
* `delete_recursive_allowed` - (Optional, bool) Allow recursive delete of spaces within the organization. Default: `true`.
3738

3839
## Attributes Reference
3940

docs/resources/space.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ The following arguments are supported:
4242
Works only on cloud foundry with api >= v3.63.
4343
* `annotations` - (Optional, map string of string) Add annotations as described [here](https://docs.cloudfoundry.org/adminguide/metadata.html#-view-metadata-for-an-object).
4444
Works only on cloud foundry with api >= v3.63.
45+
* `delete_recursive_allowed` - (Optional, bool) Allow recursive delete of apps, routes and service instances within the space. Default: `true`.
4546

4647
## Attributes Reference
4748

0 commit comments

Comments
 (0)