Skip to content

Commit

Permalink
Added Resource Definition
Browse files Browse the repository at this point in the history
  • Loading branch information
PranoSA committed Jan 24, 2024
1 parent eb80355 commit 085f077
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ website/vendor

# Keep windows files with windows line endings
*.winfile eol=crlf

terraform-provider-unicode
14 changes: 10 additions & 4 deletions examples/provider-install-verification/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ terraform {
}

provider "unicode" {
user = "tray"
//user = "tray"
user = var.user
}

data "unicode_unicode_chars" "example" {
Expand All @@ -27,7 +28,7 @@ output "unicode_char" {
*/

resource "unicode_app" "example_app" {
id = "examplrrrre"
id = "example 14"
name = "example app234"
description = "example"
updated_at = "2021-07-01T00:00:00Z"
Expand All @@ -38,13 +39,18 @@ output "name" {
value = resource.unicode_app.example_app
}

resource "unicode_unicode_string" "my_string" {
/*resource "unicode_unicode_string" "my_string" {
app_id = unicode_app.example_app.id
name = "Hello, World!"
index = 0
id = "example"
}
output "my_gamer" {
*/

/*output "my_gamer" {
value = resource.unicode_unicode_string.my_string
}
*/

// Get Resources and Print Them
6 changes: 6 additions & 0 deletions examples/provider-install-verification/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

// User To Use For Provider + default of terry
variable "user" {
type = string
default = "terry"
}
125 changes: 105 additions & 20 deletions internal/provider/app_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,30 @@ 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"
)

var (
_ resource.Resource = &UnicodeAppResource{}
_ resource.Resource = &UnicodeAppResource{}
_ resource.ResourceWithConfigure = &UnicodeAppResource{}
)

func NewUnicodeAppResource() resource.Resource {
return &UnicodeAppResource{}
}

type UnicodeAppResource struct {
unicodeClient *unicode_client.UnicodeProviderClient
}

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

// What Types and Annotations Will I need if I want to create a resource that will create a unicode character
type UnicodeAppModel struct {
Id string `json:"id" tfsdk:"id"`
Name string `json:"name" tfsdk:"name"`
Description string `json:"description" tfsdk:"description"`
Created_at string `json:"created_at" tfsdk:"created_at"`
Updated_at string `json:"updated_at" tfsdk:"updated_at"`
}

func (r *UnicodeAppResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
//resp.Schema = schema.Schema{}
Expand Down Expand Up @@ -58,24 +55,26 @@ func (r *UnicodeAppResource) Schema(_ context.Context, _ resource.SchemaRequest,

// Create a New Resource
func (r *UnicodeAppResource) Create(_ context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
//resp.State = req.Plan
var plan UnicodeAppModel
diags := req.Config.Get(context.Background(), &plan)
resp.Diagnostics.Append(diags...)
var plan unicode_client.UnicodeAppModel
//resp.State = req.NewState
req.Config.Get(context.Background(), &plan)

if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.AddWarning("Client Resource", plan.Id)

rt := &UnicodeAppModel{
//Get the Resource Client
res, err := r.unicodeClient.CreateApplication(unicode_client.UnicodeAppModel{
Id: plan.Id,
Name: plan.Name,
Description: plan.Description,
Created_at: plan.Created_at,
Updated_at: plan.Updated_at,
})
if err != nil {
resp.Diagnostics.AddError("Unable to get Unicode Applications", err.Error())
return
}

diags = resp.State.Set(context.Background(), rt)
diags := resp.State.Set(context.Background(), res)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -84,14 +83,100 @@ func (r *UnicodeAppResource) Create(_ context.Context, req resource.CreateReques
}

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

//
//
response, err := r.unicodeClient.GetApplications()
if err != nil {
resp.Diagnostics.AddError("Unable to get Unicode Applications", err.Error())
resp.Diagnostics.AddWarning("OLD ID ", response.Id)
return
}

var old_state unicode_client.UnicodeAppModel

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

diags := resp.State.Set(context.Background(), old_state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

}

func (r *UnicodeAppResource) Update(_ context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan unicode_client.UnicodeAppModel
//resp.State = req.NewState
resp.State = req.State
req.Config.Get(context.Background(), &plan)
//req.State.Get(context.Background(), &plan)

var old_plan unicode_client.UnicodeAppModel

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

resp.Diagnostics.AddWarning("Client Resource", old_plan.Id)

//Delete Old Resource
err := r.unicodeClient.DeleteApplication(old_plan.Id) //Assuming It keeps Same ID -> Stop Being a Silly Goose ...
if err != nil {
resp.Diagnostics.AddError("Unable to get Unicode Applications", err.Error())
return
}

//Get the Resource Client
res, err := r.unicodeClient.CreateApplication(unicode_client.UnicodeAppModel{
Id: plan.Id,
Name: plan.Name,
Description: plan.Description,
Created_at: plan.Created_at,
Updated_at: plan.Updated_at,
})
if err != nil {
resp.Diagnostics.AddError("Unable to get Unicode Applications", err.Error())
return
}

diags := resp.State.Set(context.Background(), res)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

}

func (r *UnicodeAppResource) Delete(_ context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
resp.State = req.State
//Delete Application

var plan unicode_client.UnicodeAppModel

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

err := r.unicodeClient.DeleteApplication(plan.Id)

if err != nil {
resp.Diagnostics.AddError("Unable to get Unicode Applications", err.Error())
return
}

// Return
resp.State.Set(context.Background(), nil)

}

func (r *UnicodeAppResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {

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

return
}
2 changes: 1 addition & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (up *unicodeProvider) Configure(ctx context.Context, req provider.Configure
client := unicode_client.NewUnicodeProviderClient(username)

if client == nil {
resp.Diagnostics.AddError("Unable to create client", "Client is NULL After NewUnicodeProviderClient")
resp.Diagnostics.AddError("Unable to create client provider", "Client is NULL After NewUnicodeProviderClient")
return
}

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/string_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (r *unicodeStringResource) Read(_ context.Context, req resource.ReadRequest
}

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

func (r *unicodeStringResource) Delete(_ context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
Expand Down
3 changes: 1 addition & 2 deletions internal/provider/unicode_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ func (d *UnicodeDataSource) Metadata(ctx context.Context, req datasource.Metadat

func (d *UnicodeDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {

resp.Diagnostics.AddWarning("configure not implemented", "")

unicodeClient, ok := req.ProviderData.(*unicode_client.UnicodeProviderClient)

if unicodeClient == nil {
resp.Diagnostics.AddWarning("invalid provider configuration", "IS NULL")
unicodeClient = unicode_client.NewUnicodeProviderClient("bob")
}

if !ok {
Expand Down
Loading

0 comments on commit 085f077

Please sign in to comment.