From 52e946c7e45bb8751a0ff2989793b59afe2ba848 Mon Sep 17 00:00:00 2001 From: Diana Barreto Date: Tue, 5 Dec 2023 16:26:49 +1000 Subject: [PATCH] Add default values and send empty array when list are not provided --- docs/index.md | 2 +- docs/resources/attribution.md | 12 ++- docs/resources/attribution_group.md | 11 ++- docs/resources/report.md | 72 +++++++++++++-- examples/main.tf | 14 ++- examples/provider/provider.tf | 2 +- .../resource.tf | 0 .../resource.tf | 0 .../resource.tf | 4 +- internal/provider/attribution_resource.go | 3 - internal/provider/model.go | 4 +- internal/provider/report.go | 2 + internal/provider/report_resource.go | 89 ++++++++++++------- test.json | 23 +++-- 14 files changed, 171 insertions(+), 67 deletions(-) rename examples/resources/{doit-console_attribution => doit_attribution}/resource.tf (100%) rename examples/resources/{doit-console_attribution_group => doit_attribution_group}/resource.tf (100%) rename examples/resources/{doit-console_report => doit_report}/resource.tf (94%) diff --git a/docs/index.md b/docs/index.md index 8a98378..82da00a 100644 --- a/docs/index.md +++ b/docs/index.md @@ -17,7 +17,7 @@ terraform { required_providers { doit = { source = "doitintl/doit" - version = "0.2.1" + version = "0.5.0" } } } diff --git a/docs/resources/attribution.md b/docs/resources/attribution.md index f4f30eb..4bbcd10 100644 --- a/docs/resources/attribution.md +++ b/docs/resources/attribution.md @@ -10,7 +10,17 @@ description: |- - +## Example Usage + +```terraform +# Manage Attribution group +resource "doit_attribution" "attri" { + name = "attritestnewname9" + description = "attritestdiana8" + formula = "A" + components = [{ type = "label", key = "iris_location", values = ["us"] }] +} +``` ## Schema diff --git a/docs/resources/attribution_group.md b/docs/resources/attribution_group.md index 9b3be1a..32501af 100644 --- a/docs/resources/attribution_group.md +++ b/docs/resources/attribution_group.md @@ -10,7 +10,16 @@ description: |- - +## Example Usage + +```terraform +# Manage Attribution group +resource "doit_attribution_group" "attributeGroup" { + name = "attritestnewgroup" + description = "attritestgroup" + attributions = [doit_attribution.attribute1.id, doit_attribution.attribute2.id] +} +``` ## Schema diff --git a/docs/resources/report.md b/docs/resources/report.md index 7b2f8b9..8d2dc9d 100644 --- a/docs/resources/report.md +++ b/docs/resources/report.md @@ -10,7 +10,68 @@ description: |- - +## Example Usage + +```terraform +resource "doit_report" "my-report" { + name = "test_report" + description = "test_report" + config = { + metric = { + type = "basic" + value = "cost" + } + include_promotional_credits = false + advanced_analysis = { + trending_up = false + trending_down = false + not_trending = false + forecast = false + } + aggregation = "total" + time_interval = "month" + dimensions = [ + { + id = "year" + type = "datetime" + }, + { + id = "month" + type = "datetime" + } + ] + time_range = { + mode = "last" + amount = 12 + include_current = true + unit = "month" + } + filters = [ + { + inverse = false + id = "attribution" + type = "attribution" + values = [ + "1CE699ZdwN5CRBw0tInY" + ] + } + ] + group = [ + { + id = "BSQZmvX6hvuKGPDHX7R3" + type = "attribution_group" + }, + { + id = "cloud_provider" + type = "fixed" + } + ] + layout = "table" + display_values = "actuals_only" + currency = "USD" + } +} +``` ## Schema @@ -32,19 +93,16 @@ description: |- ### Nested Schema for `config` -Required: - -- `advanced_analysis` (Attributes) (see [below for nested schema](#nestedatt--config--advanced_analysis)) -- `include_promotional_credits` (Boolean) Whether to include credits or not. If set, the report must use time interval “month”/”quarter”/”year” - Optional: +- `advanced_analysis` (Attributes) (see [below for nested schema](#nestedatt--config--advanced_analysis)) - `aggregation` (String) - `currency` (String) - `dimensions` (Attributes List) (see [below for nested schema](#nestedatt--config--dimensions)) - `display_values` (String) - `filters` (Attributes List) The filters to use in this report (see [below for nested schema](#nestedatt--config--filters)) - `group` (Attributes List) The groups to use in the report. (see [below for nested schema](#nestedatt--config--group)) +- `include_promotional_credits` (Boolean) Whether to include credits or not. If set, the report must use time interval “month”/”quarter”/”year” - `layout` (String) - `metric` (Attributes) (see [below for nested schema](#nestedatt--config--metric)) - `metric_filter` (Attributes) (see [below for nested schema](#nestedatt--config--metric_filter)) @@ -55,7 +113,7 @@ Optional: ### Nested Schema for `config.advanced_analysis` -Required: +Optional: - `forecast` (Boolean) Advanced analysis toggles. Each of these can be set independently - `not_trending` (Boolean) diff --git a/examples/main.tf b/examples/main.tf index 0117a80..d0eb126 100644 --- a/examples/main.tf +++ b/examples/main.tf @@ -2,13 +2,13 @@ terraform { required_providers { doit = { source = "doitintl/doit" - version = "0.3.1" + version = "0.5.0" } } } resource "doit_report" "my-report" { - name = "test10" + name = "test_new_provider" config = { metric = { type = "basic" @@ -59,6 +59,16 @@ resource "doit_report" "my-report" { type = "fixed" } ] + group = [ + { + id = "BSQZmvX6hvuKGPDHX7R3" + type = "attribution_group" + }, + { + id = "cloud_provider" + type = "fixed" + } + ] layout = "table" display_values = "actuals_only" currency = "USD" diff --git a/examples/provider/provider.tf b/examples/provider/provider.tf index 1730182..981bdf4 100644 --- a/examples/provider/provider.tf +++ b/examples/provider/provider.tf @@ -2,7 +2,7 @@ terraform { required_providers { doit = { source = "doitintl/doit" - version = "0.2.1" + version = "0.5.0" } } } diff --git a/examples/resources/doit-console_attribution/resource.tf b/examples/resources/doit_attribution/resource.tf similarity index 100% rename from examples/resources/doit-console_attribution/resource.tf rename to examples/resources/doit_attribution/resource.tf diff --git a/examples/resources/doit-console_attribution_group/resource.tf b/examples/resources/doit_attribution_group/resource.tf similarity index 100% rename from examples/resources/doit-console_attribution_group/resource.tf rename to examples/resources/doit_attribution_group/resource.tf diff --git a/examples/resources/doit-console_report/resource.tf b/examples/resources/doit_report/resource.tf similarity index 94% rename from examples/resources/doit-console_report/resource.tf rename to examples/resources/doit_report/resource.tf index 48c096b..2db2198 100644 --- a/examples/resources/doit-console_report/resource.tf +++ b/examples/resources/doit_report/resource.tf @@ -1,6 +1,6 @@ resource "doit_report" "my-report" { - name = "test10" - description = "test10" + name = "test_report" + description = "test_report" config = { metric = { type = "basic" diff --git a/internal/provider/attribution_resource.go b/internal/provider/attribution_resource.go index 8a0b45d..4b99f6e 100644 --- a/internal/provider/attribution_resource.go +++ b/internal/provider/attribution_resource.go @@ -128,9 +128,6 @@ func (r *attributionResource) Configure(_ context.Context, req resource.Configur // Create creates the resource and sets the initial Terraform state. func (r *attributionResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { log.Println(" attribution Create") - log.Println(r.client.Auth.DoiTAPITOken) - log.Println("---------------------------------------------------") - log.Println(r.client.Auth.CustomerContext) // Retrieve values from plan var plan attributionResourceModel diff --git a/internal/provider/model.go b/internal/provider/model.go index e9dac62..a038128 100644 --- a/internal/provider/model.go +++ b/internal/provider/model.go @@ -60,10 +60,10 @@ type ExternalConfig struct { DisplayValues string `json:"displayValues,omitempty"` // Filters The filters to use in this report - Filters []ExternalConfigFilter `json:"filters,omitempty"` + Filters []ExternalConfigFilter `json:"filters"` // Group The groups to use in the report. - Group []Group `json:"group,omitempty"` + Group []Group `json:"group"` // IncludePromotionalCredits Whether to include credits or not. // If set, the report must use time interval “month”/”quarter”/”year” diff --git a/internal/provider/report.go b/internal/provider/report.go index 51ab367..9b10b78 100644 --- a/internal/provider/report.go +++ b/internal/provider/report.go @@ -10,6 +10,8 @@ import ( // CreateReport - Create new report func (c *ClientTest) CreateReport(report Report) (*Report, error) { + log.Println("CreateReport----------------") + log.Println(report.Config.Filters) rb, err := json.Marshal(report) if err != nil { return nil, err diff --git a/internal/provider/report_resource.go b/internal/provider/report_resource.go index 0561740..8f85878 100644 --- a/internal/provider/report_resource.go +++ b/internal/provider/report_resource.go @@ -7,8 +7,12 @@ import ( "log" + "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/objectdefault" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" "github.com/hashicorp/terraform-plugin-framework/types" ) @@ -76,6 +80,25 @@ type AdvancedAnalysisModel struct { TrendingUp types.Bool `tfsdk:"trending_up"` } +func (aa AdvancedAnalysisModel) attrTypes() map[string]attr.Type { + return map[string]attr.Type{ + "forecast": types.BoolType, + "not_trending": types.BoolType, + "trending_down": types.BoolType, + "trending_up": types.BoolType, + } +} + +func (aa AdvancedAnalysisModel) defaultObject() map[string]attr.Value { + log.Println("defaultObject AdvancedAnalysisModel") + return map[string]attr.Value{ + "forecast": types.BoolValue(false), + "not_trending": types.BoolValue(false), + "trending_down": types.BoolValue(false), + "trending_up": types.BoolValue(false), + } +} + // GroupModel represents a group in the report. type GroupModel struct { Id types.String `tfsdk:"id"` @@ -179,6 +202,10 @@ type ExternalConfigFilterModel struct { Values []types.String `tfsdk:"values"` } +/*func (ecf ExternalConfigFilterModel) attrType() attr.Type { + return types.StringType +}*/ + // Ensure the implementation satisfies the expected interfaces. var ( _ resource.Resource = &reportResource{} @@ -212,23 +239,25 @@ func (r *reportResource) Schema(_ context.Context, _ resource.SchemaRequest, res Attributes: map[string]schema.Attribute{ "forecast": schema.BoolAttribute{ Description: "Advanced analysis toggles. Each of these can be set independently", - Required: true, + Optional: true, }, "not_trending": schema.BoolAttribute{ Description: "", - Required: true, + Optional: true, }, "trending_down": schema.BoolAttribute{ Description: "", - Required: true, + Optional: true, }, "trending_up": schema.BoolAttribute{ Description: "", - Required: true, + Optional: true, }, }, Description: "", - Required: true, + Optional: true, + Computed: true, + Default: objectdefault.StaticValue(types.ObjectValueMust(AdvancedAnalysisModel{}.attrTypes(), AdvancedAnalysisModel{}.defaultObject())), }, "aggregation": schema.StringAttribute{ Description: "", @@ -330,7 +359,9 @@ func (r *reportResource) Schema(_ context.Context, _ resource.SchemaRequest, res "include_promotional_credits": schema.BoolAttribute{ Description: "Whether to include credits or not. " + "If set, the report must use time interval “month”/”quarter”/”year”", - Required: true, + Optional: true, + Computed: true, + Default: booldefault.StaticBool(false), }, "layout": schema.StringAttribute{ Description: "", @@ -467,6 +498,8 @@ func (r *reportResource) Schema(_ context.Context, _ resource.SchemaRequest, res "description": schema.StringAttribute{ Description: "Report description", Optional: true, + Default: stringdefault.StaticString("-"), + Computed: true, }, "name": schema.StringAttribute{ Description: "Report name", @@ -508,13 +541,10 @@ func (r *reportResource) Configure(_ context.Context, req resource.ConfigureRequ // Create creates the resource and sets the initial Terraform state. func (r *reportResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { log.Println(" report Create") - log.Println(r.client.Auth.DoiTAPITOken) - log.Println("---------------------------------------------------") - log.Println(r.client.Auth.CustomerContext) // Retrieve values from plan var plan reportResourceModel - log.Println("before getting plan 1") + log.Println("before getting plan") diags := req.Plan.Get(ctx, &plan) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { @@ -522,10 +552,9 @@ func (r *reportResource) Create(ctx context.Context, req resource.CreateRequest, } log.Println("after getting plan") // Generate API request body from plan + log.Println("plan.Config:") log.Println(plan.Config) config := ExternalConfig{} - log.Println("1") - if plan.Config.AdvancedAnalysis != nil { advancedAnalysis := AdvancedAnalysis{ Forecast: plan.Config.AdvancedAnalysis.Forecast.ValueBool(), @@ -534,19 +563,8 @@ func (r *reportResource) Create(ctx context.Context, req resource.CreateRequest, TrendingUp: plan.Config.AdvancedAnalysis.TrendingUp.ValueBool(), } config.AdvancedAnalysis = &advancedAnalysis - } /*else { - // It needs to be initialized by default because the api return this value - // even if it was not provided when created and the terraform plugin complain - // because when creating the resource was null but when read it is not. - advancedAnalysis := AdvancedAnalysis{ - Forecast: false, - NotTrending: false, - TrendingDown: false, - TrendingUp: false, - } - config.AdvancedAnalysis = &advancedAnalysis - }*/ - log.Println("2") + } + log.Println("config.AdvancedAnalysis:") log.Println(config.AdvancedAnalysis) config.Aggregation = plan.Config.Aggregation.ValueString() config.Currency = plan.Config.Currency.ValueString() @@ -560,8 +578,8 @@ func (r *reportResource) Create(ctx context.Context, req resource.CreateRequest, dimensions = append(dimensions, dimension) } config.Dimensions = dimensions + filters := []ExternalConfigFilter{} if plan.Config.Filters != nil { - var filters []ExternalConfigFilter log.Println("3") for _, filter := range plan.Config.Filters { var values []string @@ -580,13 +598,15 @@ func (r *reportResource) Create(ctx context.Context, req resource.CreateRequest, filters = append(filters, filter) } log.Println("4") - config.Filters = filters } + config.Filters = filters + log.Println("config.Filters:") + log.Println(config.Filters) log.Println("DisplayValues") log.Println(plan.Config.DisplayValues) config.DisplayValues = plan.Config.DisplayValues.ValueString() + groups := []Group{} if plan.Config.Group != nil { - var groups []Group for _, group := range plan.Config.Group { if group.Limit != nil { log.Println("group.Limit") @@ -612,9 +632,10 @@ func (r *reportResource) Create(ctx context.Context, req resource.CreateRequest, }) } } - config.Group = groups } - log.Println("after group") + config.Group = groups + log.Println("config.Group:") + log.Println(config.Group) // It needs to be initialized by default because the api return this value // even if it was not provided when created and the terraform plugin complain // because when creating the resource was null but when read it is not. @@ -649,7 +670,6 @@ func (r *reportResource) Create(ctx context.Context, req resource.CreateRequest, } config.MetricFilter = &metricFilter } - log.Println("8") if plan.Config.Splits != nil { var splits []ExternalSplit for _, split := range plan.Config.Splits { @@ -678,9 +698,9 @@ func (r *reportResource) Create(ctx context.Context, req resource.CreateRequest, } config.Splits = splits } - log.Println("61") + log.Println("plan.Config.Splits:") log.Println(plan.Config.Splits) - log.Println("6") + log.Println("plan.Config.TimeRange:") log.Println(plan.Config.TimeRange) config.TimeInterval = plan.Config.TimeInterval.ValueString() if plan.Config.TimeRange != nil { @@ -692,7 +712,7 @@ func (r *reportResource) Create(ctx context.Context, req resource.CreateRequest, } config.TimeRange = &timeRange } - log.Println("7") + log.Println("plan.Description") log.Println(plan.Description) report := Report{ Config: config, @@ -924,6 +944,7 @@ func (r *reportResource) Update(ctx context.Context, req resource.UpdateRequest, report.Config.DisplayValues = plan.Config.DisplayValues.ValueString() log.Println("plan.Config.Filters") log.Println(plan.Config.Filters) + report.Config.Filters = []ExternalConfigFilter{} for _, filter := range plan.Config.Filters { var values []string for _, value := range filter.Values { diff --git a/test.json b/test.json index ced2f59..cfaa96c 100644 --- a/test.json +++ b/test.json @@ -19,16 +19,7 @@ } ], "displayValues": "actuals_only", - "filters": [ - { - "id": "attribution", - "inverse": false, - "type": "attribution", - "values": [ - "1CE699ZdwN5CRBw0tInY" - ] - } - ], + "filters": null, "group": [ { "id": "BSQZmvX6hvuKGPDHX7R3", @@ -45,8 +36,14 @@ "type": "basic", "value": "cost" }, - "timeInterval": "month" + "timeInterval": "month", + "timeRange": { + "amount": 12, + "includeCurrent": true, + "mode": "last", + "unit": "month" + } }, - "id": "LC1RuXGBMHXUllXpvfio", - "name": "test10" + "description": "-", + "name": "test_new_provider" } \ No newline at end of file