Skip to content

Commit

Permalink
removing ruleset_id from profile, added profile_id to ruleset
Browse files Browse the repository at this point in the history
  • Loading branch information
dgulinobw committed Mar 2, 2023
1 parent 8d72799 commit 240d37b
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 79 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/hashicorp/terraform-plugin-go v0.14.1
github.com/hashicorp/terraform-plugin-log v0.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.0
github.com/relaypro-open/dog_api_golang v0.0.0-20230301202705-a8388bf5dace
github.com/relaypro-open/dog_api_golang v0.0.0-20230302175531-9d6c47df744f
golang.org/x/exp v0.0.0-20221114191408-850992195362
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ github.com/relaypro-open/dog_api_golang v0.0.0-20230225221129-625f80ac48dd h1:YO
github.com/relaypro-open/dog_api_golang v0.0.0-20230225221129-625f80ac48dd/go.mod h1:gVISNtT0C/Ej5ZnCePDyppaDQiPRANiq2A/qhx/tH+8=
github.com/relaypro-open/dog_api_golang v0.0.0-20230301202705-a8388bf5dace h1:9sbbqxAR1pF5W346wEh9aHrvBidsR8c3Z5EZw24+S5I=
github.com/relaypro-open/dog_api_golang v0.0.0-20230301202705-a8388bf5dace/go.mod h1:gVISNtT0C/Ej5ZnCePDyppaDQiPRANiq2A/qhx/tH+8=
github.com/relaypro-open/dog_api_golang v0.0.0-20230302144827-df2f22eb619a h1:CnC2b+kRRzyCmBBBPmxlcdidWoyrSGz+6spGJNMJ180=
github.com/relaypro-open/dog_api_golang v0.0.0-20230302144827-df2f22eb619a/go.mod h1:gVISNtT0C/Ej5ZnCePDyppaDQiPRANiq2A/qhx/tH+8=
github.com/relaypro-open/dog_api_golang v0.0.0-20230302175531-9d6c47df744f h1:hWSrRabbh1uOA3cNlJWDfGDQ3Y+oIg4hs9uwCf3FSAQ=
github.com/relaypro-open/dog_api_golang v0.0.0-20230302175531-9d6c47df744f/go.mod h1:gVISNtT0C/Ej5ZnCePDyppaDQiPRANiq2A/qhx/tH+8=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
Expand Down
1 change: 0 additions & 1 deletion internal/provider/datasource_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type (
Profile struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
RulesetId types.String `tfsdk:"ruleset_id"`
Version types.String `tfsdk:"version"`
}
)
Expand Down
5 changes: 4 additions & 1 deletion internal/provider/datasource_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type (
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Rules *rulesetResourceRules `tfsdk:"rules"`
//ProfileId types.String `tfsdk:"profile_id" force:",omitempty"`
ProfileId types.String `tfsdk:"profile_id" json:"profile_id,omitempty"`

}

Rules struct {
Expand Down Expand Up @@ -123,7 +126,7 @@ func (d *rulesetDataSource) Read(ctx context.Context, req datasource.ReadRequest

// Set state
for _, api_ruleset := range res {
ruleset := ApiToRuleset(api_ruleset)
ruleset := ApiToRuleset(ctx, api_ruleset)
state = append(state, ruleset)
}
diags := resp.State.Set(ctx, &state)
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (*groupResource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnos
},
"profile_id": {
MarkdownDescription: "group profile id",
Optional: true,
Required: true,
Type: types.StringType,
},
"profile_name": {
Expand Down
9 changes: 0 additions & 9 deletions internal/provider/resource_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
type profileResourceData struct {
ID types.String `tfsdk:"id"`
Name string `tfsdk:"name"`
RulesetId string `tfsdk:"ruleset_id"`
Version string `tfsdk:"version"`
}

Expand Down Expand Up @@ -50,11 +49,6 @@ func (*profileResource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagn
Required: true,
Type: types.StringType,
},
"ruleset_id": {
MarkdownDescription: "Rule Id",
Required: true,
Type: types.StringType,
},
"version": {
MarkdownDescription: "Profile version",
Required: true,
Expand Down Expand Up @@ -101,7 +95,6 @@ func ProfileToCreateRequest(plan profileResourceData) api.ProfileCreateRequest {

newProfile := api.ProfileCreateRequest{
Name: plan.Name,
RulesetId: plan.RulesetId,
Version: plan.Version,
}
return newProfile
Expand All @@ -110,7 +103,6 @@ func ProfileToCreateRequest(plan profileResourceData) api.ProfileCreateRequest {
func ProfileToUpdateRequest(plan profileResourceData) api.ProfileUpdateRequest {
newProfile := api.ProfileUpdateRequest{
Name: plan.Name,
RulesetId: plan.RulesetId,
Version: plan.Version,
}
return newProfile
Expand All @@ -121,7 +113,6 @@ func ApiToProfile(profile api.Profile) Profile {
//Created: types.Int64{Value: int64(profile.Created)},
ID: types.String{Value: profile.ID},
Name: types.String{Value: profile.Name},
RulesetId: types.String{Value: profile.RulesetId},
Version: types.String{Value: profile.Version},
}
return h
Expand Down
137 changes: 95 additions & 42 deletions internal/provider/resource_ruleset.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

package dog

import (
"context"
"fmt"
"log"

"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -46,6 +46,11 @@ func (*rulesetResource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagn
Required: true,
Type: types.StringType,
},
"profile_id": {
MarkdownDescription: "profile id",
Optional: true,
Type: types.StringType,
},
"rules": {
MarkdownDescription: "Rule rules",
Required: true,
Expand Down Expand Up @@ -137,9 +142,10 @@ func (*rulesetResource) ImportState(ctx context.Context, req resource.ImportStat


type rulesetResourceData struct {
ID types.String `tfsdk:"id"`
Rules *rulesetResourceRules `tfsdk:"rules"`
Name string `tfsdk:"name"`
ID types.String `tfsdk:"id"`
Rules *rulesetResourceRules `tfsdk:"rules"`
Name string `tfsdk:"name"`
ProfileId *string `tfsdk:"profile_id" force:",omitempty"`
}

type rulesetResourceRules struct {
Expand All @@ -163,7 +169,7 @@ type rulesetResourceRule struct {
Type types.String `tfsdk:"type"`
}

func RulesetToCreateRequest(plan rulesetResourceData) api.RulesetCreateRequest {
func RulesetToCreateRequest(ctx context.Context, plan rulesetResourceData) api.RulesetCreateRequest {
inboundRules := []*api.Rule{}
for _, inbound_rule := range plan.Rules.Inbound {
rule := &api.Rule{
Expand Down Expand Up @@ -203,17 +209,31 @@ func RulesetToCreateRequest(plan rulesetResourceData) api.RulesetCreateRequest {
outboundRules = append(outboundRules, rule)
}

newRuleset := api.RulesetCreateRequest{
Name: plan.Name,
Rules: &api.Rules{
Inbound: inboundRules,
Outbound: outboundRules,
},
tflog.Debug(ctx, spew.Sprint("ZZZplan.ProfileId: %#v", plan.ProfileId))
if plan.ProfileId == nil {
newRuleset := api.RulesetCreateRequest{
Name: plan.Name,
Rules: &api.Rules{
Inbound: inboundRules,
Outbound: outboundRules,
},
}
tflog.Debug(ctx, spew.Sprint("ZZZnewRuleset: %#v", newRuleset))
return newRuleset
} else {
newRuleset := api.RulesetCreateRequest{
Name: plan.Name,
Rules: &api.Rules{
Inbound: inboundRules,
Outbound: outboundRules,
},
ProfileId: plan.ProfileId,
}
return newRuleset
}
return newRuleset
}

func RulesetToUpdateRequest(plan rulesetResourceData) api.RulesetUpdateRequest {
func RulesetToUpdateRequest(ctx context.Context, plan rulesetResourceData) api.RulesetUpdateRequest {
inboundRules := []*api.Rule{}
for _, inbound_rule := range plan.Rules.Inbound {
rule := &api.Rule{
Expand Down Expand Up @@ -252,19 +272,37 @@ func RulesetToUpdateRequest(plan rulesetResourceData) api.RulesetUpdateRequest {
}
outboundRules = append(outboundRules, rule)
}

newRuleset := api.RulesetUpdateRequest{
Name: plan.Name,
Rules: &api.Rules{
Inbound: inboundRules,
Outbound: outboundRules,
},

newString := "123"
newStringPointer := &newString

tflog.Debug(ctx, spew.Sprint("ZZZplan.ProfileId: %#v", plan.ProfileId))
if plan.ProfileId == nil {
newRuleset := api.RulesetUpdateRequest{
Name: plan.Name,
Rules: &api.Rules{
Inbound: inboundRules,
Outbound: outboundRules,
},
ProfileId: newStringPointer,
}
tflog.Debug(ctx, spew.Sprint("ZZZnewRuleset: %#v", newRuleset))
return newRuleset
} else {
newRuleset := api.RulesetUpdateRequest{
Name: plan.Name,
Rules: &api.Rules{
Inbound: inboundRules,
Outbound: outboundRules,
},
ProfileId: plan.ProfileId,
}
return newRuleset
}
return newRuleset
}


func ApiToRuleset(ruleset api.Ruleset) Ruleset {
func ApiToRuleset(ctx context.Context, ruleset api.Ruleset) Ruleset {
newInboundRules := []*rulesetResourceRule{}
for _, inbound_rule := range ruleset.Rules.Inbound {
rule := &rulesetResourceRule{
Expand Down Expand Up @@ -303,17 +341,32 @@ func ApiToRuleset(ruleset api.Ruleset) Ruleset {
}
newOutboundRules = append(newOutboundRules, rule)
}
h := Ruleset{
//Created: types.Int64{Value: int64(rule.Created)},
ID: types.String{Value: ruleset.ID},
Name: types.String{Value: ruleset.Name},
//Rules: &Rules{
Rules: &rulesetResourceRules{
Inbound: newInboundRules,
Outbound: newOutboundRules,
},

tflog.Debug(ctx, spew.Sprint("ZZZruleset: %#v", ruleset))
if ruleset.ProfileId == nil {
h := Ruleset{
ID: types.String{Value: ruleset.ID},
Name: types.String{Value: ruleset.Name},
Rules: &rulesetResourceRules{
Inbound: newInboundRules,
Outbound: newOutboundRules,
},
ProfileId: types.StringNull(),
}
tflog.Debug(ctx, spew.Sprint("ZZZh: %#v", h))
return h
} else {
h := Ruleset{
ID: types.String{Value: ruleset.ID},
Name: types.String{Value: ruleset.Name},
Rules: &rulesetResourceRules{
Inbound: newInboundRules,
Outbound: newOutboundRules,
},
ProfileId: types.String{Value: *ruleset.ProfileId},
}
return h
}
return h
}

func (r *rulesetResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
Expand All @@ -327,11 +380,11 @@ func (r *rulesetResource) Create(ctx context.Context, req resource.CreateRequest
return
}

newRuleset := RulesetToCreateRequest(plan)
newRuleset := RulesetToCreateRequest(ctx, plan)
log.Printf(fmt.Sprintf("r.p.dog: %+v\n", r.p.dog))
ruleset, statusCode, err := r.p.dog.CreateRuleset(newRuleset, nil)
log.Printf(fmt.Sprintf("ruleset: %+v\n", ruleset))
tflog.Trace(ctx, fmt.Sprintf("ruleset: %+v\n", ruleset))
tflog.Debug(ctx, fmt.Sprintf("ruleset: %+v\n", ruleset))
if statusCode != 201 {
resp.Diagnostics.AddError("Client Unsuccesful", fmt.Sprintf("Status Code: %d", statusCode))
}
Expand All @@ -341,14 +394,14 @@ func (r *rulesetResource) Create(ctx context.Context, req resource.CreateRequest
if resp.Diagnostics.HasError() {
return
}
state = ApiToRuleset(ruleset)
state = ApiToRuleset(ctx, ruleset)

plan.ID = state.ID

// write logs using the tflog package
// see https://pkg.go.dev/github.com/hashicorp/terraform-plugin-log/tflog
// for more information
tflog.Trace(ctx, "created a resource")
tflog.Debug(ctx, "created a resource")

diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -378,7 +431,7 @@ func (r *rulesetResource) Read(ctx context.Context, req resource.ReadRequest, re
if resp.Diagnostics.HasError() {
return
}
state = ApiToRuleset(ruleset)
state = ApiToRuleset(ctx, ruleset)
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
}
Expand All @@ -402,11 +455,11 @@ func (r *rulesetResource) Update(ctx context.Context, req resource.UpdateRequest
return
}

newRuleset := RulesetToUpdateRequest(plan)
newRuleset := RulesetToUpdateRequest(ctx, plan)
ruleset, statusCode, err := r.p.dog.UpdateRuleset(rulesetID, newRuleset, nil)
log.Printf(fmt.Sprintf("ruleset: %+v\n", ruleset))
tflog.Trace(ctx, fmt.Sprintf("ruleset: %+v\n", ruleset))
state = ApiToRuleset(ruleset)
tflog.Debug(ctx, fmt.Sprintf("ruleset: %+v\n", ruleset))
state = ApiToRuleset(ctx, ruleset)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create ruleset, got error: %s", err))
}
Expand All @@ -420,7 +473,7 @@ func (r *rulesetResource) Update(ctx context.Context, req resource.UpdateRequest

plan.ID = state.ID

tflog.Trace(ctx, "created a resource")
tflog.Debug(ctx, "created a resource")

diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -448,7 +501,7 @@ func (r *rulesetResource) Delete(ctx context.Context, req resource.DeleteRequest
if resp.Diagnostics.HasError() {
return
}
tflog.Trace(ctx, fmt.Sprintf("ruleset deleted: %+v\n", ruleset))
tflog.Debug(ctx, fmt.Sprintf("ruleset deleted: %+v\n", ruleset))

diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
Expand Down
1 change: 0 additions & 1 deletion internal/provider/test/datasource_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func testAccDogGroupProfileDataSourceConfig() string {
resource "dog_profile" "datasource_group" {
name = "datasource_group"
version = "1.0"
ruleset_id = dog_ruleset.datasource_group.id
}
`)
}
Expand Down
Loading

0 comments on commit 240d37b

Please sign in to comment.