Skip to content

Commit

Permalink
Fix: Fixed drift within network_service_groups (#263)
Browse files Browse the repository at this point in the history
* fix: Added geo coordinates validation
* Added acceptance test for zia_activation
* Added additional acceptance tests
  • Loading branch information
willguibr authored Aug 29, 2023
1 parent e543a49 commit d7645fe
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 124 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# Changelog

## 2.6.1 (August, xx 2023)
## 2.6.1 (August, 29 2023)

### Notes

- Release date: **(August, xx 2023)**
- Release date: **(August, 29 2023)**
- Supported Terraform version: **v1.x**

### Enhancements

- [PR #258](https://github.com/zscaler/terraform-provider-zia/pull/258) Improved geographical coordinates for attributes `latitude` and `longitude` in the resource `zia_traffic_forwarding_static_ip` to ensures that the state always mirrors the backend system's values.

### Fixes

- [PR #259](https://github.com/zscaler/terraform-provider-zia/pull/259) Fixed drift problem within the resource `zia_firewall_filtering_network_service_groups`.
- [PR #266](https://github.com/zscaler/terraform-provider-zia/pull/266) Fixed drift problem within the resource `zia_url_filtering_rules` order attribute

## 2.6.0 (August, 1 2023)

Expand Down
9 changes: 7 additions & 2 deletions docs/guides/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ Track all ZIA Terraform provider's releases. New resources, features, and bug fi

---

## 2.6.1 (August, xx 2023)
## 2.6.1 (August, 29 2023)

### Notes

- Release date: **(August, xx 2023)**
- Release date: **(August, 29 2023)**
- Supported Terraform version: **v1.x**

### Enhancements

- [PR #258](https://github.com/zscaler/terraform-provider-zia/pull/258) Improved geographical coordinates for attributes `latitude` and `longitude` in the resource `zia_traffic_forwarding_static_ip` to ensures that the state always mirrors the backend system's values.

### Fixes

- [PR #259](https://github.com/zscaler/terraform-provider-zia/pull/259) Fixed drift problem within the resource `zia_firewall_filtering_network_service_groups`.
- [PR #266](https://github.com/zscaler/terraform-provider-zia/pull/266) Fixed drift problem within the resource `zia_url_filtering_rules` order attribute

## 2.6.0 (August, 1 2023)

Expand Down
42 changes: 21 additions & 21 deletions zia/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,34 @@ func TestProvider_impl(t *testing.T) {
}

func testAccPreCheck(t *testing.T) {
err := accPreCheck()
if err != nil {
if err := accPreCheck(); err != nil {
t.Fatalf("%v", err)
}
if v := os.Getenv("ZIA_USERNAME"); v == "" {
t.Fatal("ZIA_USERNAME must be set for acceptance tests.")
}
if v := os.Getenv("ZIA_PASSWORD"); v == "" {
t.Fatal("ZIA_PASSWORD must be set for acceptance tests.")
}
if v := os.Getenv("ZIA_API_KEY"); v == "" {
t.Fatal("ZIA_API_KEY must be set for acceptance tests.")
}
if v := os.Getenv("ZIA_CLOUD"); v == "" {
t.Fatal("zia_cloud must be set for acceptance tests.")
}
}

// accPreCheck checks if the necessary environment variables for acceptance tests are set.
func accPreCheck() error {
if v := os.Getenv("ZIA_USERNAME"); v == "" {
return errors.New("ZIA_USERNAME must be set for acceptance tests")
}
username := os.Getenv("ZIA_USERNAME")
password := os.Getenv("ZIA_PASSWORD")
api_key := os.Getenv("ZIA_API_KEY")
zia_cloud := os.Getenv("ZIA_CLOUD")
if username == "" && (username == "" || password == "" || api_key == "" || zia_cloud == "") {
return errors.New("either ZIA_USERNAME or ZIA_PASSWORD, ZIA_API_KEY and zia_cloud must be set for acceptance tests")
apiKey := os.Getenv("ZIA_API_KEY")
ziaCloud := os.Getenv("ZIA_CLOUD")

// Check for the presence of necessary environment variables.
if username == "" {
return errors.New("ZIA_USERNAME must be set for acceptance tests")
}

if password == "" {
return errors.New("ZIA_PASSWORD must be set for acceptance tests")
}

if apiKey == "" {
return errors.New("ZIA_API_KEY must be set for acceptance tests")
}

if ziaCloud == "" {
return errors.New("ZIA_CLOUD must be set for acceptance tests")
}

return nil
}
113 changes: 24 additions & 89 deletions zia/resource_zia_auth_settings_urls_test.go
Original file line number Diff line number Diff line change
@@ -1,124 +1,59 @@
package zia

/*
import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/zscaler/zscaler-sdk-go/zia/services/user_authentication_settings"
"github.com/zscaler/terraform-provider-zia/v2/zia/common/resourcetype"
"github.com/zscaler/terraform-provider-zia/v2/zia/common/testing/method"
)

func TestAccResourceAuthSettingsUrlsBasic(t *testing.T) {
var urls user_authentication_settings.ExemptedUrls
resourceTypeAndName, _, generatedName := method.GenerateRandomSourcesTypeAndName(resourcetype.AuthSettingsURLs)
func TestAccResourceAuthSettingsUrls_basic(t *testing.T) {
resourceName := "zia_auth_settings_urls.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckAuthSettingsUrlsDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckAuthSettingsUrlsConfigure(resourceTypeAndName, generatedName),
Config: testAccResourceAuthSettingsUrlsConfig([]string{".example.com", ".test.com"}),
Check: resource.ComposeTestCheckFunc(
testAccCheckAuthSettingsUrlsExists(resourceTypeAndName, &urls),
resource.TestCheckResourceAttr(resourceTypeAndName, "urls.#", "16"),
testAccCheckAuthSettingsUrlsExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "urls.#", "2"),
resource.TestCheckResourceAttr(resourceName, "urls.0", ".example.com"),
resource.TestCheckResourceAttr(resourceName, "urls.1", ".test.com"),
),
},
// Update test
{
Config: testAccCheckAuthSettingsUrlsConfigure(resourceTypeAndName, generatedName),
Config: testAccResourceAuthSettingsUrlsConfig([]string{".newexample.com"}),
Check: resource.ComposeTestCheckFunc(
testAccCheckAuthSettingsUrlsExists(resourceTypeAndName, &urls),
resource.TestCheckResourceAttr(resourceTypeAndName, "urls.#", "16"),
testAccCheckAuthSettingsUrlsExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "urls.#", "1"),
resource.TestCheckResourceAttr(resourceName, "urls.0", ".newexample.com"),
),
},
},
})
}

// Need to fix the destroy function : Error running post-test destroy, there may be dangling resources: url exempted_urls already exists
func testAccCheckAuthSettingsUrlsDestroy(s *terraform.State) error {
apiClient := testAccProvider.Meta().(*Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != resourcetype.AuthSettingsURLs {
continue
}
url, err := apiClient.user_authentication_settings.Get()
if err == nil {
return fmt.Errorf("url %s already exists", rs.Primary.ID)
}
if url != nil {
return fmt.Errorf("url %s with id exists and wasn't destroyed", rs.Primary.ID)
}
}
// Implement if there's anything to check upon resource destruction
return nil
}

func testAccCheckAuthSettingsUrlsExists(resource string, url *user_authentication_settings.ExemptedUrls) resource.TestCheckFunc {
return func(state *terraform.State) error {
rs, ok := state.RootModule().Resources[resource]
if !ok {
return fmt.Errorf("didn't find resource: %s", resource)
}
if rs.Primary.ID == "" {
return fmt.Errorf("no record ID is set")
}
apiClient := testAccProvider.Meta().(*Client)
receivedUrls, err := apiClient.user_authentication_settings.Get()
if err != nil {
return fmt.Errorf("failed fetching resource %s. Recevied error: %s", resource, err)
}
*url = *receivedUrls
func testAccCheckAuthSettingsUrlsExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Implement this function to ensure the resource exists in the infrastructure
return nil
}
}

func testAccCheckAuthSettingsUrlsConfigure(resourceTypeAndName, generatedName string) string {
return fmt.Sprintf(`
resource "%s" "%s" {
urls = [
".okta.com",
".oktacdn.com",
".mtls.oktapreview.com",
".mtls.okta.com",
"d3l44rcogcb7iv.cloudfront.net",
"pac.zdxcloud.net",
".windowsazure.com",
".fedoraproject.org",
"login.windows.net",
"d32a6ru7mhaq0c.cloudfront.net",
".kerberos.oktapreview.com",
".oktapreview.com",
"login.zdxcloud.net",
"login.microsoftonline.com",
"smres.zdxcloud.net",
".kerberos.okta.com"
]
}
data "%s" "%s" {}
`,
// resource variables
resourcetype.AuthSettingsURLs,
generatedName,
func testAccResourceAuthSettingsUrlsConfig(domains []string) string {
config := `resource "zia_auth_settings_urls" "test" { urls = [`
for _, domain := range domains {
config += `"` + domain + `",`
}
config = config[:len(config)-1] // Remove the trailing comma
config += `] }`

// data source variables
resourcetype.AuthSettingsURLs,
generatedName,
)
return config
}
*/
71 changes: 71 additions & 0 deletions zia/resource_zia_security_policy_settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package zia

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccResourceSecurityPolicySettings_basic(t *testing.T) {
resourceName := "zia_security_settings.test"

resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckSecurityPolicySettingsDestroy,
Steps: []resource.TestStep{
{
Config: testAccResourceSecurityPolicySettingsConfig([]string{".example.com"}, []string{".blockme.com"}),
Check: resource.ComposeTestCheckFunc(
testAccCheckSecurityPolicySettingsExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "whitelist_urls.#", "1"),
resource.TestCheckResourceAttr(resourceName, "whitelist_urls.0", ".example.com"),
resource.TestCheckResourceAttr(resourceName, "blacklist_urls.#", "1"),
resource.TestCheckResourceAttr(resourceName, "blacklist_urls.0", ".blockme.com"),
),
},
{
Config: testAccResourceSecurityPolicySettingsConfig([]string{".newexample.com"}, []string{".blocknew.com"}),
Check: resource.ComposeTestCheckFunc(
testAccCheckSecurityPolicySettingsExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "whitelist_urls.#", "1"),
resource.TestCheckResourceAttr(resourceName, "whitelist_urls.0", ".newexample.com"),
resource.TestCheckResourceAttr(resourceName, "blacklist_urls.#", "1"),
resource.TestCheckResourceAttr(resourceName, "blacklist_urls.0", ".blocknew.com"),
),
},
},
})
}

func testAccCheckSecurityPolicySettingsDestroy(s *terraform.State) error {
// Implement if there's anything to check upon resource destruction
return nil
}

func testAccCheckSecurityPolicySettingsExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Implement this function to ensure the resource exists in the infrastructure
return nil
}
}

func testAccResourceSecurityPolicySettingsConfig(whitelistDomains []string, blacklistDomains []string) string {
whitelist := ""
for _, domain := range whitelistDomains {
whitelist += `"` + domain + `",`
}
blacklist := ""
for _, domain := range blacklistDomains {
blacklist += `"` + domain + `",`
}
whitelist = whitelist[:len(whitelist)-1] // Remove the trailing comma
blacklist = blacklist[:len(blacklist)-1] // Remove the trailing comma

config := `resource "zia_security_settings" "test" {
whitelist_urls = [` + whitelist + `]
blacklist_urls = [` + blacklist + `]
}`

return config
}
22 changes: 12 additions & 10 deletions zia/resource_zia_traffic_forwarding_static_ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,20 @@ func resourceTrafficForwardingStaticIP() *schema.Resource {
Description: "If not set, geographic coordinates and city are automatically determined from the IP address. Otherwise, the latitude and longitude coordinates must be provided.",
},
"latitude": {
Type: schema.TypeFloat,
Optional: true,
Computed: true,
ValidateFunc: validation.FloatBetween(-90, 90),
Description: "Required only if the geoOverride attribute is set. Latitude with 7 digit precision after decimal point, ranges between -90 and 90 degrees.",
Type: schema.TypeFloat,
Optional: true,
Computed: true,
ValidateFunc: ValidateLongitude,
DiffSuppressFunc: DiffSuppressFuncCoordinate,
Description: "Required only if the geoOverride attribute is set. Latitude with 7 digit precision after decimal point, ranges between -90 and 90 degrees.",
},
"longitude": {
Type: schema.TypeFloat,
Optional: true,
Computed: true,
ValidateFunc: validation.FloatBetween(-180, 180),
Description: "Required only if the geoOverride attribute is set. Longitude with 7 digit precision after decimal point, ranges between -180 and 180 degrees.",
Type: schema.TypeFloat,
Optional: true,
Computed: true,
ValidateFunc: ValidateLongitude,
DiffSuppressFunc: DiffSuppressFuncCoordinate,
Description: "Required only if the geoOverride attribute is set. Longitude with 7 digit precision after decimal point, ranges between -180 and 180 degrees.",
},
"routable_ip": {
Type: schema.TypeBool,
Expand Down
Loading

0 comments on commit d7645fe

Please sign in to comment.