Skip to content

Commit

Permalink
Added Support For Creation Of Strings
Browse files Browse the repository at this point in the history
  • Loading branch information
PranoSA committed Jan 24, 2024
1 parent 085f077 commit a9b3e19
Show file tree
Hide file tree
Showing 4 changed files with 403 additions and 59 deletions.
38 changes: 36 additions & 2 deletions examples/provider-install-verification/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ output "unicode_char" {
*/

resource "unicode_app" "example_app" {
id = "example 14"
name = "example app234"
id = "example14"
name = "example app234 for bob the builder the 2nd"
description = "example"
updated_at = "2021-07-01T00:00:00Z"
created_at = "2021-07-01T00:00:00Z"
Expand All @@ -39,6 +39,40 @@ output "name" {
value = resource.unicode_app.example_app
}

/*resource "unicode_unicode_string" "my_string2" {
app_id = unicode_app.example_app.id
value = "Hello, World!"
}
output "my_string2" {
value = resource.unicode_unicode_string.my_string2
}
*/

resource "unicode_unicode_string" "my_string" {
app_id = unicode_app.example_app.id
value = "!"
}

resource "unicode_unicode_string" "my_string2" {
app_id = unicode_unicode_string.my_string.app_id
value = "Hello, BOBS WORLD!"
}

resource "unicode_unicode_string" "my_string3" {
app_id = unicode_unicode_string.my_string2.app_id
value = "Hello, TRUCE OF THE MATTER!"
}

output "my_string2" {
value = resource.unicode_unicode_string.my_string2
}

output "my_string" {
value = resource.unicode_unicode_string.my_string
}

/*resource "unicode_unicode_string" "my_string" {
app_id = unicode_app.example_app.id
name = "Hello, World!"
Expand Down
21 changes: 11 additions & 10 deletions internal/provider/app_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,23 @@ func (r *UnicodeAppResource) Create(_ context.Context, req resource.CreateReques
}

func (r *UnicodeAppResource) Read(_ context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var response *unicode_client.UnicodeAppModel

var request *unicode_client.UnicodeAppModel

req.State.Get(context.Background(), &request)

//
//
response, err := r.unicodeClient.GetApplications()
response, err := r.unicodeClient.GetApplication(request.Id)

if err != nil {
resp.Diagnostics.AddError("Unable to get Unicode Applications", err.Error())
resp.Diagnostics.AddWarning("OLD ID ", response.Id)
return
//resp.Diagnostics.AddError("Unable to get Unicode Applications", err.Error())
//return
resp.Diagnostics.AddWarning("May Not Be Created Yet", err.Error())
req.State.Get(context.Background(), response)
}

var old_state unicode_client.UnicodeAppModel

req.State.Get(context.Background(), &old_state)

diags := resp.State.Set(context.Background(), old_state)
diags := resp.State.Set(context.Background(), response)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand Down
113 changes: 84 additions & 29 deletions internal/provider/string_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,54 @@ package provider

import (
"context"
unicode_client "terraform-provider-unicode/internal/unicode"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
)

type UnicodeStringModel struct {
Id string `json:"id" tfsdk:"id"`
Name string `json:"name" tfsdk:"name"`
Index int `json:"index" tfsdk:"index"`
AppId string `json:"app_id" tfsdk:"app_id"`
}
var (
_ resource.Resource = &unicodeStringResource{}
_ resource.ResourceWithConfigure = &unicodeStringResource{}
)

func NewUnicodeStringResource() resource.Resource {
return &unicodeStringResource{}
}

type unicodeStringResource struct{}
type unicodeStringResource struct {
unicodeClient *unicode_client.UnicodeProviderClient
}

func (r *unicodeStringResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_unicode_string"
}

func (r *unicodeStringResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
//resp.State = req.NewState
//req.Config.Get(context.Background(), &plan)

//r.unicodeClient = unicode_client.NewUnicodeProviderClient("test")
unicodeClient, ok := req.ProviderData.(*unicode_client.UnicodeProviderClient)

if !ok {
//resp.Diagnostics.AddError("Unable to create client in resource", "Client is NULL After NewUnicodeProviderClient")
//return
resp.Diagnostics.AddWarning("Unable to create client in resource", "Client is NULL After NewUnicodeProviderClient")
unicodeClient = unicode_client.NewUnicodeProviderClient("bob")
}

r.unicodeClient = unicodeClient
}

func (r *unicodeStringResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {

resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"app_id": schema.StringAttribute{
Required: true,
},
"name": schema.StringAttribute{
Required: false,
Optional: true,
},
"index": schema.NumberAttribute{
Required: true,
},
"id": schema.StringAttribute{
"value": schema.StringAttribute{
Required: false,
Optional: true,
},
Expand All @@ -48,7 +59,7 @@ func (r *unicodeStringResource) Schema(_ context.Context, _ resource.SchemaReque

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

var plan UnicodeStringModel
var plan unicode_client.UnicodeStringModel

diags := req.Config.Get(context.Background(), &plan)

Expand All @@ -58,25 +69,32 @@ func (r *unicodeStringResource) Create(_ context.Context, req resource.CreateReq
return
}

rr := &UnicodeStringModel{
Id: plan.Id,
Name: plan.Name,
Index: plan.Index,
AppId: plan.AppId,
// Now Create the Conversionf
res, c, err := r.unicodeClient.AddConversionToApplication(plan)

if err != nil {
resp.Diagnostics.AddError("Unable to Create Conversion", err.Error())
return
}

diags = resp.State.Set(context.Background(), rr)
diags = resp.State.Set(context.Background(), res)

resp.Diagnostics.Append(diags...)

var conv string
for _, v := range c {
conv += "" + v.Value
}

resp.Diagnostics.AddWarning("Conversion Created", conv)

if resp.Diagnostics.HasError() {
return
}

}

func (r *unicodeStringResource) Read(_ context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var plan UnicodeStringModel
var plan unicode_client.UnicodeStringModel

diags := req.State.Get(context.Background(), &plan)

Expand All @@ -86,11 +104,13 @@ func (r *unicodeStringResource) Read(_ context.Context, req resource.ReadRequest
return
}

rr := &UnicodeStringModel{
Id: plan.Id,
Name: plan.Name,
Index: plan.Index,
AppId: plan.AppId,
// Now Create the Conversion
rr, err := r.unicodeClient.GetConversionFromApplication(plan)
if err != nil {
resp.Diagnostics.AddWarning("Conversion Not Found", err.Error())

rr = &plan
//return
}

diags = resp.State.Set(context.Background(), rr)
Expand All @@ -100,12 +120,47 @@ func (r *unicodeStringResource) Read(_ context.Context, req resource.ReadRequest
if resp.Diagnostics.HasError() {
return
}

}

func (r *unicodeStringResource) Update(_ context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Didn't Change

// Not Implemented For Now ..
var plan unicode_client.UnicodeStringModel

diags := req.Config.Get(context.Background(), &plan)

resp.Diagnostics.Append(diags...)

req.State.Set(context.Background(), plan)
}

func (r *unicodeStringResource) Delete(_ context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
//
var plan unicode_client.UnicodeStringModel

diags := req.State.Get(context.Background(), &plan)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

// Now Create the Conversion
_, err := r.unicodeClient.RemoveConversionFromApplication(plan)

if err != nil {
resp.Diagnostics.AddError("Unable to Delete Conversion", err.Error())
return
}

diags = resp.State.Set(context.Background(), plan)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}
}
Loading

0 comments on commit a9b3e19

Please sign in to comment.