Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add force_create to create reserved capacity with exisitng name #3306

Merged
merged 1 commit into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ibm/data_source_ibm_compute_reserved_capacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func dataSourceIBMComputeReservedCapacityRead(context context.Context, d *schema
grp = mostRecentReservedCapacity(grps)
} else {
return diag.FromErr(fmt.Errorf(
"[Error] More than one reserved capacity found with name"+
"[Error] More than one reserved capacity found with name "+
"matching [%s]. Set 'most_recent' to true in your configuration to force the most recent reserved capacity "+
"to be used", name))
}
Expand Down
26 changes: 26 additions & 0 deletions ibm/resource_ibm_compute_reserved_capacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func resourceIBMComputeReservedCapacity() *schema.Resource {
DeleteContext: resourceIBMComputeReservedCapacityDelete,
Exists: resourceIBMComputeReservedCapacityExists,
Importer: &schema.ResourceImporter{},
CustomizeDiff: resourceReservedCapacityValidate,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Expand Down Expand Up @@ -81,6 +82,12 @@ func resourceIBMComputeReservedCapacity() *schema.Resource {
Set: schema.HashString,
Description: "List of tags",
},
"force_create": {
Type: schema.TypeBool,
Optional: true,
DiffSuppressFunc: applyOnce,
Description: "Force the creation of reserved capacity with same name",
},
},
}
}
Expand Down Expand Up @@ -291,3 +298,22 @@ func resourceIBMComputeReservedCapacityDelete(context context.Context, d *schema
d.SetId("")
return nil
}

func resourceReservedCapacityValidate(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {
forceCreate := diff.Get("force_create").(bool)
if diff.Id() == "" && !forceCreate {
name := diff.Get("name").(string)
service := services.GetAccountService(meta.(ClientSession).SoftLayerSession())
reservedCapacities, _ := service.Filter(
filter.Build(
filter.Path("reservedCapacityGroups.name").Eq(name),
),
).Mask("id,createDate").GetReservedCapacityGroups()
if len(reservedCapacities) > 0 {
return fmt.Errorf("reserved capacity exists with same name [%s] if you still want to provision with same name set force_create argument to true", name)
}
}

return nil

}
1 change: 1 addition & 0 deletions website/docs/r/compute_reserved_capacity.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Review the argument references that you can specify for your resource.

- `datacenter` - (Required, Forces new resource, String) The datacenter in which you want to provision the reserved capacity.
- `flavor`- (Required, String) Capacity keyname. For example, C1_2X2_1_YEAR_TERM.
- `force_create` - (Optional, Boolean) Force the creation of reserved capacity with same name.
- `instances`- (Required, Forces new resource, Integer) Number of VSI instances this capacity reservation can support.
- `name` - (Required, String) The descriptive that is used to identify a reserved capacity.
- `pod` - (Required, Forces new resource, String) The data center pod where you want to create the reserved capacity.
Expand Down