Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azuread_named_location: Implement support for the country_lookup_method parameter #1589

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/resources/named_location.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ The following arguments are supported:

* `countries_and_regions` - (Required) List of countries and/or regions in two-letter format specified by ISO 3166-2.
* `include_unknown_countries_and_regions` - (Optional) Whether IP addresses that don't map to a country or region should be included in the named location. Defaults to `false`.
* `country_lookup_method` - (Optional) Method of detecting country the user is located in. Possible values are `clientIpAddress` for IP-based location and `authenticatorAppGps` for Authenticator app GPS-based location. Defaults to `clientIpAddress`.

---

Expand Down
10 changes: 10 additions & 0 deletions internal/services/conditionalaccess/conditionalaccess.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,16 @@ func flattenCountryNamedLocation(in *stable.CountryNamedLocation) []interface{}
includeUnknown = *in.IncludeUnknownCountriesAndRegions
}

countryLookupMethod := stable.CountryLookupMethodType_ClientIPAddress
if in.CountryLookupMethod != nil {
countryLookupMethod = *in.CountryLookupMethod
}

return []interface{}{
map[string]interface{}{
"countries_and_regions": tf.FlattenStringSlice(in.CountriesAndRegions),
"include_unknown_countries_and_regions": includeUnknown,
"country_lookup_method": countryLookupMethod,
},
}
}
Expand Down Expand Up @@ -659,6 +665,10 @@ func expandCountryNamedLocation(in []interface{}) *stable.CountryNamedLocation {
result.CountriesAndRegions = tf.ExpandStringSlice(countriesAndRegions)
result.IncludeUnknownCountriesAndRegions = pointer.To(includeUnknown.(bool))

if countryLookupMethodType, ok := config["country_lookup_method"]; ok && countryLookupMethodType.(string) != "" {
result.CountryLookupMethod = pointer.To(stable.CountryLookupMethodType(countryLookupMethodType.(string)))
}

return &result
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func namedLocationDataSource() *pluginsdk.Resource {
Type: pluginsdk.TypeBool,
Computed: true,
},

"country_lookup_method": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ func TestAccNamedLocationDataSource_country(t *testing.T) {
check.That(data.ResourceName).Key("country.#").HasValue("1"),
check.That(data.ResourceName).Key("country.0.countries_and_regions.#").HasValue("3"),
check.That(data.ResourceName).Key("country.0.include_unknown_countries_and_regions").HasValue("true"),
check.That(data.ResourceName).Key("country.0.country_lookup_method").HasValue("clientIpAddress"),
),
},
})
}

func TestAccNamedLocationDataSource_countryByGps(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azuread_named_location", "test")

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: NamedLocationDataSource{}.countryByGps(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("country.#").HasValue("1"),
check.That(data.ResourceName).Key("country.0.countries_and_regions.#").HasValue("3"),
check.That(data.ResourceName).Key("country.0.include_unknown_countries_and_regions").HasValue("true"),
check.That(data.ResourceName).Key("country.0.country_lookup_method").HasValue("authenticatorAppGps"),
),
},
})
Expand Down Expand Up @@ -53,6 +70,16 @@ data "azuread_named_location" "test" {
`, NamedLocationResource{}.completeCountry(data))
}

func (NamedLocationDataSource) countryByGps(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s

data "azuread_named_location" "test" {
display_name = azuread_named_location.test.display_name
}
`, NamedLocationResource{}.completeCountryByGps(data))
}

func (NamedLocationDataSource) ip(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ func namedLocationResource() *pluginsdk.Resource {
Type: pluginsdk.TypeBool,
Optional: true,
},

"country_lookup_method": {
Type: pluginsdk.TypeString,
Default: stable.CountryLookupMethodType_ClientIPAddress,
ValidateFunc: validation.StringInSlice(stable.PossibleValuesForCountryLookupMethodType(), false),
Optional: true,
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,50 @@ func TestAccNamedLocation_updateCountry(t *testing.T) {
})
}

func TestAccNamedLocation_completeCountryByGps(t *testing.T) {
data := acceptance.BuildTestData(t, "azuread_named_location", "test")
r := NamedLocationResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.completeCountryByGps(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccNamedLocation_updateCountryByGps(t *testing.T) {
data := acceptance.BuildTestData(t, "azuread_named_location", "test")
r := NamedLocationResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basicCountry(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.completeCountryByGps(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.basicCountry(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (r NamedLocationResource) Exists(ctx context.Context, clients *clients.Client, state *terraform.InstanceState) (*bool, error) {
client := clients.ConditionalAccess.NamedLocationClient

Expand Down Expand Up @@ -213,6 +257,24 @@ resource "azuread_named_location" "test" {
"JP",
]
include_unknown_countries_and_regions = true
country_lookup_method = "clientIpAddress"
}
}
`, data.RandomInteger)
}

func (NamedLocationResource) completeCountryByGps(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azuread_named_location" "test" {
display_name = "acctestNLC-%[1]d"
country {
countries_and_regions = [
"GB",
"US",
"JP",
]
include_unknown_countries_and_regions = true
country_lookup_method = "authenticatorAppGps"
}
}
`, data.RandomInteger)
Expand Down
Loading