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

azurerm_search_service - Add support for allowed_ips #8557

Merged
merged 3 commits into from
Sep 22, 2020
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
50 changes: 49 additions & 1 deletion azurerm/internal/services/search/resource_arm_search_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/search/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
Expand Down Expand Up @@ -108,6 +109,15 @@ func resourceArmSearchService() *schema.Resource {
Default: true,
},

"allowed_ips": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validate.IPv4Address,
},
},

"tags": tags.Schema(),
},
}
Expand Down Expand Up @@ -150,6 +160,9 @@ func resourceArmSearchServiceCreateUpdate(d *schema.ResourceData, meta interface
},
ServiceProperties: &search.ServiceProperties{
PublicNetworkAccess: publicNetworkAccess,
NetworkRuleSet: &search.NetworkRuleSet{
IPRules: expandSearchServiceIPRules(d.Get("allowed_ips").([]interface{})),
},
},
Tags: tags.Expand(t),
}
Expand All @@ -164,10 +177,15 @@ func resourceArmSearchServiceCreateUpdate(d *schema.ResourceData, meta interface
properties.ServiceProperties.PartitionCount = utils.Int32(partitionCount)
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, name, properties, nil); err != nil {
future, err := client.CreateOrUpdate(ctx, resourceGroup, name, properties, nil)
if err != nil {
return fmt.Errorf("Error issuing create/update request for Search Service %q (ResourceGroup %q): %s", name, resourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("error waiting for the completion of the creating/updating of Search Service %q (Resource Group %q): %+v", name, resourceGroup, err)
}

resp, err := client.Get(ctx, resourceGroup, name, nil)
if err != nil {
return fmt.Errorf("Error issuing get request for Search Service %q (ResourceGroup %q): %s", name, resourceGroup, err)
Expand Down Expand Up @@ -219,6 +237,8 @@ func resourceArmSearchServiceRead(d *schema.ResourceData, meta interface{}) erro
}

d.Set("public_network_access_enabled", props.PublicNetworkAccess != "Disabled")

d.Set("allowed_ips", flattenSearchServiceIPRules(props.NetworkRuleSet))
}

adminKeysClient := meta.(*clients.Client).Search.AdminKeysClient
Expand Down Expand Up @@ -276,3 +296,31 @@ func flattenSearchQueryKeys(input []search.QueryKey) []interface{} {

return results
}

func expandSearchServiceIPRules(input []interface{}) *[]search.IPRule {
output := make([]search.IPRule, 0)
if input == nil {
return &output
}

for _, rule := range input {
if rule != nil {
output = append(output, search.IPRule{
Value: utils.String(rule.(string)),
})
}
}

return &output
}

func flattenSearchServiceIPRules(input *search.NetworkRuleSet) []interface{} {
if input == nil || *input.IPRules == nil || len(*input.IPRules) == 0 {
return nil
}
result := make([]interface{}, 0)
for _, rule := range *input.IPRules {
result = append(result, rule.Value)
}
return result
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,42 @@ func TestAccAzureRMSearchService_update(t *testing.T) {
})
}

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMSearchServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMSearchService_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSearchServiceExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "1"),
),
},
data.ImportStep(),
{
Config: testAccAzureRMSearchService_ipRules(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSearchServiceExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "1"),
),
},
data.ImportStep(),
{
Config: testAccAzureRMSearchService_basic(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMSearchServiceExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "tags.%", "1"),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMSearchServiceExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).Search.ServicesClient
Expand Down Expand Up @@ -232,3 +268,29 @@ resource "azurerm_search_service" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func testAccAzureRMSearchService_ipRules(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-search-%d"
location = "%s"
}

resource "azurerm_search_service" "test" {
name = "acctestsearchservice%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku = "standard"

allowed_ips = ["168.1.5.65"]

tags = {
environment = "staging"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
2 changes: 2 additions & 0 deletions website/docs/r/search_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ The following arguments are supported:

-> **Note:** `partition_count` and `replica_count` can only be configured when using a `standard` sku.

* `allowed_ips` - (Optional) A list of IPv4 addresses that are allowed access to the search service endpoint.

* `tags` - (Optional) A mapping of tags which should be assigned to the Search Service.

## Attributes Reference
Expand Down