Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into dont-overwrite-de…
Browse files Browse the repository at this point in the history
…ployment-id-with-self

* upstream/master:
  Persist snapshot source in the read response (elastic#788)
  Stateless resources require kibana (elastic#792)
  Fixed error when universal profiling is activated (elastic#783)
  fix(deps): update module github.com/hashicorp/terraform-plugin-framework to v1.6.0 (elastic#790)
  • Loading branch information
tobio committed Mar 11, 2024
2 parents 8d8fd82 + cc5eba7 commit 8bbc64e
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .changelog/783.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
resource/deployment: Added support for symbols and profiling endpoints.
```
3 changes: 3 additions & 0 deletions .changelog/788.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/deployment: Persist the snapshot source settings during reads. This fixes a [provider crash](https://github.com/elastic/terraform-provider-ec/issues/787) when creating a deployment from a snapshot.
```
3 changes: 3 additions & 0 deletions .changelog/792.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
resource/deployment: Validate the Kibana is present when attempting to enable other stateless resources.
```
6 changes: 6 additions & 0 deletions ec/ecresource/deploymentresource/apm/v2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ package v2

import (
"github.com/elastic/terraform-provider-ec/ec/internal/planmodifiers"
"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

func ApmConfigSchema() schema.Attribute {
return schema.SingleNestedAttribute{
Description: `Optionally define the Apm configuration options for the APM Server`,
Optional: true,
Validators: []validator.Object{
objectvalidator.AlsoRequires(path.MatchRoot("kibana")),
},
Attributes: map[string]schema.Attribute{
"docker_image": schema.StringAttribute{
Description: "Optionally override the docker image the APM nodes will use. This option will not work in ESS customers and should only be changed if you know what you're doing.",
Expand Down
24 changes: 24 additions & 0 deletions ec/ecresource/deploymentresource/deployment/v2/deployment_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/elastic/cloud-sdk-go/pkg/util/ec"

apmv2 "github.com/elastic/terraform-provider-ec/ec/ecresource/deploymentresource/apm/v2"
elasticsearchv1 "github.com/elastic/terraform-provider-ec/ec/ecresource/deploymentresource/elasticsearch/v1"
elasticsearchv2 "github.com/elastic/terraform-provider-ec/ec/ecresource/deploymentresource/elasticsearch/v2"
enterprisesearchv2 "github.com/elastic/terraform-provider-ec/ec/ecresource/deploymentresource/enterprisesearch/v2"
integrationsserverv2 "github.com/elastic/terraform-provider-ec/ec/ecresource/deploymentresource/integrationsserver/v2"
Expand All @@ -41,6 +42,7 @@ import (
"github.com/elastic/terraform-provider-ec/ec/internal/converters"
"github.com/elastic/terraform-provider-ec/ec/internal/util"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)
Expand Down Expand Up @@ -68,6 +70,28 @@ type Deployment struct {
MigrateToLatestHardware *bool `tfsdk:"migrate_to_latest_hardware"`
}

func (dep *Deployment) PersistSnapshotSource(ctx context.Context, esPlan *elasticsearchv2.ElasticsearchTF) diag.Diagnostics {
if dep == nil || dep.Elasticsearch == nil {
return nil
}

if esPlan == nil || esPlan.SnapshotSource.IsNull() || esPlan.SnapshotSource.IsUnknown() {
return nil
}

var snapshotSource *elasticsearchv1.ElasticsearchSnapshotSourceTF
if diags := tfsdk.ValueAs(ctx, esPlan.SnapshotSource, &snapshotSource); diags.HasError() {
return diags
}

dep.Elasticsearch.SnapshotSource = &elasticsearchv2.ElasticsearchSnapshotSource{
SourceElasticsearchClusterId: snapshotSource.SourceElasticsearchClusterId.ValueString(),
SnapshotName: snapshotSource.SnapshotName.ValueString(),
}

return nil
}

// Nullify Elasticsearch topologies that have zero size and are not specified in plan
func (dep *Deployment) NullifyUnusedEsTopologies(ctx context.Context, esPlan *elasticsearchv2.ElasticsearchTF) {
if dep.Elasticsearch == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/elastic/cloud-sdk-go/pkg/models"
"github.com/elastic/cloud-sdk-go/pkg/util/ec"
apmv2 "github.com/elastic/terraform-provider-ec/ec/ecresource/deploymentresource/apm/v2"
elasticsearchv1 "github.com/elastic/terraform-provider-ec/ec/ecresource/deploymentresource/elasticsearch/v1"
elasticsearchv2 "github.com/elastic/terraform-provider-ec/ec/ecresource/deploymentresource/elasticsearch/v2"
enterprisesearchv2 "github.com/elastic/terraform-provider-ec/ec/ecresource/deploymentresource/enterprisesearch/v2"
kibanav2 "github.com/elastic/terraform-provider-ec/ec/ecresource/deploymentresource/kibana/v2"
Expand Down Expand Up @@ -1741,8 +1742,7 @@ func Test_ProcessSelfInObservability(t *testing.T) {
{
name: "should noop if deployment is nil",
},
{
name: "should noop if observability section is nil",
{name: "should noop if observability section is nil",
deployment: &Deployment{},
},
{
Expand Down Expand Up @@ -1855,3 +1855,59 @@ func Test_ProcessSelfInObservability(t *testing.T) {
})
}
}

func Test_PersistSnapshotSource(t *testing.T) {
tests := []struct {
name string
deployment *Deployment
snapshotSource *elasticsearchv1.ElasticsearchSnapshotSource
expectedSourceElasticsearchClusterId string
expectedSnapshotName string
}{
{
name: "should noop if deployment is nil",
},
{
name: "should noop if the esplan snapshot source is null",
deployment: &Deployment{},
},
{
name: "should set the snapshot source cluster and snapshot name if specified in the plan",
deployment: &Deployment{
Elasticsearch: &elasticsearchv2.Elasticsearch{},
},
snapshotSource: &elasticsearchv1.ElasticsearchSnapshotSource{
SourceElasticsearchClusterId: "source-cluster-id",
SnapshotName: "snapshot-name",
},
expectedSourceElasticsearchClusterId: "source-cluster-id",
expectedSnapshotName: "snapshot-name",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var obj types.Object
if tt.snapshotSource != nil {
diags := tfsdk.ValueFrom(context.Background(), tt.snapshotSource, elasticsearchv2.ElasticsearchSnapshotSourceSchema().GetType(), &obj)
require.Nil(t, diags)
}

esPlan := elasticsearchv2.ElasticsearchTF{
SnapshotSource: obj,
}

diags := tt.deployment.PersistSnapshotSource(context.Background(), &esPlan)
require.Nil(t, diags)

var snapshotName, sourceESClusterID string
if tt.deployment != nil && tt.deployment.Elasticsearch != nil && tt.deployment.Elasticsearch.SnapshotSource != nil {
snapshotName = tt.deployment.Elasticsearch.SnapshotSource.SnapshotName
sourceESClusterID = tt.deployment.Elasticsearch.SnapshotSource.SourceElasticsearchClusterId
}

require.Equal(t, tt.expectedSnapshotName, snapshotName)
require.Equal(t, tt.expectedSourceElasticsearchClusterId, sourceESClusterID)
})
}
}
4 changes: 2 additions & 2 deletions ec/ecresource/deploymentresource/elasticsearch/v2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func ElasticsearchSchema() schema.Attribute {

"snapshot": elasticsearchSnapshotSchema(),

"snapshot_source": elasticsearchSnapshotSourceSchema(),
"snapshot_source": ElasticsearchSnapshotSourceSchema(),

"extension": elasticsearchExtensionSchema(),

Expand Down Expand Up @@ -361,7 +361,7 @@ func elasticsearchSnapshotRepositoryReferenceSchema() schema.Attribute {
}
}

func elasticsearchSnapshotSourceSchema() schema.Attribute {
func ElasticsearchSnapshotSourceSchema() schema.Attribute {
return schema.SingleNestedAttribute{
Description: `Restores data from a snapshot of another deployment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@ package v2

import (
"github.com/elastic/terraform-provider-ec/ec/internal/planmodifiers"
"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

func EnterpriseSearchSchema() schema.Attribute {
return schema.SingleNestedAttribute{
Description: "Enterprise Search cluster definition.",
Optional: true,
Validators: []validator.Object{
objectvalidator.AlsoRequires(path.MatchRoot("kibana")),
},
Attributes: map[string]schema.Attribute{
"elasticsearch_cluster_ref_id": schema.StringAttribute{
Optional: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ type IntegrationsServerTF struct {
}

type EndpointsTF struct {
Fleet *string `tfsdk:"fleet"`
APM *string `tfsdk:"apm"`
Fleet *string `tfsdk:"fleet"`
APM *string `tfsdk:"apm"`
Symbols *string `tfsdk:"symbols"`
Profiling *string `tfsdk:"profiling"`
}

func (srv IntegrationsServerTF) payload(ctx context.Context, payload models.IntegrationsServerPayload) (*models.IntegrationsServerPayload, diag.Diagnostics) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ type IntegrationsServer struct {
}

type Endpoints struct {
Fleet *string `tfsdk:"fleet"`
APM *string `tfsdk:"apm"`
Fleet *string `tfsdk:"fleet"`
APM *string `tfsdk:"apm"`
Symbols *string `tfsdk:"symbols"`
Profiling *string `tfsdk:"profiling"`
}

func ReadIntegrationsServers(in []*models.IntegrationsServerResourceInfo) (*IntegrationsServer, error) {
Expand Down Expand Up @@ -121,6 +123,12 @@ func readEndpoints(in *models.IntegrationsServerResourceInfo) *Endpoints {
case "fleet":
endpoints.Fleet = url.URL
hasValidEndpoints = true
case "symbols":
endpoints.Symbols = url.URL
hasValidEndpoints = true
case "profiling":
endpoints.Profiling = url.URL
hasValidEndpoints = true
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ func Test_readIntegrationsServer(t *testing.T) {
Service: ec.String("fleet"),
URL: ec.String("https://fleet_endpoint.cloud.elastic.co"),
},
{
Service: ec.String("symbols"),
URL: ec.String("https://symbols_endpoint.cloud.elastic.co"),
},
{
Service: ec.String("profiling"),
URL: ec.String("https://profiling_endpoint.cloud.elastic.co"),
},
},
},
PlanInfo: &models.IntegrationsServerPlansInfo{Current: &models.IntegrationsServerPlanInfo{
Expand Down Expand Up @@ -115,8 +123,10 @@ func Test_readIntegrationsServer(t *testing.T) {
HttpEndpoint: ec.String("http://integrations_serverresource.cloud.elastic.co:9200"),
HttpsEndpoint: ec.String("https://integrations_serverresource.cloud.elastic.co:9243"),
Endpoints: &Endpoints{
Fleet: ec.String("https://fleet_endpoint.cloud.elastic.co"),
APM: ec.String("https://apm_endpoint.cloud.elastic.co"),
Fleet: ec.String("https://fleet_endpoint.cloud.elastic.co"),
APM: ec.String("https://apm_endpoint.cloud.elastic.co"),
Symbols: ec.String("https://symbols_endpoint.cloud.elastic.co"),
Profiling: ec.String("https://profiling_endpoint.cloud.elastic.co"),
},
InstanceConfigurationId: ec.String("aws.integrations_server.r4"),
InstanceConfigurationVersion: ec.Int(5),
Expand Down Expand Up @@ -153,6 +163,14 @@ func Test_readIntegrationsServer(t *testing.T) {
Service: ec.String("fleet"),
URL: ec.String("https://fleet_endpoint.cloud.elastic.co"),
},
{
Service: ec.String("symbols"),
URL: ec.String("https://symbols_endpoint.cloud.elastic.co"),
},
{
Service: ec.String("profiling"),
URL: ec.String("https://profiling_endpoint.cloud.elastic.co"),
},
},
},
PlanInfo: &models.IntegrationsServerPlansInfo{Current: &models.IntegrationsServerPlanInfo{
Expand Down Expand Up @@ -207,6 +225,14 @@ func Test_readIntegrationsServer(t *testing.T) {
Service: ec.String("fleet"),
URL: ec.String("https://fleet_endpoint.cloud.elastic.co"),
},
{
Service: ec.String("symbols"),
URL: ec.String("https://symbols_endpoint.cloud.elastic.co"),
},
{
Service: ec.String("profiling"),
URL: ec.String("https://profiling_endpoint.cloud.elastic.co"),
},
},
},
PlanInfo: &models.IntegrationsServerPlansInfo{Current: &models.IntegrationsServerPlanInfo{
Expand Down Expand Up @@ -247,8 +273,10 @@ func Test_readIntegrationsServer(t *testing.T) {
HttpEndpoint: ec.String("http://integrations_serverresource.cloud.elastic.co:9200"),
HttpsEndpoint: ec.String("https://integrations_serverresource.cloud.elastic.co:9243"),
Endpoints: &Endpoints{
Fleet: ec.String("https://fleet_endpoint.cloud.elastic.co"),
APM: ec.String("https://apm_endpoint.cloud.elastic.co"),
Fleet: ec.String("https://fleet_endpoint.cloud.elastic.co"),
APM: ec.String("https://apm_endpoint.cloud.elastic.co"),
Symbols: ec.String("https://symbols_endpoint.cloud.elastic.co"),
Profiling: ec.String("https://profiling_endpoint.cloud.elastic.co"),
},
InstanceConfigurationId: ec.String("aws.integrations_server.r4"),
Size: ec.String("1g"),
Expand Down Expand Up @@ -289,6 +317,14 @@ func Test_readIntegrationsServer(t *testing.T) {
Service: ec.String("fleet"),
URL: ec.String("https://fleet_endpoint.cloud.elastic.co"),
},
{
Service: ec.String("symbols"),
URL: ec.String("https://symbols_endpoint.cloud.elastic.co"),
},
{
Service: ec.String("profiling"),
URL: ec.String("https://profiling_endpoint.cloud.elastic.co"),
},
},
},
PlanInfo: &models.IntegrationsServerPlansInfo{Current: &models.IntegrationsServerPlanInfo{
Expand Down Expand Up @@ -330,8 +366,10 @@ func Test_readIntegrationsServer(t *testing.T) {
HttpEndpoint: ec.String("http://integrations_serverresource.cloud.elastic.co:9200"),
HttpsEndpoint: ec.String("https://integrations_serverresource.cloud.elastic.co:9243"),
Endpoints: &Endpoints{
Fleet: ec.String("https://fleet_endpoint.cloud.elastic.co"),
APM: ec.String("https://apm_endpoint.cloud.elastic.co"),
Fleet: ec.String("https://fleet_endpoint.cloud.elastic.co"),
APM: ec.String("https://apm_endpoint.cloud.elastic.co"),
Symbols: ec.String("https://symbols_endpoint.cloud.elastic.co"),
Profiling: ec.String("https://profiling_endpoint.cloud.elastic.co"),
},
InstanceConfigurationId: ec.String("aws.integrations_server.r4"),
Size: ec.String("1g"),
Expand Down
12 changes: 10 additions & 2 deletions ec/ecresource/deploymentresource/integrationsserver/v2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,25 @@ package v2

import (
"github.com/elastic/terraform-provider-ec/ec/internal/planmodifiers"
"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func IntegrationsServerSchema() schema.Attribute {
return schema.SingleNestedAttribute{
Description: "Integrations Server cluster definition. Integrations Server replaces `apm` in Stack versions > 8.0",
Optional: true,
Validators: []validator.Object{
objectvalidator.AlsoRequires(path.MatchRoot("kibana")),
},
Attributes: map[string]schema.Attribute{
"elasticsearch_cluster_ref_id": schema.StringAttribute{
Optional: true,
Expand Down Expand Up @@ -76,8 +82,10 @@ func IntegrationsServerSchema() schema.Attribute {
Computed: true,
Description: "URLs for the accessing the Fleet and APM API's within this Integrations Server resource.",
AttributeTypes: map[string]attr.Type{
"apm": types.StringType,
"fleet": types.StringType,
"apm": types.StringType,
"fleet": types.StringType,
"symbols": types.StringType,
"profiling": types.StringType,
},
PlanModifiers: []planmodifier.Object{
objectplanmodifier.UseStateForUnknown(),
Expand Down
1 change: 1 addition & 0 deletions ec/ecresource/deploymentresource/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func (r *Resource) read(ctx context.Context, id string, state *deploymentv2.Depl
diags.Append(deployment.ProcessSelfInObservability(ctx, base)...)

deployment.NullifyUnusedEsTopologies(ctx, baseElasticsearch)
diags.Append(deployment.PersistSnapshotSource(ctx, baseElasticsearch)...)

if !deployment.HasNodeTypes() {
// The MigrateDeploymentTemplate request can only be performed for deployments that use node roles.
Expand Down
Loading

0 comments on commit 8bbc64e

Please sign in to comment.