-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from kaleido-io/stacks-updates
Stacks updates
- Loading branch information
Showing
10 changed files
with
1,038 additions
and
647 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// Copyright © Kaleido, Inc. 2024 | ||
|
||
// Licensed 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 platform | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
// stacks interface implementations for the resource and API | ||
type StacksResourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
Environment types.String `tfsdk:"environment"` | ||
EnvironmentMemberID types.String `tfsdk:"environment_member_id"` | ||
Name types.String `tfsdk:"name"` | ||
Type types.String `tfsdk:"type"` | ||
NetworkType types.String `tfsdk:"network_type"` | ||
} | ||
|
||
type StacksAPIModel struct { | ||
ID string `json:"id,omitempty"` | ||
Created *time.Time `json:"created,omitempty"` | ||
Updated *time.Time `json:"updated,omitempty"` | ||
EnvironmentMemberID string `json:"environmentMemberId,omitempty"` | ||
Name string `json:"name"` | ||
Type string `json:"type"` | ||
NetworkType string `json:"networkType,omitempty"` | ||
} | ||
|
||
func StacksResourceFactory() resource.Resource { | ||
return &stacksResource{} | ||
} | ||
|
||
type stacksResource struct { | ||
commonResource | ||
} | ||
|
||
func (r *stacksResource) Metadata(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = "kaleido_platform_stack" | ||
} | ||
|
||
func (r *stacksResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": &schema.StringAttribute{ | ||
Computed: true, | ||
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, | ||
}, | ||
"name": &schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
"type": &schema.StringAttribute{ | ||
Required: true, | ||
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, | ||
}, | ||
"environment": &schema.StringAttribute{ | ||
Required: true, | ||
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, | ||
}, | ||
"environment_member_id": &schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"network_type": &schema.StringAttribute{ | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (data *StacksResourceModel) toAPI(api *StacksAPIModel) { | ||
api.Type = data.Type.ValueString() | ||
api.Name = data.Name.ValueString() | ||
if !data.NetworkType.IsNull() { | ||
api.NetworkType = data.NetworkType.ValueString() | ||
} | ||
} | ||
|
||
func (api *StacksAPIModel) toData(data *StacksResourceModel) { | ||
data.ID = types.StringValue(api.ID) | ||
data.EnvironmentMemberID = types.StringValue(api.EnvironmentMemberID) | ||
data.NetworkType = types.StringValue(api.NetworkType) | ||
data.Name = types.StringValue(api.Name) | ||
data.Type = types.StringValue(api.Type) | ||
if api.NetworkType != "" && !data.NetworkType.IsNull() { | ||
data.NetworkType = types.StringValue(api.NetworkType) | ||
} | ||
} | ||
|
||
func (r *stacksResource) apiPath(data *StacksResourceModel) string { | ||
path := fmt.Sprintf("/api/v1/environments/%s/stacks", data.Environment.ValueString()) | ||
if data.ID.ValueString() != "" { | ||
path = path + "/" + data.ID.ValueString() | ||
} | ||
return path | ||
} | ||
|
||
func (r *stacksResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
|
||
var data StacksResourceModel | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) | ||
|
||
var api StacksAPIModel | ||
data.toAPI(&api) | ||
ok, _ := r.apiRequest(ctx, http.MethodPost, r.apiPath(&data), api, &api, &resp.Diagnostics) | ||
if !ok { | ||
return | ||
} | ||
|
||
api.toData(&data) // need the ID copied over | ||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||
} | ||
|
||
func (r *stacksResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
|
||
var data StacksResourceModel | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) | ||
resp.Diagnostics.Append(req.State.GetAttribute(ctx, path.Root("id"), &data.ID)...) | ||
|
||
// Read full current object | ||
var api StacksAPIModel | ||
if ok, _ := r.apiRequest(ctx, http.MethodGet, r.apiPath(&data), nil, &api, &resp.Diagnostics); !ok { | ||
return | ||
} | ||
|
||
// Update from plan | ||
data.toAPI(&api) | ||
if ok, _ := r.apiRequest(ctx, http.MethodPut, r.apiPath(&data), api, &api, &resp.Diagnostics); !ok { | ||
return | ||
} | ||
|
||
api.toData(&data) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||
} | ||
|
||
func (r *stacksResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
var data StacksResourceModel | ||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...) | ||
|
||
var api StacksAPIModel | ||
api.ID = data.ID.ValueString() | ||
ok, status := r.apiRequest(ctx, http.MethodGet, r.apiPath(&data), nil, &api, &resp.Diagnostics, Allow404()) | ||
if !ok { | ||
return | ||
} | ||
if status == 404 { | ||
resp.State.RemoveResource(ctx) | ||
return | ||
} | ||
|
||
api.toData(&data) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||
} | ||
|
||
func (r *stacksResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
var data StacksResourceModel | ||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...) | ||
|
||
_, _ = r.apiRequest(ctx, http.MethodDelete, r.apiPath(&data), nil, nil, &resp.Diagnostics, Allow404()) | ||
|
||
r.waitForRemoval(ctx, r.apiPath(&data), &resp.Diagnostics) | ||
} |
Oops, something went wrong.