-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
375 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package cloudability | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/skyscrapr/cloudability-sdk-go/cloudability" | ||
) | ||
|
||
func resourceBusinessMetric() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceBusinessMetricCreate, | ||
Read: resourceBusinessMetricRead, | ||
// Update: resourceBusinessMetricUpdate, | ||
Delete: resourceBusinessMetricDelete, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"index": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"number_format": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: func(val any, key string) (warns []string, errs []error) { | ||
switch val.(string) { | ||
case | ||
"currency", | ||
"number": | ||
return | ||
} | ||
errs = append(errs, fmt.Errorf("invalid value for number_format. Must ne 'currency' or 'number'")) | ||
return | ||
}, | ||
Default: "number", | ||
ForceNew: true, | ||
}, | ||
"default_value": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ConflictsWith: []string{"default_value_expression"}, | ||
}, | ||
"default_value_expression": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ConflictsWith: []string{"default_value"}, | ||
}, | ||
"pre_match_expression": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "", | ||
ForceNew: true, | ||
}, | ||
"statement": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
ForceNew: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"match_expression": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"value_expression": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"updated_at": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceBusinessMetricCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudability.Client) | ||
payload := cloudability.BusinessMapping{ | ||
Kind: "BUSINESS_METRIC", | ||
Name: d.Get("name").(string), | ||
NumberFormat: d.Get("number_format").(string), | ||
PreMatchExpression: d.Get("pre_match_expression").(string), | ||
DefaultValueExpression: d.Get("default_value_expression").(string), | ||
DefaultValue: d.Get("default_value").(string), | ||
Statements: inflateStatements(d.Get("statement").([]interface{})), | ||
} | ||
businessMapping, err := client.BusinessMappings().NewBusinessMetric(&payload) | ||
if err != nil { | ||
return err | ||
} | ||
ctx := context.TODO() | ||
tflog.Info(ctx, fmt.Sprintf("New business metric created with index: %d", businessMapping.Index)) | ||
d.SetId(strconv.Itoa(businessMapping.Index)) | ||
return resourceBusinessMetricRead(d, meta) | ||
} | ||
|
||
func resourceBusinessMetricRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudability.Client) | ||
index, err := strconv.Atoi(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
ctx := context.TODO() | ||
tflog.Info(ctx, fmt.Sprintf("Reading business metric with index: %d", index)) | ||
businessMapping, err := client.BusinessMappings().GetBusinessMetric(index) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if businessMapping != nil { | ||
d.Set("index", businessMapping.Index) | ||
d.Set("name", businessMapping.Name) | ||
d.Set("number_format", businessMapping.NumberFormat) | ||
d.Set("default_value", businessMapping.DefaultValue) | ||
// d.Set("default_value_expression", businessMapping.DefaultValueExpression) | ||
d.Set("pre_match_expression", businessMapping.PreMatchExpression) | ||
d.Set("statement", flattenStatements(businessMapping.Statements)) | ||
d.Set("updated_at", businessMapping.UpdatedAt) | ||
d.SetId(strconv.Itoa(businessMapping.Index)) | ||
} | ||
return nil | ||
} | ||
|
||
// func resourceBusinessMetricUpdate(d *schema.ResourceData, meta interface{}) error { | ||
// client := meta.(*cloudability.Client) | ||
// id, err := strconv.Atoi(d.Id()) | ||
// if err != nil { | ||
// return nil | ||
// } | ||
// payload := cloudability.BusinessMapping{ | ||
// Kind: "BUSINESS_METRIC", | ||
// Index: d.Get("index").(int), | ||
// Name: d.Get("name").(string), | ||
// NumberFormat: d.Get("number_format").(string), | ||
// PreMatchExpression: d.Get("pre_match_expression").(string), | ||
// DefaultValueExpression: d.Get("default_value_expression").(string), | ||
// DefaultValue: d.Get("default_value").(string), | ||
// Statements: inflateStatements(d.Get("statement").([]interface{})), | ||
// } | ||
// err = client.BusinessMappings().UpdateBusinessMetric(&payload) | ||
// if err != nil { | ||
// return err | ||
// } | ||
// ctx := context.TODO() | ||
// tflog.Info(ctx, fmt.Sprintf("Updating business metric with index: %d", id)) | ||
// return resourceBusinessMetricRead(d, meta) | ||
// } | ||
|
||
func resourceBusinessMetricDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudability.Client) | ||
id, err := strconv.Atoi(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
ctx := context.TODO() | ||
tflog.Info(ctx, fmt.Sprintf("Deleting business metric with index: %d", id)) | ||
err = client.BusinessMappings().DeleteBusinessMetric(id) | ||
if err != nil { | ||
// Ignore 404 errors (No resource found) | ||
var apiError cloudability.APIError | ||
jsonErr := json.Unmarshal([]byte(err.Error()), &apiError) | ||
if jsonErr == nil && apiError.Error.Status == 404 { | ||
ctx := context.TODO() | ||
tflog.Info(ctx, "resourceBusinessMetricDelete Resource not found. Ignoring") | ||
return nil | ||
} | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package cloudability | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func TestAccBusinessMetricResource(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
// Create and Read testing | ||
{ | ||
Config: testAccBusinessMetricResourceConfig("A"), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("cloudability_business_metric.test", "id"), | ||
resource.TestCheckResourceAttr("cloudability_business_metric.test", "name", "Cost (Surcharge) A"), | ||
// resource.TestCheckResourceAttr("cloudability_business_metric.test", "default_value", "METRIC['unblended_cost']"), | ||
// resource.TestCheckResourceAttr("cloudability_business_metric.test", "default_value_expression", "METRIC['unblended_cost']"), | ||
), | ||
}, | ||
// ImportState testing | ||
{ | ||
ResourceName: "cloudability_business_metric.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
// This is not normally necessary, but is here because this | ||
// example code does not have an actual upstream service. | ||
// Once the Read method is able to refresh information from | ||
// the upstream service, this can be removed. | ||
ImportStateVerifyIgnore: []string{"default_value_expression", "default_value"}, | ||
}, | ||
// Update and Read testing | ||
{ | ||
Config: testAccBusinessMetricResourceConfig("B"), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("cloudability_business_metric.test", "name", "Cost (Surcharge) B"), | ||
), | ||
}, | ||
// Delete testing automatically occurs in TestCase | ||
}, | ||
}) | ||
} | ||
|
||
func testAccBusinessMetricResourceConfig(name string) string { | ||
return fmt.Sprintf(` | ||
resource "cloudability_business_metric" "test" { | ||
name = "Cost (Surcharge) %s" | ||
number_format = "number" | ||
default_value_expression = "METRIC['unblended_cost']" | ||
statement { | ||
match_expression = "DIMENSION['vendor'] == 'Amazon' || DIMENSION['vendor'] == 'Azure'" | ||
value_expression = "METRIC['unblended_cost'] * 1.15" | ||
} | ||
lifecycle { | ||
ignore_changes = [ | ||
default_value_expression, | ||
default_value | ||
] | ||
} | ||
// name = "Cost (Storage Backup)" | ||
// // "kind": "BUSINESS_METRIC" | ||
// number_format = "currency" | ||
// pre_match_expression = "(DIMENSION['vendor'] == 'amazon' && DIMENSION['usage_family'] == 'storage'" | ||
// default_value_expression = "METRIC['unblended_cost']" | ||
// statement { | ||
// match_expression = "DIMENSION['invoice_date'] == '2019-09-01' && DIMENSION['transaction_type'] == 'usage' && DIMENSION['usage_type'] CONTAINS 'snapshot'" | ||
// value_expression = "METRIC['unblended_cost'] * 1.10" | ||
// } | ||
// statement { | ||
// match_expression = "DIMENSION['invoice_date'] == '2019-10-01' && DIMENSION['transaction_type'] == 'usage' && DIMENSION['usage_type'] CONTAINS 'snapshot'" | ||
// value_expression = "METRIC['unblended_cost'] * 1.10" | ||
// } | ||
// statement { | ||
// match_expression = "DIMENSION['invoice_date'] == '2019-11-01' && DIMENSION['transaction_type'] == 'usage' && DIMENSION['usage_type'] CONTAINS 'snapshot'" | ||
// value_expression = "METRIC['unblended_cost'] * 1.10" | ||
// } | ||
} | ||
`, name) | ||
} | ||
|
||
// func testAccBusinessMetricMultipleConfig() string { | ||
// return ` | ||
// resource "cloudability_business_mapping" "test1" { | ||
// name = "test1" | ||
// default_value = "Unknown1" | ||
// kind = "BUSINESS_DIMENSION" | ||
// statement { | ||
// match_expression = "DIMENSION['vendor'] == 'vendor1_1'" | ||
// value_expression = "'Vendor1_1'" | ||
// } | ||
// statement { | ||
// match_expression = "DIMENSION['vendor'] == 'vendor1_2'" | ||
// value_expression = "'Vendor1_2'" | ||
// } | ||
// } | ||
|
||
// resource "cloudability_business_mapping" "test2" { | ||
// name = "test2" | ||
// default_value = "Unknown2" | ||
// kind = "BUSINESS_DIMENSION" | ||
// statement { | ||
// match_expression = "DIMENSION['vendor'] == 'vendor2_1'" | ||
// value_expression = "'Vendor2_1'" | ||
// } | ||
// statement { | ||
// match_expression = "DIMENSION['vendor'] == 'vendor2_2'" | ||
// value_expression = "'Vendor2_2'" | ||
// } | ||
// depends_on = [cloudability_business_mapping.test1] | ||
// } | ||
|
||
// resource "cloudability_business_mapping" "test3" { | ||
// name = "test3" | ||
// default_value = "Unknown3" | ||
// kind = "BUSINESS_DIMENSION" | ||
// statement { | ||
// match_expression = "DIMENSION['vendor'] == 'vendor3_1'" | ||
// value_expression = "'Vendor3_1'" | ||
// } | ||
// statement { | ||
// match_expression = "DIMENSION['vendor'] == 'vendor3_2'" | ||
// value_expression = "'Vendor3_2'" | ||
// } | ||
// depends_on = [cloudability_business_mapping.test2] | ||
// } | ||
// ` | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.