Skip to content

Commit

Permalink
Initial work - adding field to the schema
Browse files Browse the repository at this point in the history
  • Loading branch information
mieciu committed Jan 11, 2024
1 parent a3dc7c8 commit 4a4618e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ resource "ec_deployment" "example_minimal" {
hot = {
autoscaling = {}
}
ml = {
autoscaling = {
tier_autoscale = true
}
}
}
kibana = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package v2

import (
"context"

"github.com/elastic/cloud-sdk-go/pkg/api"
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/deptemplateapi"
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/esremoteclustersapi"
Expand Down Expand Up @@ -84,6 +83,9 @@ func (dep DeploymentTF) CreateRequest(ctx context.Context, client *api.API) (*mo
return nil, diagsnostics
}

// We don't want this setting to be inferred from deployment template when using TF provider - it should be specified explicitly in resource declaration
removeAutoscalingTierOverridesFromTemplate(template)

baseUpdatePayloads := &models.DeploymentUpdateResources{
Apm: template.DeploymentTemplate.Resources.Apm,
Appsearch: template.DeploymentTemplate.Resources.Appsearch,
Expand Down Expand Up @@ -170,6 +172,14 @@ func (dep DeploymentTF) CreateRequest(ctx context.Context, client *api.API) (*mo
return &result, diagsnostics
}

func removeAutoscalingTierOverridesFromTemplate(template *models.DeploymentTemplateInfoV2) {
for _, esResource := range template.DeploymentTemplate.Resources.Elasticsearch {
for _, topologyElem := range esResource.Plan.ClusterTopology {
topologyElem.AutoscalingTierOverride = nil
}
}
}

// trafficFilterToModel expands the flattened "traffic_filter" settings to a DeploymentCreateRequest.
func trafficFilterToModel(ctx context.Context, set types.Set, req *models.DeploymentCreateRequest) diag.Diagnostics {
if len(set.Elements()) == 0 || req == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func (plan DeploymentTF) getBaseUpdatePayloads(ctx context.Context, client *api.
return nil, err
}

// Similarly to deployment creation, we don't want this setting to be inferred from deployment template
removeAutoscalingTierOverridesFromTemplate(template)

baseUpdatePayloads := &models.DeploymentUpdateResources{
Apm: template.DeploymentTemplate.Resources.Apm,
Appsearch: template.DeploymentTemplate.Resources.Appsearch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ElasticsearchTopologyAutoscalingTF struct {
MinSizeResource types.String `tfsdk:"min_size_resource"`
MinSize types.String `tfsdk:"min_size"`
PolicyOverrideJson types.String `tfsdk:"policy_override_json"`
TierAutoscale types.Bool `tfsdk:"tier_autoscale"`
}

type ElasticsearchTopologyAutoscaling struct {
Expand All @@ -35,6 +36,7 @@ type ElasticsearchTopologyAutoscaling struct {
MinSizeResource *string `tfsdk:"min_size_resource"`
MinSize *string `tfsdk:"min_size"`
PolicyOverrideJson *string `tfsdk:"policy_override_json"`
TierAutoscale *bool `tfsdk:"tier_autoscale"`
}

type ElasticsearchTopologyAutoscalings []ElasticsearchTopologyAutoscaling
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ func readElasticsearchTopologyAutoscaling(topology *models.ElasticsearchClusterT
a.PolicyOverrideJson = ec.String(string(b))
}

if topology.AutoscalingTierOverride != nil && *topology.AutoscalingTierOverride == true {

Check failure on line 195 in ec/ecresource/deploymentresource/elasticsearch/v2/elasticsearch_topology.go

View workflow job for this annotation

GitHub Actions / Unit

S1002: should omit comparison to bool constant, can be simplified to `*topology.AutoscalingTierOverride` (gosimple)
a.TierAutoscale = topology.AutoscalingTierOverride
}

return &a, nil
}

Expand Down Expand Up @@ -357,6 +361,10 @@ func elasticsearchTopologyAutoscalingPayload(ctx context.Context, autoObj attr.V
}
}

if !autoscale.TierAutoscale.IsNull() && !autoscale.TierAutoscale.IsUnknown() {
payload.AutoscalingTierOverride = autoscale.TierAutoscale.ValueBoolPointer()
}

return diag
}

Expand Down
8 changes: 8 additions & 0 deletions ec/ecresource/deploymentresource/elasticsearch/v2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ func elasticsearchTopologyAutoscalingSchema(topologyAttributeName string) schema
UseTopologyStateForUnknown(topologyAttributeName),
},
},
"tier_autoscale": schema.BoolAttribute{
Description: `Whether this specific tier should be auto-scaled, overrides deployment-wide setting`,
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.Bool{
UseTopologyStateForUnknown(topologyAttributeName),
},
},
"policy_override_json": schema.StringAttribute{
Description: "Computed policy overrides set directly via the API or other clients.",
Computed: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func (m useTopologyState) PlanModifyInt64(ctx context.Context, req planmodifier.
}
}

func (m useTopologyState) PlanModifyBool(ctx context.Context, req planmodifier.BoolRequest, resp *planmodifier.BoolResponse) {
useState, diags := m.UseState(ctx, req.ConfigValue, req.Plan, req.State, resp.PlanValue)
resp.Diagnostics.Append(diags...)
if useState {
resp.PlanValue = req.StateValue
}
}

type PlanModifierResponse interface {
planmodifier.StringResponse | planmodifier.Int64Response
}
Expand Down

0 comments on commit 4a4618e

Please sign in to comment.