forked from elastic/terraform-provider-ec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate resource ec_deployment_extension to terraform-plugin-framework (
#12) * Migrate resource ec_deployment_extension to terraform-plugin-framework * Remove useless use of ec.Bool() and ec.String() Co-authored-by: Pascal Hofmann <[email protected]>
- Loading branch information
1 parent
07aecc1
commit f310127
Showing
25 changed files
with
824 additions
and
964 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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)...) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.