-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #326 from tt810/master
Support Logs Index API
- Loading branch information
Showing
10 changed files
with
621 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package datadog | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"testing" | ||
) | ||
|
||
const indexForImportConfig = ` | ||
resource "datadog_logs_index" "import_index_test" { | ||
name = "main" | ||
filter { | ||
query = "*" | ||
} | ||
exclusion_filter { | ||
is_enabled = true | ||
filter { | ||
query = "source:agent" | ||
sample_rate = 0.2 | ||
} | ||
} | ||
exclusion_filter { | ||
name = "absent is_enabled" | ||
filter { | ||
query = "source:kafka" | ||
sample_rate = 0.0 | ||
} | ||
} | ||
exclusion_filter { | ||
name = "absent query" | ||
is_enabled = false | ||
filter { | ||
sample_rate = 0.2 | ||
} | ||
} | ||
exclusion_filter { | ||
name = "absent sample_rate" | ||
is_enabled = false | ||
filter { | ||
query = "source:kafka" | ||
} | ||
} | ||
} | ||
` | ||
|
||
const indexOrderForImportConfig = ` | ||
resource "datadog_logs_index_order" "import_index_list" { | ||
name = "main list" | ||
indexes = [ | ||
"main" | ||
] | ||
} | ||
` | ||
|
||
func TestAccLogsIndex_importBasic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckIndexDestroyDoNothing, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: indexForImportConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"datadog_logs_index.import_index_test", "name", "main"), | ||
), | ||
}, | ||
{ | ||
ResourceName: "datadog_logs_index.import_index_test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccLogsIndexOrder_importBasic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: indexOrderForImportConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"datadog_logs_index_order.import_index_list", "name", "main list"), | ||
), | ||
}, | ||
{ | ||
ResourceName: "datadog_logs_index_order.import_index_list", | ||
ImportState: true, | ||
}, | ||
}, | ||
}) | ||
} |
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,171 @@ | ||
package datadog | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/zorkian/go-datadog-api" | ||
"strings" | ||
) | ||
|
||
var indexSchema = map[string]*schema.Schema{ | ||
"name": {Type: schema.TypeString, Required: true}, | ||
"filter": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"query": {Type: schema.TypeString, Required: true}, | ||
}, | ||
}, | ||
}, | ||
"exclusion_filter": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Resource{ | ||
Schema: exclusionFilterSchema, | ||
}, | ||
}, | ||
} | ||
|
||
var exclusionFilterSchema = map[string]*schema.Schema{ | ||
"name": {Type: schema.TypeString, Optional: true}, | ||
"is_enabled": {Type: schema.TypeBool, Optional: true}, | ||
"filter": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"query": {Type: schema.TypeString, Optional: true}, | ||
"sample_rate": {Type: schema.TypeFloat, Optional: true}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
func resourceDatadogLogsIndex() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceDatadogLogsIndexCreate, | ||
Update: resourceDatadogLogsIndexUpdate, | ||
Read: resourceDatadogLogsIndexRead, | ||
Delete: resourceDatadogLogsIndexDelete, | ||
Exists: resourceDatadogLogsIndexExists, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Schema: indexSchema, | ||
} | ||
} | ||
|
||
func resourceDatadogLogsIndexCreate(d *schema.ResourceData, meta interface{}) error { | ||
return resourceDatadogLogsIndexUpdate(d, meta) | ||
} | ||
|
||
func resourceDatadogLogsIndexRead(d *schema.ResourceData, meta interface{}) error { | ||
ddIndex, err := meta.(*datadog.Client).GetLogsIndex(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
if err = d.Set("name", ddIndex.GetName()); err != nil { | ||
return err | ||
} | ||
if err = d.Set("filter", buildTerraformFilter(ddIndex.Filter)); err != nil { | ||
return err | ||
} | ||
if err = d.Set("exclusion_filter", buildTerraformExclusionFilters(ddIndex.ExclusionFilters)); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func resourceDatadogLogsIndexUpdate(d *schema.ResourceData, meta interface{}) error { | ||
ddIndex, err := buildDatadogIndex(d) | ||
if err != nil { | ||
return err | ||
} | ||
client := meta.(*datadog.Client) | ||
tfName := d.Get("name").(string) | ||
if _, err := client.UpdateLogsIndex(tfName, ddIndex); err != nil { | ||
if strings.Contains(err.Error(), "404 Not Found") { | ||
return fmt.Errorf("logs index creation is not allowed, index_name: %s", tfName) | ||
} | ||
return fmt.Errorf("error updating logs index: (%s)", err.Error()) | ||
} | ||
d.SetId(tfName) | ||
return resourceDatadogLogsIndexRead(d, meta) | ||
} | ||
|
||
func resourceDatadogLogsIndexDelete(d *schema.ResourceData, meta interface{}) error { | ||
return nil | ||
} | ||
|
||
func resourceDatadogLogsIndexExists(d *schema.ResourceData, meta interface{}) (bool, error) { | ||
client := meta.(*datadog.Client) | ||
if _, err := client.GetLogsIndex(d.Id()); err != nil { | ||
if strings.Contains(err.Error(), "404 Not Found") { | ||
return false, nil | ||
} | ||
return false, err | ||
} | ||
return true, nil | ||
} | ||
|
||
func buildDatadogIndex(d *schema.ResourceData) (*datadog.LogsIndex, error) { | ||
var ddIndex datadog.LogsIndex | ||
if tfFilter := d.Get("filter").([]interface{}); len(tfFilter) > 0 { | ||
ddIndex.SetFilter(buildDatadogFilter(tfFilter[0].(map[string]interface{}))) | ||
} | ||
|
||
ddIndex.ExclusionFilters = buildDatadogExclusionFilters(d.Get("exclusion_filter").([]interface{})) | ||
return &ddIndex, nil | ||
} | ||
|
||
func buildDatadogExclusionFilters(tfEFilters []interface{}) []datadog.ExclusionFilter { | ||
ddEFilters := make([]datadog.ExclusionFilter, len(tfEFilters)) | ||
for i, tfEFilter := range tfEFilters { | ||
ddEFilters[i] = buildDatadogExclusionFilter(tfEFilter.(map[string]interface{})) | ||
} | ||
return ddEFilters | ||
} | ||
|
||
func buildDatadogExclusionFilter(tfEFilter map[string]interface{}) datadog.ExclusionFilter { | ||
ddEFilter := datadog.ExclusionFilter{} | ||
if tfName, exists := tfEFilter["name"].(string); exists { | ||
ddEFilter.SetName(tfName) | ||
} | ||
if tfIsEnabled, exists := tfEFilter["is_enabled"].(bool); exists { | ||
ddEFilter.SetIsEnabled(tfIsEnabled) | ||
} | ||
if tfFs, exists := tfEFilter["filter"].([]interface{}); exists && len(tfFs) > 0 { | ||
tfFilter := tfFs[0].(map[string]interface{}) | ||
ddFilter := datadog.Filter{} | ||
if tfQuery, exist := tfFilter["query"].(string); exist { | ||
ddFilter.SetQuery(tfQuery) | ||
} | ||
if tfSampleRate, exist := tfFilter["sample_rate"].(float64); exist { | ||
ddFilter.SetSampleRate(tfSampleRate) | ||
} | ||
ddEFilter.SetFilter(ddFilter) | ||
} | ||
return ddEFilter | ||
} | ||
|
||
func buildTerraformExclusionFilters(ddEFilters []datadog.ExclusionFilter) []map[string]interface{} { | ||
tfEFilters := make([]map[string]interface{}, len(ddEFilters)) | ||
for i, ddEFilter := range ddEFilters { | ||
tfEFilters[i] = buildTerraformExclusionFilter(ddEFilter) | ||
} | ||
return tfEFilters | ||
} | ||
|
||
func buildTerraformExclusionFilter(ddEFilter datadog.ExclusionFilter) map[string]interface{} { | ||
tfEFilter := make(map[string]interface{}) | ||
ddFilter := ddEFilter.GetFilter() | ||
tfFilter := map[string]interface{}{ | ||
"query": ddFilter.GetQuery(), | ||
"sample_rate": ddFilter.GetSampleRate(), | ||
} | ||
tfEFilter["filter"] = []map[string]interface{}{tfFilter} | ||
tfEFilter["name"] = ddEFilter.GetName() | ||
tfEFilter["is_enabled"] = ddEFilter.GetIsEnabled() | ||
return tfEFilter | ||
} |
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,70 @@ | ||
package datadog | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/zorkian/go-datadog-api" | ||
) | ||
|
||
func resourceDatadogLogsIndexOrder() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceDatadogLogsIndexOrderCreate, | ||
Update: resourceDatadogLogsIndexOrderUpdate, | ||
Read: resourceDatadogLogsIndexOrderRead, | ||
Delete: resourceDatadogLogsIndexOrderDelete, | ||
Exists: resourceDatadogLogsIndexOrderExists, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"name": {Type: schema.TypeString, Required: true}, | ||
"indexes": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceDatadogLogsIndexOrderCreate(d *schema.ResourceData, meta interface{}) error { | ||
return resourceDatadogLogsIndexOrderUpdate(d, meta) | ||
} | ||
|
||
func resourceDatadogLogsIndexOrderUpdate(d *schema.ResourceData, meta interface{}) error { | ||
var ddIndexList datadog.LogsIndexList | ||
tfList := d.Get("indexes").([]interface{}) | ||
ddList := make([]string, len(tfList)) | ||
for i, tfName := range tfList { | ||
ddList[i] = tfName.(string) | ||
} | ||
ddIndexList.IndexNames = ddList | ||
var tfId string | ||
if name, exists := d.GetOk("name"); exists { | ||
tfId = name.(string) | ||
} | ||
if _, err := meta.(*datadog.Client).UpdateLogsIndexList(&ddIndexList); err != nil { | ||
return fmt.Errorf("error updating logs index list: (%s)", err.Error()) | ||
} | ||
d.SetId(tfId) | ||
return resourceDatadogLogsIndexOrderRead(d, meta) | ||
} | ||
|
||
func resourceDatadogLogsIndexOrderRead(d *schema.ResourceData, meta interface{}) error { | ||
ddIndexList, err := meta.(*datadog.Client).GetLogsIndexList() | ||
if err != nil { | ||
return err | ||
} | ||
if err := d.Set("indexes", ddIndexList.IndexNames); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func resourceDatadogLogsIndexOrderDelete(d *schema.ResourceData, meta interface{}) error { | ||
return nil | ||
} | ||
|
||
func resourceDatadogLogsIndexOrderExists(d *schema.ResourceData, meta interface{}) (bool, error) { | ||
return true, nil | ||
} |
Oops, something went wrong.