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

Adding variable set resource #76

Merged
merged 6 commits into from
Jun 19, 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
7 changes: 3 additions & 4 deletions kaleido/platform/ams_dmlistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -85,7 +84,7 @@ func (r *ams_dmlistenerResource) Schema(_ context.Context, _ resource.SchemaRequ
}
}

func (data *AMSDMListenerResourceModel) toAPI(api *AMSDMListenerAPIModel, diagnostics *diag.Diagnostics) bool {
func (data *AMSDMListenerResourceModel) toAPI(api *AMSDMListenerAPIModel) bool {
api.Name = data.Name.ValueString()
api.TaskID = data.TaskID.ValueString()
if !data.TopicFilter.IsNull() {
Expand All @@ -112,7 +111,7 @@ func (r *ams_dmlistenerResource) Create(ctx context.Context, req resource.Create
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

var api AMSDMListenerAPIModel
ok := data.toAPI(&api, &resp.Diagnostics)
ok := data.toAPI(&api)
if ok {
ok, _ = r.apiRequest(ctx, http.MethodPut, r.apiPath(&data, data.Name.ValueString()), &api, &api, &resp.Diagnostics)
}
Expand All @@ -132,7 +131,7 @@ func (r *ams_dmlistenerResource) Update(ctx context.Context, req resource.Update
resp.Diagnostics.Append(req.State.GetAttribute(ctx, path.Root("id"), &data.ID)...)

var api AMSDMListenerAPIModel
ok := data.toAPI(&api, &resp.Diagnostics)
ok := data.toAPI(&api)
if ok {
ok, _ = r.apiRequest(ctx, http.MethodPut, r.apiPath(&data, data.ID.ValueString()), &api, &api, &resp.Diagnostics)
}
Expand Down
114 changes: 114 additions & 0 deletions kaleido/platform/ams_dmupsert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// 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"

"github.com/hashicorp/terraform-plugin-framework/diag"
"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"
"gopkg.in/yaml.v3"
)

type AMSDMUpsertResourceModel struct {
Environment types.String `tfsdk:"environment"`
Service types.String `tfsdk:"service"`
BulkUpsertYAML types.String `tfsdk:"bulk_upsert_yaml"`
}

func AMSDMUpsertResourceFactory() resource.Resource {
return &ams_dmupsertResource{}
}

type ams_dmupsertResource struct {
commonResource
}

func (r *ams_dmupsertResource) Metadata(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = "kaleido_platform_ams_dmupsert"
}

func (r *ams_dmupsertResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"environment": &schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"service": &schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"bulk_upsert_yaml": &schema.StringAttribute{
Required: true,
Description: "This is a bulk upsert input payload in YAML/JSON",
},
},
}
}

func (data *AMSDMUpsertResourceModel) toAPI(diagnostics *diag.Diagnostics) map[string]interface{} {
var parsedYAML map[string]interface{}
err := yaml.Unmarshal([]byte(data.BulkUpsertYAML.ValueString()), &parsedYAML)
if err != nil {
diagnostics.AddError("invalid task YAML", err.Error())
return nil
}
return parsedYAML
}

func (r *ams_dmupsertResource) apiPath(data *AMSDMUpsertResourceModel) string {
return fmt.Sprintf("/endpoint/%s/%s/rest/api/v1/bulk/datamodel", data.Environment.ValueString(), data.Service.ValueString())
}

func (r *ams_dmupsertResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {

var data AMSDMUpsertResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

parsedYAML := data.toAPI(&resp.Diagnostics)
if parsedYAML != nil {
_, _ = r.apiRequest(ctx, http.MethodPut, r.apiPath(&data), parsedYAML, nil, &resp.Diagnostics)
}

resp.Diagnostics.Append(resp.State.Set(ctx, data)...)

}

func (r *ams_dmupsertResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {

var data AMSDMUpsertResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

parsedYAML := data.toAPI(&resp.Diagnostics)
if parsedYAML != nil {
_, _ = r.apiRequest(ctx, http.MethodPut, r.apiPath(&data), parsedYAML, nil, &resp.Diagnostics)
}

resp.Diagnostics.Append(resp.State.Set(ctx, data)...)
}

func (r *ams_dmupsertResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// no-op
}

func (r *ams_dmupsertResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// no-op
}
109 changes: 109 additions & 0 deletions kaleido/platform/ams_dmupsert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// 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 (
"net/http"
"testing"

"github.com/gorilla/mux"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"

_ "embed"
)

var ams_dmupsertStep1 = `
resource "kaleido_platform_ams_dmupsert" "ams_dmupsert1" {
environment = "env1"
service = "service1"
bulk_upsert_yaml = yamlencode(
{
"addresses": [
{
"updateType": "create_or_replace",
"address": "0x93976AE88d24130979FE554bFdfF32008839b04B",
"displayName": "bob"
}
]
}
)
}
`

var ams_dmupsertStep2 = `
resource "kaleido_platform_ams_dmupsert" "ams_dmupsert1" {
environment = "env1"
service = "service1"
bulk_upsert_yaml = yamlencode(
{
"addresses": [
{
"updateType": "create_or_replace",
"address": "0x93976AE88d24130979FE554bFdfF32008839b04B",
"displayName": "sally"
}
]
}
)
}
`

func TestAMSDMUpsert1(t *testing.T) {

mp, providerConfig := testSetup(t)
defer func() {
mp.checkClearCalls([]string{
"PUT /endpoint/{env}/{service}/rest/api/v1/bulk/datamodel",
"PUT /endpoint/{env}/{service}/rest/api/v1/bulk/datamodel",
})
mp.server.Close()
}()

resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: providerConfig + ams_dmupsertStep1,
},
{
Config: providerConfig + ams_dmupsertStep2,
Check: resource.ComposeAggregateTestCheckFunc(
func(s *terraform.State) error {
// Compare the final result on the mock-server side
obj := mp.amsDMUpserts["env1/service1"]
testYAMLEqual(t, obj, `{
"addresses": [
{
"updateType": "create_or_replace",
"address": "0x93976AE88d24130979FE554bFdfF32008839b04B",
"displayName": "sally"
}
]
}`)
return nil
},
),
},
},
})
}

func (mp *mockPlatform) putAMSDMUpsert(res http.ResponseWriter, req *http.Request) {
var newObj map[string]interface{}
mp.getBody(req, &newObj)
mp.amsDMUpserts[mux.Vars(req)["env"]+"/"+mux.Vars(req)["service"]] = newObj
mp.respond(res, &newObj, 200)
}
Loading
Loading