-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(datasource/sites): support sites list (#160)
* fix merge conflict * feat(data-source/sites): allow filtering of sites by name/display_name * build(make): support local testing of provider * docs: remove FIXME as it is expected, after doing some local testing * fix(lint): remove genID as it's no longer used * omit bin directory
- Loading branch information
1 parent
2f7a101
commit 2e4f25c
Showing
8 changed files
with
313 additions
and
12 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 |
---|---|---|
|
@@ -50,3 +50,4 @@ override.tf.json | |
terraform-provider-sigsci | ||
|
||
dist/ | ||
bin/ |
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,213 @@ | ||
package provider | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/signalsciences/go-sigsci" | ||
) | ||
|
||
func dataSourceSites() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: readSites, | ||
Schema: map[string]*schema.Schema{ | ||
"filter": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "Filter listed domains by either the site 'name' or 'display_name'", | ||
}, | ||
"sites": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "A list of all sites for a given corp.", | ||
Elem: &schema.Resource{ | ||
// NOTE: The API returns multiple objects with a single 'uri' field. | ||
// To avoid extra type complexity we flatten those hierarchies. | ||
// These are the fields below that have the '_uri' suffix. | ||
Schema: map[string]*schema.Schema{ | ||
"agent_anon_mode": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Agent IP anonimization mode - 'EU' or 'off'", | ||
}, | ||
"agent_level": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Agent action level - 'block', 'log' or 'off'", | ||
}, | ||
"agents_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Reference to the site's agents", | ||
}, | ||
"alerts_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Reference to the site's alerts", | ||
}, | ||
"analytics_events_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Reference to the site's analytics events", | ||
}, | ||
"blacklist_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Reference to the site's blacklist", | ||
}, | ||
"block_duration_secs": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
Description: "Duration to block an IP in seconds", | ||
}, | ||
"block_http_code": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
Description: "HTTP response code to send when when traffic is being blocked", | ||
}, | ||
"block_redirect_url": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "URL to redirect to when blockHTTPCode is 301 or 302", | ||
}, | ||
"created": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Created RFC3339 date time", | ||
}, | ||
"display_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Display name of the site", | ||
}, | ||
"events_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Reference to the site's events", | ||
}, | ||
"header_links_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Reference to the site's header links", | ||
}, | ||
"integrations_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Reference to the site's integrations", | ||
}, | ||
"members_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Reference to the site's members", | ||
}, | ||
"monitors_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Reference to the site's monitors", | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Identifying name of the site", | ||
}, | ||
"redactions_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Reference to the site's redactions", | ||
}, | ||
"requests_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Reference to the site's requests", | ||
}, | ||
"suspicious_ips_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Reference to the site's suspicious IPs", | ||
}, | ||
"top_attacks_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Reference to the site's top attacks", | ||
}, | ||
"whitelist_uri": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Reference to the site's whitelist", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func readSites(d *schema.ResourceData, m any) error { | ||
pm := m.(providerMetadata) | ||
sc := pm.Client | ||
corp := pm.Corp | ||
|
||
// API documentation: | ||
// https://docs.fastly.com/signalsciences/api/#_corps__corpName__sites_get | ||
sites, err := sc.ListSites(corp) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId("list_sites") | ||
|
||
filter := d.Get("filter").(string) | ||
|
||
return d.Set("sites", flattenSites(sites, filter)) | ||
} | ||
|
||
// flattenSites models data into format suitable for saving to Terraform state. | ||
func flattenSites(data []sigsci.Site, filter string) []map[string]any { | ||
results := []map[string]any{} | ||
if len(data) == 0 { | ||
return results | ||
} | ||
|
||
for _, site := range data { | ||
data := map[string]any{ | ||
"agent_anon_mode": site.AgentAnonMode, | ||
"agent_level": site.AgentLevel, | ||
"agents_uri": site.Agents["uri"], | ||
"alerts_uri": site.Alerts["uri"], | ||
"analytics_events_uri": site.AnalyticsEvents["uri"], | ||
"blacklist_uri": site.Blacklist["uri"], | ||
"block_duration_secs": site.BlockDurationSeconds, | ||
"block_http_code": site.BlockHTTPCode, | ||
"block_redirect_url": site.BlockRedirectURL, | ||
"created": site.Created.String(), | ||
"display_name": site.DisplayName, | ||
"events_uri": site.Events["uri"], | ||
"header_links_uri": site.HeaderLinks["uri"], | ||
"integrations_uri": site.Integrations["uri"], | ||
"members_uri": site.Members["uri"], | ||
"monitors_uri": site.Monitors["uri"], | ||
"name": site.Name, | ||
"redactions_uri": site.Redactions["uri"], | ||
"requests_uri": site.Requests["uri"], | ||
"suspicious_ips_uri": site.SuspiciousIPs["uri"], | ||
"top_attacks_uri": site.TopAttacks["uri"], | ||
"whitelist_uri": site.Whitelist["uri"], | ||
} | ||
|
||
// Prune any empty values that come from the default string value in structs. | ||
for k, v := range data { | ||
if v == "" { | ||
delete(data, k) | ||
} | ||
} | ||
results = append(results, data) | ||
} | ||
|
||
if filter != "" { | ||
for idx, site := range results { | ||
if site["name"] == filter || site["display_name"] == filter { | ||
return results[idx : idx+1] | ||
} | ||
} | ||
} | ||
|
||
return results | ||
} |
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,63 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
func TestAccSigSciDataSourceSites(t *testing.T) { | ||
t.Parallel() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
}, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: ` | ||
resource "sigsci_site" "example1" { | ||
short_name = "terraform_site1" | ||
display_name = "terraform test example site1" | ||
} | ||
resource "sigsci_site" "example2" { | ||
short_name = "terraform_site2" | ||
display_name = "terraform test example site2" | ||
} | ||
data "sigsci_sites" "example" { | ||
depends_on = [sigsci_site.example1, sigsci_site.example2] | ||
filter = "terraform_site2" | ||
} | ||
`, | ||
Check: resource.ComposeTestCheckFunc( | ||
func(s *terraform.State) error { | ||
r := s.RootModule().Resources["data.sigsci_sites.example"] | ||
a := r.Primary.Attributes | ||
|
||
sites, err := strconv.Atoi(a["sites.#"]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if sites != 1 { | ||
return fmt.Errorf("expected one site to be returned as per the filter") | ||
} | ||
|
||
got := a["sites.0.name"] | ||
want := "terraform_site2" | ||
if got != want { | ||
return fmt.Errorf("got: %s, want: %s", got, want) | ||
} | ||
|
||
return nil | ||
}, | ||
), | ||
ExpectNonEmptyPlan: 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
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,22 @@ | ||
#!/bin/bash | ||
|
||
BIN_DIR=$PWD/bin | ||
OVERRIDES_FILENAME=developer_overrides.tfrc | ||
|
||
cat <<EOF >$BIN_DIR/$OVERRIDES_FILENAME | ||
provider_installation { | ||
dev_overrides { | ||
"signalsciences/sigsci" = "$BIN_DIR" | ||
} | ||
direct {} | ||
} | ||
EOF | ||
|
||
echo "" | ||
echo "A development overrides file has been generated at ./bin/$OVERRIDES_FILENAME." | ||
echo "To make Terraform temporarily use your locally built version of the provider, set TF_CLI_CONFIG_FILE within your terminal" | ||
echo "" | ||
printf "\texport TF_CLI_CONFIG_FILE=$BIN_DIR/$OVERRIDES_FILENAME" | ||
echo "" |