|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + "terraform-provider-doit/internal/provider/models" |
| 11 | + "terraform-provider-doit/internal/provider/resource_allocation" |
| 12 | + "terraform-provider-doit/internal/provider/resource_allocation_group" |
| 13 | + |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 17 | +) |
| 18 | + |
| 19 | +func (plan *allocationGroupResourceModel) toRequest(ctx context.Context) (groupAllocationRequest models.GroupAllocationRequest, d diag.Diagnostics) { |
| 20 | + var diags diag.Diagnostics |
| 21 | + groupAllocationRequest.UnallocatedCosts = plan.UnallocatedCosts.ValueStringPointer() |
| 22 | + groupAllocationRequest.Description = plan.Description.ValueStringPointer() |
| 23 | + groupAllocationRequest.Name = plan.Name.ValueString() |
| 24 | + if !plan.Rules.IsNull() { |
| 25 | + planRules := []resource_allocation_group.RulesValue{} |
| 26 | + diags = plan.Rules.ElementsAs(ctx, &planRules, false) |
| 27 | + d.Append(diags...) |
| 28 | + if d.HasError() { |
| 29 | + return |
| 30 | + } |
| 31 | + groupAllocationRequest.Rules = make([]models.GroupAllocationRule, len(planRules)) |
| 32 | + for i := range planRules { |
| 33 | + fmt.Println("Action is nil", planRules[i].Action.IsNull(), planRules[i].Action.ValueString()) |
| 34 | + groupAllocationRequest.Rules[i] = models.GroupAllocationRule{ |
| 35 | + Name: planRules[i].Name.ValueStringPointer(), |
| 36 | + Id: planRules[i].Id.ValueStringPointer(), |
| 37 | + Action: models.GroupAllocationRuleAction(planRules[i].Action.ValueString()), |
| 38 | + Description: planRules[i].Description.ValueStringPointer(), |
| 39 | + Formula: planRules[i].Formula.ValueStringPointer(), |
| 40 | + } |
| 41 | + if !planRules[i].Components.IsNull() { |
| 42 | + ruleComponents := []resource_allocation_group.ComponentsValue{} |
| 43 | + diags = planRules[i].Components.ElementsAs(ctx, &ruleComponents, true) |
| 44 | + d.Append(diags...) |
| 45 | + if d.HasError() { |
| 46 | + return |
| 47 | + } |
| 48 | + createComponents := make([]models.AllocationComponent, len(ruleComponents)) |
| 49 | + for j := range ruleComponents { |
| 50 | + createComponents[j] = models.AllocationComponent{ |
| 51 | + IncludeNull: ruleComponents[j].IncludeNull.ValueBoolPointer(), |
| 52 | + InverseSelection: ruleComponents[j].InverseSelection.ValueBoolPointer(), |
| 53 | + Key: ruleComponents[j].Key.ValueString(), |
| 54 | + Mode: models.AllocationComponentMode(ruleComponents[j].Mode.ValueString()), |
| 55 | + Type: models.DimensionsTypes(ruleComponents[j].ComponentsType.ValueString()), |
| 56 | + } |
| 57 | + values := make([]attr.Value, len(ruleComponents[j].Values.Elements())) |
| 58 | + for k := range ruleComponents[j].Values.Elements() { |
| 59 | + values[k] = ruleComponents[j].Values.Elements()[k] |
| 60 | + createComponents[j].Values[k] = values[k].String() |
| 61 | + } |
| 62 | + } |
| 63 | + groupAllocationRequest.Rules[i].Components = &createComponents |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + return groupAllocationRequest, d |
| 68 | +} |
| 69 | + |
| 70 | +func (state *allocationGroupResourceModel) populate(groupAllocation *models.GroupAllocation, client *ClientTest, ctx context.Context) (d diag.Diagnostics) { |
| 71 | + var diags diag.Diagnostics |
| 72 | + state.Description = types.StringPointerValue(groupAllocation.Description) |
| 73 | + state.Id = types.StringPointerValue(groupAllocation.Id) |
| 74 | + state.AllocationType = types.StringValue("group") |
| 75 | + state.Type = types.StringValue("managed") |
| 76 | + state.Name = types.StringPointerValue(groupAllocation.Name) |
| 77 | + state.Cloud = types.StringNull() |
| 78 | + state.UnallocatedCosts = types.StringPointerValue(groupAllocation.UnallocatedCosts) |
| 79 | + state.TimeCreated = types.Int64PointerValue(groupAllocation.TimeCreated) |
| 80 | + state.TimeModified = types.Int64PointerValue(groupAllocation.TimeModified) |
| 81 | + // Overwrite components with refreshed state |
| 82 | + if groupAllocation.Rules != nil { |
| 83 | + stateRules := make([]attr.Value, len(*groupAllocation.Rules)) |
| 84 | + for i, rule := range *groupAllocation.Rules { |
| 85 | + stateRule := resource_allocation_group.RulesValue{ |
| 86 | + CreateTime: types.Int64PointerValue(rule.CreateTime), |
| 87 | + Id: types.StringPointerValue(rule.Id), |
| 88 | + Name: types.StringPointerValue(rule.Name), |
| 89 | + Owner: types.StringPointerValue(rule.Owner), |
| 90 | + RulesType: types.StringPointerValue(rule.Type), |
| 91 | + UpdateTime: types.Int64PointerValue(rule.UpdateTime), |
| 92 | + } |
| 93 | + var singleAllocation *models.SingleAllocation |
| 94 | + singleAllocation, err := client.GetAllocation(stateRule.Id.ValueString()) |
| 95 | + if err != nil { |
| 96 | + diags.AddError( |
| 97 | + "Error Reading Doit Console Allocation", |
| 98 | + "Could not read Doit Console Allocation ID "+state.Id.ValueString()+": "+err.Error(), |
| 99 | + ) |
| 100 | + return |
| 101 | + } |
| 102 | + singleAllocationState := allocationResourceModel{} |
| 103 | + diags = singleAllocationState.populate(singleAllocation, ctx) |
| 104 | + d.Append(diags...) |
| 105 | + if d.HasError() { |
| 106 | + return |
| 107 | + } |
| 108 | + stateRule.Formula, diags = singleAllocationState.Rule.Formula.ToStringValue(ctx) |
| 109 | + d.Append(diags...) |
| 110 | + if d.HasError() { |
| 111 | + return |
| 112 | + } |
| 113 | + singleComponents := []resource_allocation.ComponentsValue{} |
| 114 | + diags = singleAllocationState.Rule.Components.ElementsAs(ctx, &singleComponents, false) |
| 115 | + d.Append(diags...) |
| 116 | + if d.HasError() { |
| 117 | + return |
| 118 | + } |
| 119 | + groupComponents := make([]attr.Value, len(singleComponents)) |
| 120 | + for i := range singleComponents { |
| 121 | + groupComponents[i], diags = resource_allocation_group.NewComponentsValue(singleComponents[i].AttributeTypes(ctx), map[string]attr.Value{ |
| 122 | + "include_null": singleComponents[i].IncludeNull, |
| 123 | + "inverse_selection": singleComponents[i].InverseSelection, |
| 124 | + "key": singleComponents[i].Key, |
| 125 | + "mode": singleComponents[i].Mode, |
| 126 | + "type": singleComponents[i].ComponentsType, |
| 127 | + "values": singleComponents[i].Values, |
| 128 | + }) |
| 129 | + d.Append(diags...) |
| 130 | + if d.HasError() { |
| 131 | + return |
| 132 | + } |
| 133 | + } |
| 134 | + stateRule.Components, diags = types.ListValue(groupComponents[0].Type(ctx), groupComponents) |
| 135 | + d.Append(diags...) |
| 136 | + if d.HasError() { |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + stateRules[i], diags = resource_allocation_group.NewRulesValue(stateRule.AttributeTypes(ctx), map[string]attr.Value{ |
| 141 | + "action": types.StringValue("select"), |
| 142 | + "allocation_type": stateRule.AllocationType, |
| 143 | + "components": stateRule.Components, |
| 144 | + "create_time": stateRule.CreateTime, |
| 145 | + "description": stateRule.Description, |
| 146 | + "formula": stateRule.Formula, |
| 147 | + "id": stateRule.Id, |
| 148 | + "name": stateRule.Name, |
| 149 | + "owner": stateRule.Owner, |
| 150 | + "type": stateRule.RulesType, |
| 151 | + "update_time": stateRule.UpdateTime, |
| 152 | + "url_ui": stateRule.UrlUi, |
| 153 | + }) |
| 154 | + d.Append(diags...) |
| 155 | + if d.HasError() { |
| 156 | + return |
| 157 | + } |
| 158 | + } |
| 159 | + state.Rules, diags = types.ListValue(stateRules[0].Type(ctx), stateRules) |
| 160 | + d.Append(diags...) |
| 161 | + if d.HasError() { |
| 162 | + return |
| 163 | + } |
| 164 | + } |
| 165 | + return diags |
| 166 | +} |
| 167 | + |
| 168 | +// CreateAllocationGroup - Create new allocation |
| 169 | +func (c *ClientTest) CreateAllocationGroup(allocation models.GroupAllocationRequest) (*models.GroupAllocation, error) { |
| 170 | + rb, err := json.Marshal(allocation) |
| 171 | + if err != nil { |
| 172 | + return nil, err |
| 173 | + } |
| 174 | + urlRequestBase := fmt.Sprintf("%s/analytics/v1/allocations", c.HostURL) |
| 175 | + urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase) |
| 176 | + req, err := http.NewRequest("POST", urlRequestContext, strings.NewReader(string(rb))) |
| 177 | + log.Println("URL----------------") |
| 178 | + log.Println(req.URL) |
| 179 | + if err != nil { |
| 180 | + return nil, err |
| 181 | + } |
| 182 | + |
| 183 | + body, err := c.doRequest(req) |
| 184 | + if err != nil { |
| 185 | + log.Println("ERROR REQUEST----------------") |
| 186 | + log.Println(err) |
| 187 | + log.Println(string(rb)) |
| 188 | + return nil, err |
| 189 | + } |
| 190 | + |
| 191 | + allocationResponse := models.GroupAllocation{} |
| 192 | + err = json.Unmarshal(body, &allocationResponse) |
| 193 | + if err != nil { |
| 194 | + log.Println("ERROR UNMARSHALL----------------") |
| 195 | + log.Println(err) |
| 196 | + return nil, err |
| 197 | + } |
| 198 | + log.Println("AllocationGroup response----------------") |
| 199 | + log.Println(allocationResponse) |
| 200 | + return &allocationResponse, nil |
| 201 | +} |
| 202 | + |
| 203 | +// UpdateAllocationGroup - Updates an allocation |
| 204 | +func (c *ClientTest) UpdateAllocationGroup(allocationID string, allocation models.GroupAllocationRequest) (*models.GroupAllocation, error) { |
| 205 | + rb, err := json.Marshal(allocation) |
| 206 | + if err != nil { |
| 207 | + return nil, err |
| 208 | + } |
| 209 | + urlRequestBase := fmt.Sprintf("%s/analytics/v1/allocations/%s", c.HostURL, allocationID) |
| 210 | + urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase) |
| 211 | + req, err := http.NewRequest("PATCH", urlRequestContext, strings.NewReader(string(rb))) |
| 212 | + if err != nil { |
| 213 | + return nil, err |
| 214 | + } |
| 215 | + log.Println("Update URL----------------") |
| 216 | + log.Println(req.URL) |
| 217 | + body, err := c.doRequest(req) |
| 218 | + if err != nil { |
| 219 | + return nil, err |
| 220 | + } |
| 221 | + |
| 222 | + allocationResponse := models.GroupAllocation{} |
| 223 | + err = json.Unmarshal(body, &allocationResponse) |
| 224 | + if err != nil { |
| 225 | + return nil, err |
| 226 | + } |
| 227 | + log.Println("AllocationGroup response----------------") |
| 228 | + log.Println(allocationResponse) |
| 229 | + return &allocationResponse, nil |
| 230 | +} |
| 231 | + |
| 232 | +func (c *ClientTest) DeleteAllocationGroup(allocationID string) error { |
| 233 | + urlRequestBase := fmt.Sprintf("%s/analytics/v1/allocations/%s", c.HostURL, allocationID) |
| 234 | + urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase) |
| 235 | + req, err := http.NewRequest("DELETE", urlRequestContext, nil) |
| 236 | + if err != nil { |
| 237 | + return err |
| 238 | + } |
| 239 | + |
| 240 | + _, err = c.doRequest(req) |
| 241 | + if err != nil { |
| 242 | + return err |
| 243 | + } |
| 244 | + |
| 245 | + return nil |
| 246 | +} |
| 247 | + |
| 248 | +// GetAllocationGroup - Returns a specifc allocation |
| 249 | +func (c *ClientTest) GetAllocationGroup(orderID string) (*models.GroupAllocation, error) { |
| 250 | + urlRequestBase := fmt.Sprintf("%s/analytics/v1/allocations/%s", c.HostURL, orderID) |
| 251 | + urlRequestContext := addContextToURL(c.Auth.CustomerContext, urlRequestBase) |
| 252 | + req, err := http.NewRequest("GET", urlRequestContext, nil) |
| 253 | + if err != nil { |
| 254 | + return nil, err |
| 255 | + } |
| 256 | + |
| 257 | + body, err := c.doRequest(req) |
| 258 | + if err != nil { |
| 259 | + return nil, err |
| 260 | + } |
| 261 | + |
| 262 | + allocation := models.GroupAllocation{} |
| 263 | + err = json.Unmarshal(body, &allocation) |
| 264 | + if err != nil { |
| 265 | + return nil, err |
| 266 | + } |
| 267 | + |
| 268 | + return &allocation, nil |
| 269 | +} |
0 commit comments