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

Migrate resource ec_deployment_extension to terraform-plugin-framework #12

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
40 changes: 40 additions & 0 deletions ec/acc/deployment_extension_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,46 @@ func TestAccDeploymentExtension_basic(t *testing.T) {
})
}

func TestAccDeploymentExtension_UpgradeFrom0_4_1(t *testing.T) {
resName := "ec_deployment_extension.my_extension"
randomName := prefix + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

cfg := fixtureAccExtensionBasicWithTF(t, "testdata/extension_basic.tf", randomName, "desc")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
CheckDestroy: testAccDeploymentTrafficFilterDestroy,
Steps: []resource.TestStep{
{
ExternalProviders: map[string]resource.ExternalProvider{
"ec": {
VersionConstraint: "0.4.1",
Source: "elastic/ec",
},
},
Config: cfg,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resName, "name", randomName),
resource.TestCheckResourceAttr(resName, "version", "*"),
resource.TestCheckResourceAttr(resName, "description", "desc"),
resource.TestCheckResourceAttr(resName, "extension_type", "bundle"),
),
},
{
PlanOnly: true,
ProtoV6ProviderFactories: testAccProviderFactory,
Config: cfg,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resName, "name", randomName),
resource.TestCheckResourceAttr(resName, "version", "*"),
resource.TestCheckResourceAttr(resName, "description", "desc"),
resource.TestCheckResourceAttr(resName, "extension_type", "bundle"),
),
},
},
})
}

func fixtureAccExtensionBasicWithTF(t *testing.T, tfFileName, extensionName, description string) string {
t.Helper()

Expand Down
6 changes: 3 additions & 3 deletions ec/ecdatasource/deploymentsdatasource/expanders.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func expandFilters(ctx context.Context, state modelV0) (*models.SearchRequest, d
Prefix: map[string]models.PrefixQuery{
// The "keyword" addition denotes that the query will be using a keyword
// field rather than a text field in order to ensure the query is not analyzed
"name.keyword": {Value: ec.String(namePrefix)},
"name.keyword": {Value: &namePrefix},
},
})
}
Expand Down Expand Up @@ -180,11 +180,11 @@ func expandResourceFilters(ctx context.Context, resources *types.List, resourceK
func newNestedTermQuery(path, term string, value string) *models.QueryContainer {
return &models.QueryContainer{
Nested: &models.NestedQuery{
Path: ec.String(path),
Path: &path,
Query: &models.QueryContainer{
Term: map[string]models.TermQuery{
term: {
Value: ec.String(value),
Value: &value,
},
},
},
Expand Down
10 changes: 5 additions & 5 deletions ec/ecresource/elasticsearchkeystoreresource/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ func (r Resource) Create(ctx context.Context, request resource.CreateRequest, re
found, diags := r.read(ctx, newState.DeploymentID.Value, &newState)
response.Diagnostics.Append(diags...)
if !found {
// We can't unset the state here, and must make sure to set the state according to the plan below.
// So all we do is add a warning.
diags.AddWarning(
"Failed to read Elasticsearch keystore.",
"Please run terraform refresh to ensure a consistent state.",
response.Diagnostics.AddError(
"Failed to read Elasticsearch keystore after create.",
"Failed to read Elasticsearch keystore after create.",
)
response.State.RemoveResource(ctx)
return
}
if response.Diagnostics.HasError() {
return
Expand Down
3 changes: 1 addition & 2 deletions ec/ecresource/elasticsearchkeystoreresource/expanders.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/json"

"github.com/elastic/cloud-sdk-go/pkg/models"
"github.com/elastic/cloud-sdk-go/pkg/util/ec"
)

func expandModel(ctx context.Context, state modelV0) *models.KeystoreContents {
Expand All @@ -39,7 +38,7 @@ func expandModel(ctx context.Context, state modelV0) *models.KeystoreContents {
return &models.KeystoreContents{
Secrets: map[string]models.KeystoreSecret{
secretName: {
AsFile: ec.Bool(state.AsFile.Value),
AsFile: &state.AsFile.Value,
Value: value,
},
},
Expand Down
6 changes: 3 additions & 3 deletions ec/ecresource/elasticsearchkeystoreresource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ func TestResourceElasticsearchKeyStore_notFoundAfterCreate_and_gracefulDeletion(
),
Steps: []r.TestStep{
{ // Create resource
Config: externalKeystore1,
Check: checkResource1(),
ExpectNonEmptyPlan: true,
Config: externalKeystore1,
Check: checkResource1(),
ExpectError: regexp.MustCompile(`Failed to read Elasticsearch keystore after create.`),
},
},
})
Expand Down
10 changes: 5 additions & 5 deletions ec/ecresource/elasticsearchkeystoreresource/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ func (r Resource) Update(ctx context.Context, request resource.UpdateRequest, re
return
}
if !found {
// We can't unset the state here, and must make sure to set the state according to the plan below.
// So all we do is add a warning.
diags.AddWarning(
"Failed to read Elasticsearch keystore.",
"Please run terraform refresh to ensure a consistent state.",
response.Diagnostics.AddError(
"Failed to read Elasticsearch keystore after update.",
"Failed to read Elasticsearch keystore after update.",
)
response.State.RemoveResource(ctx)
return
}

// Finally, set the state
Expand Down
96 changes: 44 additions & 52 deletions ec/ecresource/extensionresource/create.go
Original file line number Diff line number Diff line change
@@ -1,73 +1,65 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package extensionresource

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/elastic/cloud-sdk-go/pkg/api"
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/extensionapi"
"github.com/elastic/cloud-sdk-go/pkg/models"
"github.com/elastic/cloud-sdk-go/pkg/multierror"
)

// createResource will create a new deployment extension
func createResource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*api.API)
func (r *Resource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
if !resourceReady(r, &response.Diagnostics) {
return
}

var newState modelV0

diags := request.Plan.Get(ctx, &newState)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}

model, err := createRequest(client, d)
model, err := extensionapi.Create(
extensionapi.CreateParams{
API: r.client,
Name: newState.Name.Value,
Version: newState.Version.Value,
Type: newState.ExtensionType.Value,
Description: newState.Description.Value,
DownloadURL: newState.DownloadURL.Value,
},
)
if err != nil {
return diag.FromErr(err)
response.Diagnostics.AddError(err.Error(), err.Error())
return
}

d.SetId(*model.ID)
newState.ID = types.String{Value: *model.ID}

if _, ok := d.GetOk("file_path"); ok {
if err := uploadExtension(client, d); err != nil {
return diag.FromErr(multierror.NewPrefixed("failed to upload file", err))
if !newState.FilePath.IsNull() && newState.FilePath.Value != "" {
response.Diagnostics.Append(r.uploadExtension(newState)...)
if response.Diagnostics.HasError() {
return
}
}
return readResource(ctx, d, meta)
}

func createRequest(client *api.API, d *schema.ResourceData) (*models.Extension, error) {
name := d.Get("name").(string)
version := d.Get("version").(string)
extensionType := d.Get("extension_type").(string)
description := d.Get("description").(string)
downloadURL := d.Get("download_url").(string)

body := extensionapi.CreateParams{
API: client,
Name: name,
Version: version,
Type: extensionType,
Description: description,
DownloadURL: downloadURL,
found, diags := r.read(newState.ID.Value, &newState)
response.Diagnostics.Append(diags...)
if !found {
pascal-hofmann marked this conversation as resolved.
Show resolved Hide resolved
response.Diagnostics.AddError(
"Failed to read deployment extension after create.",
"Failed to read deployment extension after create.",
)
response.State.RemoveResource(ctx)
return
}

res, err := extensionapi.Create(body)
if err != nil {
return nil, err
if response.Diagnostics.HasError() {
return
}

return res, nil
// Finally, set the state
response.Diagnostics.Append(response.State.Set(ctx, newState)...)
}
94 changes: 0 additions & 94 deletions ec/ecresource/extensionresource/create_test.go

This file was deleted.

Loading