Skip to content
Open
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
6 changes: 6 additions & 0 deletions cloudstack/service_offering_constrained_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strconv"

"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int32planmodifier"
Expand Down Expand Up @@ -111,6 +112,7 @@ func (r *serviceOfferingConstrainedResource) Create(ctx context.Context, req res
// common params
params := r.client.ServiceOffering.NewCreateServiceOfferingParams(plan.DisplayText.ValueString(), plan.Name.ValueString())
plan.commonCreateParams(ctx, params)
plan.applyTags(params)
planDiskQosHypervisor.commonCreateParams(ctx, params)
planDiskOffering.commonCreateParams(ctx, params)
planDiskQosStorage.commonCreateParams(ctx, params)
Expand Down Expand Up @@ -287,6 +289,10 @@ func (r *serviceOfferingConstrainedResource) Delete(ctx context.Context, req res
}
}

func (r *serviceOfferingConstrainedResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func (r *serviceOfferingConstrainedResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Add a nil check when handling ProviderData because Terraform
// sets that data after it calls the ConfigureProvider RPC.
Expand Down
5 changes: 5 additions & 0 deletions cloudstack/service_offering_constrained_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func TestAccServiceOfferingConstrained(t *testing.T) {
resource.TestCheckResourceAttr("cloudstack_service_offering_constrained.constrained1", "name", "constrained1"),
),
},
{
ResourceName: "cloudstack_service_offering_constrained.constrained1",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccServiceOfferingCustomConstrained1ZoneAll,
Check: resource.ComposeTestCheckFunc(
Expand Down
6 changes: 6 additions & 0 deletions cloudstack/service_offering_fixed_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"

"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int32planmodifier"
Expand Down Expand Up @@ -96,6 +97,7 @@ func (r *serviceOfferingFixedResource) Create(ctx context.Context, req resource.
// cloudstack params
params := r.client.ServiceOffering.NewCreateServiceOfferingParams(plan.DisplayText.ValueString(), plan.Name.ValueString())
plan.commonCreateParams(ctx, params)
plan.applyTags(params)
planDiskQosHypervisor.commonCreateParams(ctx, params)
planDiskOffering.commonCreateParams(ctx, params)
planDiskQosStorage.commonCreateParams(ctx, params)
Expand Down Expand Up @@ -227,6 +229,10 @@ func (r *serviceOfferingFixedResource) Delete(ctx context.Context, req resource.
}
}

func (r *serviceOfferingFixedResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func (r *serviceOfferingFixedResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Add a nil check when handling ProviderData because Terraform
// sets that data after it calls the ConfigureProvider RPC.
Expand Down
5 changes: 5 additions & 0 deletions cloudstack/service_offering_fixed_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func TestAccServiceOfferingFixed(t *testing.T) {
resource.TestCheckResourceAttr("cloudstack_service_offering_fixed.fixed1", "name", "fixed1"),
),
},
{
ResourceName: "cloudstack_service_offering_fixed.fixed1",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccServiceOfferingFixed2,
Check: resource.ComposeTestCheckFunc(
Expand Down
1 change: 1 addition & 0 deletions cloudstack/service_offering_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type serviceOfferingCommonResourceModel struct {
Name types.String `tfsdk:"name"`
NetworkRate types.Int32 `tfsdk:"network_rate"`
OfferHa types.Bool `tfsdk:"offer_ha"`
Tags types.String `tfsdk:"tags"`
ZoneIds types.Set `tfsdk:"zone_ids"`
ServiceOfferingDiskQosHypervisor types.Object `tfsdk:"disk_hypervisor"`
ServiceOfferingDiskOffering types.Object `tfsdk:"disk_offering"`
Expand Down
4 changes: 4 additions & 0 deletions cloudstack/service_offering_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ func serviceOfferingMergeCommonSchema(s1 map[string]schema.Attribute) map[string
},
Default: booldefault.StaticBool(false),
},
"tags": schema.StringAttribute{
Description: "The tags for the service offering",
Optional: true,
},
"zone_ids": schema.SetAttribute{
Description: "The ID of the zone(s)",
Optional: true,
Expand Down
6 changes: 6 additions & 0 deletions cloudstack/service_offering_unconstrained_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"

"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -73,6 +74,7 @@ func (r *serviceOfferingUnconstrainedResource) Create(ctx context.Context, req r
// cloudstack params
params := r.client.ServiceOffering.NewCreateServiceOfferingParams(plan.DisplayText.ValueString(), plan.Name.ValueString())
plan.commonCreateParams(ctx, params)
plan.applyTags(params)
planDiskQosHypervisor.commonCreateParams(ctx, params)
planDiskOffering.commonCreateParams(ctx, params)
planDiskQosStorage.commonCreateParams(ctx, params)
Expand Down Expand Up @@ -181,6 +183,10 @@ func (r *serviceOfferingUnconstrainedResource) Delete(ctx context.Context, req r
}
}

func (r *serviceOfferingUnconstrainedResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func (r *serviceOfferingUnconstrainedResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
Expand Down
5 changes: 5 additions & 0 deletions cloudstack/service_offering_unconstrained_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func TestAccServiceOfferingUnconstrained(t *testing.T) {
resource.TestCheckResourceAttr("cloudstack_service_offering_unconstrained.unconstrained1", "name", "unconstrained1"),
),
},
{
ResourceName: "cloudstack_service_offering_unconstrained.unconstrained1",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccServiceOfferingUnconstrained2,
Check: resource.ComposeTestCheckFunc(
Expand Down
36 changes: 26 additions & 10 deletions cloudstack/service_offering_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,33 @@ func (state *serviceOfferingCommonResourceModel) commonUpdate(ctx context.Contex
if cs.Name != "" {
state.Name = types.StringValue(cs.Name)
}
if cs.Zoneid != "" {
state.ZoneIds, _ = types.SetValueFrom(ctx, types.StringType, strings.Split(cs.Zoneid, ","))
if cs.Zoneid != "" && cs.Zoneid != "all" {
z, _ := types.SetValueFrom(ctx, types.StringType, strings.Split(cs.Zoneid, ","))
state.ZoneIds = z
}
// else: preserve prior state value (null or []) — both mean "all zones"
}

func (plan *serviceOfferingCommonResourceModel) commonUpdateParams(ctx context.Context, p *cloudstack.UpdateServiceOfferingParams) *cloudstack.UpdateServiceOfferingParams {
if !plan.DisplayText.IsNull() {
p.SetDisplaytext(plan.DisplayText.ValueString())
}
if !plan.DomainIds.IsNull() {
p.SetDomainid(plan.DomainIds.String())
domainIDs := make([]string, len(plan.DomainIds.Elements()))
plan.DomainIds.ElementsAs(ctx, &domainIDs, false)
p.SetDomainid(strings.Join(domainIDs, ","))
}
if !plan.HostTags.IsNull() {
p.SetHosttags(plan.HostTags.ValueString())
}
if !plan.Name.IsNull() {
p.SetName(plan.Name.ValueString())
}
if !plan.ZoneIds.IsNull() && len(plan.ZoneIds.Elements()) > 0 {
p.SetZoneid(plan.ZoneIds.String())
zoneIDs := plan.ZoneIds
if !zoneIDs.IsNull() && len(zoneIDs.Elements()) > 0 {
zoneIDSlice := make([]string, len(zoneIDs.Elements()))
zoneIDs.ElementsAs(ctx, &zoneIDSlice, false)
p.SetZoneid(strings.Join(zoneIDSlice, ","))
} else {
p.SetZoneid("all")
}
Expand Down Expand Up @@ -99,9 +106,11 @@ func (state *serviceOfferingCommonResourceModel) commonRead(ctx context.Context,
if cs.Networkrate > 0 {
state.NetworkRate = types.Int32Value(int32(cs.Networkrate))
}
if cs.Zoneid != "" {
state.ZoneIds, _ = types.SetValueFrom(ctx, types.StringType, strings.Split(cs.Zoneid, ","))
if cs.Zoneid != "" && cs.Zoneid != "all" {
z, _ := types.SetValueFrom(ctx, types.StringType, strings.Split(cs.Zoneid, ","))
state.ZoneIds = z
}
// else: preserve prior state value (null or []) — both mean "all zones"

state.DynamicScalingEnabled = types.BoolValue(cs.Dynamicscalingenabled)
state.IsVolatile = types.BoolValue(cs.Isvolatile)
Expand Down Expand Up @@ -205,9 +214,10 @@ func (plan *serviceOfferingCommonResourceModel) commonCreateParams(ctx context.C
if !plan.OfferHa.IsNull() {
p.SetOfferha(plan.OfferHa.ValueBool())
}
if !plan.ZoneIds.IsNull() {
zoneIds := make([]string, len(plan.ZoneIds.Elements()))
plan.ZoneIds.ElementsAs(ctx, &zoneIds, false)
zoneIDs := plan.ZoneIds
if !zoneIDs.IsNull() {
zoneIds := make([]string, len(zoneIDs.Elements()))
zoneIDs.ElementsAs(ctx, &zoneIds, false)
p.SetZoneid(zoneIds)
}

Expand Down Expand Up @@ -262,6 +272,12 @@ func (plan *ServiceOfferingDiskOffering) commonCreateParams(ctx context.Context,

}

func (plan *serviceOfferingCommonResourceModel) applyTags(p *cloudstack.CreateServiceOfferingParams) {
if !plan.Tags.IsNull() {
p.SetTags(plan.Tags.ValueString())
}
}

func (plan *ServiceOfferingDiskQosStorage) commonCreateParams(ctx context.Context, p *cloudstack.CreateServiceOfferingParams) *cloudstack.CreateServiceOfferingParams {
if !plan.CustomizedIops.IsNull() {
p.SetCustomizediops(plan.CustomizedIops.ValueBool())
Expand Down