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

EPM fixes #854

Merged
merged 9 commits into from
Oct 23, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fix validation of `throttle`, and `interval` attributes in `elasticstack_kibana_alerting_rule` allowing all Elastic duration values ([#846](https://github.com/elastic/terraform-provider-elasticstack/pull/846))
- Fix boolean setting parsing for `elasticstack_elasticsearch_indices` data source. ([#842](https://github.com/elastic/terraform-provider-elasticstack/pull/842))
- Update all Fleet and utils/tfsdk instances of diagnostics parameters to pass by pointer instead of pass by value. Added upgrader for fleet_integration_policy v0 to handle empty string vars_json/streams_json. ([#855](https://github.com/elastic/terraform-provider-elasticstack/pull/855))
- Fix handling of EPM packages when uninstalled outside Terraform, and diags in create/update. ([#854](https://github.com/elastic/terraform-provider-elasticstack/pull/854))

## [0.11.9] - 2024-10-14

Expand Down
139 changes: 107 additions & 32 deletions generated/fleet/fleet.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 43 additions & 6 deletions generated/fleet/getschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ var transformers = []TransformFunc{
transformAddPackagePolicyVars,
transformAddPackagePolicySecretReferences,
transformFixPackageSearchResult,
transformFixGetPackageResult,
}

// transformFilterPaths filters the paths in a schema down to
Expand Down Expand Up @@ -232,16 +233,36 @@ func transformInlinePackageDefinitions(schema *Schema) {

// Get
{
props, ok := epmPath.Get.Responses.GetFields("200.content.application/json.schema.allOf.1.properties")
respSchema, ok := epmPath.Get.Responses.GetFields("200.content.application/json.schema")
if !ok {
panic("properties not found")
}

// status needs to be moved to schemes and a ref inserted in its place.
value, _ := props.Get("status")
// allOf.1 should also be inside "item"
_allOf, _ := respSchema.Get("allOf")
allOf := _allOf.([]any)
respSchema.Delete("allOf")

list := make([]any, 2)
allOf0, _ := Fields(allOf[0].(map[string]any)).GetFields("properties.item")
allOf1 := Fields(allOf[1].(map[string]any))
list[0] = allOf0
list[1] = allOf1
respSchema.Set("properties.item.allOf", list)

// item needs to be moved to schemas and a ref inserted in its place.
value, _ := respSchema.Get("properties.item.allOf")
respSchema.Delete("properties.item.allOf")
schema.Components.Set("schemas.get_package_item.allOf", value)
respSchema.Set("properties.item.$ref", "#/components/schemas/get_package_item")

// status needs to be moved to schemas and a ref inserted in its place.
props, _ := schema.Components.GetFields("schemas.get_package_item.allOf.1.properties")
value, _ = props.GetFields("status")
schema.Components.Set("schemas.package_status", value)
props.Delete("status")
props.Set("status.$ref", "#/components/schemas/package_status")

}

// Post
Expand Down Expand Up @@ -369,6 +390,18 @@ func transformFixPackageSearchResult(schema *Schema) {
properties.Delete("installationInfo")
}

// transformFixGetPackageResult removes unneeded fields from the
// GetPackageResult struct. These fields are also causing parsing errors.
func transformFixGetPackageResult(schema *Schema) {
properties, ok := schema.Components.GetFields("schemas.package_info.properties")
if !ok {
panic("properties not found")
}
properties.Delete("assets")
properties.Delete("icons")
properties.Delete("installationInfo")
}

// downloadFile will download a file from url and return the
// bytes. If the request fails, or a non 200 error code is
// observed in the response, an error is returned instead.
Expand Down Expand Up @@ -462,11 +495,15 @@ func (f Fields) Get(key string) (any, bool) {
if !ok {
return nil, false
}
if m, isMap := slicedValue.(map[string]any); ok && isMap {

switch m := slicedValue.(type) {
case map[string]any:
return Fields(m).Get(postSliceKeys)
case Fields:
return m.Get(postSliceKeys)
default:
return slicedValue, true
}
return slicedValue, true

default:
rootKey = key
}
Expand Down
40 changes: 17 additions & 23 deletions internal/clients/fleet/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"io"
"net/http"

fleetapi "github.com/elastic/terraform-provider-elasticstack/generated/fleet"
Expand Down Expand Up @@ -333,27 +332,21 @@ func DeletePackagePolicy(ctx context.Context, client *Client, id string, force b
}

// ReadPackage reads a specific package from the API.
func ReadPackage(ctx context.Context, client *Client, name, version string) diag.Diagnostics {
func ReadPackage(ctx context.Context, client *Client, name, version string) (*fleetapi.GetPackageItem, diag.Diagnostics) {
params := fleetapi.GetPackageParams{}

resp, err := client.API.GetPackage(ctx, name, version, &params)
resp, err := client.API.GetPackageWithResponse(ctx, name, version, &params)
if err != nil {
return utils.FrameworkDiagFromError(err)
return nil, utils.FrameworkDiagFromError(err)
}
defer resp.Body.Close()

switch resp.StatusCode {
switch resp.StatusCode() {
case http.StatusOK:
return nil
return resp.JSON200.Item, nil
case http.StatusNotFound:
return utils.FrameworkDiagFromError(ErrPackageNotFound)
return nil, utils.FrameworkDiagFromError(ErrPackageNotFound)
default:
errData, err := io.ReadAll(resp.Body)
if err != nil {
return utils.FrameworkDiagFromError(err)
}

return reportUnknownError(resp.StatusCode, errData)
return nil, reportUnknownError(resp.StatusCode(), resp.Body)
}
}

Expand All @@ -365,22 +358,16 @@ func InstallPackage(ctx context.Context, client *Client, name, version string, f
IgnoreConstraints: nil,
}

resp, err := client.API.InstallPackage(ctx, name, version, &params, body)
resp, err := client.API.InstallPackageWithResponse(ctx, name, version, &params, body)
if err != nil {
return utils.FrameworkDiagFromError(err)
}
defer resp.Body.Close()

switch resp.StatusCode {
switch resp.StatusCode() {
case http.StatusOK:
return nil
default:
errData, err := io.ReadAll(resp.Body)
if err != nil {
return utils.FrameworkDiagFromError(err)
}

return reportUnknownError(resp.StatusCode, errData)
return reportUnknownError(resp.StatusCode(), resp.Body)
}
}

Expand All @@ -399,6 +386,13 @@ func Uninstall(ctx context.Context, client *Client, name, version string, force
switch resp.StatusCode() {
case http.StatusOK:
return nil
case http.StatusBadRequest:
msg := resp.JSON400.Message
if msg != nil && *msg == fmt.Sprintf("%s is not installed", name) {
return nil
} else {
return reportUnknownError(resp.StatusCode(), resp.Body)
}
case http.StatusNotFound:
return nil
default:
Expand Down
7 changes: 6 additions & 1 deletion internal/fleet/integration/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package integration
import (
"context"

fleetapi "github.com/elastic/terraform-provider-elasticstack/generated/fleet"
"github.com/elastic/terraform-provider-elasticstack/internal/clients/fleet"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand All @@ -25,12 +26,16 @@ func (r *integrationResource) Read(ctx context.Context, req resource.ReadRequest

name := stateModel.Name.ValueString()
version := stateModel.Version.ValueString()
diags = fleet.ReadPackage(ctx, client, name, version)
pkg, diags := fleet.ReadPackage(ctx, client, name, version)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
resp.State.RemoveResource(ctx)
return
}
if pkg.Status != fleetapi.Installed {
resp.State.RemoveResource(ctx)
return
}

stateModel.ID = types.StringValue(getPackageID(name, version))

Expand Down
Loading
Loading