-
Notifications
You must be signed in to change notification settings - Fork 676
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
1,337 additions
and
6 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,215 @@ | ||
package ibm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func dataSourceIBMCISRateLimit() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceIBMCISRateLimitRead, | ||
Schema: map[string]*schema.Schema{ | ||
"cis_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"domain_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"rate_limit": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"disabled": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"bypass": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"value": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"threshold": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"period": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"correlate": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"by": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"action": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"mode": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"timeout": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"response": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"content_type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"body": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"match": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"request": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"methods": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"schemes": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"url": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"response": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"status": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeInt}, | ||
}, | ||
"origin_traffic": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"headers": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"op": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"value": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"rule_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceIBMCISRateLimitRead(d *schema.ResourceData, meta interface{}) error { | ||
cisClient, err := meta.(ClientSession).CisAPI() | ||
if err != nil { | ||
return err | ||
} | ||
cisID := d.Get("cis_id").(string) | ||
zoneID, _, err := convertTftoCisTwoVar(d.Get("domain_id").(string)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rateLimitRecord, err := cisClient.RateLimit().ListRateLimit(cisID, zoneID) | ||
if err != nil { | ||
return fmt.Errorf("Failed to read RateLimit: %v", err) | ||
} | ||
rules := make([]map[string]interface{}, 0, len(rateLimitRecord)) | ||
for _, r := range rateLimitRecord { | ||
rule := make(map[string]interface{}) | ||
rule["rule_id"] = r.ID | ||
rule["disabled"] = r.Disabled | ||
rule["description"] = r.Description | ||
rule["threshold"] = r.Threshold | ||
rule["period"] = r.Period | ||
rule["action"] = flattenRateLimitAction(r.Action) | ||
rule["match"] = flattenRateLimitMatch(r.Match) | ||
rule["correlate"] = flattenRateLimitCorrelate(*r.Correlate) | ||
rule["bypass"] = flattenRateLimitByPass(r.Bypass) | ||
rules = append(rules, rule) | ||
|
||
} | ||
d.SetId(cisID) | ||
d.Set("rate_limit", rules) | ||
d.Set("cis_id", cisID) | ||
d.Set("domain_id", convertCisToTfTwoVar(zoneID, cisID)) | ||
return nil | ||
} |
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,43 @@ | ||
package ibm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccIBMCisRateLimitDataSource_Basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccCheckIBMCisRateLimitDataSourceConfig_basic1(), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.ibm_cis_rate_limit.ratelimit", "cis_id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckIBMCisRateLimitDataSourceConfig_basic1() string { | ||
return fmt.Sprintf(` | ||
data "ibm_resource_group" "test_acc" { | ||
name = "Default" | ||
} | ||
data "ibm_cis" "cis" { | ||
resource_group_id = data.ibm_resource_group.test_acc.id | ||
name = "CISTest" | ||
} | ||
data "ibm_cis_domain" "cis_domain" { | ||
cis_id = data.ibm_cis.cis.id | ||
domain = "cis-terraform.com" | ||
} | ||
data "ibm_cis_rate_limit" "ratelimit" { | ||
cis_id = data.ibm_cis.cis.id | ||
domain_id = data.ibm_cis_domain.cis_domain.id | ||
} | ||
`) | ||
} |
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.